@Override protected void formatAddresses(boolean includePrivateKeys, NetworkParameters params, StringBuilder builder2) { for (DeterministicKeyChain followingChain : followingKeyChains) builder2.append("Following chain: ").append(followingChain.getWatchingKey().serializePubB58(params)) .append('\n'); builder2.append('\n'); for (RedeemData redeemData : marriedKeysRedeemData.values()) formatScript(ScriptBuilder.createP2SHOutputScript(redeemData.redeemScript), builder2, params); }
@Override protected void formatAddresses(boolean includePrivateKeys, NetworkParameters params, StringBuilder builder2) { for (DeterministicKeyChain followingChain : followingKeyChains) builder2.append("Following chain: ").append(followingChain.getWatchingKey().serializePubB58(params)) .append('\n'); builder2.append('\n'); for (RedeemData redeemData : marriedKeysRedeemData.values()) formatScript(ScriptBuilder.createP2SHOutputScript(redeemData.redeemScript), builder2, params); }
@Override protected void formatAddresses(boolean includePrivateKeys, @Nullable KeyParameter aesKey, NetworkParameters params, StringBuilder builder2) { for (DeterministicKeyChain followingChain : followingKeyChains) builder2.append("Following chain: ").append(followingChain.getWatchingKey().serializePubB58(params)) .append('\n'); builder2.append('\n'); for (RedeemData redeemData : marriedKeysRedeemData.values()) formatScript(ScriptBuilder.createP2SHOutputScript(redeemData.redeemScript), builder2, params); }
@Override protected void formatAddresses(boolean includePrivateKeys, NetworkParameters params, StringBuilder builder2) { for (DeterministicKeyChain followingChain : followingKeyChains) builder2.append("Following chain: ").append(followingChain.getWatchingKey().serializePubB58(params)) .append('\n'); builder2.append('\n'); for (RedeemData redeemData : marriedKeysRedeemData.values()) formatScript(ScriptBuilder.createP2SHOutputScript(redeemData.redeemScript), builder2, params); }
public String getXpub() { return node.serializePubB58(PersistentUrls.getInstance().getCurrentNetworkParams()); }
public String toString(boolean includePrivateKeys, NetworkParameters params) { final DeterministicKey watchingKey = getWatchingKey(); final StringBuilder builder = new StringBuilder(); if (seed != null) { if (seed.isEncrypted()) { builder.append("Seed is encrypted\n"); } else if (includePrivateKeys) { final List<String> words = seed.getMnemonicCode(); builder.append("Seed as words: ").append(Utils.join(words)).append('\n'); builder.append("Seed as hex: ").append(seed.toHexString()).append('\n'); } builder.append("Seed birthday: ").append(seed.getCreationTimeSeconds()).append(" [") .append(Utils.dateTimeFormat(seed.getCreationTimeSeconds() * 1000)).append("]\n"); } else { builder.append("Key birthday: ").append(watchingKey.getCreationTimeSeconds()).append(" [") .append(Utils.dateTimeFormat(watchingKey.getCreationTimeSeconds() * 1000)).append("]\n"); } builder.append("Key to watch: ").append(watchingKey.serializePubB58(params)).append('\n'); formatAddresses(includePrivateKeys, params, builder); return builder.toString(); }
public String toString(boolean includePrivateKeys, NetworkParameters params) { final DeterministicKey watchingKey = getWatchingKey(); final StringBuilder builder = new StringBuilder(); if (seed != null) { if (seed.isEncrypted()) { builder.append("Seed is encrypted\n"); } else if (includePrivateKeys) { final List<String> words = seed.getMnemonicCode(); builder.append("Seed as words: ").append(Utils.join(words)).append('\n'); builder.append("Seed as hex: ").append(seed.toHexString()).append('\n'); } builder.append("Seed birthday: ").append(seed.getCreationTimeSeconds()).append(" [") .append(Utils.dateTimeFormat(seed.getCreationTimeSeconds() * 1000)).append("]\n"); } else { builder.append("Key birthday: ").append(watchingKey.getCreationTimeSeconds()).append(" [") .append(Utils.dateTimeFormat(watchingKey.getCreationTimeSeconds() * 1000)).append("]\n"); } builder.append("Key to watch: ").append(watchingKey.serializePubB58(params)).append('\n'); formatAddresses(includePrivateKeys, params, builder); return builder.toString(); }
public String toString(boolean includePrivateKeys, NetworkParameters params) { final DeterministicKey watchingKey = getWatchingKey(); final StringBuilder builder = new StringBuilder(); if (seed != null) { if (seed.isEncrypted()) { builder.append("Seed is encrypted\n"); } else if (includePrivateKeys) { final List<String> words = seed.getMnemonicCode(); builder.append("Seed as words: ").append(Utils.join(words)).append('\n'); builder.append("Seed as hex: ").append(seed.toHexString()).append('\n'); } builder.append("Seed birthday: ").append(seed.getCreationTimeSeconds()).append(" [") .append(Utils.dateTimeFormat(seed.getCreationTimeSeconds() * 1000)).append("]\n"); } else { builder.append("Key birthday: ").append(watchingKey.getCreationTimeSeconds()).append(" [") .append(Utils.dateTimeFormat(watchingKey.getCreationTimeSeconds() * 1000)).append("]\n"); } builder.append("Key to watch: ").append(watchingKey.serializePubB58(params)).append('\n'); formatAddresses(includePrivateKeys, params, builder); return builder.toString(); }
/** * Constructor for account. * * @param params Example MainNetParams, RegTestParams, TestNet2Params * @param wKey deterministic key for this account * @param child id within the wallet for this account */ public HDAccount(NetworkParameters params, DeterministicKey wKey, int child) { this.params = params; aID = child; // L0PRV & STDVx: private derivation. int childnum = child; childnum |= ChildNumber.HARDENED_BIT; aKey = HDKeyDerivation.deriveChildKey(wKey, childnum); strXPUB = aKey.serializePubB58(params); chains = new ArrayList<HDChain>(); chains.add(new HDChain(params, aKey, true)); chains.add(new HDChain(params, aKey, false)); strPath = aKey.getPathAsString(); }
public String toString(boolean includePrivateKeys, @Nullable KeyParameter aesKey, NetworkParameters params) { final DeterministicKey watchingKey = getWatchingKey(); final StringBuilder builder = new StringBuilder(); if (seed != null) { if (includePrivateKeys) { DeterministicSeed decryptedSeed = seed.isEncrypted() ? seed.decrypt(getKeyCrypter(), DEFAULT_PASSPHRASE_FOR_MNEMONIC, aesKey) : seed; final List<String> words = decryptedSeed.getMnemonicCode(); builder.append("Seed as words: ").append(Utils.SPACE_JOINER.join(words)).append('\n'); builder.append("Seed as hex: ").append(decryptedSeed.toHexString()).append('\n'); } else { if (seed.isEncrypted()) builder.append("Seed is encrypted\n"); } builder.append("Seed birthday: ").append(seed.getCreationTimeSeconds()).append(" [") .append(Utils.dateTimeFormat(seed.getCreationTimeSeconds() * 1000)).append("]\n"); } else { builder.append("Key birthday: ").append(watchingKey.getCreationTimeSeconds()).append(" [") .append(Utils.dateTimeFormat(watchingKey.getCreationTimeSeconds() * 1000)).append("]\n"); } builder.append("Key to watch: ").append(watchingKey.serializePubB58(params)).append('\n'); formatAddresses(includePrivateKeys, aesKey, params, builder); return builder.toString(); }
@Override public String getPublicKeySerialized() { // Change the path of the key to match the BIP32 paths i.e. 0H/<account index>H DeterministicKey key = keys.getWatchingKey(); ImmutableList<ChildNumber> path = ImmutableList.of(key.getChildNumber()); key = new DeterministicKey(path, key.getChainCode(), key.getPubKeyPoint(), null, null); return key.serializePubB58(); }
@Override public String getPublicKeySerialized() { // Change the path of the key to match the BIP32 paths i.e. 0H/<account index>H DeterministicKey key = keys.getWatchingKey(); ImmutableList<ChildNumber> path = ImmutableList.of(key.getChildNumber()); key = new DeterministicKey(path, key.getChainCode(), key.getPubKeyPoint(), null, null); return key.serializePubB58(); }
@Test public void testSerializationMainAndTestNetworks() { DeterministicKey key1 = HDKeyDerivation.createMasterPrivateKey("satoshi lives!".getBytes()); NetworkParameters params = MainNetParams.get(); String pub58 = key1.serializePubB58(params); String priv58 = key1.serializePrivB58(params); assertEquals("xpub661MyMwAqRbcF7mq7Aejj5xZNzFfgi3ABamE9FedDHVmViSzSxYTgAQGcATDo2J821q7Y9EAagjg5EP3L7uBZk11PxZU3hikL59dexfLkz3", pub58); assertEquals("xprv9s21ZrQH143K2dhN197jMx1ppxRBHFKJpMqdLsF1ewxncv7quRED8N5nksxphju3W7naj1arF56L5PUEWfuSk8h73Sb2uh7bSwyXNrjzhAZ", priv58); params = TestNet3Params.get(); pub58 = key1.serializePubB58(params); priv58 = key1.serializePrivB58(params); assertEquals("tpubD6NzVbkrYhZ4WuxgZMdpw1Hvi7MKg6YDjDMXVohmZCFfF17hXBPYpc56rCY1KXFMovN29ik37nZimQseiykRTBTJTZJmjENyv2k3R12BJ1M", pub58); assertEquals("tprv8ZgxMBicQKsPdSvtfhyEXbdp95qPWmMK9ukkDHfU8vTGQWrvtnZxe7TEg48Ui7HMsZKMj7CcQRg8YF1ydtFPZBxha5oLa3qeN3iwpYhHPVZ", priv58); }
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))); } }
private void createMarriedWallet(int threshold, int numKeys, boolean addSigners) throws BlockStoreException { wallet = new Wallet(PARAMS); blockStore = new MemoryBlockStore(PARAMS); chain = new BlockChain(PARAMS, wallet, blockStore); List<DeterministicKey> followingKeys = Lists.newArrayList(); for (int i = 0; i < numKeys - 1; i++) { final DeterministicKeyChain keyChain = new DeterministicKeyChain(new SecureRandom()); DeterministicKey partnerKey = DeterministicKey.deserializeB58(null, keyChain.getWatchingKey().serializePubB58(PARAMS), PARAMS); followingKeys.add(partnerKey); if (addSigners && i < threshold - 1) wallet.addTransactionSigner(new KeyChainTransactionSigner(keyChain)); } MarriedKeyChain chain = MarriedKeyChain.builder() .random(new SecureRandom()) .followingKeys(followingKeys) .threshold(threshold).build(); wallet.addAndActivateHDChain(chain); }
@Test public void watchingMarriedWallet() throws Exception { DeterministicKey watchKey = wallet.getWatchingKey(); String serialized = watchKey.serializePubB58(PARAMS); Wallet wallet = Wallet.fromWatchingKeyB58(PARAMS, serialized, 0); blockStore = new MemoryBlockStore(PARAMS); DeterministicKey partnerKey = DeterministicKey.deserializeB58(null, keyChain.getWatchingKey().serializePubB58(PARAMS), PARAMS);
@Test public void testRoundTripMarriedWallet() throws Exception { // create 2-of-2 married wallet myWallet = new Wallet(PARAMS); final DeterministicKeyChain partnerChain = new DeterministicKeyChain(new SecureRandom()); DeterministicKey partnerKey = DeterministicKey.deserializeB58(null, partnerChain.getWatchingKey().serializePubB58(PARAMS), PARAMS); MarriedKeyChain chain = MarriedKeyChain.builder() .random(new SecureRandom()) .followingKeys(partnerKey) .threshold(2).build(); myWallet.addAndActivateHDChain(chain); myAddress = myWallet.currentAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS); Wallet wallet1 = roundTrip(myWallet); assertEquals(0, wallet1.getTransactions(true).size()); assertEquals(Coin.ZERO, wallet1.getBalance()); assertEquals(2, wallet1.getActiveKeyChain().getSigsRequiredToSpend()); assertEquals(myAddress, wallet1.currentAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS)); }
@Test(expected = ECKey.MissingPrivateKeyException.class) public void watchingWalletWithCreationTime() throws Exception { DeterministicKey watchKey = wallet.getWatchingKey(); String serialized = watchKey.serializePubB58(PARAMS); Wallet watchingWallet = Wallet.fromWatchingKeyB58(PARAMS, serialized, 1415282801); DeterministicKey key2 = watchingWallet.freshReceiveKey(); assertEquals(myKey, key2); ECKey key = wallet.freshKey(KeyChain.KeyPurpose.CHANGE); key2 = watchingWallet.freshKey(KeyChain.KeyPurpose.CHANGE); assertEquals(key, key2); key.sign(Sha256Hash.ZERO_HASH); key2.sign(Sha256Hash.ZERO_HASH); }
@Test public void testRoundTripWatchingWallet() throws Exception { final String xpub = "tpubD9LrDvFDrB6wYNhbR2XcRRaT4yCa37TjBR3YthBQvrtEwEq6CKeEXUs3TppQd38rfxmxD1qLkC99iP3vKcKwLESSSYdFAftbrpuhSnsw6XM"; final long creationTimeSeconds = 1457019819; Wallet wallet = Wallet.fromWatchingKeyB58(PARAMS, xpub, creationTimeSeconds); Wallet wallet2 = roundTrip(wallet); Wallet wallet3 = roundTrip(wallet2); assertEquals(xpub, wallet.getWatchingKey().serializePubB58(PARAMS)); assertEquals(creationTimeSeconds, wallet.getWatchingKey().getCreationTimeSeconds()); assertEquals(creationTimeSeconds, wallet2.getWatchingKey().getCreationTimeSeconds()); assertEquals(creationTimeSeconds, wallet3.getWatchingKey().getCreationTimeSeconds()); assertEquals(creationTimeSeconds, wallet.getEarliestKeyCreationTime()); assertEquals(creationTimeSeconds, wallet2.getEarliestKeyCreationTime()); assertEquals(creationTimeSeconds, wallet3.getEarliestKeyCreationTime()); }
@Test public void watchingWallet() throws Exception { DeterministicKey watchKey = wallet.getWatchingKey(); String serialized = watchKey.serializePubB58(PARAMS); // Construct watching wallet. Wallet watchingWallet = Wallet.fromWatchingKey(PARAMS, DeterministicKey.deserializeB58(null, serialized, PARAMS)); DeterministicKey key2 = watchingWallet.freshReceiveKey(); assertEquals(myKey, key2); ECKey key = wallet.freshKey(KeyChain.KeyPurpose.CHANGE); key2 = watchingWallet.freshKey(KeyChain.KeyPurpose.CHANGE); assertEquals(key, key2); key.sign(Sha256Hash.ZERO_HASH); try { key2.sign(Sha256Hash.ZERO_HASH); fail(); } catch (ECKey.MissingPrivateKeyException e) { // Expected } receiveATransaction(watchingWallet, myKey.toAddress(PARAMS)); assertEquals(COIN, watchingWallet.getBalance()); assertEquals(COIN, watchingWallet.getBalance(Wallet.BalanceType.AVAILABLE)); assertEquals(ZERO, watchingWallet.getBalance(Wallet.BalanceType.AVAILABLE_SPENDABLE)); }