Skip to content

Commit

Permalink
Merge branch 'release/0.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
mirceanis committed Dec 17, 2019
2 parents 6362a98 + aa20d75 commit a1e9e4b
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* 0.4.0
* feat - easy configuration with infuraProjectID ( #8 )
* feat - find networks by name after configuration ( #9 )

* 0.3.2
* build - bump kethereum to 0.76.2 ( 61d7582d )

Expand Down
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

Core interface definitions and default implementations for uPort kotlin SDK classes.

[FAQ and helpdesk support](http://bit.ly/uPort_helpdesk)

## Modules

### core
Expand All @@ -30,15 +32,18 @@ Networks.registerNetwork(
)
)

//register all popular public networks using your infura project ID
Networks.registerAllNetworksWithInfura("<YOUR INFURA ID>")

//use testnet
val net = Networks.get("0x1234")

or
val net = Networks.get("local")

// override defaults
Networks.registerNetwork(
Networks.mainnet.copy(rpcUrl = "http://localhost:8545")
)

assertTrue(Networks.mainnet.rpcUrl == "http://localhost:8545")

```
Expand Down Expand Up @@ -87,7 +92,7 @@ allprojects {
In your module `build.gradle` file, add:

```groovy
def uport_kotlin_common_version = "0.3.2"
def uport_kotlin_common_version = "0.4.0"
dependencies {
//...
// core lib
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ buildscript {
kethereum_version = "0.76.2"
khex_version = "1.0.0-RC3"

current_release_version = "0.3.2"
current_release_version = "0.4.0"
}

repositories {
Expand Down
31 changes: 22 additions & 9 deletions core/src/main/java/me/uport/sdk/core/Networks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ object Networks {
private val NETWORK_CONFIG = emptyMap<String, EthNetwork>().toMutableMap()

init {
registerAllNetworksWithInfura("e72b472993ff46d3b5b88faa47214d7f")
}

fun registerAllNetworksWithInfura(infuraId: String) {
registerNetwork(
EthNetwork(
name = "mainnet",
networkId = mainnetId,
rpcUrl = "https://mainnet.infura.io/v3/e72b472993ff46d3b5b88faa47214d7f",
rpcUrl = "https://mainnet.infura.io/v3/${infuraId}",
ethrDidRegistry = DEFAULT_ERC1056_REGISTRY,
explorerUrl = "https://etherscan.io",
// MNID.encode(mainnetId, "0xab5c8051b9a1df1aab0149f8b0630848b7ecabf6"),
Expand All @@ -41,7 +45,7 @@ object Networks {
EthNetwork(
name = "rinkeby",
networkId = rinkebyId,
rpcUrl = "https://rinkeby.infura.io/v3/e72b472993ff46d3b5b88faa47214d7f",
rpcUrl = "https://rinkeby.infura.io/v3/${infuraId}",
ethrDidRegistry = DEFAULT_ERC1056_REGISTRY,
explorerUrl = "https://rinkeby.etherscan.io",
//MNID.encode(rinkebyId, "0x2cc31912b2b0f3075a87b3640923d45a26cef3ee"),
Expand All @@ -55,7 +59,7 @@ object Networks {
EthNetwork(
name = "ropsten",
networkId = ropstenId,
rpcUrl = "https://ropsten.infura.io/v3/e72b472993ff46d3b5b88faa47214d7f",
rpcUrl = "https://ropsten.infura.io/v3/${infuraId}",
ethrDidRegistry = DEFAULT_ERC1056_REGISTRY,
explorerUrl = "https://ropsten.etherscan.io",
//MNID.encode(ropstenId, "0x41566e3a081f5032bdcad470adb797635ddfe1f0"),
Expand All @@ -69,7 +73,7 @@ object Networks {
EthNetwork(
name = "kovan",
networkId = kovanId,
rpcUrl = "https://kovan.infura.io/v3/e72b472993ff46d3b5b88faa47214d7f",
rpcUrl = "https://kovan.infura.io/v3/${infuraId}",
ethrDidRegistry = DEFAULT_ERC1056_REGISTRY,
explorerUrl = "https://kovan.etherscan.io",
//MNID.encode(kovanId, "0x5f8e9351dc2d238fb878b6ae43aa740d62fc9758"),
Expand Down Expand Up @@ -117,13 +121,22 @@ object Networks {
}

/**
* Gets an [EthNetwork] based on a [networkId]
* Gets an [EthNetwork] based on a [networkId] or [name]
*/
fun get(networkId: String): EthNetwork {
val cleanNetId = cleanId(networkId)
fun get(nameOrId: String): EthNetwork {
val cleanNetId = cleanId(nameOrId)
return NETWORK_CONFIG[cleanNetId]
?: NETWORK_CONFIG[networkId]
?: throw IllegalStateException("network [$networkId] not configured")
?: NETWORK_CONFIG[nameOrId]
?: getNetworkByName(nameOrId)
?: throw IllegalStateException("network [$nameOrId] not configured")
}

/**
* Searches for an [EthNetwork] based on a [name]
*/
private fun getNetworkByName(name: String): EthNetwork? {
val queryName = name.toLowerCase()
return NETWORK_CONFIG.values.find { it.name.toLowerCase() == queryName }
}

private fun cleanId(id: String) = id.clean0xPrefix().trimStart('0').prepend0xPrefix()
Expand Down
26 changes: 26 additions & 0 deletions core/src/test/java/me/uport/sdk/core/NetworksTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,32 @@ class NetworksTest {
.isEqualTo(Networks.mainnet.ethrDidRegistry)
}

@Test
fun `can find a registered network using the name`() {

assertThat(Networks.get("mainnet")).isEqualTo(Networks.mainnet)

assertThat(Networks.get("rinkeby")).isEqualTo(Networks.rinkeby)

assertThat(Networks.get("kovan")).isEqualTo(Networks.kovan)

assertThat(Networks.get("ropsten")).isEqualTo(Networks.ropsten)
}

@Test
fun `can find a registered network using the different case name`() {
assertThat(Networks.get("MainNet")).isEqualTo(Networks.mainnet)
}

@Test
fun `throws error if the network name does not match`() {
assertThat {
Networks.get("manet")
}.thrownError {
isInstanceOf(IllegalStateException::class)
}
}

@Test
fun `getting an unknown network throws`() {
assertThat {
Expand Down

0 comments on commit a1e9e4b

Please sign in to comment.