Transaction: rename (de)serialize to [to/from]Buffer
This commit is contained in:
parent
e033a872c9
commit
0468c4710c
4 changed files with 26 additions and 31 deletions
|
@ -20,7 +20,7 @@ function Transaction(doc) {
|
||||||
|
|
||||||
if (doc) {
|
if (doc) {
|
||||||
if (typeof doc == "string" || Array.isArray(doc)) {
|
if (typeof doc == "string" || Array.isArray(doc)) {
|
||||||
doc = Transaction.deserialize(doc)
|
doc = Transaction.fromBuffer(doc)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (doc.hash) this.hash = doc.hash;
|
if (doc.hash) this.hash = doc.hash;
|
||||||
|
@ -114,13 +114,7 @@ Transaction.prototype.addOutput = function (address, value) {
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
Transaction.prototype.toBuffer = function () {
|
||||||
* Serialize this transaction.
|
|
||||||
*
|
|
||||||
* Returns the transaction as a binary buffer in
|
|
||||||
* accordance with the Bitcoin protocol.
|
|
||||||
*/
|
|
||||||
Transaction.prototype.serialize = function () {
|
|
||||||
var txInSize = this.ins.reduce(function(a, x) {
|
var txInSize = this.ins.reduce(function(a, x) {
|
||||||
return a + (40 + bufferutils.varIntSize(x.script.buffer.length) + x.script.buffer.length)
|
return a + (40 + bufferutils.varIntSize(x.script.buffer.length) + x.script.buffer.length)
|
||||||
}, 0)
|
}, 0)
|
||||||
|
@ -185,8 +179,8 @@ Transaction.prototype.serialize = function () {
|
||||||
return buffer
|
return buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
Transaction.prototype.serializeHex = function() {
|
Transaction.prototype.toHex = function() {
|
||||||
return this.serialize().toString('hex')
|
return this.toBuffer().toString('hex')
|
||||||
}
|
}
|
||||||
|
|
||||||
//var OP_CODESEPARATOR = 171
|
//var OP_CODESEPARATOR = 171
|
||||||
|
@ -246,12 +240,12 @@ Transaction.prototype.hashForSignature =
|
||||||
var htB = new Buffer(4)
|
var htB = new Buffer(4)
|
||||||
htB.writeUInt32LE(hashType, 0)
|
htB.writeUInt32LE(hashType, 0)
|
||||||
|
|
||||||
var buffer = Buffer.concat([txTmp.serialize(), htB])
|
var buffer = Buffer.concat([txTmp.toBuffer(), htB])
|
||||||
return crypto.hash256(buffer)
|
return crypto.hash256(buffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
Transaction.prototype.getHash = function () {
|
Transaction.prototype.getHash = function () {
|
||||||
var buffer = crypto.hash256(this.serialize())
|
var buffer = crypto.hash256(this.toBuffer())
|
||||||
|
|
||||||
// Big-endian is used for TxHash
|
// Big-endian is used for TxHash
|
||||||
Array.prototype.reverse.call(buffer)
|
Array.prototype.reverse.call(buffer)
|
||||||
|
@ -276,10 +270,7 @@ Transaction.prototype.clone = function ()
|
||||||
return newTx
|
return newTx
|
||||||
}
|
}
|
||||||
|
|
||||||
Transaction.deserialize = function(buffer) {
|
Transaction.fromBuffer = function(buffer) {
|
||||||
if (typeof buffer == "string") buffer = new Buffer(buffer, 'hex')
|
|
||||||
else if (Array.isArray(buffer)) buffer = new Buffer(buffer)
|
|
||||||
|
|
||||||
// Copy because we mutate (reverse TxOutHashs)
|
// Copy because we mutate (reverse TxOutHashs)
|
||||||
buffer = new Buffer(buffer)
|
buffer = new Buffer(buffer)
|
||||||
|
|
||||||
|
@ -355,6 +346,10 @@ Transaction.deserialize = function(buffer) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Transaction.fromHex = function(hex) {
|
||||||
|
return Transaction.fromBuffer(new Buffer(hex, 'hex'))
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Signs a standard output at some index with the given key
|
* Signs a standard output at some index with the given key
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -59,7 +59,7 @@ describe('p2sh', function() {
|
||||||
tx.setScriptSig(0, scriptSig)
|
tx.setScriptSig(0, scriptSig)
|
||||||
|
|
||||||
// Send from mutlsigAddress to targetAddress
|
// Send from mutlsigAddress to targetAddress
|
||||||
helloblock.transactions.propagate(tx.serializeHex(), function(err, resp, resource) {
|
helloblock.transactions.propagate(tx.toHex(), function(err, resp, resource) {
|
||||||
// no err means that transaction has been successfully propagated
|
// no err means that transaction has been successfully propagated
|
||||||
if (err) done(err);
|
if (err) done(err);
|
||||||
|
|
||||||
|
|
|
@ -26,11 +26,11 @@ describe('Transaction', function() {
|
||||||
'3901ffffffff0100f2052a010000001976a914dd40dedd8f7e3746662',
|
'3901ffffffff0100f2052a010000001976a914dd40dedd8f7e3746662',
|
||||||
'4c4dacc6362d8e7be23dd88ac00000000'
|
'4c4dacc6362d8e7be23dd88ac00000000'
|
||||||
].join('')
|
].join('')
|
||||||
tx = Transaction.deserialize(serializedTx)
|
tx = Transaction.fromHex(serializedTx)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('returns the original after serialized again', function() {
|
it('returns the original after serialized again', function() {
|
||||||
var actual = tx.serialize()
|
var actual = tx.toBuffer()
|
||||||
var expected = serializedTx
|
var expected = serializedTx
|
||||||
|
|
||||||
assert.equal(b2h(actual), expected)
|
assert.equal(b2h(actual), expected)
|
||||||
|
@ -38,7 +38,7 @@ describe('Transaction', function() {
|
||||||
|
|
||||||
it('does not mutate the input buffer', function() {
|
it('does not mutate the input buffer', function() {
|
||||||
var buffer = new Buffer(serializedTx, 'hex')
|
var buffer = new Buffer(serializedTx, 'hex')
|
||||||
Transaction.deserialize(buffer)
|
Transaction.fromBuffer(buffer)
|
||||||
|
|
||||||
assert.equal(buffer.toString('hex'), serializedTx)
|
assert.equal(buffer.toString('hex'), serializedTx)
|
||||||
})
|
})
|
||||||
|
@ -84,7 +84,7 @@ describe('Transaction', function() {
|
||||||
tx.addInput("0cb859105100ebc3344f749c835c7af7d7103ec0d8cbc3d8ccbd5d28c3c36b57", 0)
|
tx.addInput("0cb859105100ebc3344f749c835c7af7d7103ec0d8cbc3d8ccbd5d28c3c36b57", 0)
|
||||||
tx.addOutput("15mMHKL96tWAUtqF3tbVf99Z8arcmnJrr3", 100)
|
tx.addOutput("15mMHKL96tWAUtqF3tbVf99Z8arcmnJrr3", 100)
|
||||||
|
|
||||||
var buffer = tx.serialize()
|
var buffer = tx.toBuffer()
|
||||||
|
|
||||||
// we're going to replace the 8bit VarInt for tx.ins.length with a stretched 32bit equivalent
|
// we're going to replace the 8bit VarInt for tx.ins.length with a stretched 32bit equivalent
|
||||||
var mutated = Buffer.concat([
|
var mutated = Buffer.concat([
|
||||||
|
@ -94,7 +94,7 @@ describe('Transaction', function() {
|
||||||
])
|
])
|
||||||
|
|
||||||
// the deserialized-serialized transaction should return to its non-mutated state (== tx)
|
// the deserialized-serialized transaction should return to its non-mutated state (== tx)
|
||||||
var buffer2 = Transaction.deserialize(mutated).serialize()
|
var buffer2 = Transaction.fromBuffer(mutated).toBuffer()
|
||||||
assert.deepEqual(buffer, buffer2)
|
assert.deepEqual(buffer, buffer2)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -102,7 +102,7 @@ describe('Transaction', function() {
|
||||||
describe('creating a transaction', function() {
|
describe('creating a transaction', function() {
|
||||||
var tx, prevTx
|
var tx, prevTx
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
prevTx = Transaction.deserialize(fixtureTx1Hex)
|
prevTx = Transaction.fromHex(fixtureTx1Hex)
|
||||||
tx = new Transaction()
|
tx = new Transaction()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -207,7 +207,7 @@ describe('Transaction', function() {
|
||||||
var validTx
|
var validTx
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
validTx = Transaction.deserialize(fixtureTx2Hex)
|
validTx = Transaction.fromHex(fixtureTx2Hex)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('returns true for valid signature', function(){
|
it('returns true for valid signature', function(){
|
||||||
|
@ -221,22 +221,22 @@ describe('Transaction', function() {
|
||||||
|
|
||||||
describe('estimateFee', function(){
|
describe('estimateFee', function(){
|
||||||
it('works for fixture tx 1', function(){
|
it('works for fixture tx 1', function(){
|
||||||
var tx = Transaction.deserialize(fixtureTx1Hex)
|
var tx = Transaction.fromHex(fixtureTx1Hex)
|
||||||
assert.equal(tx.estimateFee(), 20000)
|
assert.equal(tx.estimateFee(), 20000)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('works for fixture big tx', function(){
|
it('works for fixture big tx', function(){
|
||||||
var tx = Transaction.deserialize(fixtureTxBigHex)
|
var tx = Transaction.fromHex(fixtureTxBigHex)
|
||||||
assert.equal(tx.estimateFee(), 60000)
|
assert.equal(tx.estimateFee(), 60000)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('allow feePerKb to be passed in as an argument', function(){
|
it('allow feePerKb to be passed in as an argument', function(){
|
||||||
var tx = Transaction.deserialize(fixtureTx2Hex)
|
var tx = Transaction.fromHex(fixtureTx2Hex)
|
||||||
assert.equal(tx.estimateFee(10000), 10000)
|
assert.equal(tx.estimateFee(10000), 10000)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('allow feePerKb to be set to 0', function(){
|
it('allow feePerKb to be set to 0', function(){
|
||||||
var tx = Transaction.deserialize(fixtureTx2Hex)
|
var tx = Transaction.fromHex(fixtureTx2Hex)
|
||||||
assert.equal(tx.estimateFee(0), 0)
|
assert.equal(tx.estimateFee(0), 0)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -270,7 +270,7 @@ describe('Transaction', function() {
|
||||||
})
|
})
|
||||||
|
|
||||||
var expected = '010000000189632848f99722915727c5c75da8db2dbf194342a0429828f66ff88fab2af7d600000000fd1b0100483045022100e5be20d440b2bbbc886161f9095fa6d0bca749a4e41d30064f30eb97adc7a1f5022061af132890d8e4e90fedff5e9365aeeb77021afd8ef1d5c114d575512e9a130a0147304402205054e38e9d7b5c10481b6b4991fde5704cd94d49e344406e3c2ce4d18a43bf8e022051d7ba8479865b53a48bee0cce86e89a25633af5b2918aa276859489e232f51c014c8752410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a52aeffffffff0101000000000000001976a914751e76e8199196d454941c45d1b3a323f1433bd688ac00000000'
|
var expected = '010000000189632848f99722915727c5c75da8db2dbf194342a0429828f66ff88fab2af7d600000000fd1b0100483045022100e5be20d440b2bbbc886161f9095fa6d0bca749a4e41d30064f30eb97adc7a1f5022061af132890d8e4e90fedff5e9365aeeb77021afd8ef1d5c114d575512e9a130a0147304402205054e38e9d7b5c10481b6b4991fde5704cd94d49e344406e3c2ce4d18a43bf8e022051d7ba8479865b53a48bee0cce86e89a25633af5b2918aa276859489e232f51c014c8752410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a52aeffffffff0101000000000000001976a914751e76e8199196d454941c45d1b3a323f1433bd688ac00000000'
|
||||||
assert.equal(tx.serializeHex(), expected)
|
assert.equal(tx.toHex(), expected)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -318,7 +318,7 @@ describe('Wallet', function() {
|
||||||
'1BBjuhF2jHxu7tPinyQGCuaNhEs6f5u59u'
|
'1BBjuhF2jHxu7tPinyQGCuaNhEs6f5u59u'
|
||||||
]
|
]
|
||||||
|
|
||||||
tx = Transaction.deserialize(fixtureTx1Hex)
|
tx = Transaction.fromHex(fixtureTx1Hex)
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("when tx outs contains an address owned by the wallet, an 'output' gets added to wallet.outputs", function(){
|
describe("when tx outs contains an address owned by the wallet, an 'output' gets added to wallet.outputs", function(){
|
||||||
|
@ -363,7 +363,7 @@ describe('Wallet', function() {
|
||||||
wallet.addresses = [addresses[0]] // the address fixtureTx2 used as input
|
wallet.addresses = [addresses[0]] // the address fixtureTx2 used as input
|
||||||
wallet.processTx(tx)
|
wallet.processTx(tx)
|
||||||
|
|
||||||
tx = Transaction.deserialize(fixtureTx2Hex)
|
tx = Transaction.fromHex(fixtureTx2Hex)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("does not add to wallet.outputs", function(){
|
it("does not add to wallet.outputs", function(){
|
||||||
|
|
Loading…
Add table
Reference in a new issue