@Override public SimpleHDKeyChain toEncrypted(KeyCrypter keyCrypter, KeyParameter aesKey) { return new SimpleHDKeyChain(keyCrypter, aesKey, this); }
public WalletPocketHD(DeterministicKey rootKey, CoinType coinType, @Nullable KeyCrypter keyCrypter, @Nullable KeyParameter key) { this(new SimpleHDKeyChain(rootKey, keyCrypter, key), coinType); }
chain = new SimpleHDKeyChain(rootKey, crypter); chain.lookaheadSize = LAZY_CALCULATE_LOOKAHEAD; rootTreeSize = rootKey.getPath().size();
@Override public SimpleHDKeyChain toDecrypted(KeyParameter aesKey) { checkState(getKeyCrypter() != null, "Key chain not encrypted"); checkState(rootKey.isEncrypted(), "Root key not encrypted"); DeterministicKey decKey = rootKey.decrypt(getKeyCrypter(), aesKey); SimpleHDKeyChain chain = new SimpleHDKeyChain(decKey); // Now double check that the keys match to catch the case where the key is wrong but padding didn't catch it. if (!chain.getWatchingKey().getPubKeyPoint().equals(getWatchingKey().getPubKeyPoint())) throw new KeyCrypterException("Provided AES key is wrong"); chain.lookaheadSize = lookaheadSize; // Now copy the (pubkey only) leaf keys across to avoid rederiving them. The private key bytes are missing // anyway so there's nothing to decrypt. for (ECKey eckey : simpleKeyChain.getKeys()) { DeterministicKey key = (DeterministicKey) eckey; if (!isLeaf(key)) continue; // Not a leaf key. checkState(key.isEncrypted(), "Key is not encrypted"); DeterministicKey parent = chain.hierarchy.get(checkNotNull(key.getParent(), "Key has null parent").getPath(), false, false); // Clone the key to the new decrypted hierarchy. key = new DeterministicKey(key.getPubOnly(), parent); chain.hierarchy.putKey(key); chain.simpleKeyChain.importKeys(key); } chain.issuedExternalKeys = issuedExternalKeys; chain.issuedInternalKeys = issuedInternalKeys; return chain; }
@Before public void setup() { BriefLogFormatter.init(); DeterministicSeed seed = new DeterministicSeed(ENTROPY, "", 0); masterKey = HDKeyDerivation.createMasterPrivateKey(seed.getSeedBytes()); DeterministicHierarchy hierarchy = new DeterministicHierarchy(masterKey); DeterministicKey rootKey = hierarchy.get(ImmutableList.of(ChildNumber.ZERO_HARDENED), false, true); chain = new SimpleHDKeyChain(rootKey); chain.setLookaheadSize(10); }
@Test public void encryptionChildRoot() throws UnreadableWalletException { DeterministicHierarchy hierarchy = new DeterministicHierarchy(masterKey); DeterministicKey rootKey = hierarchy.get(BitcoinTest.get().getBip44Path(0), false, true); SimpleHDKeyChain newChain = new SimpleHDKeyChain(rootKey); encryption(newChain); }
@Test public void serializeUnencryptedChildRoot() throws UnreadableWalletException { DeterministicHierarchy hierarchy = new DeterministicHierarchy(masterKey); DeterministicKey rootKey = hierarchy.get(BitcoinTest.get().getBip44Path(0), false, true); SimpleHDKeyChain newChain = new SimpleHDKeyChain(rootKey); serializeUnencrypted(newChain, DETERMINISTIC_WALLET_SERIALIZATION_TXT_CHILD_ROOT_KEY); }
@Test public void deriveCoin() throws Exception { DeterministicHierarchy hierarchy = new DeterministicHierarchy(masterKey); DeterministicKey rootKey = hierarchy.get(BitcoinMain.get().getBip44Path(0), false, true); chain = new SimpleHDKeyChain(rootKey); ECKey key1 = chain.getKey(SimpleHDKeyChain.KeyPurpose.RECEIVE_FUNDS); ECKey key2 = chain.getKey(SimpleHDKeyChain.KeyPurpose.RECEIVE_FUNDS); final Address address = new Address(BitcoinMain.get(), "1Fp7CA7ZVqZNFVNQ9TpeqWUas7K28K9zig"); assertEquals(address, key1.toAddress(BitcoinMain.get())); assertEquals("1AKqkQM4VqyVis6hscj8695WHPCCzgHNY3", key2.toAddress(BitcoinMain.get()).toString()); assertEquals(key1, chain.findKeyFromPubHash(address.getHash160())); assertEquals(key2, chain.findKeyFromPubKey(key2.getPubKey())); key1.sign(Sha256Hash.ZERO_HASH); ECKey key3 = chain.getKey(SimpleHDKeyChain.KeyPurpose.CHANGE); assertEquals("18YvGiRqXKxrzB72ckfrRSizWeHgwRP94V", key3.toAddress(BitcoinMain.get()).toString()); key3.sign(Sha256Hash.ZERO_HASH); ECKey key4 = chain.getKey(SimpleHDKeyChain.KeyPurpose.CHANGE); assertEquals("1861TX2MbyPEUrxDQVWgV4Tp9991bK1zpy", key4.toAddress(BitcoinMain.get()).toString()); key4.sign(Sha256Hash.ZERO_HASH); }