Java Code Examples for org.bitcoinj.core.Utils#sha256hash160()

The following examples show how to use org.bitcoinj.core.Utils#sha256hash160() . 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: SignedWitnessTest.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
@Before
public void setUp() {
    arbitrator1Key = new ECKey();
    witnessOwner1PubKey = Sig.getPublicKeyBytes(Sig.generateKeyPair().getPublic());
    witnessHash = Utils.sha256hash160(new byte[]{1});
    witnessHashSignature = arbitrator1Key.signMessage(Utilities.encodeToHex(witnessHash)).getBytes(Charsets.UTF_8);
}
 
Example 2
Source File: ScriptBuilder.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Creates a scriptPubKey for the given redeem script.
 */
public static Script createP2SHOutputScript(Script redeemScript) {
    byte[] hash = Utils.sha256hash160(redeemScript.getProgram());
    return ScriptBuilder.createP2SHOutputScript(hash);
}
 
Example 3
Source File: LedgerKey.java    From java-sdk with Apache License 2.0 4 votes vote down vote up
public String getAddress() {
    byte[] hash = Utils.sha256hash160(this.pubKey);
    return Crypto.encodeAddress(hrp, hash);
}
 
Example 4
Source File: SegWitWalletTest.java    From token-core-android with Apache License 2.0 4 votes vote down vote up
@Test
public void transactionSignTest() {
  String address = "1Fyxts6r24DpEieygQiNnWxUdb18ANa5p7";
  byte[] hash160 = Address.fromBase58(MainNetParams.get(), address).getHash160();
  System.out.println("Public Key: " + NumericUtil.bytesToHex(hash160));

  String privateKey = "eb696a065ef48a2192da5b28b694f87544b30fae8327c4510137a922f32c6dcf";
  ECKey ecKey = ECKey.fromPrivate(NumericUtil.hexToBytes(privateKey), true);
  assertEquals("public key", "03ad1d8e89212f0b92c74d23bb710c00662ad1470198ac48c43f7d6f93a2a26873", ecKey.getPublicKeyAsHex());
  byte[] pubKeyHash = ecKey.getPubKeyHash();
  String redeemScript = String.format("0x0014%s", NumericUtil.bytesToHex(pubKeyHash));
  assertEquals("redeem script", "0x001479091972186c449eb1ded22b78e40d009bdf0089", redeemScript);
  byte[] redeemScriptBytes = Utils.sha256hash160(NumericUtil.hexToBytes(redeemScript));
  byte[] scriptCode = NumericUtil.hexToBytes(String.format("0x1976a914%s88ac", NumericUtil.bytesToHex(pubKeyHash)));
  String scriptPub = Integer.toHexString(169) + Integer.toHexString(redeemScriptBytes.length) + NumericUtil.bytesToHex(redeemScriptBytes) + Integer.toHexString(135);
  assertEquals("scriptPubKey", "a9144733f37cf4db86fbc2efed2500b4f4e49f31202387", scriptPub);
  byte[] hashPrevouts = Sha256Hash.hashTwice(NumericUtil.hexToBytes("db6b1b20aa0fd7b23880be2ecbd4a98130974cf4748fb66092ac4d3ceb1a547701000000"));
  assertEquals("hash Prevouts", "b0287b4a252ac05af83d2dcef00ba313af78a3e9c329afa216eb3aa2a7b4613a", NumericUtil.bytesToHex(hashPrevouts));
  byte[] hashSequence = Sha256Hash.hashTwice(NumericUtil.hexToBytes("feffffff"));
  assertEquals("hashSequence", "18606b350cd8bf565266bc352f0caddcf01e8fa789dd8a15386327cf8cabe198", NumericUtil.bytesToHex(hashSequence));
  byte[] hashOutputs = Sha256Hash.hashTwice(NumericUtil.hexToBytes("b8b4eb0b000000001976a914a457b684d7f0d539a46a45bbc043f35b59d0d96388ac0008af2f000000001976a914fd270b1ee6abcaea97fea7ad0402e8bd8ad6d77c88ac"));
  assertEquals("hashOutputs", "de984f44532e2173ca0d64314fcefe6d30da6f8cf27bafa706da61df8a226c83", NumericUtil.bytesToHex(hashOutputs));

  UnsafeByteArrayOutputStream stream = new UnsafeByteArrayOutputStream();
  try {
    Utils.uint32ToByteStreamLE(1L, stream);
    stream.write(hashPrevouts);
    stream.write(hashSequence);
    stream.write(NumericUtil.hexToBytes("db6b1b20aa0fd7b23880be2ecbd4a98130974cf4748fb66092ac4d3ceb1a547701000000"));
    stream.write(scriptCode);
    stream.write(NumericUtil.hexToBytes("00ca9a3b00000000"));
    stream.write(NumericUtil.hexToBytes("feffffff"));
    stream.write(hashOutputs);
    Utils.uint32ToByteStreamLE(1170, stream);
    Utils.uint32ToByteStreamLE(1, stream);
    String hashPreimage = NumericUtil.bytesToHex(stream.toByteArray());
    String expectedHashPreimage = "01000000b0287b4a252ac05af83d2dcef00ba313af78a3e9c329afa216eb3aa2a7b4613a18606b350cd8bf565266bc352f0caddcf01e8fa789dd8a15386327cf8cabe198db6b1b20aa0fd7b23880be2ecbd4a98130974cf4748fb66092ac4d3ceb1a5477010000001976a91479091972186c449eb1ded22b78e40d009bdf008988ac00ca9a3b00000000feffffffde984f44532e2173ca0d64314fcefe6d30da6f8cf27bafa706da61df8a226c839204000001000000";
    assertEquals(hashPreimage, expectedHashPreimage);
    byte[] sigHash = Sha256Hash.hashTwice(stream.toByteArray());
    assertEquals("64f3b0f4dd2bb3aa1ce8566d220cc74dda9df97d8490cc81d89d735c92e59fb6", NumericUtil.bytesToHex(sigHash));
    ECKey.ECDSASignature signature = ecKey.sign(Sha256Hash.wrap(sigHash));
    byte hashType = 0x01;
    System.out.println(NumericUtil.bytesToHex(ByteUtil.concat(signature.encodeToDER(), new byte[]{hashType})));

  } catch (IOException e) {
    e.printStackTrace();
  }

}
 
Example 5
Source File: LedgerKey.java    From java-sdk with Apache License 2.0 4 votes vote down vote up
public String getAddress() {
    byte[] hash = Utils.sha256hash160(this.pubKey);
    return Crypto.encodeAddress(hrp, hash);
}
 
Example 6
Source File: Hash.java    From balzac with Apache License 2.0 4 votes vote down vote up
public static Hash hash160(byte[] bytes) {
    return new Hash(Utils.sha256hash160(bytes));
}
 
Example 7
Source File: Hash.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Calculates RIPEMD160(SHA256(data)).
 */
public static byte[] getSha256Ripemd160hash(byte[] data) {
    return Utils.sha256hash160(data);
}