Merge pull request #6 from proxyco/fix/coininfo-network-issue

fix: Fixed Index out of bounds issue with network ordinal
This commit is contained in:
craigraw 2022-09-13 15:31:26 +02:00 committed by GitHub
commit 9bc8f6b021
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 10 deletions

View file

@ -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,7 +32,7 @@ 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() {
@ -85,6 +91,22 @@ public class CryptoCoinInfo extends RegistryItem {
}
public enum Network {
MAINNET, TESTNET
MAINNET(0), TESTNET(1), GOERLI(4);
Integer networkValue;
Network(Integer 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;
}
}
}

View file

@ -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