Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Added new networks #157 #158

Merged
merged 2 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public static NetworkId getNetworkId(Network networkInfo) {
network = NetworkId.MAINNET;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to change this logic. In existing code, Network object is being compared but instead we should compare NetworkId of the network and return NetworkId.MAINNET or NetworkId.TESTNET

Some thing like

if (NetworkId.MAINNET.equals(networkInfo.getNetworkId()) return NetworkId.MAINNET;
else  if //check for network TESTNET network id

else throw AddressRuntimeException

} else if (Networks.testnet().equals(networkInfo)) {
network = NetworkId.TESTNET;
} else if (Networks.preview().equals(networkInfo)) {
network = NetworkId.TESTNET;
} else if (Networks.preprod().equals(networkInfo)) {
network = NetworkId.TESTNET;
} else {
throw new AddressRuntimeException("Unknown network type - " + networkInfo);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,14 @@ public static Network testnet() {
Network testnet = new Network(0b0000, 1097911063);
return testnet;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. You may want to change the local variable name in prepod & preview methods to prepod/preview instead of testnet.

public static Network preprod() {
Network preprod = new Network(0b0000, 1);
return preprod;
}

public static Network preview() {
Network preview = new Network(0b0000, 2);
return preview;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public enum NetworkId {
TESTNET,
MAINNET
MAINNET,
}
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ public static TransactionBody deserialize(Map bodyMap) throws CborDeserializatio
int networkIdInt = networkIdUI.getValue().intValue();
if (networkIdInt == 0) {
transactionBody.setNetworkId(NetworkId.TESTNET);
}else if (networkIdInt == 1) {
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above. This change is not required.

else if (networkIdInt == 1) {
transactionBody.setNetworkId(NetworkId.MAINNET);
} else {
throw new CborDeserializationException("Invalid networkId value : " + networkIdInt);
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/com/bloxbean/cardano/client/account/AccountTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ void getNewBaseAddress_Test() {
assertEquals(24, mnemonic.split(" ").length );
}

@Test
public void testBaseAddressHasSamePrefixForAllTestnets() {
String mnemonic = "damp wish scrub sentence vibrant gauge tumble raven game extend winner acid side amused vote edge affair buzz hospital slogan patient drum day vital";
Account testnetAccount = new Account(Networks.testnet(), mnemonic);
Account previewAccount = new Account(Networks.preview(), mnemonic);
Account preprodAccount = new Account(Networks.preprod(), mnemonic);


String testnetPrefix="addr_test";
assertTrue(testnetAccount.baseAddress().startsWith(testnetPrefix));
assertTrue(previewAccount.baseAddress().startsWith(testnetPrefix));
assertTrue(preprodAccount.baseAddress().startsWith(testnetPrefix));
}

@Test
void getBaseAddressFromMnemonic() {
String phrase24W = "coconut you order found animal inform tent anxiety pepper aisle web horse source indicate eyebrow viable lawsuit speak dragon scheme among animal slogan exchange";
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/com/bloxbean/cardano/client/address/AddressTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,31 @@ public void testBaseAddress_whenMainnet() {
assertThat(address.getPrefix()).isEqualTo("addr");
}

@Test
public void testBaseAddress_whenPreview() {
String mnemonic = "damp wish scrub sentence vibrant gauge tumble raven game extend winner acid side amused vote edge affair buzz hospital slogan patient drum day vital";
Account account = new Account(Networks.preview(), mnemonic);

Address address = new Address(account.baseAddress());

assertThat(address.getAddressType()).isEqualTo(AddressType.Base);
assertThat(address.getPrefix()).isEqualTo("addr_test");

}
@Test
public void testBaseAddress_whenPreprod() {
String mnemonic = "damp wish scrub sentence vibrant gauge tumble raven game extend winner acid side amused vote edge affair buzz hospital slogan patient drum day vital";
Account account = new Account(Networks.preprod(), mnemonic);

Address address = new Address(account.baseAddress());

assertThat(address.getAddressType()).isEqualTo(AddressType.Base);
assertThat(address.getPrefix()).isEqualTo("addr_test");

}



@Test
public void testBaseAddress_whenTestnet() {
String mnemonic = "damp wish scrub sentence vibrant gauge tumble raven game extend winner acid side amused vote edge affair buzz hospital slogan patient drum day vital";
Expand Down