11package org .arkecosystem .crypto .identities ;
22
33import static org .junit .jupiter .api .Assertions .assertEquals ;
4+ import static org .junit .jupiter .api .Assertions .assertThrows ;
45
6+ import java .io .IOException ;
7+ import org .arkecosystem .crypto .configuration .Network ;
8+ import org .arkecosystem .crypto .networks .Devnet ;
9+ import org .arkecosystem .crypto .networks .Mainnet ;
10+ import org .junit .jupiter .api .AfterEach ;
11+ import org .junit .jupiter .api .BeforeEach ;
512import org .junit .jupiter .api .Test ;
613
714public class PrivateKeyTest {
815
16+ @ BeforeEach
17+ void setUp () {
18+ Network .set (new Devnet ());
19+ }
20+
21+ @ AfterEach
22+ void tearDown () {
23+ Network .set (new Devnet ());
24+ }
25+
926 @ Test
1027 public void fromPassphrase () {
1128 String actual =
@@ -21,4 +38,35 @@ public void fromHex() {
2138 .getPrivateKeyAsHex ();
2239 assertEquals ("d8839c2432bfd0a67ef10a804ba991eabba19f154a3d707917681d45822a5712" , actual );
2340 }
41+
42+ @ Test
43+ public void fromWif_round_trips_with_fromPassphrase () throws IOException {
44+ String wif = WIF .fromPassphrase ("this is a top secret passphrase" );
45+
46+ String fromWif = PrivateKey .fromWif (wif ).getPrivateKeyAsHex ();
47+ String fromPassphrase =
48+ PrivateKey .fromPassphrase ("this is a top secret passphrase" ).getPrivateKeyAsHex ();
49+
50+ assertEquals (fromPassphrase , fromWif );
51+ }
52+
53+ @ Test
54+ public void fromWif_rejects_when_version_byte_belongs_to_another_network () throws IOException {
55+ String mainnetWif ;
56+ try {
57+ Network .set (new Mainnet ());
58+ mainnetWif = WIF .fromPassphrase ("this is a top secret passphrase" );
59+ } finally {
60+ Network .set (new Devnet ());
61+ }
62+
63+ // Mainnet and Devnet share the same wif byte (170) so this branch only
64+ // triggers when the prefix differs, e.g. against testnet (186).
65+ Network .set (new org .arkecosystem .crypto .networks .Testnet ());
66+ try {
67+ assertThrows (IllegalArgumentException .class , () -> PrivateKey .fromWif (mainnetWif ));
68+ } finally {
69+ Network .set (new Devnet ());
70+ }
71+ }
2472}
0 commit comments