Java Code Examples for org.bitcoinj.core.Sha256Hash#hashTwice()

The following examples show how to use org.bitcoinj.core.Sha256Hash#hashTwice() . 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: Main.java    From BitcoinWallet with MIT License 6 votes vote down vote up
private static void generateSegwitAddress(String address) {
    byte[] decoded = Utils.parseAsHexOrBase58(address);
    // We should throw off header byte that is 0 for Bitcoin (Main)
    byte[] pureBytes = new byte[20];
    System.arraycopy(decoded, 1, pureBytes, 0, 20);
    // Than we should prepend the following bytes:
    byte[] scriptSig = new byte[pureBytes.length + 2];
    scriptSig[0] = 0x00;
    scriptSig[1] = 0x14;
    System.arraycopy(pureBytes, 0, scriptSig, 2, pureBytes.length);
    byte[] addressBytes = org.bitcoinj.core.Utils.sha256hash160(scriptSig);
    // Here are the address bytes
    byte[] readyForAddress = new byte[addressBytes.length + 1 + 4];
    // prepending p2sh header:
    readyForAddress[0] = (byte) 5;
    System.arraycopy(addressBytes, 0, readyForAddress, 1, addressBytes.length);
    // But we should also append check sum:
    byte[] checkSum = Sha256Hash.hashTwice(readyForAddress, 0, addressBytes.length + 1);
    System.arraycopy(checkSum, 0, readyForAddress, addressBytes.length + 1, 4);
    // To get the final address:
    String segwitAddress = Base58.base58Encode(readyForAddress);
    System.out.println("segwit address:" + segwitAddress);
}
 
Example 2
Source File: EOSFormatter.java    From eosio-java with MIT License 5 votes vote down vote up
/**
 * Validate checksum by double Sha256
 *
 * @param inputKey - input key to validate
 * @param checkSumToValidate - checksum to validate with the checksum inside input key
 * @return This checksum returns whether the checksum comparison was invalid.
 */
private static boolean invalidSha256x2CheckSum(@NotNull byte[] inputKey,
        @NotNull byte[] checkSumToValidate) {
    if (inputKey.length == 0 || checkSumToValidate.length == 0) {
        throw new IllegalArgumentException(ErrorConstants.BASE58_EMPTY_CHECKSUM_OR_KEY);
    }

    byte[] sha256x2 = Sha256Hash.hashTwice(inputKey);
    byte[] checkSumFromInputKey = Arrays.copyOfRange(sha256x2, 0, CHECKSUM_BYTES);

    //This checksum returns whether the checksum comparison was invalid.
    return !Arrays.equals(checkSumToValidate, checkSumFromInputKey);
}
 
Example 3
Source File: EOSFormatter.java    From eosio-java with MIT License 5 votes vote down vote up
/**
 * Extracting checksum for Sha256x2 format
 *
 * @param pemKey - input pem key
 * @return checksum
 */
@NotNull
private static byte[] extractCheckSumSha256x2(@NotNull byte[] pemKey) {
    byte[] sha256x2 = Sha256Hash.hashTwice(pemKey);

    return Arrays.copyOfRange(sha256x2, 0, CHECKSUM_BYTES);
}
 
Example 4
Source File: Base58Util.java    From chain33-sdk-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Encodes the given version and bytes as a base58 string. A checksum is appended.
 * 
 * @param version the version to encode
 * @param payload the bytes to encode, e.g. pubkey hash
 * @return the base58-encoded string
 */
public static String encodeChecked(int version, byte[] payload) {
    if (version < 0 || version > 255)
        throw new IllegalArgumentException("Version not in range.");

    // A stringified buffer is:
    // 1 byte version + data bytes + 4 bytes check code (a truncated hash)
    byte[] addressBytes = new byte[1 + payload.length + 4];
    addressBytes[0] = (byte) version;
    System.arraycopy(payload, 0, addressBytes, 1, payload.length);
    byte[] checksum = Sha256Hash.hashTwice(addressBytes, 0, payload.length + 1);
    System.arraycopy(checksum, 0, addressBytes, payload.length + 1, 4);
    return Base58Util.encode(addressBytes);
}
 
Example 5
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 6
Source File: Hash.java    From balzac with Apache License 2.0 4 votes vote down vote up
public static Hash hash256(byte[] bytes) {
    return new Hash(Sha256Hash.hashTwice(bytes));
}