Java Code Examples for net.bither.bitherj.utils.Utils#toP2SHAddress()

The following examples show how to use net.bither.bitherj.utils.Utils#toP2SHAddress() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Script.java    From bitherj with Apache License 2.0 6 votes vote down vote up
public String getFromAddress() throws ScriptException {
    if (this.chunks.size() == 2
            && this.chunks.get(0).data != null && this.chunks.get(0).data.length > 2
            && this.chunks.get(1).data != null && this.chunks.get(1).data.length > 2) {
        return Utils.toAddress(Utils.sha256hash160(this.chunks.get(1).data));
    } else if (this.chunks.size() >= 3 && this.chunks.get(0).opcode == OP_0) {
        boolean isPay2SHScript = true;
        for (int i = 1; i < this.chunks.size(); i++) {
            isPay2SHScript &= (this.chunks.get(i).data != null && this.chunks.get(i).data.length > 2);
        }
        if (isPay2SHScript) {
            return Utils.toP2SHAddress(Utils.sha256hash160(this.chunks.get(this.chunks.size() - 1).data));
        }
    }
    return null;
}
 
Example 2
Source File: Script.java    From bitherj with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the destination address from this script, if it's in the required form (see getPubKey).
 */
public String getToAddress() throws ScriptException {
    if (isSentToAddress())
        return Utils.toAddress(getPubKeyHash());
    else if (isSentToP2SH())
        return Utils.toP2SHAddress(this.getPubKeyHash());
    else
        throw new ScriptException("Cannot cast this script to a pay-to-address type");
}
 
Example 3
Source File: HDMAddress.java    From bitherj with Apache License 2.0 4 votes vote down vote up
public String getAddress() {
    return Utils.toP2SHAddress(Utils.sha256hash160(getMultiSigScript().getProgram()));
}
 
Example 4
Source File: EnterpriseHDMAddress.java    From bitherj with Apache License 2.0 4 votes vote down vote up
public String getAddress() {
    return Utils.toP2SHAddress(Utils.sha256hash160(getMultiSigScript().getProgram()));
}
 
Example 5
Source File: MultisigTest.java    From bitherj with Apache License 2.0 4 votes vote down vote up
@Test
public void testAddress() {
    String pubHot = "026e3f39cd82606a3aa0d9c8194cf516b98ee51c1107e6c7f334cde22b5059e928";
    String pubCold = "034d490441de1cc4a8f1e7192083583c16e513b3b550c8410500db7853fd1fa5fe";
    String pubRemote = "0255b72bc52dfa0ffc40742b1a3eb01858714341c1f72bc1f8fdc731098323e96e";
    String address = "3K2Cbzxfoxey8cbq1w2YutLzhvxByxvNxy";

    ECKey keyHot = new ECKey(null, Utils.hexStringToByteArray(pubHot));
    ECKey keyCold = new ECKey(null, Utils.hexStringToByteArray(pubCold));
    ECKey keyRemote = new ECKey(null, Utils.hexStringToByteArray(pubRemote));
    List<byte[]> pubKeyList = new ArrayList<byte[]>();
    pubKeyList.add(keyHot.getPubKey());
    pubKeyList.add(keyCold.getPubKey());
    pubKeyList.add(keyRemote.getPubKey());

    Script script = ScriptBuilder.createMultiSigOutputScript(2, pubKeyList);
    String multisigAddress = Utils.toP2SHAddress(Utils.sha256hash160(script.getProgram()));

    assertEquals(address, multisigAddress);

    pubHot = "033dc6dcf7d90cb8f4ee3adbc87bf55c700d6c32a74800af6de6e1af57f46bfc41";
    pubCold = "025ed1f76ae3fc0cb84782131594020e885a060daf9f55c199fdb299e7169779b9";
    pubRemote = "0378b509c95fd7aa30dc82c4bbe8b84dcb8bb7d7224d891cce7ccf454c79527b5d";
    address = "3ELN8yYSGoz4fTy8HSbfgLRoDWBU6p9zev";

    keyHot = new ECKey(null, Utils.hexStringToByteArray(pubHot));
    keyCold = new ECKey(null, Utils.hexStringToByteArray(pubCold));
    keyRemote = new ECKey(null, Utils.hexStringToByteArray(pubRemote));
    pubKeyList = new ArrayList<byte[]>();
    pubKeyList.add(keyHot.getPubKey());
    pubKeyList.add(keyCold.getPubKey());
    pubKeyList.add(keyRemote.getPubKey());

    script = ScriptBuilder.createMultiSigOutputScript(2, pubKeyList);
    multisigAddress = Utils.toP2SHAddress(Utils.sha256hash160(script.getProgram()));

    assertEquals(address, multisigAddress);

    pubHot = "03d29143a6b76d393075d620df9cf80bbb5eaceb2e2b57e5cc4704a6eb3c125a8d";
    pubCold = "03f7d2d484d903fa498d6069009e77ed9ad0842947a7a58441f9406a4728ae2240";
    pubRemote = "02a5fc2584b879fa5a7b04e67d7ab8abb3b08d7981f9f24b03e9353355162c2e04";
    address = "34RgHSRfg3P7FSk3YBbcWnHaMWxapMtrWf";

    keyHot = new ECKey(null, Utils.hexStringToByteArray(pubHot));
    keyCold = new ECKey(null, Utils.hexStringToByteArray(pubCold));
    keyRemote = new ECKey(null, Utils.hexStringToByteArray(pubRemote));
    pubKeyList = new ArrayList<byte[]>();
    pubKeyList.add(keyHot.getPubKey());
    pubKeyList.add(keyCold.getPubKey());
    pubKeyList.add(keyRemote.getPubKey());

    script = ScriptBuilder.createMultiSigOutputScript(2, pubKeyList);
    multisigAddress = Utils.toP2SHAddress(Utils.sha256hash160(script.getProgram()));

    assertEquals(address, multisigAddress);
}