From 0738702e20039862babbb845b3a6bd488d62336d Mon Sep 17 00:00:00 2001 From: Jorge Rego Date: Mon, 5 Sep 2022 12:56:58 +0200 Subject: [PATCH 1/3] fix: fixed Index out of bounds issue with network ordinal Added Goerli support Added tests for Goerli --- .../hummingbird/registry/CryptoCoinInfo.java | 52 +++++++++++++------ .../registry/CryptoCoinInfoTest.java | 19 ++++--- 2 files changed, 49 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/sparrowwallet/hummingbird/registry/CryptoCoinInfo.java b/src/main/java/com/sparrowwallet/hummingbird/registry/CryptoCoinInfo.java index 1ce35be..fa620a3 100644 --- a/src/main/java/com/sparrowwallet/hummingbird/registry/CryptoCoinInfo.java +++ b/src/main/java/com/sparrowwallet/hummingbird/registry/CryptoCoinInfo.java @@ -12,13 +12,19 @@ public class CryptoCoinInfo extends RegistryItem { private final Integer network; public CryptoCoinInfo(Integer type, Integer network) { + if(network.equals(Network.GOERLI.networkValue) && !type.equals(Type.ETHEREUM.typeValue)) { + throw new IllegalArgumentException("Goerli network can only be selected for Ethereum"); + } this.type = type; this.network = network; } public CryptoCoinInfo(Type type, Network network) { + if(network == Network.GOERLI && type != Type.ETHEREUM) { + throw new IllegalArgumentException("Goerli network can only be selected for Ethereum"); + } this.type = (type != null ? type.typeValue : null); - this.network = (network != null ? network.ordinal() : null); + this.network = (network != null ? network.networkValue : null); } public Type getType() { @@ -26,15 +32,15 @@ public class CryptoCoinInfo extends RegistryItem { } public Network getNetwork() { - return network == null ? Network.MAINNET : Network.values()[network]; + return network == null ? Network.MAINNET : Network.getNetworkFromValue(network); } public DataItem toCbor() { Map map = new Map(); - if(type != null) { + if (type != null) { map.put(new UnsignedInteger(TYPE_KEY), new UnsignedInteger(type)); } - if(network != null) { + if (network != null) { map.put(new UnsignedInteger(NETWORK_KEY), new UnsignedInteger(network)); } return map; @@ -49,15 +55,15 @@ public class CryptoCoinInfo extends RegistryItem { Integer type = null; Integer network = null; - Map map = (Map)item; - for(DataItem key : map.getKeys()) { - UnsignedInteger uintKey = (UnsignedInteger)key; + Map map = (Map) item; + for (DataItem key : map.getKeys()) { + UnsignedInteger uintKey = (UnsignedInteger) key; int intKey = uintKey.getValue().intValue(); - if(intKey == TYPE_KEY) { - type = ((UnsignedInteger)map.get(key)).getValue().intValue(); - } else if(intKey == NETWORK_KEY) { - network = ((UnsignedInteger)map.get(key)).getValue().intValue(); + if (intKey == TYPE_KEY) { + type = ((UnsignedInteger) map.get(key)).getValue().intValue(); + } else if (intKey == NETWORK_KEY) { + network = ((UnsignedInteger) map.get(key)).getValue().intValue(); } } @@ -67,16 +73,16 @@ public class CryptoCoinInfo extends RegistryItem { public enum Type { BITCOIN(0), ETHEREUM(60); - Integer typeValue; + int typeValue; - Type(Integer typeValue) { + Type(int typeValue) { this.typeValue = typeValue; } static Type getTypeFromValue(int value) { for (int i = 0; i < Type.values().length; i++) { Type current = Type.values()[i]; - if(value == current.typeValue) { + if (value == current.typeValue) { return current; } } @@ -85,6 +91,22 @@ public class CryptoCoinInfo extends RegistryItem { } public enum Network { - MAINNET, TESTNET + MAINNET(0), TESTNET(1), GOERLI(4); + + int networkValue; + + Network(int networkValue) { + this.networkValue = networkValue; + } + + static Network getNetworkFromValue(int value) { + for (int i = 0; i < Network.values().length; i++) { + Network current = Network.values()[i]; + if (value == current.networkValue) { + return current; + } + } + return null; + } } } diff --git a/src/test/java/com/sparrowwallet/hummingbird/registry/CryptoCoinInfoTest.java b/src/test/java/com/sparrowwallet/hummingbird/registry/CryptoCoinInfoTest.java index 7ad975f..58d6844 100644 --- a/src/test/java/com/sparrowwallet/hummingbird/registry/CryptoCoinInfoTest.java +++ b/src/test/java/com/sparrowwallet/hummingbird/registry/CryptoCoinInfoTest.java @@ -18,15 +18,20 @@ public class CryptoCoinInfoTest { } @Test - public void testBitcoinTypeCoinInfo() { - CryptoCoinInfo coinInfo = new CryptoCoinInfo(CryptoCoinInfo.Type.BITCOIN, CryptoCoinInfo.Network.MAINNET); - Assert.assertSame(coinInfo.getType(), CryptoCoinInfo.Type.BITCOIN); + public void testGoerliEthereumCoinInfo() { + CryptoCoinInfo coinInfo = new CryptoCoinInfo(CryptoCoinInfo.Type.ETHEREUM, CryptoCoinInfo.Network.GOERLI); + Assert.assertSame(coinInfo.getType().typeValue, CryptoCoinInfo.Type.ETHEREUM.typeValue); + Assert.assertSame(coinInfo.getNetwork().networkValue, CryptoCoinInfo.Network.GOERLI.networkValue); } - @Test - public void testEthereumTypeCoinInfo() { - CryptoCoinInfo coinInfo = new CryptoCoinInfo(CryptoCoinInfo.Type.ETHEREUM, CryptoCoinInfo.Network.MAINNET); - Assert.assertSame(coinInfo.getType(), CryptoCoinInfo.Type.ETHEREUM); + @Test(expected = IllegalArgumentException.class) + public void testGoerliSupportedOnlyWithEthereum() { + new CryptoCoinInfo(CryptoCoinInfo.Type.BITCOIN, CryptoCoinInfo.Network.GOERLI); + } + + @Test(expected = IllegalArgumentException.class) + public void testGoerliSupportedOnlyWithEthereumWithValues() { + new CryptoCoinInfo(CryptoCoinInfo.Type.BITCOIN.typeValue, CryptoCoinInfo.Network.GOERLI.networkValue); } @Test From de6794e9b29e1d9fe560ade47e3bd0dc23b0f3bb Mon Sep 17 00:00:00 2001 From: Jorge Rego Date: Mon, 5 Sep 2022 13:07:47 +0200 Subject: [PATCH 2/3] fix: rolled back types to keep consistency with previous code. --- .../hummingbird/registry/CryptoCoinInfo.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/sparrowwallet/hummingbird/registry/CryptoCoinInfo.java b/src/main/java/com/sparrowwallet/hummingbird/registry/CryptoCoinInfo.java index fa620a3..f3be53a 100644 --- a/src/main/java/com/sparrowwallet/hummingbird/registry/CryptoCoinInfo.java +++ b/src/main/java/com/sparrowwallet/hummingbird/registry/CryptoCoinInfo.java @@ -73,9 +73,9 @@ public class CryptoCoinInfo extends RegistryItem { public enum Type { BITCOIN(0), ETHEREUM(60); - int typeValue; + Integer typeValue; - Type(int typeValue) { + Type(Integer typeValue) { this.typeValue = typeValue; } @@ -93,9 +93,9 @@ public class CryptoCoinInfo extends RegistryItem { public enum Network { MAINNET(0), TESTNET(1), GOERLI(4); - int networkValue; + Integer networkValue; - Network(int networkValue) { + Network(Integer networkValue) { this.networkValue = networkValue; } From 47ee6bc11dfca41041e069aef5f4b7902adf97ad Mon Sep 17 00:00:00 2001 From: Jorge Rego Date: Thu, 8 Sep 2022 18:03:53 +0200 Subject: [PATCH 3/3] fix: removed spaces coming from code style auto format. --- .../hummingbird/registry/CryptoCoinInfo.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/sparrowwallet/hummingbird/registry/CryptoCoinInfo.java b/src/main/java/com/sparrowwallet/hummingbird/registry/CryptoCoinInfo.java index f3be53a..cf4dcca 100644 --- a/src/main/java/com/sparrowwallet/hummingbird/registry/CryptoCoinInfo.java +++ b/src/main/java/com/sparrowwallet/hummingbird/registry/CryptoCoinInfo.java @@ -37,10 +37,10 @@ public class CryptoCoinInfo extends RegistryItem { public DataItem toCbor() { Map map = new Map(); - if (type != null) { + if(type != null) { map.put(new UnsignedInteger(TYPE_KEY), new UnsignedInteger(type)); } - if (network != null) { + if(network != null) { map.put(new UnsignedInteger(NETWORK_KEY), new UnsignedInteger(network)); } return map; @@ -55,15 +55,15 @@ public class CryptoCoinInfo extends RegistryItem { Integer type = null; Integer network = null; - Map map = (Map) item; - for (DataItem key : map.getKeys()) { - UnsignedInteger uintKey = (UnsignedInteger) key; + Map map = (Map)item; + for(DataItem key : map.getKeys()) { + UnsignedInteger uintKey = (UnsignedInteger)key; int intKey = uintKey.getValue().intValue(); - if (intKey == TYPE_KEY) { - type = ((UnsignedInteger) map.get(key)).getValue().intValue(); - } else if (intKey == NETWORK_KEY) { - network = ((UnsignedInteger) map.get(key)).getValue().intValue(); + if(intKey == TYPE_KEY) { + type = ((UnsignedInteger)map.get(key)).getValue().intValue(); + } else if(intKey == NETWORK_KEY) { + network = ((UnsignedInteger)map.get(key)).getValue().intValue(); } } @@ -82,7 +82,7 @@ public class CryptoCoinInfo extends RegistryItem { static Type getTypeFromValue(int value) { for (int i = 0; i < Type.values().length; i++) { Type current = Type.values()[i]; - if (value == current.typeValue) { + if(value == current.typeValue) { return current; } }