Skip to content

Commit

Permalink
autoconfiguration of networkId from blockchain provider url (#33)
Browse files Browse the repository at this point in the history
* autoconfiguration of networkId from blockchain provider url
* rename getChainId to getNetwork
* split network test into 2
* add rinkeby test
* used infura instead of linkpool in readme example
  • Loading branch information
JohnnyJumper authored Feb 18, 2021
1 parent 19df746 commit eeaaa37
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.12.0
* Autoconfigure chainId from the blockchainprovider url when setting one via Builder#ProviderUrl

## 1.11.0
* Use Infura Ethereum Provider by default

Expand Down
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ To resolve ENS domains on production it's recommended to change Ethereum provide
DomainResolution resolution = new Resolution();

// Optionally override default config using builder options:

// providerUrl overwrites chainId by making net_version JSON RPC call to the provider
// in the following example blockchain would be set to the rinkeby testnet
DomainResolution resolution = Resolution.builder()
.chainId(NamingServiceType.ENS, Network.ROPSTEN)
.providerUrl(NamingServiceType.ENS, "https://ropsten-rpc.linkpool.io/")
.providerUrl(NamingServiceType.ENS, "https://rinkeby.infura.io/v3/e0c0cb9d12c440a29379df066de587e6")
.build();

// Infura config:
Expand Down Expand Up @@ -71,13 +71,13 @@ DomainResolution resolution = Resolution.builder()
DefaultProvider myProvider = DefaultProvider
.cleanBuild()
.setHeader("custom-header", "custom-value")
.setHeader("new-key", "new-value");
.setHeader("new-header", "new-value");

DomainResolution resolution = Resolution.builder()
.provider(myProvider)
.build();

// All network calls will be made with headers "custom-header" and "new-key" instead of default ones
// All network calls will be made with headers "custom-header" and "new-header" instead of default ones

```

Expand Down Expand Up @@ -135,8 +135,6 @@ public enum NSExceptionCode {

> Note: if you don't wish to install Gradle you can use it with wrapper: `./gradlew` instead of `gradle`.
- Configure a `TESTING_PROVIDER_URL` and `TESTING_INFURA_PROJECT_ID` environment variables with your blockchain
provider for testing.
- To run a build with associated tests, use `gradle build`.
- To run a build without running the tests, use `gradle build -x test`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,13 @@ public enum Network {

private final int code;
private final String name;

public static Network getNetwork(int chainId) {
for (Network value : Network.values()) {
if (value.code == chainId) {
return value;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import java.util.List;

import com.unstoppabledomains.config.network.model.Network;
import com.unstoppabledomains.exceptions.dns.DnsException;
import com.unstoppabledomains.exceptions.ns.NamingServiceException;
import com.unstoppabledomains.resolution.dns.DnsRecord;
import com.unstoppabledomains.resolution.dns.DnsRecordsType;
import com.unstoppabledomains.resolution.naming.service.NamingServiceType;

public interface DomainResolution {

Expand All @@ -17,6 +19,13 @@ public interface DomainResolution {
*/
boolean isSupported(String domain);

/**
* Returns configured network id
* @param type which NamingService you are interested in
* @return Network object with name and code property
*/
Network getNetwork(NamingServiceType type);

/**
* Resolves domain for a specific ticker address
*
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/com/unstoppabledomains/resolution/Resolution.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.unstoppabledomains.resolution;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.unstoppabledomains.config.network.model.Network;
import com.unstoppabledomains.exceptions.dns.DnsException;
import com.unstoppabledomains.exceptions.ns.NSExceptionCode;
Expand Down Expand Up @@ -70,6 +72,12 @@ public boolean isSupported(String domain) {
return services.values().stream().anyMatch(s -> s.isSupported(domain));
}

@Override
public Network getNetwork(NamingServiceType type) {
NamingService service = services.get(type);
return service.getNetwork();
}

@Override
public String getAddress(String domain, String ticker) throws NamingServiceException {
NamingService service = findService(domain);
Expand Down Expand Up @@ -201,7 +209,12 @@ public Builder chainId(NamingServiceType nsType, Network chainId) {
*/
public Builder providerUrl(NamingServiceType nsType, String providerUrl) {
NSConfig nsConfig = serviceConfigs.get(nsType);
Network chainId = getNetworkId(providerUrl);
if (chainId == null) {
chainId = nsConfig.getChainId();
}
nsConfig.setBlockchainProviderUrl(providerUrl);
nsConfig.setChainId(chainId);
return this;
}

Expand Down Expand Up @@ -266,5 +279,25 @@ public Resolution build() {
private void setProvider(IProvider provider) {
this.provider = provider;
}

/**
* Makes a call via provider to the blockchainProviderUrl and returns the networkId
* @param blockchainProviderUrl
* @return Network object or null if couldn't retrive the network
*/
private Network getNetworkId(String blockchainProviderUrl) {
JsonObject body = new JsonObject();
body.addProperty("jsonrpc", "2.0");
body.addProperty("method", "net_version");
body.add("params", new JsonArray());
body.addProperty("id", 67);
try {
JsonObject response = provider.request(blockchainProviderUrl, body);
return Network.getNetwork(response.get("result").getAsInt());
} catch(Exception e) {
e.printStackTrace();
return null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ protected BaseNamingService(NSConfig nsConfig, IProvider provider) {
this.blockchainProviderUrl = nsConfig.getBlockchainProviderUrl();
this.provider = provider;
}

public Network getNetwork() {
return chainId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package com.unstoppabledomains.resolution.naming.service;

import com.unstoppabledomains.config.network.NetworkConfigLoader;
import com.unstoppabledomains.config.network.model.Network;
import com.unstoppabledomains.exceptions.ContractCallException;
import com.unstoppabledomains.exceptions.dns.DnsException;
import com.unstoppabledomains.exceptions.ns.NSExceptionCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import com.unstoppabledomains.config.network.model.Network;
import com.unstoppabledomains.exceptions.dns.DnsException;
import com.unstoppabledomains.exceptions.ns.NamingServiceException;
import com.unstoppabledomains.resolution.dns.DnsRecord;
Expand All @@ -19,4 +20,6 @@ public interface NamingService {
String getNamehash(String domain) throws NamingServiceException;

List<DnsRecord> getDns(String domain, List<DnsRecordsType> types) throws NamingServiceException, DnsException;
}

Network getNetwork();
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "1.11.0"
"version": "1.12.0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,43 @@ public static void init() {
resolution = new Resolution();
}

@Test
public void resolveRinkebyDomain() throws Exception {
DomainResolution rinkebyResolution = Resolution.builder()
.providerUrl(NamingServiceType.CNS, "https://rinkeby.infura.io/v3/e0c0cb9d12c440a29379df066de587e6")
.build();
String ethAddress = rinkebyResolution.getAddress("udtestdev-creek.crypto", "eth");
assertEquals("0x1C8b9B78e3085866521FE206fa4c1a67F49f153A", ethAddress);

}

@Test
public void testDifferentNetworks() throws Exception {
DomainResolution customCnsNetwork = Resolution.builder()
.providerUrl(NamingServiceType.CNS, "https://rinkeby.infura.io/v3/e0c0cb9d12c440a29379df066de587e6")
.providerUrl(NamingServiceType.ENS, "https://goerli.infura.io/v3/e0c0cb9d12c440a29379df066de587e6")
.chainId(NamingServiceType.ZNS, Network.KOVAN)
.build();

Network customCnsChainId = customCnsNetwork.getNetwork(NamingServiceType.CNS);
Network customEnsChainId = customCnsNetwork.getNetwork(NamingServiceType.ENS);
Network customZnsChainId = customCnsNetwork.getNetwork(NamingServiceType.ZNS);
assertEquals(Network.RINKEBY, customCnsChainId);
assertEquals(Network.GOERLI, customEnsChainId);
assertEquals(Network.KOVAN, customZnsChainId);
}

@Test
public void testDefaultNetworks() throws Exception {
DomainResolution defaultSettings = new Resolution();
Network defaultCnsChainId = defaultSettings.getNetwork(NamingServiceType.CNS);
Network defaultEnsChainId = defaultSettings.getNetwork(NamingServiceType.ENS);
Network defaultZnsChainId = defaultSettings.getNetwork(NamingServiceType.ZNS);
assertEquals(Network.MAINNET, defaultCnsChainId);
assertEquals(Network.MAINNET, defaultEnsChainId);
assertEquals(Network.MAINNET, defaultZnsChainId);
}

@Test
public void shouldResolveFromResolutionCreatedByBuilder() throws Exception {
DomainResolution resolutionFromBuilder = Resolution.builder()
Expand Down

0 comments on commit eeaaa37

Please sign in to comment.