Serialize now returns a buffer
This commit is contained in:
parent
d17f85f5a6
commit
10ee5532c3
3 changed files with 32 additions and 19 deletions
|
@ -13,8 +13,14 @@ function Script(data) {
|
||||||
this.parse()
|
this.parse()
|
||||||
}
|
}
|
||||||
|
|
||||||
Script.fromHex = function(data) {
|
Script.fromBuffer = function(buffer) {
|
||||||
return new Script(convert.hexToBytes(data))
|
// assert(Buffer.isBuffer(buffer)) // FIXME: transitionary
|
||||||
|
|
||||||
|
return new Script(Array.prototype.slice.call(buffer))
|
||||||
|
}
|
||||||
|
|
||||||
|
Script.fromHex = function(hex) {
|
||||||
|
return Script.fromBuffer(new Buffer(hex, 'hex'))
|
||||||
}
|
}
|
||||||
|
|
||||||
Script.fromPubKey = function(str) {
|
Script.fromPubKey = function(str) {
|
||||||
|
|
|
@ -118,9 +118,8 @@ Transaction.prototype.addOutput = function (address, value, network) {
|
||||||
/**
|
/**
|
||||||
* Serialize this transaction.
|
* Serialize this transaction.
|
||||||
*
|
*
|
||||||
* Returns the transaction as a byte array in the standard Bitcoin binary
|
* Returns the transaction as a binary buffer in
|
||||||
* format. This method is byte-perfect, i.e. the resulting byte array can
|
* accordance with the Bitcoin protocol.
|
||||||
* be hashed to get the transaction's standard Bitcoin hash.
|
|
||||||
*/
|
*/
|
||||||
Transaction.prototype.serialize = function () {
|
Transaction.prototype.serialize = function () {
|
||||||
var buffer = []
|
var buffer = []
|
||||||
|
@ -152,11 +151,11 @@ Transaction.prototype.serialize = function () {
|
||||||
|
|
||||||
buffer = buffer.concat(convert.numToBytes(parseInt(this.locktime), 4))
|
buffer = buffer.concat(convert.numToBytes(parseInt(this.locktime), 4))
|
||||||
|
|
||||||
return buffer
|
return new Buffer(buffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
Transaction.prototype.serializeHex = function() {
|
Transaction.prototype.serializeHex = function() {
|
||||||
return convert.bytesToHex(this.serialize())
|
return this.serialize().toString('hex')
|
||||||
}
|
}
|
||||||
|
|
||||||
//var OP_CODESEPARATOR = 171
|
//var OP_CODESEPARATOR = 171
|
||||||
|
@ -213,9 +212,10 @@ Transaction.prototype.hashTransactionForSignature =
|
||||||
txTmp.ins = [txTmp.ins[inIndex]]
|
txTmp.ins = [txTmp.ins[inIndex]]
|
||||||
}
|
}
|
||||||
|
|
||||||
var buffer = txTmp.serialize()
|
var htB = new Buffer(4)
|
||||||
buffer = buffer.concat(convert.numToBytes(parseInt(hashType), 4))
|
htB.writeUInt32LE(hashType, 0)
|
||||||
|
|
||||||
|
var buffer = Buffer.concat([txTmp.serialize(), htB])
|
||||||
return crypto.hash256(buffer)
|
return crypto.hash256(buffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -283,12 +283,15 @@ Transaction.deserialize = function(buffer) {
|
||||||
var i
|
var i
|
||||||
|
|
||||||
for (i = 0; i < ins; i++) {
|
for (i = 0; i < ins; i++) {
|
||||||
|
var hash = readBytes(32)
|
||||||
|
Array.prototype.reverse.call(hash)
|
||||||
|
|
||||||
obj.ins.push({
|
obj.ins.push({
|
||||||
outpoint: {
|
outpoint: {
|
||||||
hash: convert.bytesToHex(readBytes(32).reverse()),
|
hash: convert.bytesToHex(hash),
|
||||||
index: readAsInt(4)
|
index: readAsInt(4)
|
||||||
},
|
},
|
||||||
script: new Script(readVarString()),
|
script: Script.fromBuffer(readVarString()),
|
||||||
sequence: readAsInt(4)
|
sequence: readAsInt(4)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -297,7 +300,7 @@ Transaction.deserialize = function(buffer) {
|
||||||
for (i = 0; i < outs; i++) {
|
for (i = 0; i < outs; i++) {
|
||||||
obj.outs.push({
|
obj.outs.push({
|
||||||
value: convert.bytesToNum(readBytes(8)),
|
value: convert.bytesToNum(readBytes(8)),
|
||||||
script: new Script(readVarString())
|
script: Script.fromBuffer(readVarString())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -81,14 +81,18 @@ describe('Transaction', function() {
|
||||||
tx.addInput("0cb859105100ebc3344f749c835c7af7d7103ec0d8cbc3d8ccbd5d28c3c36b57", 0)
|
tx.addInput("0cb859105100ebc3344f749c835c7af7d7103ec0d8cbc3d8ccbd5d28c3c36b57", 0)
|
||||||
tx.addOutput("15mMHKL96tWAUtqF3tbVf99Z8arcmnJrr3", 100)
|
tx.addOutput("15mMHKL96tWAUtqF3tbVf99Z8arcmnJrr3", 100)
|
||||||
|
|
||||||
// but we're going to replace the tx.ins.length VarInt with a 32-bit equivalent
|
var buffer = tx.serialize()
|
||||||
// however the same resultant number of inputs (1)
|
|
||||||
var bytes = tx.serialize()
|
|
||||||
var mutated = bytes.slice(0, 4).concat([254, 1, 0, 0, 0], bytes.slice(5))
|
|
||||||
|
|
||||||
// the deserialized-serialized transaction should return to its original state (== tx)
|
// we're going to replace the 8bit VarInt for tx.ins.length with a stretched 32bit equivalent
|
||||||
var bytes2 = Transaction.deserialize(mutated).serialize()
|
var mutated = Buffer.concat([
|
||||||
assert.deepEqual(bytes, bytes2)
|
buffer.slice(0, 4),
|
||||||
|
new Buffer([254, 1, 0, 0, 0]),
|
||||||
|
buffer.slice(5)
|
||||||
|
])
|
||||||
|
|
||||||
|
// the deserialized-serialized transaction should return to its non-mutated state (== tx)
|
||||||
|
var buffer2 = Transaction.deserialize(mutated).serialize()
|
||||||
|
assert.deepEqual(buffer, buffer2)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue