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

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

本文整理了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

/**
 * 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());
}

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

/**
 * 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);
  }
}

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

/**
 * 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);
  }
}

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

/**
 * 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);
  }
}

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

/**
 * 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);
  }
}

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

/**
 * 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();
  }
}

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

/**
 * 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();
  }
}

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

/**
 * 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();
  }
}

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

/**
 * 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();
  }
}

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

/**
 * 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");
}

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

/**
 * 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");
}

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

/**
 * 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");
}

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

/**
 * 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");
}

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

@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());
}

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

@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));
}

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

@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());
}

相关文章