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

The following examples show how to use org.bitcoinj.core.Utils#uint32ToByteStreamLE() . 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: ScriptChunk.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
public void write(OutputStream stream) throws IOException {
    if (isOpCode()) {
        checkState(data == null);
        stream.write(opcode);
    } else if (data != null) {
        if (opcode < OP_PUSHDATA1) {
            checkState(data.length == opcode);
            stream.write(opcode);
        } else if (opcode == OP_PUSHDATA1) {
            checkState(data.length <= 0xFF);
            stream.write(OP_PUSHDATA1);
            stream.write(data.length);
        } else if (opcode == OP_PUSHDATA2) {
            checkState(data.length <= 0xFFFF);
            stream.write(OP_PUSHDATA2);
            Utils.uint16ToByteStreamLE(data.length, stream);
        } else if (opcode == OP_PUSHDATA4) {
            checkState(data.length <= Script.MAX_SCRIPT_ELEMENT_SIZE);
            stream.write(OP_PUSHDATA4);
            Utils.uint32ToByteStreamLE(data.length, stream);
        } else {
            throw new RuntimeException("Unimplemented");
        }
        stream.write(data);
    } else {
        stream.write(opcode); // smallNum
    }
}
 
Example 2
Source File: ScriptChunk.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
public void write(OutputStream stream) throws IOException {
    if (isOpCode()) {
        checkState(data == null);
        stream.write(opcode);
    } else if (data != null) {
        if (opcode < OP_PUSHDATA1) {
            checkState(data.length == opcode);
            stream.write(opcode);
        } else if (opcode == OP_PUSHDATA1) {
            checkState(data.length <= 0xFF);
            stream.write(OP_PUSHDATA1);
            stream.write(data.length);
        } else if (opcode == OP_PUSHDATA2) {
            checkState(data.length <= 0xFFFF);
            stream.write(OP_PUSHDATA2);
            stream.write(0xFF & data.length);
            stream.write(0xFF & (data.length >> 8));
        } else if (opcode == OP_PUSHDATA4) {
            checkState(data.length <= Script.MAX_SCRIPT_ELEMENT_SIZE);
            stream.write(OP_PUSHDATA4);
            Utils.uint32ToByteStreamLE(data.length, stream);
        } else {
            throw new RuntimeException("Unimplemented");
        }
        stream.write(data);
    } else {
        stream.write(opcode); // smallNum
    }
}
 
Example 3
Source File: ScriptChunk.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
public void write(OutputStream stream) throws IOException {
    if (isOpCode()) {
        checkState(data == null);
        stream.write(opcode);
    } else if (data != null) {
        if (opcode < OP_PUSHDATA1) {
            checkState(data.length == opcode);
            stream.write(opcode);
        } else if (opcode == OP_PUSHDATA1) {
            checkState(data.length <= 0xFF);
            stream.write(OP_PUSHDATA1);
            stream.write(data.length);
        } else if (opcode == OP_PUSHDATA2) {
            checkState(data.length <= 0xFFFF);
            stream.write(OP_PUSHDATA2);
            stream.write(0xFF & data.length);
            stream.write(0xFF & (data.length >> 8));
        } else if (opcode == OP_PUSHDATA4) {
            checkState(data.length <= Script.MAX_SCRIPT_ELEMENT_SIZE);
            stream.write(OP_PUSHDATA4);
            Utils.uint32ToByteStreamLE(data.length, stream);
        } else {
            throw new RuntimeException("Unimplemented");
        }
        stream.write(data);
    } else {
        stream.write(opcode); // smallNum
    }
}
 
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();
  }

}