/** * Return a P2SH-P2WPKH address for this key * This is the recommended way of implementing segwit atm */ public Address toSegwitAddress(NetworkParameters params) { return Address.fromP2SHScript(params, getSegwitScript()); }
/** * Returns address for a {@link #freshKey(KeyChain.KeyPurpose)} */ public Address freshAddress(KeyChain.KeyPurpose purpose) { DeterministicKeyChain chain = getActiveKeyChain(); if (chain.isMarried()) { Script outputScript = chain.freshOutputScript(purpose); checkState(outputScript.isPayToScriptHash()); // Only handle P2SH for now Address freshAddress = Address.fromP2SHScript(params, outputScript); maybeLookaheadScripts(); currentAddresses.put(purpose, freshAddress); return freshAddress; } else { return freshKey(purpose).toAddress(params); } }
/** * Returns address for a {@link #freshKey(KeyChain.KeyPurpose)} */ public Address freshAddress(KeyChain.KeyPurpose purpose) { DeterministicKeyChain chain = getActiveKeyChain(); if (chain.isMarried()) { Script outputScript = chain.freshOutputScript(purpose); checkState(outputScript.isPayToScriptHash()); // Only handle P2SH for now Address freshAddress = Address.fromP2SHScript(params, outputScript); maybeLookaheadScripts(); currentAddresses.put(purpose, freshAddress); return freshAddress; } else { return freshKey(purpose).toAddress(params); } }
/** * Returns address for a {@link #freshKey(KeyChain.KeyPurpose)} */ public Address freshAddress(KeyChain.KeyPurpose purpose) { DeterministicKeyChain chain = getActiveKeyChain(); if (chain.isMarried()) { Script outputScript = chain.freshOutputScript(purpose); checkState(outputScript.isPayToScriptHash()); // Only handle P2SH for now Address freshAddress = Address.fromP2SHScript(params, outputScript); maybeLookaheadScripts(); currentAddresses.put(purpose, freshAddress); return freshAddress; } else { return freshKey(purpose).toAddress(params); } }
/** * Returns address for a {@link #freshKey(KeyChain.KeyPurpose)} */ public Address freshAddress(KeyChain.KeyPurpose purpose) { DeterministicKeyChain chain = getActiveKeyChain(); if (chain.isMarried()) { Script outputScript = chain.freshOutputScript(purpose); checkState(outputScript.isPayToScriptHash()); // Only handle P2SH for now Address freshAddress = Address.fromP2SHScript(params, outputScript); maybeLookaheadScripts(); currentAddresses.put(purpose, freshAddress); return freshAddress; } else { return useSegwit ? freshKey(purpose).toSegwitAddress(params) : freshKey(purpose).toAddress(params); } }
/** * Marks all keys used in the transaction output as used in the wallet. * See {@link org.bitcoinj.wallet.DeterministicKeyChain#markKeyAsUsed(DeterministicKey)} for more info on this. */ private void markKeysAsUsed(Transaction tx) { keyChainGroupLock.lock(); try { for (TransactionOutput o : tx.getOutputs()) { try { Script script = o.getScriptPubKey(); if (script.isSentToRawPubKey()) { byte[] pubkey = script.getPubKey(); keyChainGroup.markPubKeyAsUsed(pubkey); } else if (script.isSentToAddress()) { byte[] pubkeyHash = script.getPubKeyHash(); keyChainGroup.markPubKeyHashAsUsed(pubkeyHash); } else if (script.isPayToScriptHash()) { Address a = Address.fromP2SHScript(tx.getParams(), script); keyChainGroup.markP2SHAddressAsUsed(a); } } catch (ScriptException e) { // Just means we didn't understand the output of this transaction: ignore it. log.warn("Could not parse tx output script: {}", e.toString()); } } } finally { keyChainGroupLock.unlock(); } }
/** * Marks all keys used in the transaction output as used in the wallet. * See {@link org.bitcoinj.wallet.DeterministicKeyChain#markKeyAsUsed(DeterministicKey)} for more info on this. */ private void markKeysAsUsed(Transaction tx) { keyChainGroupLock.lock(); try { for (TransactionOutput o : tx.getOutputs()) { try { Script script = o.getScriptPubKey(); if (script.isSentToRawPubKey()) { byte[] pubkey = script.getPubKey(); keyChainGroup.markPubKeyAsUsed(pubkey); } else if (script.isSentToAddress()) { byte[] pubkeyHash = script.getPubKeyHash(); keyChainGroup.markPubKeyHashAsUsed(pubkeyHash); } else if (script.isPayToScriptHash()) { Address a = Address.fromP2SHScript(tx.getParams(), script); keyChainGroup.markP2SHAddressAsUsed(a); } } catch (ScriptException e) { // Just means we didn't understand the output of this transaction: ignore it. log.warn("Could not parse tx output script: {}", e.toString()); } } } finally { keyChainGroupLock.unlock(); } }
/** * Marks all keys used in the transaction output as used in the wallet. * See {@link org.bitcoinj.wallet.DeterministicKeyChain#markKeyAsUsed(DeterministicKey)} for more info on this. */ private void markKeysAsUsed(Transaction tx) { keyChainGroupLock.lock(); try { for (TransactionOutput o : tx.getOutputs()) { try { Script script = o.getScriptPubKey(); if (script.isSentToRawPubKey()) { byte[] pubkey = script.getPubKey(); keyChainGroup.markPubKeyAsUsed(pubkey); } else if (script.isSentToAddress()) { byte[] pubkeyHash = script.getPubKeyHash(); keyChainGroup.markPubKeyHashAsUsed(pubkeyHash); } else if (script.isPayToScriptHash()) { Address a = Address.fromP2SHScript(tx.getParams(), script); keyChainGroup.markP2SHAddressAsUsed(a); } } catch (ScriptException e) { // Just means we didn't understand the output of this transaction: ignore it. log.warn("Could not parse tx output script: {}", e.toString()); } } } finally { keyChainGroupLock.unlock(); } }
/** * Marks all keys used in the transaction output as used in the wallet. * See {@link org.bitcoinj.wallet.DeterministicKeyChain#markKeyAsUsed(DeterministicKey)} for more info on this. */ private void markKeysAsUsed(Transaction tx) { keyChainGroupLock.lock(); try { for (TransactionOutput o : tx.getOutputs()) { try { Script script = o.getScriptPubKey(); if (script.isSentToRawPubKey()) { byte[] pubkey = script.getPubKey(); keyChainGroup.markPubKeyAsUsed(pubkey); } else if (script.isSentToAddress()) { byte[] pubkeyHash = script.getPubKeyHash(); keyChainGroup.markPubKeyHashAsUsed(pubkeyHash); } else if (script.isPayToScriptHash()) { Address a = Address.fromP2SHScript(tx.getParams(), script); keyChainGroup.markP2SHAddressAsUsed(a); } } catch (ScriptException e) { // Just means we didn't understand the output of this transaction: ignore it. log.warn("Could not parse tx output script: {}", e.toString()); } } } finally { keyChainGroupLock.unlock(); } }
/** * Gets the destination address from this script, if it's in the required form (see getPubKey). * * @param forcePayToPubKey * If true, allow payToPubKey to be casted to the corresponding address. This is useful if you prefer * showing addresses rather than pubkeys. */ public Address getToAddress(NetworkParameters params, boolean forcePayToPubKey) throws ScriptException { if (isSentToAddress()) return new Address(params, getPubKeyHash()); else if (isPayToScriptHash()) return Address.fromP2SHScript(params, this); else if (forcePayToPubKey && isSentToRawPubKey()) return ECKey.fromPublicOnly(getPubKey()).toAddress(params); else throw new ScriptException("Cannot cast this script to a pay-to-address type"); }
/** * Gets the destination address from this script, if it's in the required form (see getPubKey). * * @param forcePayToPubKey * If true, allow payToPubKey to be casted to the corresponding address. This is useful if you prefer * showing addresses rather than pubkeys. */ public Address getToAddress(NetworkParameters params, boolean forcePayToPubKey) throws ScriptException { if (isSentToAddress()) return new Address(params, getPubKeyHash()); else if (isPayToScriptHash()) return Address.fromP2SHScript(params, this); else if (forcePayToPubKey && isSentToRawPubKey()) return ECKey.fromPublicOnly(getPubKey()).toAddress(params); else throw new ScriptException("Cannot cast this script to a pay-to-address type"); }
/** * Gets the destination address from this script, if it's in the required form (see getPubKey). * * @param forcePayToPubKey * If true, allow payToPubKey to be casted to the corresponding address. This is useful if you prefer * showing addresses rather than pubkeys. */ public Address getToAddress(NetworkParameters params, boolean forcePayToPubKey) throws ScriptException { if (isSentToAddress()) return new Address(params, getPubKeyHash()); else if (isPayToScriptHash()) return Address.fromP2SHScript(params, this); else if (forcePayToPubKey && isSentToRawPubKey()) return ECKey.fromPublicOnly(getPubKey()).toAddress(params); else throw new ScriptException(ScriptError.SCRIPT_ERR_UNKNOWN_ERROR, "Cannot cast this script to a pay-to-address type"); }
/** * Gets the destination address from this script, if it's in the required form (see getPubKey). * * @param forcePayToPubKey * If true, allow payToPubKey to be casted to the corresponding address. This is useful if you prefer * showing addresses rather than pubkeys. */ public Address getToAddress(NetworkParameters params, boolean forcePayToPubKey) throws ScriptException { if (isSentToAddress()) return new Address(params, getPubKeyHash()); else if (isPayToScriptHash()) return Address.fromP2SHScript(params, this); else if (isSentToP2WPKH()) return Address.fromP2WPKHHash(params, getPubKeyHash()); else if (isSentToP2WSH()) return Address.fromP2WSHHash(params, getPubKeyHash()); else if (forcePayToPubKey && isSentToRawPubKey()) return ECKey.fromPublicOnly(getPubKey()).toAddress(params); else throw new ScriptException("Cannot cast this script to a pay-to-address type"); }
@Test public void p2shAddressCreationFromKeys() throws Exception { // import some keys from this example: https://gist.github.com/gavinandresen/3966071 ECKey key1 = DumpedPrivateKey.fromBase58(mainParams, "5JaTXbAUmfPYZFRwrYaALK48fN6sFJp4rHqq2QSXs8ucfpE4yQU").getKey(); key1 = ECKey.fromPrivate(key1.getPrivKeyBytes()); ECKey key2 = DumpedPrivateKey.fromBase58(mainParams, "5Jb7fCeh1Wtm4yBBg3q3XbT6B525i17kVhy3vMC9AqfR6FH2qGk").getKey(); key2 = ECKey.fromPrivate(key2.getPrivKeyBytes()); ECKey key3 = DumpedPrivateKey.fromBase58(mainParams, "5JFjmGo5Fww9p8gvx48qBYDJNAzR9pmH5S389axMtDyPT8ddqmw").getKey(); key3 = ECKey.fromPrivate(key3.getPrivKeyBytes()); List<ECKey> keys = Arrays.asList(key1, key2, key3); Script p2shScript = ScriptBuilder.createP2SHOutputScript(2, keys); Address address = Address.fromP2SHScript(mainParams, p2shScript); assertEquals("3N25saC4dT24RphDAwLtD8LUN4E2gZPJke", address.toString()); }
@Test public void getToAddress() throws Exception { // pay to pubkey ECKey toKey = new ECKey(); Address toAddress = toKey.toAddress(PARAMS); assertEquals(toAddress, ScriptBuilder.createOutputScript(toKey).getToAddress(PARAMS, true)); // pay to pubkey hash assertEquals(toAddress, ScriptBuilder.createOutputScript(toAddress).getToAddress(PARAMS, true)); // pay to script hash Script p2shScript = ScriptBuilder.createP2SHOutputScript(new byte[20]); Address scriptAddress = Address.fromP2SHScript(PARAMS, p2shScript); assertEquals(scriptAddress, p2shScript.getToAddress(PARAMS, true)); }
@Test public void p2shAddress() throws Exception { // Test that we can construct P2SH addresses Address mainNetP2SHAddress = Address.fromBase58(MainNetParams.get(), "35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU"); assertEquals(mainNetP2SHAddress.version, MainNetParams.get().p2shHeader); assertTrue(mainNetP2SHAddress.isP2SHAddress()); Address testNetP2SHAddress = Address.fromBase58(TestNet3Params.get(), "2MuVSxtfivPKJe93EC1Tb9UhJtGhsoWEHCe"); assertEquals(testNetP2SHAddress.version, TestNet3Params.get().p2shHeader); assertTrue(testNetP2SHAddress.isP2SHAddress()); // Test that we can determine what network a P2SH address belongs to NetworkParameters mainNetParams = Address.getParametersFromAddress("35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU"); assertEquals(MainNetParams.get().getId(), mainNetParams.getId()); NetworkParameters testNetParams = Address.getParametersFromAddress("2MuVSxtfivPKJe93EC1Tb9UhJtGhsoWEHCe"); assertEquals(TestNet3Params.get().getId(), testNetParams.getId()); // Test that we can convert them from hashes byte[] hex = HEX.decode("2ac4b0b501117cc8119c5797b519538d4942e90e"); Address a = Address.fromP2SHHash(mainParams, hex); assertEquals("35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU", a.toString()); Address b = Address.fromP2SHHash(testParams, HEX.decode("18a0e827269b5211eb51a4af1b2fa69333efa722")); assertEquals("2MuVSxtfivPKJe93EC1Tb9UhJtGhsoWEHCe", b.toString()); Address c = Address.fromP2SHScript(mainParams, ScriptBuilder.createP2SHOutputScript(hex)); assertEquals("35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU", c.toString()); }