/** * Creates a deterministic key chain that watches the given (public only) root key. You can use this to calculate * balances and generally follow along, but spending is not possible with such a chain. Currently you can't use * this method to watch an arbitrary fragment of some other tree, this limitation may be removed in future. */ public DeterministicKeyChain(DeterministicKey watchingKey) { checkArgument(watchingKey.isPubKeyOnly(), "Private subtrees not currently supported: if you got this key from DKC.getWatchingKey() then use .dropPrivate().dropParent() on it first."); checkArgument(watchingKey.getPath().size() == getAccountPath().size(), "You can only watch an account key currently"); basicKeyChain = new BasicKeyChain(); this.seed = null; rootKey = null; addToBasicChain(watchingKey); hierarchy = new DeterministicHierarchy(watchingKey); initializeHierarchyUnencrypted(watchingKey); }
/** * Creates a deterministic key chain that watches the given (public only) root key. You can use this to calculate * balances and generally follow along, but spending is not possible with such a chain. Currently you can't use * this method to watch an arbitrary fragment of some other tree, this limitation may be removed in future. */ public DeterministicKeyChain(DeterministicKey watchingKey) { checkArgument(watchingKey.isPubKeyOnly(), "Private subtrees not currently supported: if you got this key from DKC.getWatchingKey() then use .dropPrivate().dropParent() on it first."); checkArgument(watchingKey.getPath().size() == getAccountPath().size(), "You can only watch an account key currently"); basicKeyChain = new BasicKeyChain(); this.seed = null; this.rootKey = null; basicKeyChain.importKey(watchingKey); hierarchy = new DeterministicHierarchy(watchingKey); initializeHierarchyUnencrypted(watchingKey); }
/** * Creates a deterministic key chain that watches the given (public only) root key. You can use this to calculate * balances and generally follow along, but spending is not possible with such a chain. Currently you can't use * this method to watch an arbitrary fragment of some other tree, this limitation may be removed in future. */ public DeterministicKeyChain(DeterministicKey watchingKey, boolean useSegwit) { checkArgument(watchingKey.isPubKeyOnly(), "Private subtrees not currently supported: if you got this key from DKC.getWatchingKey() then use .dropPrivate().dropParent() on it first."); checkArgument(watchingKey.getPath().size() == getAccountPath().size(), "You can only watch an account key currently"); this.useSegwit = useSegwit; basicKeyChain = new BasicKeyChain(useSegwit); this.seed = null; this.rootKey = null; basicKeyChain.importKey(watchingKey); hierarchy = new DeterministicHierarchy(watchingKey); initializeHierarchyUnencrypted(watchingKey); }
/** * Creates a deterministic key chain that watches the given (public only) root key. You can use this to calculate * balances and generally follow along, but spending is not possible with such a chain. */ public DeterministicKeyChain(DeterministicKey watchingKey) { checkArgument(watchingKey.isPubKeyOnly(), "Private subtrees not currently supported: if you got this key from DKC.getWatchingKey() then use .dropPrivate().dropParent() on it first."); setAccountPath(watchingKey.getPath()); basicKeyChain = new BasicKeyChain(); this.seed = null; rootKey = null; addToBasicChain(watchingKey); hierarchy = new DeterministicHierarchy(watchingKey); initializeHierarchyUnencrypted(watchingKey); }
private void initializeHierarchyUnencrypted(DeterministicKey baseKey) { rootKey = baseKey; addToBasicChain(rootKey); hierarchy = new DeterministicHierarchy(rootKey); externalKey = hierarchy.get(EXTERNAL_PATH, true, true); internalKey = hierarchy.get(INTERNAL_PATH, true, true); addToBasicChain(externalKey); addToBasicChain(internalKey); }
private void initializeHierarchyUnencrypted(DeterministicKey baseKey) { rootKey = baseKey; addToBasicChain(rootKey); hierarchy = new DeterministicHierarchy(rootKey); externalKey = hierarchy.get(EXTERNAL_PATH, true, true); internalKey = hierarchy.get(INTERNAL_PATH, true, true); addToBasicChain(externalKey); addToBasicChain(internalKey); }
private WalletFile createWalletFile(List<String> words) throws MnemonicException.MnemonicLengthException, MnemonicException.MnemonicWordException, MnemonicException.MnemonicChecksumException, CipherException { byte[] seeds = MnemonicCode.INSTANCE.toEntropy(words); DeterministicKey masterPrivateKey = HDKeyDerivation.createMasterPrivateKey(seeds); DeterministicHierarchy deterministicHierarchy = new DeterministicHierarchy(masterPrivateKey); DeterministicKey child = deterministicHierarchy.deriveChild(DeterministicKeyChain.BIP44_ACCOUNT_ZERO_PATH, true, true, ChildNumber.ZERO); ECKeyPair ecKeyPair = ECKeyPair.create(child.getPrivKeyBytes()); return Wallet.createLight(PASSWORD, ecKeyPair); }
/** * For use in {@link KeyChainFactory} during deserialization. */ protected DeterministicKeyChain(DeterministicSeed seed, @Nullable KeyCrypter crypter) { this.seed = seed; basicKeyChain = new BasicKeyChain(crypter); if (!seed.isEncrypted()) { rootKey = HDKeyDerivation.createMasterPrivateKey(checkNotNull(seed.getSeedBytes())); rootKey.setCreationTimeSeconds(seed.getCreationTimeSeconds()); addToBasicChain(rootKey); hierarchy = new DeterministicHierarchy(rootKey); for (int i = 1; i <= getAccountPath().size(); i++) { addToBasicChain(hierarchy.get(getAccountPath().subList(0, i), false, true)); } initializeHierarchyUnencrypted(rootKey); } // Else... // We can't initialize ourselves with just an encrypted seed, so we expected deserialization code to do the // rest of the setup (loading the root key). }
@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); }
@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 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 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 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); }
private void testVector(int testCase) { log.info("======= Test vector {}", testCase); HDWTestVector tv = tvs[testCase]; NetworkParameters params = MainNetParams.get(); DeterministicKey masterPrivateKey = HDKeyDerivation.createMasterPrivateKey(HEX.decode(tv.seed)); assertEquals(testEncode(tv.priv), testEncode(masterPrivateKey.serializePrivB58(params))); assertEquals(testEncode(tv.pub), testEncode(masterPrivateKey.serializePubB58(params))); DeterministicHierarchy dh = new DeterministicHierarchy(masterPrivateKey); for (int i = 0; i < tv.derived.size(); i++) { HDWTestVector.DerivedTestCase tc = tv.derived.get(i); log.info("{}", tc.name); assertEquals(tc.name, String.format(Locale.US, "Test%d %s", testCase + 1, tc.getPathDescription())); int depth = tc.path.length - 1; DeterministicKey ehkey = dh.deriveChild(Arrays.asList(tc.path).subList(0, depth), false, true, tc.path[depth]); assertEquals(testEncode(tc.priv), testEncode(ehkey.serializePrivB58(params))); assertEquals(testEncode(tc.pub), testEncode(ehkey.serializePubB58(params))); } }
@Before public void setup() throws MnemonicException, UnreadableWalletException { DeterministicSeed seed = new DeterministicSeed(recoveryPhrase, null, "", 0); DeterministicKey masterKey = HDKeyDerivation.createMasterPrivateKey(seed.getSeedBytes()); hierarchy = new DeterministicHierarchy(masterKey); wallet = new Wallet(recoveryPhrase); nxtAccount = (NxtFamilyWallet)wallet.createAccount(NXT, null); otherAccount = new NxtFamilyWallet(hierarchy.get(NXT.getBip44Path(1), false, true), NXT); }
@Before public void setup() throws MnemonicException, UnreadableWalletException { DeterministicSeed seed = new DeterministicSeed(recoveryPhrase, null, "", 0); DeterministicKey masterKey = HDKeyDerivation.createMasterPrivateKey(seed.getSeedBytes()); hierarchy = new DeterministicHierarchy(masterKey); wallet = new Wallet(recoveryPhrase); nxtAccount = (NxtFamilyWallet)wallet.createAccount(NXT, null); otherAccount = new NxtFamilyWallet(hierarchy.get(NXT.getBip44Path(1), false, true), NXT); }
@Test public void testHDAccountNxt() throws MnemonicException, UnreadableWalletException { DeterministicSeed seed = new DeterministicSeed(recoveryPhrase, null, "", 0); DeterministicKey masterKey = HDKeyDerivation.createMasterPrivateKey(seed.getSeedBytes()); DeterministicHierarchy hierarchy = new DeterministicHierarchy(masterKey); DeterministicKey entropy = hierarchy.get(NxtMain.get().getBip44Path(0), false, true); NxtFamilyKey nxtKey = new NxtFamilyKey(entropy, null, null); byte[] privateKey = nxtKey.getPrivateKey(); byte[] publicKey = nxtKey.getPublicKey(); NxtAddress address = new NxtAddress(NxtMain.get(), publicKey); assertArrayEquals(nxtPrivateKey, privateKey); assertArrayEquals(nxtPublicKey, publicKey); assertEquals(nxtRsAddress, address.toString()); assertEquals(nxtAccountId, address.getAccountId()); }
@Test public void testHDAccountNxt() throws MnemonicException, UnreadableWalletException { DeterministicSeed seed = new DeterministicSeed(recoveryPhrase, null, "", 0); DeterministicKey masterKey = HDKeyDerivation.createMasterPrivateKey(seed.getSeedBytes()); DeterministicHierarchy hierarchy = new DeterministicHierarchy(masterKey); DeterministicKey entropy = hierarchy.get(NxtMain.get().getBip44Path(0), false, true); NxtFamilyKey nxtKey = new NxtFamilyKey(entropy, null, null); byte[] privateKey = nxtKey.getPrivateKey(); byte[] publicKey = nxtKey.getPublicKey(); NxtAddress address = new NxtAddress(NxtMain.get(), publicKey); assertArrayEquals(nxtPrivateKey, privateKey); assertArrayEquals(nxtPublicKey, publicKey); assertEquals(nxtRsAddress, address.toString()); assertEquals(nxtAccountId, address.getAccountId()); }
@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); }