Skip to content

Instantly share code, notes, and snippets.

@sidazhang
Created February 9, 2014 19:58
Show Gist options
  • Save sidazhang/8905038 to your computer and use it in GitHub Desktop.
Save sidazhang/8905038 to your computer and use it in GitHub Desktop.
decode json
// There is a bug https://github.com/cryptocoinjs/btc-transaction/pull/5
// Below works:
// couple of points:
// change the key of scriptPubkey and scriptSig to 'script'. This is something I intend to change
// The bug is at parsing the values. (the values should not be a number and its should be a string)
// e.g. '2000000' rather than 2000000
var binConv = require( "binstring" );
var btcTx = require( "btc-transaction" );
var Script = require( 'btc-script' );
var BigInteger = require('bigi')
// This is basically the issue we had. I will make a fix and hopefully can merge today
var value1 = BigInteger.valueOf('2000000').toByteArrayUnsigned().reverse()
while (value1.length < 8) value1.push(0);
var value2 = BigInteger.valueOf('4950000').toByteArrayUnsigned().reverse()
while (value2.length < 8) value2.push(0);
var doc = {
"hash": "dc55d9c6ec03ceccf0db43d29e7d626a8b107f41066e3917f30398bb01dda2b5",
"version": 1,
"locktime": 0,
"ins": [ {
"outpoint": {
"index": 1,
"hash": "de2c3fa45a1b8fa7057fc2db09efa6acd6524551a13d01a846ac1e3acc1476f8"
},
"script": "4830450221009aa61b4798a5a4c04f86d0de6c936b60bd046f15d4eb55a244d9206bc3af6f93022010a9f0f2186d76e71d781c5e4d36d704212b99efcf6ba5791db88379e6a23fbe014104b4c157eb55f8a04111d2eb55d2f4b632fa03d066a844254e350ff7ba67d2e355aad46ac73693c33bea5ddc86c426ee1557098554da9bc02acf0445f9775efdb5",
"sequence": '4294967295'
} ],
"outs": [ {
"value": value1,
"script": "76a9140c79f15f08b0f94a2cba8de036272dee3ecb096188ac"
}, {
"value": value2,
"script": "76a9143858230049f6c83ba435eac829ae332cab3609f788ac"
} ]
}
var tx = new btcTx.Transaction( doc );
var ser = binConv( tx.serialize(), {
out: 'hex'
} )
console.log(ser);
var txDeser = btcTx.Transaction.deserialize(
binConv(ser, {in: 'hex', out: 'buffer'})
)
var hash = binConv( tx.getHash(), {
out: 'hex'
} )
console.log(ser, hash)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment