org.bitcoinj.core.Address.fromP2SHScript()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(12.5k)|赞(0)|评价(0)|浏览(172)

本文整理了Java中org.bitcoinj.core.Address.fromP2SHScript()方法的一些代码示例,展示了Address.fromP2SHScript()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Address.fromP2SHScript()方法的具体详情如下:
包路径:org.bitcoinj.core.Address
类名称:Address
方法名:fromP2SHScript

Address.fromP2SHScript介绍

[英]Returns an Address that represents the script hash extracted from the given scriptPubKey
[中]返回表示从给定scriptPubKey提取的脚本哈希的地址

代码示例

代码示例来源:origin: fr.acinq/bitcoinj-core

  1. /**
  2. * Return a P2SH-P2WPKH address for this key
  3. * This is the recommended way of implementing segwit atm
  4. */
  5. public Address toSegwitAddress(NetworkParameters params) {
  6. return Address.fromP2SHScript(params, getSegwitScript());
  7. }

代码示例来源:origin: cash.bitcoinj/bitcoinj-core

  1. /**
  2. * Returns address for a {@link #freshKey(KeyChain.KeyPurpose)}
  3. */
  4. public Address freshAddress(KeyChain.KeyPurpose purpose) {
  5. DeterministicKeyChain chain = getActiveKeyChain();
  6. if (chain.isMarried()) {
  7. Script outputScript = chain.freshOutputScript(purpose);
  8. checkState(outputScript.isPayToScriptHash()); // Only handle P2SH for now
  9. Address freshAddress = Address.fromP2SHScript(params, outputScript);
  10. maybeLookaheadScripts();
  11. currentAddresses.put(purpose, freshAddress);
  12. return freshAddress;
  13. } else {
  14. return freshKey(purpose).toAddress(params);
  15. }
  16. }

代码示例来源:origin: greenaddress/GreenBits

  1. /**
  2. * Returns address for a {@link #freshKey(KeyChain.KeyPurpose)}
  3. */
  4. public Address freshAddress(KeyChain.KeyPurpose purpose) {
  5. DeterministicKeyChain chain = getActiveKeyChain();
  6. if (chain.isMarried()) {
  7. Script outputScript = chain.freshOutputScript(purpose);
  8. checkState(outputScript.isPayToScriptHash()); // Only handle P2SH for now
  9. Address freshAddress = Address.fromP2SHScript(params, outputScript);
  10. maybeLookaheadScripts();
  11. currentAddresses.put(purpose, freshAddress);
  12. return freshAddress;
  13. } else {
  14. return freshKey(purpose).toAddress(params);
  15. }
  16. }

代码示例来源:origin: HashEngineering/dashj

  1. /**
  2. * Returns address for a {@link #freshKey(KeyChain.KeyPurpose)}
  3. */
  4. public Address freshAddress(KeyChain.KeyPurpose purpose) {
  5. DeterministicKeyChain chain = getActiveKeyChain();
  6. if (chain.isMarried()) {
  7. Script outputScript = chain.freshOutputScript(purpose);
  8. checkState(outputScript.isPayToScriptHash()); // Only handle P2SH for now
  9. Address freshAddress = Address.fromP2SHScript(params, outputScript);
  10. maybeLookaheadScripts();
  11. currentAddresses.put(purpose, freshAddress);
  12. return freshAddress;
  13. } else {
  14. return freshKey(purpose).toAddress(params);
  15. }
  16. }

代码示例来源:origin: fr.acinq/bitcoinj-core

  1. /**
  2. * Returns address for a {@link #freshKey(KeyChain.KeyPurpose)}
  3. */
  4. public Address freshAddress(KeyChain.KeyPurpose purpose) {
  5. DeterministicKeyChain chain = getActiveKeyChain();
  6. if (chain.isMarried()) {
  7. Script outputScript = chain.freshOutputScript(purpose);
  8. checkState(outputScript.isPayToScriptHash()); // Only handle P2SH for now
  9. Address freshAddress = Address.fromP2SHScript(params, outputScript);
  10. maybeLookaheadScripts();
  11. currentAddresses.put(purpose, freshAddress);
  12. return freshAddress;
  13. } else {
  14. return useSegwit ? freshKey(purpose).toSegwitAddress(params) : freshKey(purpose).toAddress(params);
  15. }
  16. }

代码示例来源:origin: fr.acinq/bitcoinj-core

  1. /**
  2. * Marks all keys used in the transaction output as used in the wallet.
  3. * See {@link org.bitcoinj.wallet.DeterministicKeyChain#markKeyAsUsed(DeterministicKey)} for more info on this.
  4. */
  5. private void markKeysAsUsed(Transaction tx) {
  6. keyChainGroupLock.lock();
  7. try {
  8. for (TransactionOutput o : tx.getOutputs()) {
  9. try {
  10. Script script = o.getScriptPubKey();
  11. if (script.isSentToRawPubKey()) {
  12. byte[] pubkey = script.getPubKey();
  13. keyChainGroup.markPubKeyAsUsed(pubkey);
  14. } else if (script.isSentToAddress()) {
  15. byte[] pubkeyHash = script.getPubKeyHash();
  16. keyChainGroup.markPubKeyHashAsUsed(pubkeyHash);
  17. } else if (script.isPayToScriptHash()) {
  18. Address a = Address.fromP2SHScript(tx.getParams(), script);
  19. keyChainGroup.markP2SHAddressAsUsed(a);
  20. }
  21. } catch (ScriptException e) {
  22. // Just means we didn't understand the output of this transaction: ignore it.
  23. log.warn("Could not parse tx output script: {}", e.toString());
  24. }
  25. }
  26. } finally {
  27. keyChainGroupLock.unlock();
  28. }
  29. }

代码示例来源:origin: cash.bitcoinj/bitcoinj-core

  1. /**
  2. * Marks all keys used in the transaction output as used in the wallet.
  3. * See {@link org.bitcoinj.wallet.DeterministicKeyChain#markKeyAsUsed(DeterministicKey)} for more info on this.
  4. */
  5. private void markKeysAsUsed(Transaction tx) {
  6. keyChainGroupLock.lock();
  7. try {
  8. for (TransactionOutput o : tx.getOutputs()) {
  9. try {
  10. Script script = o.getScriptPubKey();
  11. if (script.isSentToRawPubKey()) {
  12. byte[] pubkey = script.getPubKey();
  13. keyChainGroup.markPubKeyAsUsed(pubkey);
  14. } else if (script.isSentToAddress()) {
  15. byte[] pubkeyHash = script.getPubKeyHash();
  16. keyChainGroup.markPubKeyHashAsUsed(pubkeyHash);
  17. } else if (script.isPayToScriptHash()) {
  18. Address a = Address.fromP2SHScript(tx.getParams(), script);
  19. keyChainGroup.markP2SHAddressAsUsed(a);
  20. }
  21. } catch (ScriptException e) {
  22. // Just means we didn't understand the output of this transaction: ignore it.
  23. log.warn("Could not parse tx output script: {}", e.toString());
  24. }
  25. }
  26. } finally {
  27. keyChainGroupLock.unlock();
  28. }
  29. }

代码示例来源:origin: HashEngineering/dashj

  1. /**
  2. * Marks all keys used in the transaction output as used in the wallet.
  3. * See {@link org.bitcoinj.wallet.DeterministicKeyChain#markKeyAsUsed(DeterministicKey)} for more info on this.
  4. */
  5. private void markKeysAsUsed(Transaction tx) {
  6. keyChainGroupLock.lock();
  7. try {
  8. for (TransactionOutput o : tx.getOutputs()) {
  9. try {
  10. Script script = o.getScriptPubKey();
  11. if (script.isSentToRawPubKey()) {
  12. byte[] pubkey = script.getPubKey();
  13. keyChainGroup.markPubKeyAsUsed(pubkey);
  14. } else if (script.isSentToAddress()) {
  15. byte[] pubkeyHash = script.getPubKeyHash();
  16. keyChainGroup.markPubKeyHashAsUsed(pubkeyHash);
  17. } else if (script.isPayToScriptHash()) {
  18. Address a = Address.fromP2SHScript(tx.getParams(), script);
  19. keyChainGroup.markP2SHAddressAsUsed(a);
  20. }
  21. } catch (ScriptException e) {
  22. // Just means we didn't understand the output of this transaction: ignore it.
  23. log.warn("Could not parse tx output script: {}", e.toString());
  24. }
  25. }
  26. } finally {
  27. keyChainGroupLock.unlock();
  28. }
  29. }

代码示例来源:origin: greenaddress/GreenBits

  1. /**
  2. * Marks all keys used in the transaction output as used in the wallet.
  3. * See {@link org.bitcoinj.wallet.DeterministicKeyChain#markKeyAsUsed(DeterministicKey)} for more info on this.
  4. */
  5. private void markKeysAsUsed(Transaction tx) {
  6. keyChainGroupLock.lock();
  7. try {
  8. for (TransactionOutput o : tx.getOutputs()) {
  9. try {
  10. Script script = o.getScriptPubKey();
  11. if (script.isSentToRawPubKey()) {
  12. byte[] pubkey = script.getPubKey();
  13. keyChainGroup.markPubKeyAsUsed(pubkey);
  14. } else if (script.isSentToAddress()) {
  15. byte[] pubkeyHash = script.getPubKeyHash();
  16. keyChainGroup.markPubKeyHashAsUsed(pubkeyHash);
  17. } else if (script.isPayToScriptHash()) {
  18. Address a = Address.fromP2SHScript(tx.getParams(), script);
  19. keyChainGroup.markP2SHAddressAsUsed(a);
  20. }
  21. } catch (ScriptException e) {
  22. // Just means we didn't understand the output of this transaction: ignore it.
  23. log.warn("Could not parse tx output script: {}", e.toString());
  24. }
  25. }
  26. } finally {
  27. keyChainGroupLock.unlock();
  28. }
  29. }

代码示例来源:origin: cash.bitcoinj/bitcoinj-core

  1. /**
  2. * Gets the destination address from this script, if it's in the required form (see getPubKey).
  3. *
  4. * @param forcePayToPubKey
  5. * If true, allow payToPubKey to be casted to the corresponding address. This is useful if you prefer
  6. * showing addresses rather than pubkeys.
  7. */
  8. public Address getToAddress(NetworkParameters params, boolean forcePayToPubKey) throws ScriptException {
  9. if (isSentToAddress())
  10. return new Address(params, getPubKeyHash());
  11. else if (isPayToScriptHash())
  12. return Address.fromP2SHScript(params, this);
  13. else if (forcePayToPubKey && isSentToRawPubKey())
  14. return ECKey.fromPublicOnly(getPubKey()).toAddress(params);
  15. else
  16. throw new ScriptException("Cannot cast this script to a pay-to-address type");
  17. }

代码示例来源:origin: HashEngineering/dashj

  1. /**
  2. * Gets the destination address from this script, if it's in the required form (see getPubKey).
  3. *
  4. * @param forcePayToPubKey
  5. * If true, allow payToPubKey to be casted to the corresponding address. This is useful if you prefer
  6. * showing addresses rather than pubkeys.
  7. */
  8. public Address getToAddress(NetworkParameters params, boolean forcePayToPubKey) throws ScriptException {
  9. if (isSentToAddress())
  10. return new Address(params, getPubKeyHash());
  11. else if (isPayToScriptHash())
  12. return Address.fromP2SHScript(params, this);
  13. else if (forcePayToPubKey && isSentToRawPubKey())
  14. return ECKey.fromPublicOnly(getPubKey()).toAddress(params);
  15. else
  16. throw new ScriptException("Cannot cast this script to a pay-to-address type");
  17. }

代码示例来源:origin: fr.acinq/bitcoinj-core

  1. /**
  2. * Gets the destination address from this script, if it's in the required form (see getPubKey).
  3. *
  4. * @param forcePayToPubKey
  5. * If true, allow payToPubKey to be casted to the corresponding address. This is useful if you prefer
  6. * showing addresses rather than pubkeys.
  7. */
  8. public Address getToAddress(NetworkParameters params, boolean forcePayToPubKey) throws ScriptException {
  9. if (isSentToAddress())
  10. return new Address(params, getPubKeyHash());
  11. else if (isPayToScriptHash())
  12. return Address.fromP2SHScript(params, this);
  13. else if (forcePayToPubKey && isSentToRawPubKey())
  14. return ECKey.fromPublicOnly(getPubKey()).toAddress(params);
  15. else
  16. throw new ScriptException(ScriptError.SCRIPT_ERR_UNKNOWN_ERROR, "Cannot cast this script to a pay-to-address type");
  17. }

代码示例来源:origin: greenaddress/GreenBits

  1. /**
  2. * Gets the destination address from this script, if it's in the required form (see getPubKey).
  3. *
  4. * @param forcePayToPubKey
  5. * If true, allow payToPubKey to be casted to the corresponding address. This is useful if you prefer
  6. * showing addresses rather than pubkeys.
  7. */
  8. public Address getToAddress(NetworkParameters params, boolean forcePayToPubKey) throws ScriptException {
  9. if (isSentToAddress())
  10. return new Address(params, getPubKeyHash());
  11. else if (isPayToScriptHash())
  12. return Address.fromP2SHScript(params, this);
  13. else if (isSentToP2WPKH())
  14. return Address.fromP2WPKHHash(params, getPubKeyHash());
  15. else if (isSentToP2WSH())
  16. return Address.fromP2WSHHash(params, getPubKeyHash());
  17. else if (forcePayToPubKey && isSentToRawPubKey())
  18. return ECKey.fromPublicOnly(getPubKey()).toAddress(params);
  19. else
  20. throw new ScriptException("Cannot cast this script to a pay-to-address type");
  21. }

代码示例来源:origin: greenaddress/GreenBits

  1. @Test
  2. public void p2shAddressCreationFromKeys() throws Exception {
  3. // import some keys from this example: https://gist.github.com/gavinandresen/3966071
  4. ECKey key1 = DumpedPrivateKey.fromBase58(mainParams, "5JaTXbAUmfPYZFRwrYaALK48fN6sFJp4rHqq2QSXs8ucfpE4yQU").getKey();
  5. key1 = ECKey.fromPrivate(key1.getPrivKeyBytes());
  6. ECKey key2 = DumpedPrivateKey.fromBase58(mainParams, "5Jb7fCeh1Wtm4yBBg3q3XbT6B525i17kVhy3vMC9AqfR6FH2qGk").getKey();
  7. key2 = ECKey.fromPrivate(key2.getPrivKeyBytes());
  8. ECKey key3 = DumpedPrivateKey.fromBase58(mainParams, "5JFjmGo5Fww9p8gvx48qBYDJNAzR9pmH5S389axMtDyPT8ddqmw").getKey();
  9. key3 = ECKey.fromPrivate(key3.getPrivKeyBytes());
  10. List<ECKey> keys = Arrays.asList(key1, key2, key3);
  11. Script p2shScript = ScriptBuilder.createP2SHOutputScript(2, keys);
  12. Address address = Address.fromP2SHScript(mainParams, p2shScript);
  13. assertEquals("3N25saC4dT24RphDAwLtD8LUN4E2gZPJke", address.toString());
  14. }

代码示例来源:origin: greenaddress/GreenBits

  1. @Test
  2. public void getToAddress() throws Exception {
  3. // pay to pubkey
  4. ECKey toKey = new ECKey();
  5. Address toAddress = toKey.toAddress(PARAMS);
  6. assertEquals(toAddress, ScriptBuilder.createOutputScript(toKey).getToAddress(PARAMS, true));
  7. // pay to pubkey hash
  8. assertEquals(toAddress, ScriptBuilder.createOutputScript(toAddress).getToAddress(PARAMS, true));
  9. // pay to script hash
  10. Script p2shScript = ScriptBuilder.createP2SHOutputScript(new byte[20]);
  11. Address scriptAddress = Address.fromP2SHScript(PARAMS, p2shScript);
  12. assertEquals(scriptAddress, p2shScript.getToAddress(PARAMS, true));
  13. }

代码示例来源:origin: greenaddress/GreenBits

  1. @Test
  2. public void p2shAddress() throws Exception {
  3. // Test that we can construct P2SH addresses
  4. Address mainNetP2SHAddress = Address.fromBase58(MainNetParams.get(), "35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU");
  5. assertEquals(mainNetP2SHAddress.version, MainNetParams.get().p2shHeader);
  6. assertTrue(mainNetP2SHAddress.isP2SHAddress());
  7. Address testNetP2SHAddress = Address.fromBase58(TestNet3Params.get(), "2MuVSxtfivPKJe93EC1Tb9UhJtGhsoWEHCe");
  8. assertEquals(testNetP2SHAddress.version, TestNet3Params.get().p2shHeader);
  9. assertTrue(testNetP2SHAddress.isP2SHAddress());
  10. // Test that we can determine what network a P2SH address belongs to
  11. NetworkParameters mainNetParams = Address.getParametersFromAddress("35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU");
  12. assertEquals(MainNetParams.get().getId(), mainNetParams.getId());
  13. NetworkParameters testNetParams = Address.getParametersFromAddress("2MuVSxtfivPKJe93EC1Tb9UhJtGhsoWEHCe");
  14. assertEquals(TestNet3Params.get().getId(), testNetParams.getId());
  15. // Test that we can convert them from hashes
  16. byte[] hex = HEX.decode("2ac4b0b501117cc8119c5797b519538d4942e90e");
  17. Address a = Address.fromP2SHHash(mainParams, hex);
  18. assertEquals("35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU", a.toString());
  19. Address b = Address.fromP2SHHash(testParams, HEX.decode("18a0e827269b5211eb51a4af1b2fa69333efa722"));
  20. assertEquals("2MuVSxtfivPKJe93EC1Tb9UhJtGhsoWEHCe", b.toString());
  21. Address c = Address.fromP2SHScript(mainParams, ScriptBuilder.createP2SHOutputScript(hex));
  22. assertEquals("35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU", c.toString());
  23. }

相关文章