|
24 | 24 | import org.bitcoinj.core.Sha256Hash; |
25 | 25 | import org.bitcoinj.core.VerificationException; |
26 | 26 | import org.bitcoinj.crypto.DeterministicKey; |
| 27 | +import org.bitcoinj.crypto.HDKeyDerivation; |
27 | 28 | import org.bitcoinj.params.TestNet3Params; |
28 | 29 | import org.bouncycastle.util.encoders.Hex; |
29 | 30 | import org.junit.Before; |
30 | 31 | import org.junit.Test; |
31 | 32 |
|
32 | 33 | import static org.junit.Assert.assertArrayEquals; |
33 | 34 | import static org.junit.Assert.assertEquals; |
| 35 | +import static org.junit.Assert.assertNotNull; |
34 | 36 | import static org.junit.Assert.assertThrows; |
35 | 37 | import static org.junit.Assert.assertTrue; |
36 | 38 | import static com.squareup.subzero.shared.SubzeroUtils.ERROR_ENCRYPTED_MASTER_SEED_SIZE; |
@@ -109,7 +111,7 @@ public void setUp() { |
109 | 111 | assertEquals("2MuAdStu2xZtRSyA5B6wRtj7SmaLjDyfm1H", address.toString()); |
110 | 112 | } |
111 | 113 |
|
112 | | - @Test public void derivePublicKey() { |
| 114 | + @Test public void testDerivePublicKeyRandom() { |
113 | 115 | // Using https://iancoleman.io/bip39/, I created a random key. The derivation we use for cold |
114 | 116 | // wallet is m/coin_type'/change/address_index. The coin_type' is taken care of for us, |
115 | 117 | // so we only care about change/address_index. |
@@ -139,6 +141,38 @@ public void setUp() { |
139 | 141 | assertArrayEquals(expected, childKey.getPubKey()); |
140 | 142 | } |
141 | 143 |
|
| 144 | + @Test public void testDerivePublicKeyNegativeIndex() { |
| 145 | + DeterministicKey rootKey = HDKeyDerivation.createMasterPrivateKey(new byte[32]); |
| 146 | + Path invalidPath = Path.newBuilder().setIsChange(false).setIndex(-1).build(); |
| 147 | + |
| 148 | + Exception exception = assertThrows(IllegalStateException.class, () -> { |
| 149 | + SubzeroUtils.derivePublicKey(rootKey, invalidPath); |
| 150 | + }); |
| 151 | + |
| 152 | + assertEquals("index should be between 0 and 2^31-1", exception.getMessage()); |
| 153 | + } |
| 154 | + |
| 155 | + @Test public void testDerivePublicKeyMaxIndex() { |
| 156 | + DeterministicKey rootKey = HDKeyDerivation.createMasterPrivateKey(new byte[32]); |
| 157 | + // Integer.MAX_VALUE is 2^31-1. |
| 158 | + Path path = Path.newBuilder().setIsChange(false).setIndex(Integer.MAX_VALUE).build(); |
| 159 | + |
| 160 | + DeterministicKey derivedKey = SubzeroUtils.derivePublicKey(rootKey, path); |
| 161 | + assertEquals(33, derivedKey.getPubKey().length); |
| 162 | + } |
| 163 | + |
| 164 | + @Test public void testDerivePublicKeyReproducible() { |
| 165 | + DeterministicKey rootKey = HDKeyDerivation.createMasterPrivateKey(new byte[32]); |
| 166 | + Path path = Path.newBuilder().setIsChange(false).setIndex(37).build(); |
| 167 | + |
| 168 | + DeterministicKey key1 = SubzeroUtils.derivePublicKey(rootKey, path); |
| 169 | + DeterministicKey key2 = SubzeroUtils.derivePublicKey(rootKey, path); |
| 170 | + |
| 171 | + assertNotNull(key1); |
| 172 | + assertNotNull(key2); |
| 173 | + assertEquals(key1, key2); |
| 174 | + } |
| 175 | + |
142 | 176 | @Test |
143 | 177 | public void testValidateFees() { |
144 | 178 | String feeExceedsLimitString = "Transaction fee is greater than 1BTC and 10% of the total " |
|
0 commit comments