Merge pull request #179 from dcousens/txscript

Transaction function naming and ordering
This commit is contained in:
Wei Lu 2014-05-18 07:58:20 +08:00
commit cf39de4035
9 changed files with 124 additions and 137 deletions

View file

@ -295,12 +295,15 @@ Script.createP2SHScriptPubKey = function(hash) {
// m [pubKeys ...] n OP_CHECKMULTISIG
Script.createMultisigScriptPubKey = function(m, pubKeys) {
var script = new Script()
var n = pubKeys.length
script.writeOp(opcodes.OP_1 + m - 1)
for (var i = 0; i < pubKeys.length; ++i) {
script.writeBytes(pubKeys[i])
}
script.writeOp(opcodes.OP_1 + pubKeys.length - 1)
script.writeOp((opcodes.OP_1 - 1) + m)
pubKeys.forEach(function(pubKey) {
script.writeBytes(pubKey.toBuffer())
})
script.writeOp((opcodes.OP_1 - 1) + n)
script.writeOp(opcodes.OP_CHECKMULTISIG)
return script
@ -314,6 +317,13 @@ Script.createPubKeyHashScriptSig = function(signature, pubKey) {
return script
}
// <scriptSig> {serialized scriptPubKey script}
Script.createP2SHScriptSig = function(scriptSig, scriptPubKey) {
var inScript = new Script(scriptSig.buffer)
inScript.writeBytes(scriptPubKey.buffer)
return inScript
}
// OP_0 [signatures ...]
Script.createMultisigScriptSig = function(signatures, scriptPubKey) {
if (scriptPubKey) {
@ -334,13 +344,6 @@ Script.createMultisigScriptSig = function(signatures, scriptPubKey) {
return inScript
}
// <scriptSig> {serialized scriptPubKey script}
Script.createP2SHScriptSig = function(scriptSig, scriptPubKey) {
var inScript = new Script(scriptSig.buffer)
inScript.writeBytes(scriptPubKey.buffer)
return inScript
}
Script.prototype.clone = function() {
return new Script(this.buffer)
}

View file

@ -20,7 +20,7 @@ function Transaction(doc) {
if (doc) {
if (typeof doc == "string" || Array.isArray(doc)) {
doc = Transaction.deserialize(doc)
doc = Transaction.fromBuffer(doc)
}
if (doc.hash) this.hash = doc.hash;
@ -114,13 +114,7 @@ Transaction.prototype.addOutput = function (address, value) {
}))
}
/**
* Serialize this transaction.
*
* Returns the transaction as a binary buffer in
* accordance with the Bitcoin protocol.
*/
Transaction.prototype.serialize = function () {
Transaction.prototype.toBuffer = function () {
var txInSize = this.ins.reduce(function(a, x) {
return a + (40 + bufferutils.varIntSize(x.script.buffer.length) + x.script.buffer.length)
}, 0)
@ -185,8 +179,8 @@ Transaction.prototype.serialize = function () {
return buffer
}
Transaction.prototype.serializeHex = function() {
return this.serialize().toString('hex')
Transaction.prototype.toHex = function() {
return this.toBuffer().toString('hex')
}
//var OP_CODESEPARATOR = 171
@ -204,7 +198,7 @@ var SIGHASH_ANYONECANPAY = 80
* hashType, serializes and finally hashes the result. This hash can then be
* used to sign the transaction input in question.
*/
Transaction.prototype.hashTransactionForSignature =
Transaction.prototype.hashForSignature =
function (connectedScript, inIndex, hashType)
{
var txTmp = this.clone()
@ -246,12 +240,12 @@ Transaction.prototype.hashTransactionForSignature =
var htB = new Buffer(4)
htB.writeUInt32LE(hashType, 0)
var buffer = Buffer.concat([txTmp.serialize(), htB])
var buffer = Buffer.concat([txTmp.toBuffer(), htB])
return crypto.hash256(buffer)
}
Transaction.prototype.getHash = function () {
var buffer = crypto.hash256(this.serialize())
var buffer = crypto.hash256(this.toBuffer())
// Big-endian is used for TxHash
Array.prototype.reverse.call(buffer)
@ -276,10 +270,7 @@ Transaction.prototype.clone = function ()
return newTx
}
Transaction.deserialize = function(buffer) {
if (typeof buffer == "string") buffer = new Buffer(buffer, 'hex')
else if (Array.isArray(buffer)) buffer = new Buffer(buffer)
Transaction.fromBuffer = function(buffer) {
// Copy because we mutate (reverse TxOutHashs)
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
*/
@ -377,7 +372,7 @@ Transaction.prototype.signScriptSig = function(index, script, key, type) {
assert(key instanceof ECKey, 'Invalid private key')
// assert.equal(type & 0x7F, type, 'Invalid type') // TODO
var hash = this.hashTransactionForSignature(script, index, type)
var hash = this.hashForSignature(script, index, type)
return key.sign(hash).concat([type])
}
@ -387,7 +382,7 @@ Transaction.prototype.setScriptSig = function(index, script) {
Transaction.prototype.validateSig = function(index, script, pub, sig, type) {
type = type || SIGHASH_ALL
var hash = this.hashTransactionForSignature(script, index, type)
var hash = this.hashForSignature(script, index, type)
return pub.verify(hash, sig)
}

View file

@ -22,14 +22,6 @@ describe('Address', function() {
})
describe('fromBase58Check', function() {
it('throws on invalid base58check', function() {
b58fixtures.invalid.forEach(function(f) {
assert.throws(function() {
Address.fromBase58Check(f)
})
})
})
fixtures.valid.forEach(function(f) {
it('imports ' + f.description + '(' + f.network + ') correctly', function() {
var addr = Address.fromBase58Check(f.base58check)
@ -38,6 +30,14 @@ describe('Address', function() {
assert.equal(addr.hash.toString('hex'), f.hex)
})
})
it('throws on invalid base58check', function() {
b58fixtures.invalid.forEach(function(f) {
assert.throws(function() {
Address.fromBase58Check(f)
})
})
})
})
describe('fromScriptPubKey', function() {

View file

@ -1,45 +1,45 @@
module.exports = {
valid: [
"valid": [
{
description: 'pubKeyHash',
network: 'bitcoin',
version: 0,
hex: '751e76e8199196d454941c45d1b3a323f1433bd6',
base58check: '1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH',
script: '76a914751e76e8199196d454941c45d1b3a323f1433bd688ac'
"description": "pubKeyHash",
"network": "bitcoin",
"version": 0,
"hex": "751e76e8199196d454941c45d1b3a323f1433bd6",
"base58check": "1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH",
"script": "76a914751e76e8199196d454941c45d1b3a323f1433bd688ac"
},
{
description: 'pubKeyHash',
network: 'testnet',
version: 111,
hex: '751e76e8199196d454941c45d1b3a323f1433bd6',
base58check: 'mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r',
script: '76a914751e76e8199196d454941c45d1b3a323f1433bd688ac'
"description": "scriptHash",
"network": "bitcoin",
"version": 5,
"hex": "cd7b44d0b03f2d026d1e586d7ae18903b0d385f6",
"base58check": "3LRW7jeCvQCRdPF8S3yUCfRAx4eqXFmdcr",
"script": "a914cd7b44d0b03f2d026d1e586d7ae18903b0d385f687"
},
{
description: 'scriptHash',
network: 'bitcoin',
version: 5,
hex: 'cd7b44d0b03f2d026d1e586d7ae18903b0d385f6',
base58check: '3LRW7jeCvQCRdPF8S3yUCfRAx4eqXFmdcr',
script: 'a914cd7b44d0b03f2d026d1e586d7ae18903b0d385f687'
"description": "pubKeyHash",
"network": "testnet",
"version": 111,
"hex": "751e76e8199196d454941c45d1b3a323f1433bd6",
"base58check": "mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r",
"script": "76a914751e76e8199196d454941c45d1b3a323f1433bd688ac"
},
{
description: 'scriptHash',
network: 'testnet',
version: 196,
hex: 'cd7b44d0b03f2d026d1e586d7ae18903b0d385f6',
base58check: '2NByiBUaEXrhmqAsg7BbLpcQSAQs1EDwt5w',
script: 'a914cd7b44d0b03f2d026d1e586d7ae18903b0d385f687'
"description": "scriptHash",
"network": "testnet",
"version": 196,
"hex": "cd7b44d0b03f2d026d1e586d7ae18903b0d385f6",
"base58check": "2NByiBUaEXrhmqAsg7BbLpcQSAQs1EDwt5w",
"script": "a914cd7b44d0b03f2d026d1e586d7ae18903b0d385f687"
}
],
invalid: {
toScriptPubKey: [
"invalid": {
"toScriptPubKey": [
{
description: 'Unknown Address version',
version: 0x99,
hex: 'cd7b44d0b03f2d026d1e586d7ae18903b0d385f6'
"description": "Unknown Address version",
"version": 153,
"hex": "751e76e8199196d454941c45d1b3a323f1433bd6"
}
]
}
}
}

View file

@ -8,16 +8,13 @@ var secureRandom = require('secure-random')
function b2h(b) { return new Buffer(b).toString('hex') }
function h2b(h) { return new Buffer(h, 'hex') }
function randomBuf(s) {
return new Buffer(secureRandom(s))
}
function randomBuf(s) { return new Buffer(secureRandom(s)) }
request('https://raw.githubusercontent.com/bitcoin/bitcoin/master/src/test/data/base58_encode_decode.json', function (error, response, body) {
assert.ifError(error)
assert.equal(response.statusCode, 200)
var data = JSON.parse(body)
var valid = data.map(function(x) {
var valid = JSON.parse(body).map(function(x) {
return {
hex: x[0],
string: x[1]
@ -31,7 +28,7 @@ request('https://raw.githubusercontent.com/bitcoin/bitcoin/master/src/test/data/
// string: ' \t\n\v\f\r skip \r\f\v\n\t '
// })
var fixtureJSON = JSON.stringify({
var fixture = JSON.stringify({
valid: valid,
invalid: [
{
@ -58,48 +55,43 @@ request('https://raw.githubusercontent.com/bitcoin/bitcoin/master/src/test/data/
]
}, null, ' ')
fs.writeFileSync('./test/fixtures/base58.js', 'module.exports = ' + fixtureJSON)
fs.writeFileSync('./test/fixtures/base58.js', 'module.exports = ' + fixture)
})
request('https://raw.githubusercontent.com/bitcoin/bitcoin/master/src/test/data/base58_keys_valid.json', function (error, response, body) {
request('https://raw.githubusercontent.com/bitcoin/bitcoin/master/src/test/data/base58_keys_invalid.json', function (error2, response2, body2) {
assert.ifError(error)
assert.ifError(error2)
assert.equal(response.statusCode, 200)
assert.equal(response2.statusCode, 200)
request('https://raw.githubusercontent.com/bitcoin/bitcoin/master/src/test/data/base58_keys_invalid.json', function (error2, response2, body2) {
assert.ifError(error)
assert.ifError(error2)
assert.equal(response.statusCode, 200)
assert.equal(response2.statusCode, 200)
var validData = JSON.parse(body)
var invalidData = JSON.parse(body2)
var valid = JSON.parse(body).map(function(x) {
var string = x[0]
var hex = x[1]
var params = x[2]
var valid = validData.map(function(x) {
var string = x[0]
var hex = x[1]
var params = x[2]
if (params.isCompressed) hex += '01'
assert.equal(b2h(base58check.decode(string).payload), hex)
if (params.isCompressed) {
hex += '01'
return {
string: string,
decode: {
version: base58check.decode(string).version,
payload: hex,
checksum: b2h(base58check.decode(string).checksum),
}
assert.equal(b2h(base58check.decode(string).payload), hex)
}
})
var invalid2 = JSON.parse(body2).map(function(x) { return x[0] })
return {
string: string,
decode: {
version: base58check.decode(string).version,
payload: hex,
checksum: b2h(base58check.decode(string).checksum),
}
}
})
var invalid2 = invalidData.map(function(x) { return x[0] })
// Our own tests
var hash = crypto.hash160(randomBuf(65))
var checksum = base58check.decode(base58check.encode(hash)).checksum
// Our own tests
var hash = crypto.hash160(randomBuf(65))
var checksum = base58check.decode(base58check.encode(hash)).checksum
var fixtureJSON = JSON.stringify({
valid: valid,
invalid: [
{
var fixture = JSON.stringify({
valid: valid,
invalid: [
{
base58check: base58check.encode(hash.slice(0, 18), 0x05),
description: 'hash too short'
},
@ -119,10 +111,10 @@ request('https://raw.githubusercontent.com/bitcoin/bitcoin/master/src/test/data/
base58check: base58.encode(Buffer.concat([new Buffer([0x05]), hash, randomBuf(4)])),
description: 'bad SHA256 checksum',
}
],
invalid2: invalid2
}, null, ' ')
],
invalid2: invalid2
}, null, ' ')
fs.writeFileSync('./test/fixtures/base58check.js', 'module.exports = ' + fixtureJSON)
})
fs.writeFileSync('./test/fixtures/base58check.js', 'module.exports = ' + fixture)
})
})

View file

@ -27,10 +27,7 @@ describe('p2sh', function() {
var pubKeys = privKeys.map(function(eck) {
return eck.pub
})
var pubKeyBuffers = pubKeys.map(function(q) {
return q.toBuffer()
})
var redeemScript = Script.createMultisigScriptPubKey(2, pubKeyBuffers)
var redeemScript = Script.createMultisigScriptPubKey(2, pubKeys)
var hash160 = crypto.hash160(redeemScript.buffer)
var multisigAddress = new Address(hash160, networks.testnet.scriptHash)
@ -62,7 +59,7 @@ describe('p2sh', function() {
tx.setScriptSig(0, scriptSig)
// 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
if (err) done(err);

View file

@ -3,6 +3,7 @@ var crypto = require('../src/crypto')
var networks = require('../src/networks')
var Address = require('../src/address')
var ECPubKey = require('../src/ecpubkey')
var Script = require('../src/script')
var fixtures = require('./fixtures/script')
@ -95,7 +96,7 @@ describe('Script', function() {
'02ea1297665dd733d444f31ec2581020004892cdaaf3dd6c0107c615afb839785f',
'02fab2dea1458990793f56f42e4a47dbf35a12a351f26fa5d7e0cc7447eaafa21f',
'036c6802ce7e8113723dd92cdb852e492ebb157a871ca532c3cb9ed08248ff0e19'
].map(h2b)
].map(ECPubKey.fromHex)
})
it('should create valid redeemScript', function() {
@ -112,7 +113,7 @@ describe('Script', function() {
var pubKeys = [
'02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1',
'0395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a'
].map(h2b)
].map(ECPubKey.fromHex)
var signatures = [
'304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801',
'3045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501'

View file

@ -26,11 +26,11 @@ describe('Transaction', function() {
'3901ffffffff0100f2052a010000001976a914dd40dedd8f7e3746662',
'4c4dacc6362d8e7be23dd88ac00000000'
].join('')
tx = Transaction.deserialize(serializedTx)
tx = Transaction.fromHex(serializedTx)
})
it('returns the original after serialized again', function() {
var actual = tx.serialize()
var actual = tx.toBuffer()
var expected = serializedTx
assert.equal(b2h(actual), expected)
@ -38,7 +38,7 @@ describe('Transaction', function() {
it('does not mutate the input buffer', function() {
var buffer = new Buffer(serializedTx, 'hex')
Transaction.deserialize(buffer)
Transaction.fromBuffer(buffer)
assert.equal(buffer.toString('hex'), serializedTx)
})
@ -84,7 +84,7 @@ describe('Transaction', function() {
tx.addInput("0cb859105100ebc3344f749c835c7af7d7103ec0d8cbc3d8ccbd5d28c3c36b57", 0)
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
var mutated = Buffer.concat([
@ -94,7 +94,7 @@ describe('Transaction', function() {
])
// 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)
})
})
@ -102,7 +102,7 @@ describe('Transaction', function() {
describe('creating a transaction', function() {
var tx, prevTx
beforeEach(function() {
prevTx = Transaction.deserialize(fixtureTx1Hex)
prevTx = Transaction.fromHex(fixtureTx1Hex)
tx = new Transaction()
})
@ -207,7 +207,7 @@ describe('Transaction', function() {
var validTx
beforeEach(function() {
validTx = Transaction.deserialize(fixtureTx2Hex)
validTx = Transaction.fromHex(fixtureTx2Hex)
})
it('returns true for valid signature', function(){
@ -221,22 +221,22 @@ describe('Transaction', function() {
describe('estimateFee', function(){
it('works for fixture tx 1', function(){
var tx = Transaction.deserialize(fixtureTx1Hex)
var tx = Transaction.fromHex(fixtureTx1Hex)
assert.equal(tx.estimateFee(), 20000)
})
it('works for fixture big tx', function(){
var tx = Transaction.deserialize(fixtureTxBigHex)
var tx = Transaction.fromHex(fixtureTxBigHex)
assert.equal(tx.estimateFee(), 60000)
})
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)
})
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)
})
})
@ -255,8 +255,7 @@ describe('Transaction', function() {
return ECKey.fromWIF(wif)
})
var pubKeys = privKeys.map(function(eck) { return eck.pub })
var pubKeyBuffers = pubKeys.map(function(q) { return q.toBuffer() })
var redeemScript = Script.createMultisigScriptPubKey(2, pubKeyBuffers)
var redeemScript = Script.createMultisigScriptPubKey(2, pubKeys)
var signatures = privKeys.map(function(privKey) {
return tx.signScriptSig(0, redeemScript, privKey)
@ -271,7 +270,7 @@ describe('Transaction', function() {
})
var expected = '010000000189632848f99722915727c5c75da8db2dbf194342a0429828f66ff88fab2af7d600000000fd1b0100483045022100e5be20d440b2bbbc886161f9095fa6d0bca749a4e41d30064f30eb97adc7a1f5022061af132890d8e4e90fedff5e9365aeeb77021afd8ef1d5c114d575512e9a130a0147304402205054e38e9d7b5c10481b6b4991fde5704cd94d49e344406e3c2ce4d18a43bf8e022051d7ba8479865b53a48bee0cce86e89a25633af5b2918aa276859489e232f51c014c8752410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a52aeffffffff0101000000000000001976a914751e76e8199196d454941c45d1b3a323f1433bd688ac00000000'
assert.equal(tx.serializeHex(), expected)
assert.equal(tx.toHex(), expected)
})
})
})

View file

@ -318,7 +318,7 @@ describe('Wallet', function() {
'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(){
@ -363,7 +363,7 @@ describe('Wallet', function() {
wallet.addresses = [addresses[0]] // the address fixtureTx2 used as input
wallet.processTx(tx)
tx = Transaction.deserialize(fixtureTx2Hex)
tx = Transaction.fromHex(fixtureTx2Hex)
})
it("does not add to wallet.outputs", function(){