mirror of
https://github.com/sparrowwallet/hummingbird.git
synced 2024-12-26 18:16:45 +00:00
fix: fixed Index out of bounds issue with network ordinal
Added Goerli support Added tests for Goerli
This commit is contained in:
parent
8f6cb0a662
commit
0738702e20
2 changed files with 49 additions and 22 deletions
|
@ -12,13 +12,19 @@ public class CryptoCoinInfo extends RegistryItem {
|
||||||
private final Integer network;
|
private final Integer network;
|
||||||
|
|
||||||
public CryptoCoinInfo(Integer type, 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.type = type;
|
||||||
this.network = network;
|
this.network = network;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CryptoCoinInfo(Type type, 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.type = (type != null ? type.typeValue : null);
|
||||||
this.network = (network != null ? network.ordinal() : null);
|
this.network = (network != null ? network.networkValue : null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Type getType() {
|
public Type getType() {
|
||||||
|
@ -26,15 +32,15 @@ public class CryptoCoinInfo extends RegistryItem {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Network getNetwork() {
|
public Network getNetwork() {
|
||||||
return network == null ? Network.MAINNET : Network.values()[network];
|
return network == null ? Network.MAINNET : Network.getNetworkFromValue(network);
|
||||||
}
|
}
|
||||||
|
|
||||||
public DataItem toCbor() {
|
public DataItem toCbor() {
|
||||||
Map map = new Map();
|
Map map = new Map();
|
||||||
if(type != null) {
|
if (type != null) {
|
||||||
map.put(new UnsignedInteger(TYPE_KEY), new UnsignedInteger(type));
|
map.put(new UnsignedInteger(TYPE_KEY), new UnsignedInteger(type));
|
||||||
}
|
}
|
||||||
if(network != null) {
|
if (network != null) {
|
||||||
map.put(new UnsignedInteger(NETWORK_KEY), new UnsignedInteger(network));
|
map.put(new UnsignedInteger(NETWORK_KEY), new UnsignedInteger(network));
|
||||||
}
|
}
|
||||||
return map;
|
return map;
|
||||||
|
@ -49,15 +55,15 @@ public class CryptoCoinInfo extends RegistryItem {
|
||||||
Integer type = null;
|
Integer type = null;
|
||||||
Integer network = null;
|
Integer network = null;
|
||||||
|
|
||||||
Map map = (Map)item;
|
Map map = (Map) item;
|
||||||
for(DataItem key : map.getKeys()) {
|
for (DataItem key : map.getKeys()) {
|
||||||
UnsignedInteger uintKey = (UnsignedInteger)key;
|
UnsignedInteger uintKey = (UnsignedInteger) key;
|
||||||
int intKey = uintKey.getValue().intValue();
|
int intKey = uintKey.getValue().intValue();
|
||||||
|
|
||||||
if(intKey == TYPE_KEY) {
|
if (intKey == TYPE_KEY) {
|
||||||
type = ((UnsignedInteger)map.get(key)).getValue().intValue();
|
type = ((UnsignedInteger) map.get(key)).getValue().intValue();
|
||||||
} else if(intKey == NETWORK_KEY) {
|
} else if (intKey == NETWORK_KEY) {
|
||||||
network = ((UnsignedInteger)map.get(key)).getValue().intValue();
|
network = ((UnsignedInteger) map.get(key)).getValue().intValue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,16 +73,16 @@ public class CryptoCoinInfo extends RegistryItem {
|
||||||
public enum Type {
|
public enum Type {
|
||||||
BITCOIN(0), ETHEREUM(60);
|
BITCOIN(0), ETHEREUM(60);
|
||||||
|
|
||||||
Integer typeValue;
|
int typeValue;
|
||||||
|
|
||||||
Type(Integer typeValue) {
|
Type(int typeValue) {
|
||||||
this.typeValue = typeValue;
|
this.typeValue = typeValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
static Type getTypeFromValue(int value) {
|
static Type getTypeFromValue(int value) {
|
||||||
for (int i = 0; i < Type.values().length; i++) {
|
for (int i = 0; i < Type.values().length; i++) {
|
||||||
Type current = Type.values()[i];
|
Type current = Type.values()[i];
|
||||||
if(value == current.typeValue) {
|
if (value == current.typeValue) {
|
||||||
return current;
|
return current;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -85,6 +91,22 @@ public class CryptoCoinInfo extends RegistryItem {
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum Network {
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,15 +18,20 @@ public class CryptoCoinInfoTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBitcoinTypeCoinInfo() {
|
public void testGoerliEthereumCoinInfo() {
|
||||||
CryptoCoinInfo coinInfo = new CryptoCoinInfo(CryptoCoinInfo.Type.BITCOIN, CryptoCoinInfo.Network.MAINNET);
|
CryptoCoinInfo coinInfo = new CryptoCoinInfo(CryptoCoinInfo.Type.ETHEREUM, CryptoCoinInfo.Network.GOERLI);
|
||||||
Assert.assertSame(coinInfo.getType(), CryptoCoinInfo.Type.BITCOIN);
|
Assert.assertSame(coinInfo.getType().typeValue, CryptoCoinInfo.Type.ETHEREUM.typeValue);
|
||||||
|
Assert.assertSame(coinInfo.getNetwork().networkValue, CryptoCoinInfo.Network.GOERLI.networkValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void testEthereumTypeCoinInfo() {
|
public void testGoerliSupportedOnlyWithEthereum() {
|
||||||
CryptoCoinInfo coinInfo = new CryptoCoinInfo(CryptoCoinInfo.Type.ETHEREUM, CryptoCoinInfo.Network.MAINNET);
|
new CryptoCoinInfo(CryptoCoinInfo.Type.BITCOIN, CryptoCoinInfo.Network.GOERLI);
|
||||||
Assert.assertSame(coinInfo.getType(), CryptoCoinInfo.Type.ETHEREUM);
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void testGoerliSupportedOnlyWithEthereumWithValues() {
|
||||||
|
new CryptoCoinInfo(CryptoCoinInfo.Type.BITCOIN.typeValue, CryptoCoinInfo.Network.GOERLI.networkValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in a new issue