Browse Source

Merge pull request #170 from MattFaus/fix_transaction_tests2

Hook up testdata/tx_valid.json and tx_invalid.json test cases
patch-2
Ryan X. Charles 11 years ago
parent
commit
d9cbe13d93
  1. 9
      Key.js
  2. 4
      Script.js
  3. 1586
      ScriptInterpreter.js
  4. 660
      Transaction.js
  5. 23
      test/test.ScriptInterpreter.js
  6. 278
      test/test.Transaction.js
  7. 142
      util/util.js

9
Key.js

@ -77,6 +77,15 @@ if (process.versions) {
// return it as a buffer to keep c++ compatibility // return it as a buffer to keep c++ compatibility
return new Buffer(signature); return new Buffer(signature);
}; };
kSpec.prototype.verifySignature = function(hash, sig, callback) {
try {
var result = this.verifySignatureSync(hash, sig);
callback(null, result);
} catch (e) {
callback(e);
}
};
kSpec.prototype.verifySignatureSync = function(hash, sig) { kSpec.prototype.verifySignatureSync = function(hash, sig) {
var self = this; var self = this;

4
Script.js

@ -491,7 +491,7 @@ Script.prototype.toHumanReadable = function() {
} }
} }
return s; return s;
}; };
Script.stringToBuffer = function(s) { Script.stringToBuffer = function(s) {
@ -505,7 +505,7 @@ Script.stringToBuffer = function(s) {
//console.log('hex value'); //console.log('hex value');
buf.put(new Buffer(word.substring(2, word.length), 'hex')); buf.put(new Buffer(word.substring(2, word.length), 'hex'));
} else { } else {
var opcode = Opcode.map['OP_' + word]; var opcode = Opcode.map['OP_' + word] || Opcode.map[word];
if (typeof opcode !== 'undefined') { if (typeof opcode !== 'undefined') {
// op code in string form // op code in string form
//console.log('opcode'); //console.log('opcode');

1586
ScriptInterpreter.js

File diff suppressed because it is too large

660
Transaction.js

File diff suppressed because it is too large

23
test/test.ScriptInterpreter.js

@ -7,16 +7,11 @@ var buffertools = require('buffertools');
var should = chai.should(); var should = chai.should();
var testdata = testdata || require('./testdata'); var testdata = testdata || require('./testdata');
var ScriptInterpreterModule = bitcore.ScriptInterpreter;
var Script = bitcore.Script; var Script = bitcore.Script;
var ScriptInterpreter; var ScriptInterpreter = bitcore.ScriptInterpreter;
describe('ScriptInterpreter', function() { describe('ScriptInterpreter', function() {
it('should initialze the main object', function() { it('should initialze the main object', function() {
should.exist(ScriptInterpreterModule);
});
it('should be able to create class', function() {
ScriptInterpreter = ScriptInterpreterModule;
should.exist(ScriptInterpreter); should.exist(ScriptInterpreter);
}); });
it('should be able to create instance', function() { it('should be able to create instance', function() {
@ -24,7 +19,6 @@ describe('ScriptInterpreter', function() {
should.exist(si); should.exist(si);
}); });
var testScripts = function(data, valid) { var testScripts = function(data, valid) {
var i = 0;
data.forEach(function(datum) { data.forEach(function(datum) {
if (datum.length < 2) throw new Error('Invalid test data'); if (datum.length < 2) throw new Error('Invalid test data');
var scriptSig = datum[0]; // script inputs var scriptSig = datum[0]; // script inputs
@ -68,7 +62,7 @@ describe('ScriptInterpreter', function() {
testdata.dataSigCanonical.forEach(function(datum) { testdata.dataSigCanonical.forEach(function(datum) {
it('should validate valid canonical signatures', function() { it('should validate valid canonical signatures', function() {
ScriptInterpreter.isCanonicalSignature(new Buffer(datum, 'hex')).should.equal(true); new ScriptInterpreter().isCanonicalSignature(new Buffer(datum, 'hex')).should.equal(true);
}); });
}); });
testdata.dataSigNonCanonical.forEach(function(datum) { testdata.dataSigNonCanonical.forEach(function(datum) {
@ -81,9 +75,16 @@ describe('ScriptInterpreter', function() {
isHex = 1; isHex = 1;
} catch (e) {} } catch (e) {}
if (isHex) // ignore non-hex strings
ScriptInterpreter.isCanonicalSignature.bind(sig).should. if (isHex) {
throw (); var f = function() {
var si = new ScriptInterpreter();
var r = si.isCanonicalSignature(sig);
};
// how this test should be
// f.should.throw();
new ScriptInterpreter().isCanonicalSignature.bind(sig).should.throw();
}
}); });
}); });

278
test/test.Transaction.js

@ -1,12 +1,12 @@
'use strict'; 'use strict';
var chai = chai || require('chai'); var chai = chai || require('chai');
chai.Assertion.includeStack = true;
var bitcore = bitcore || require('../bitcore'); var bitcore = bitcore || require('../bitcore');
var should = chai.should(); var should = chai.should();
var TransactionModule = bitcore.Transaction; var Transaction = bitcore.Transaction;
var Transaction;
var In; var In;
var Out; var Out;
var Script = bitcore.Script; var Script = bitcore.Script;
@ -14,12 +14,41 @@ var util = bitcore.util;
var buffertools = require('buffertools'); var buffertools = require('buffertools');
var testdata = testdata || require('./testdata'); var testdata = testdata || require('./testdata');
// Read tests from test/data/tx_valid.json and tx_invalid.json
// Format is an array of arrays
// Inner arrays are either [ "comment" ]
// or [[[prevout hash, prevout index, prevout scriptPubKey], [input 2], ...],"], serializedTransaction, enforceP2SH
// ... where all scripts are stringified scripts.
// Returns an object with the Transaction object, and an array of input objects
function parse_test_transaction(entry) {
// Ignore comments
if (entry.length !== 3) return;
var inputs = {};
entry[0].forEach(function(vin) {
var hash = (vin[0]);
var index = vin[1];
var scriptPubKey = Script.fromHumanReadable(vin[2]);
var mapKey = [hash, index];
inputs[mapKey] = scriptPubKey;
});
var raw = new Buffer(entry[1], 'hex');
var tx = new Transaction();
tx.parse(raw);
// Sanity check transaction has been parsed correctly
buffertools.toHex(tx.serialize()).should.equal(buffertools.toHex(raw));
return {
'transaction': tx,
'inputs': inputs
};
}
describe('Transaction', function() { describe('Transaction', function() {
it('should initialze the main object', function() { it('should initialze the main object', function() {
should.exist(TransactionModule);
});
it('should be able to create class', function() {
Transaction = TransactionModule;
should.exist(Transaction); should.exist(Transaction);
In = Transaction.In; In = Transaction.In;
Out = Transaction.Out; Out = Transaction.Out;
@ -35,7 +64,7 @@ describe('Transaction', function() {
it('#selectUnspent should be able to select utxos', function() { it('#selectUnspent should be able to select utxos', function() {
var u = Transaction.selectUnspent(testdata.dataUnspent,1.0, true); var u = Transaction.selectUnspent(testdata.dataUnspent, 1.0, true);
u.length.should.equal(3); u.length.should.equal(3);
should.exist(u[0].amount); should.exist(u[0].amount);
@ -43,37 +72,37 @@ describe('Transaction', function() {
should.exist(u[0].scriptPubKey); should.exist(u[0].scriptPubKey);
should.exist(u[0].vout); should.exist(u[0].vout);
u = Transaction.selectUnspent(testdata.dataUnspent,0.5, true); u = Transaction.selectUnspent(testdata.dataUnspent, 0.5, true);
u.length.should.equal(3); u.length.should.equal(3);
u = Transaction.selectUnspent(testdata.dataUnspent,0.1, true); u = Transaction.selectUnspent(testdata.dataUnspent, 0.1, true);
u.length.should.equal(2); u.length.should.equal(2);
u = Transaction.selectUnspent(testdata.dataUnspent,0.05, true); u = Transaction.selectUnspent(testdata.dataUnspent, 0.05, true);
u.length.should.equal(2); u.length.should.equal(2);
u = Transaction.selectUnspent(testdata.dataUnspent,0.015, true); u = Transaction.selectUnspent(testdata.dataUnspent, 0.015, true);
u.length.should.equal(2); u.length.should.equal(2);
u = Transaction.selectUnspent(testdata.dataUnspent,0.01, true); u = Transaction.selectUnspent(testdata.dataUnspent, 0.01, true);
u.length.should.equal(1); u.length.should.equal(1);
}); });
it('#selectUnspent should return null if not enough utxos', function() { it('#selectUnspent should return null if not enough utxos', function() {
var u = Transaction.selectUnspent(testdata.dataUnspent,1.12); var u = Transaction.selectUnspent(testdata.dataUnspent, 1.12);
should.not.exist(u); should.not.exist(u);
}); });
it('#selectUnspent should check confirmations', function() { it('#selectUnspent should check confirmations', function() {
var u = Transaction.selectUnspent(testdata.dataUnspent,0.9); var u = Transaction.selectUnspent(testdata.dataUnspent, 0.9);
should.not.exist(u); should.not.exist(u);
var u = Transaction.selectUnspent(testdata.dataUnspent,0.9,true); u = Transaction.selectUnspent(testdata.dataUnspent, 0.9, true);
u.length.should.equal(3); u.length.should.equal(3);
var u = Transaction.selectUnspent(testdata.dataUnspent,0.11); u = Transaction.selectUnspent(testdata.dataUnspent, 0.11);
u.length.should.equal(2); u.length.should.equal(2);
var u = Transaction.selectUnspent(testdata.dataUnspent,0.111); u = Transaction.selectUnspent(testdata.dataUnspent, 0.111);
should.not.exist(u); should.not.exist(u);
}); });
@ -84,8 +113,11 @@ describe('Transaction', function() {
}; };
it('#create should be able to create instance', function() { it('#create should be able to create instance', function() {
var utxos =testdata.dataUnspent; var utxos = testdata.dataUnspent;
var outs = [{address:'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE', amount:0.08}]; var outs = [{
address: 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE',
amount: 0.08
}];
var ret = Transaction.create(utxos, outs, opts); var ret = Transaction.create(utxos, outs, opts);
should.exist(ret.tx); should.exist(ret.tx);
@ -106,25 +138,36 @@ describe('Transaction', function() {
}); });
it('#create should fail if not enough inputs ', function() { it('#create should fail if not enough inputs ', function() {
var utxos =testdata.dataUnspent; var utxos = testdata.dataUnspent;
var outs = [{address:'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE', amount:80}]; var outs = [{
address: 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE',
amount: 80
}];
Transaction Transaction
.create .create
.bind(utxos, outs, opts) .bind(utxos, outs, opts)
.should.throw(); .should.
throw ();
var outs2 = [{address:'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE', amount:0.5}]; var outs2 = [{
should.exist( Transaction.create(utxos, outs2, opts)); address: 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE',
amount: 0.5
}];
should.exist(Transaction.create(utxos, outs2, opts));
// do not allow unconfirmed // do not allow unconfirmed
Transaction.create.bind(utxos, outs2).should.throw(); Transaction.create.bind(utxos, outs2).should.
throw ();
}); });
it('#create should create same output as bitcoind createrawtransaction ', function() { it('#create should create same output as bitcoind createrawtransaction ', function() {
var utxos =testdata.dataUnspent; var utxos = testdata.dataUnspent;
var outs = [{address:'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE', amount:0.08}]; var outs = [{
var ret = Transaction.create(utxos, outs, opts); address: 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE',
amount: 0.08
}];
var ret = Transaction.create(utxos, outs, opts);
var tx = ret.tx; var tx = ret.tx;
// string output generated from: bitcoind createrawtransaction '[{"txid": "2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc1","vout":1},{"txid":"2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc2","vout":0} ]' '{"mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE":0.08,"mwZabyZXg8JzUtFX1pkGygsMJjnuqiNhgd":0.0299}' // string output generated from: bitcoind createrawtransaction '[{"txid": "2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc1","vout":1},{"txid":"2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc2","vout":0} ]' '{"mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE":0.08,"mwZabyZXg8JzUtFX1pkGygsMJjnuqiNhgd":0.0299}'
@ -133,28 +176,39 @@ describe('Transaction', function() {
}); });
it('#create should create same output as bitcoind createrawtransaction wo remainder', function() { it('#create should create same output as bitcoind createrawtransaction wo remainder', function() {
var utxos =testdata.dataUnspent; var utxos = testdata.dataUnspent;
// no remainder // no remainder
var outs = [{address:'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE', amount:0.08}]; var outs = [{
var ret = Transaction.create(utxos, outs, {fee:0.03} ); address: 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE',
amount: 0.08
}];
var ret = Transaction.create(utxos, outs, {
fee: 0.03
});
var tx = ret.tx; var tx = ret.tx;
// string output generated from: bitcoind createrawtransaction '[{"txid": "2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc1","vout":1},{"txid":"2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc2","vout":0} ]' '{"mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE":0.08}' // string output generated from: bitcoind createrawtransaction '[{"txid": "2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc1","vout":1},{"txid":"2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc2","vout":0} ]' '{"mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE":0.08}'
// //
tx.serialize().toString('hex').should.equal('0100000002c1cf12ab89729d19d3cdec8ae531b5038d56c741006a105d532b3a7afa65c12a0100000000ffffffffc2cf12ab89729d19d3cdec8ae531b5038d56c741006a105d532b3a7afa65c12a0000000000ffffffff0100127a00000000001976a914774e603bafb717bd3f070e68bbcccfd907c77d1388ac00000000'); tx.serialize().toString('hex').should.equal('0100000002c1cf12ab89729d19d3cdec8ae531b5038d56c741006a105d532b3a7afa65c12a0100000000ffffffffc2cf12ab89729d19d3cdec8ae531b5038d56c741006a105d532b3a7afa65c12a0000000000ffffffff0100127a00000000001976a914774e603bafb717bd3f070e68bbcccfd907c77d1388ac00000000');
}); });
it('#createAndSign should sign a tx', function() { it('#createAndSign should sign a tx', function() {
var utxos =testdata.dataUnspentSign.unspent; var utxos = testdata.dataUnspentSign.unspent;
var outs = [{address:'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE', amount:0.08}]; var outs = [{
var ret = Transaction.createAndSign(utxos, outs, testdata.dataUnspentSign.keyStrings, opts); address: 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE',
amount: 0.08
}];
var ret = Transaction.createAndSign(utxos, outs, testdata.dataUnspentSign.keyStrings, opts);
var tx = ret.tx; var tx = ret.tx;
tx.isComplete().should.equal(true); tx.isComplete().should.equal(true);
tx.ins.length.should.equal(1); tx.ins.length.should.equal(1);
tx.outs.length.should.equal(2); tx.outs.length.should.equal(2);
var outs2 = [{address:'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE', amount:16}]; var outs2 = [{
var ret2 = Transaction.createAndSign(utxos, outs2, testdata.dataUnspentSign.keyStrings, opts); address: 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE',
amount: 16
}];
var ret2 = Transaction.createAndSign(utxos, outs2, testdata.dataUnspentSign.keyStrings, opts);
var tx2 = ret2.tx; var tx2 = ret2.tx;
tx2.isComplete().should.equal(true); tx2.isComplete().should.equal(true);
tx2.ins.length.should.equal(3); tx2.ins.length.should.equal(3);
@ -163,18 +217,24 @@ describe('Transaction', function() {
it('#createAndSign should sign an incomplete tx ', function() { it('#createAndSign should sign an incomplete tx ', function() {
var keys = ['cNpW8B7XPAzCdRR9RBWxZeveSNy3meXgHD8GuhcqUyDuy8ptCDzJ']; var keys = ['cNpW8B7XPAzCdRR9RBWxZeveSNy3meXgHD8GuhcqUyDuy8ptCDzJ'];
var utxos =testdata.dataUnspentSign.unspent; var utxos = testdata.dataUnspentSign.unspent;
var outs = [{address:'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE', amount:0.08}]; var outs = [{
var ret = Transaction.createAndSign(utxos, outs, keys, opts); address: 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE',
amount: 0.08
}];
var ret = Transaction.createAndSign(utxos, outs, keys, opts);
var tx = ret.tx; var tx = ret.tx;
tx.ins.length.should.equal(1); tx.ins.length.should.equal(1);
tx.outs.length.should.equal(2); tx.outs.length.should.equal(2);
}); });
it('#isComplete should return TX signature status', function() { it('#isComplete should return TX signature status', function() {
var keys = ['cNpW8B7XPAzCdRR9RBWxZeveSNy3meXgHD8GuhcqUyDuy8ptCDzJ']; var keys = ['cNpW8B7XPAzCdRR9RBWxZeveSNy3meXgHD8GuhcqUyDuy8ptCDzJ'];
var utxos =testdata.dataUnspentSign.unspent; var utxos = testdata.dataUnspentSign.unspent;
var outs = [{address:'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE', amount:0.08}]; var outs = [{
var ret = Transaction.createAndSign(utxos, outs, keys, opts); address: 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE',
amount: 0.08
}];
var ret = Transaction.createAndSign(utxos, outs, keys, opts);
var tx = ret.tx; var tx = ret.tx;
tx.isComplete().should.equal(false); tx.isComplete().should.equal(false);
tx.sign(ret.selectedUtxos, testdata.dataUnspentSign.keyStrings); tx.sign(ret.selectedUtxos, testdata.dataUnspentSign.keyStrings);
@ -182,48 +242,57 @@ describe('Transaction', function() {
}); });
it('#sign should sign a tx in multiple steps (case1)', function() { it('#sign should sign a tx in multiple steps (case1)', function() {
var outs = [{address:'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE', amount:1.08}]; var outs = [{
var ret = Transaction.create(testdata.dataUnspentSign.unspent, outs, opts); address: 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE',
var tx = ret.tx; amount: 1.08
}];
var ret = Transaction.create(testdata.dataUnspentSign.unspent, outs, opts);
var tx = ret.tx;
var selectedUtxos = ret.selectedUtxos; var selectedUtxos = ret.selectedUtxos;
var k1 = testdata.dataUnspentSign.keyStrings.slice(0,1); var k1 = testdata.dataUnspentSign.keyStrings.slice(0, 1);
tx.isComplete().should.equal(false); tx.isComplete().should.equal(false);
tx.sign(selectedUtxos, k1).should.equal(false); tx.sign(selectedUtxos, k1).should.equal(false);
var k23 = testdata.dataUnspentSign.keyStrings.slice(1,3); var k23 = testdata.dataUnspentSign.keyStrings.slice(1, 3);
tx.sign(selectedUtxos, k23).should.equal(true); tx.sign(selectedUtxos, k23).should.equal(true);
tx.isComplete().should.equal(true); tx.isComplete().should.equal(true);
}); });
it('#sign should sign a tx in multiple steps (case2)', function() { it('#sign should sign a tx in multiple steps (case2)', function() {
var outs = [{address:'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE', amount:16}]; var outs = [{
var ret = Transaction.create(testdata.dataUnspentSign.unspent, outs, opts); address: 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE',
var tx = ret.tx; amount: 16
}];
var ret = Transaction.create(testdata.dataUnspentSign.unspent, outs, opts);
var tx = ret.tx;
var selectedUtxos = ret.selectedUtxos; var selectedUtxos = ret.selectedUtxos;
var k1 = testdata.dataUnspentSign.keyStrings.slice(0,1); var k1 = testdata.dataUnspentSign.keyStrings.slice(0, 1);
var k2 = testdata.dataUnspentSign.keyStrings.slice(1,2); var k2 = testdata.dataUnspentSign.keyStrings.slice(1, 2);
var k3 = testdata.dataUnspentSign.keyStrings.slice(2,3); var k3 = testdata.dataUnspentSign.keyStrings.slice(2, 3);
tx.sign(selectedUtxos, k1).should.equal(false); tx.sign(selectedUtxos, k1).should.equal(false);
tx.sign(selectedUtxos, k2).should.equal(false); tx.sign(selectedUtxos, k2).should.equal(false);
tx.sign(selectedUtxos, k3).should.equal(true); tx.sign(selectedUtxos, k3).should.equal(true);
}); });
it('#createAndSign: should generate dynamic fee and readjust (and not) the selected UTXOs', function() { it('#createAndSign: should generate dynamic fee and readjust (and not) the selected UTXOs', function() {
//this cases exceeds the input by 1mbtc AFTEr calculating the dynamic fee, //this cases exceeds the input by 1mbtc AFTEr calculating the dynamic fee,
//so, it should trigger adding a new 10BTC utxo //so, it should trigger adding a new 10BTC utxo
var utxos =testdata.dataUnspentSign.unspent; var utxos = testdata.dataUnspentSign.unspent;
var outs = []; var outs = [];
var n =101; var n = 101;
for (var i=0; i<n; i++) { for (var i = 0; i < n; i++) {
outs.push({address:'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE', amount:0.01}); outs.push({
address: 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE',
amount: 0.01
});
} }
var ret = Transaction.createAndSign(utxos, outs, testdata.dataUnspentSign.keyStrings, opts); var ret = Transaction.createAndSign(utxos, outs, testdata.dataUnspentSign.keyStrings, opts);
var tx = ret.tx; var tx = ret.tx;
tx.getSize().should.equal(3560); tx.getSize().should.equal(3560);
@ -238,15 +307,18 @@ describe('Transaction', function() {
//this is the complementary case, it does not trigger a new utxo //this is the complementary case, it does not trigger a new utxo
var utxos =testdata.dataUnspentSign.unspent; utxos = testdata.dataUnspentSign.unspent;
var outs = []; outs = [];
var n =100; n = 100;
for (var i=0; i<n; i++) { for (i = 0; i < n; i++) {
outs.push({address:'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE', amount:0.01}); outs.push({
address: 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE',
amount: 0.01
});
} }
var ret = Transaction.createAndSign(utxos, outs, testdata.dataUnspentSign.keyStrings, opts); ret = Transaction.createAndSign(utxos, outs, testdata.dataUnspentSign.keyStrings, opts);
var tx = ret.tx; tx = ret.tx;
tx.getSize().should.equal(3485); tx.getSize().should.equal(3485);
// ins = 1.0101 BTC (1 inputs: 1.0101); // ins = 1.0101 BTC (1 inputs: 1.0101);
@ -260,39 +332,49 @@ describe('Transaction', function() {
}); });
// Read tests from test/data/tx_valid.json
// Format is an array of arrays
// Inner arrays are either [ "comment" ]
// or [[[prevout hash, prevout index, prevout scriptPubKey], [input 2], ...],"], serializedTransaction, enforceP2SH
// ... where all scripts are stringified scripts.
testdata.dataTxValid.forEach(function(datum) {
if (datum.length === 3) {
it.skip('valid tx=' + datum[1], function(done) {
var inputs = datum[0];
var map = {};
inputs.forEach(function(vin) {
var hash = vin[0];
var index = vin[1];
var scriptPubKey = new Script(new Buffer(vin[2]));
map[[hash, index]] = scriptPubKey; //Script.fromStringContent(scriptPubKey);
console.log(scriptPubKey.getStringContent());
console.log('********************************');
done();
});
var raw = new Buffer(datum[1], 'hex');
var tx = new Transaction();
tx.parse(raw);
buffertools.toHex(tx.serialize()).should.equal(buffertools.toHex(raw)); /*
* Bitcoin core transaction tests
*/
// Verify that known valid transactions are intepretted correctly
var coreTest = function(data, valid) {
data.forEach(function(datum) {
if (datum.length < 3) return;
var raw = datum[1];
var verifyP2SH = datum[2];
var i = 0; it.skip((valid ? '' : 'in') + 'valid tx=' + raw, function(done) {
var stx = tx.getStandardizedObject(); var cb = function(err, results) {
tx.ins.forEach(function(txin) { should.not.exist(err);
var scriptPubKey = map[[stx. in [i].prev_out.hash, stx. in [i].prev_out.n]]; should.exist(results);
i += 1; results.should.equal(valid);
}); done();
};
var testTx = parse_test_transaction(datum);
buffertools.toHex(testTx.transaction.serialize()).should.equal(raw);
var inputs = testTx.transaction.inputs();
for (var i = 0; i < inputs.length; i++) {
var input = inputs[i];
buffertools.reverse(input[0]);
input[0] = buffertools.toHex(input[0]);
var mapKey = [input];
var scriptPubKey = testTx.inputs[mapKey];
if (!scriptPubKey) throw new Error('Bad test: '+datum);
testTx.transaction.verifyInput(
i,
scriptPubKey, {
verifyP2SH: verifyP2SH,
dontVerifyStrictEnc: true
},
cb);
}
}); });
} });
}); };
coreTest(testdata.dataTxValid, true);
coreTest(testdata.dataTxInvalid, false);
}); });

142
util/util.js

@ -1,54 +1,51 @@
var crypto = require('crypto'); var crypto = require('crypto');
var bignum = require('bignum'); var bignum = require('bignum');
var Binary = require('binary'); var Binary = require('binary');
var Put = require('bufferput'); var Put = require('bufferput');
var buffertools = require('buffertools'); var buffertools = require('buffertools');
var browser; var browser;
if (!process.versions) { var inBrowser = !process.versions;
// browser version if (inBrowser) {
browser = require('../browser/vendor-bundle.js'); browser = require('../browser/vendor-bundle.js');
} }
var sha256 = exports.sha256 = function (data) { var sha256 = exports.sha256 = function(data) {
return new Buffer(crypto.createHash('sha256').update(data).digest('binary'), 'binary'); return new Buffer(crypto.createHash('sha256').update(data).digest('binary'), 'binary');
}; };
var ripe160 = exports.ripe160 = function (data) { var ripe160 = exports.ripe160 = function(data) {
if (!Buffer.isBuffer(data)) { if (!Buffer.isBuffer(data)) {
throw new Error('arg should be a buffer'); throw new Error('arg should be a buffer');
} }
if (inBrowser) {
if (!process.versions) {
var w = new browser.crypto31.lib.WordArray.init(Crypto.util.bytesToWords(data), data.length); var w = new browser.crypto31.lib.WordArray.init(Crypto.util.bytesToWords(data), data.length);
var wordArray = browser.crypto31.RIPEMD160(w); var wordArray = browser.crypto31.RIPEMD160(w);
var words = wordArray.words; var words = wordArray.words;
var answer = []; var answer = [];
for (var b = 0; b < words.length * 32; b += 8) { for (var b = 0; b < words.length * 32; b += 8) {
answer.push((words[b >>> 5] >>> (24 - b % 32)) & 0xFF); answer.push((words[b >>> 5] >>> (24 - b % 32)) & 0xFF);
} }
return new Buffer(answer, 'hex'); return new Buffer(answer, 'hex');
} }
return new Buffer(crypto.createHash('rmd160').update(data).digest('binary'), 'binary'); return new Buffer(crypto.createHash('rmd160').update(data).digest('binary'), 'binary');
}; };
var sha1 = exports.sha1 = function (data) { var sha1 = exports.sha1 = function(data) {
return new Buffer(crypto.createHash('sha1').update(data).digest('binary'), 'binary'); return new Buffer(crypto.createHash('sha1').update(data).digest('binary'), 'binary');
}; };
var twoSha256 = exports.twoSha256 = function (data) { var twoSha256 = exports.twoSha256 = function(data) {
return sha256(sha256(data)); return sha256(sha256(data));
}; };
var sha256ripe160 = exports.sha256ripe160 = function (data) { var sha256ripe160 = exports.sha256ripe160 = function(data) {
return ripe160(sha256(data)); return ripe160(sha256(data));
}; };
/** /**
* Format a block hash like the official client does. * Format a block hash like the official client does.
*/ */
var formatHash = exports.formatHash = function (hash) { var formatHash = exports.formatHash = function(hash) {
var hashEnd = new Buffer(10); var hashEnd = new Buffer(10);
hash.copy(hashEnd, 0, 22, 32); hash.copy(hashEnd, 0, 22, 32);
return buffertools.reverse(hashEnd).toString('hex'); return buffertools.reverse(hashEnd).toString('hex');
@ -57,7 +54,7 @@ var formatHash = exports.formatHash = function (hash) {
/** /**
* Display the whole hash, as hex, in correct endian order. * Display the whole hash, as hex, in correct endian order.
*/ */
var formatHashFull = exports.formatHashFull = function (hash) { var formatHashFull = exports.formatHashFull = function(hash) {
var copy = new Buffer(hash.length); var copy = new Buffer(hash.length);
hash.copy(copy); hash.copy(copy);
var hex = buffertools.toHex(buffertools.reverse(copy)); var hex = buffertools.toHex(buffertools.reverse(copy));
@ -69,13 +66,13 @@ var formatHashFull = exports.formatHashFull = function (hash) {
* *
* Formats a block hash by removing leading zeros and truncating to 10 characters. * Formats a block hash by removing leading zeros and truncating to 10 characters.
*/ */
var formatHashAlt = exports.formatHashAlt = function (hash) { var formatHashAlt = exports.formatHashAlt = function(hash) {
var hex = formatHashFull(hash); var hex = formatHashFull(hash);
hex = hex.replace(/^0*/, ''); hex = hex.replace(/^0*/, '');
return hex.substr(0, 10); return hex.substr(0, 10);
}; };
var formatBuffer = exports.formatBuffer = function (buffer, maxLen) { var formatBuffer = exports.formatBuffer = function(buffer, maxLen) {
// Calculate amount of bytes to display // Calculate amount of bytes to display
if (maxLen === null) { if (maxLen === null) {
maxLen = 10; maxLen = 10;
@ -96,41 +93,47 @@ var formatBuffer = exports.formatBuffer = function (buffer, maxLen) {
return output; return output;
}; };
var valueToBigInt = exports.valueToBigInt = function (valueBuffer) { var valueToBigInt = exports.valueToBigInt = function(valueBuffer) {
if (Buffer.isBuffer(valueBuffer)) { if (Buffer.isBuffer(valueBuffer)) {
return bignum.fromBuffer(valueBuffer, {endian: 'little', size: 8}); return bignum.fromBuffer(valueBuffer, {
endian: 'little',
size: 8
});
} else { } else {
return valueBuffer; return valueBuffer;
} }
}; };
var bigIntToValue = exports.bigIntToValue = function (valueBigInt) { var bigIntToValue = exports.bigIntToValue = function(valueBigInt) {
if (Buffer.isBuffer(valueBigInt)) { if (Buffer.isBuffer(valueBigInt)) {
return valueBigInt; return valueBigInt;
} else { } else {
return valueBigInt.toBuffer({endian: 'little', size: 8}); return valueBigInt.toBuffer({
endian: 'little',
size: 8
});
} }
}; };
var fitsInNBits = function(integer, n) { var fitsInNBits = function(integer, n) {
// TODO: make this efficient!!! // TODO: make this efficient!!!
return integer.toString(2).replace('-','').length < n; return integer.toString(2).replace('-', '').length < n;
}; };
exports.bytesNeededToStore = bytesNeededToStore = function(integer) { exports.bytesNeededToStore = bytesNeededToStore = function(integer) {
if (integer === 0) return 0; if (integer === 0) return 0;
return Math.ceil(((integer).toString(2).replace('-','').length + 1)/ 8); return Math.ceil(((integer).toString(2).replace('-', '').length + 1) / 8);
}; };
exports.negativeBuffer = negativeBuffer = function(b) { exports.negativeBuffer = negativeBuffer = function(b) {
// implement two-complement negative // implement two-complement negative
var c = new Buffer(b.length); var c = new Buffer(b.length);
// negate each byte // negate each byte
for (var i=0; i<b.length; i++){ for (var i = 0; i < b.length; i++) {
c[i] = ~b[i]; c[i] = ~b[i];
if (c[i] < 0) c[i] += 256; if (c[i] < 0) c[i] += 256;
} }
// add one // add one
for (var i=b.length - 1; i>=0; i--){ for (var i = b.length - 1; i >= 0; i--) {
c[i] += 1; c[i] += 1;
if (c[i] >= 256) c[i] -= 256; if (c[i] >= 256) c[i] -= 256;
if (c[i] !== 0) break; if (c[i] !== 0) break;
@ -141,7 +144,7 @@ exports.negativeBuffer = negativeBuffer = function(b) {
/* /*
* Transforms an integer into a buffer using two-complement encoding * Transforms an integer into a buffer using two-complement encoding
* For example, 1 is encoded as 01 and -1 is encoded as ff * For example, 1 is encoded as 01 and -1 is encoded as ff
* For more info see: * For more info see:
* http://en.wikipedia.org/wiki/Signed_number_representations#Two.27s_complement * http://en.wikipedia.org/wiki/Signed_number_representations#Two.27s_complement
*/ */
exports.intToBuffer2C = function(integer) { exports.intToBuffer2C = function(integer) {
@ -149,9 +152,9 @@ exports.intToBuffer2C = function(integer) {
var buf = new Put(); var buf = new Put();
var s = integer.toString(16); var s = integer.toString(16);
var neg = s[0] === '-'; var neg = s[0] === '-';
s = s.replace('-',''); s = s.replace('-', '');
for (var i=0; i<size; i++) { for (var i = 0; i < size; i++) {
var si = s.substring(s.length - 2*(i+1), s.length - 2*(i)); var si = s.substring(s.length - 2 * (i + 1), s.length - 2 * (i));
if (si.lenght === 1) { if (si.lenght === 1) {
si = '0' + si; si = '0' + si;
} }
@ -184,7 +187,7 @@ var padSign = function(b) {
/* /*
* Transforms an integer into a buffer using sign+magnitude encoding * Transforms an integer into a buffer using sign+magnitude encoding
* For example, 1 is encoded as 01 and -1 is encoded as 81 * For example, 1 is encoded as 01 and -1 is encoded as 81
* For more info see: * For more info see:
* http://en.wikipedia.org/wiki/Signed_number_representations#Signed_magnitude_representation * http://en.wikipedia.org/wiki/Signed_number_representations#Signed_magnitude_representation
*/ */
exports.intToBufferSM = function(v) { exports.intToBufferSM = function(v) {
@ -234,50 +237,45 @@ exports.bufferSMToInt = function(v) {
var formatValue = exports.formatValue = function (valueBuffer) { var formatValue = exports.formatValue = function(valueBuffer) {
var value = valueToBigInt(valueBuffer).toString(); var value = valueToBigInt(valueBuffer).toString();
var integerPart = value.length > 8 ? value.substr(0, value.length-8) : '0'; var integerPart = value.length > 8 ? value.substr(0, value.length - 8) : '0';
var decimalPart = value.length > 8 ? value.substr(value.length-8) : value; var decimalPart = value.length > 8 ? value.substr(value.length - 8) : value;
while (decimalPart.length < 8) { while (decimalPart.length < 8) {
decimalPart = "0"+decimalPart; decimalPart = "0" + decimalPart;
} }
decimalPart = decimalPart.replace(/0*$/, ''); decimalPart = decimalPart.replace(/0*$/, '');
while (decimalPart.length < 2) { while (decimalPart.length < 2) {
decimalPart += "0"; decimalPart += "0";
} }
return integerPart+"."+decimalPart; return integerPart + "." + decimalPart;
}; };
var reFullVal = /^\s*(\d+)\.(\d+)/; var reFullVal = /^\s*(\d+)\.(\d+)/;
var reFracVal = /^\s*\.(\d+)/; var reFracVal = /^\s*\.(\d+)/;
var reWholeVal = /^\s*(\d+)/; var reWholeVal = /^\s*(\d+)/;
function padFrac(frac) function padFrac(frac) {
{ frac = frac.substr(0, 8); //truncate to 8 decimal places
frac=frac.substr(0,8); //truncate to 8 decimal places
while (frac.length < 8) while (frac.length < 8)
frac = frac + '0'; frac = frac + '0';
return frac; return frac;
} }
function parseFullValue(res) function parseFullValue(res) {
{
return bignum(res[1]).mul('100000000').add(padFrac(res[2])); return bignum(res[1]).mul('100000000').add(padFrac(res[2]));
} }
function parseFracValue(res) function parseFracValue(res) {
{
return bignum(padFrac(res[1])); return bignum(padFrac(res[1]));
} }
function parseWholeValue(res) function parseWholeValue(res) {
{
return bignum(res[1]).mul('100000000'); return bignum(res[1]).mul('100000000');
} }
exports.parseValue = function parseValue(valueStr) exports.parseValue = function parseValue(valueStr) {
{ if (typeof valueStr !== 'string')
if (typeof valueStr !== 'string')
valueStr = valueStr.toString(); valueStr = valueStr.toString();
var res = valueStr.match(reFullVal); var res = valueStr.match(reFullVal);
@ -296,11 +294,11 @@ exports.parseValue = function parseValue(valueStr)
}; };
// Utility that synchronizes function calls based on a key // Utility that synchronizes function calls based on a key
var createSynchrotron = exports.createSynchrotron = function (fn) { var createSynchrotron = exports.createSynchrotron = function(fn) {
var table = {}; var table = {};
return function (key) { return function(key) {
var args = Array.prototype.slice.call(arguments); var args = Array.prototype.slice.call(arguments);
var run = function () { var run = function() {
// Function fn() will call when it finishes // Function fn() will call when it finishes
args[0] = function next() { args[0] = function next() {
if (table[key]) { if (table[key]) {
@ -333,21 +331,23 @@ var createSynchrotron = exports.createSynchrotron = function (fn) {
* *
* @returns Buffer random nonce * @returns Buffer random nonce
*/ */
var generateNonce = exports.generateNonce = function () { var generateNonce = exports.generateNonce = function() {
var b32 = 0x100000000, ff = 0xff; var b32 = 0x100000000,
var b = new Buffer(8), i = 0; ff = 0xff;
var b = new Buffer(8),
i = 0;
// Generate eight random bytes // Generate eight random bytes
r = Math.random()*b32; r = Math.random() * b32;
b[i++] = r & ff; b[i++] = r & ff;
b[i++] = (r=r>>>8) & ff; b[i++] = (r = r >>> 8) & ff;
b[i++] = (r=r>>>8) & ff; b[i++] = (r = r >>> 8) & ff;
b[i++] = (r=r>>>8) & ff; b[i++] = (r = r >>> 8) & ff;
r = Math.random()*b32; r = Math.random() * b32;
b[i++] = r & ff; b[i++] = r & ff;
b[i++] = (r=r>>>8) & ff; b[i++] = (r = r >>> 8) & ff;
b[i++] = (r=r>>>8) & ff; b[i++] = (r = r >>> 8) & ff;
b[i++] = (r=r>>>8) & ff; b[i++] = (r = r >>> 8) & ff;
return b; return b;
}; };
@ -357,10 +357,10 @@ var generateNonce = exports.generateNonce = function () {
* *
* This function calculates the difficulty target given the difficulty bits. * This function calculates the difficulty target given the difficulty bits.
*/ */
var decodeDiffBits = exports.decodeDiffBits = function (diffBits, asBigInt) { var decodeDiffBits = exports.decodeDiffBits = function(diffBits, asBigInt) {
diffBits = +diffBits; diffBits = +diffBits;
var target = bignum(diffBits & 0xffffff); var target = bignum(diffBits & 0xffffff);
target = target.shiftLeft(8*((diffBits >>> 24) - 3)); target = target.shiftLeft(8 * ((diffBits >>> 24) - 3));
if (asBigInt) { if (asBigInt) {
return target; return target;
@ -370,7 +370,7 @@ var decodeDiffBits = exports.decodeDiffBits = function (diffBits, asBigInt) {
var diffBuf = target.toBuffer(); var diffBuf = target.toBuffer();
var targetBuf = new Buffer(32); var targetBuf = new Buffer(32);
buffertools.fill(targetBuf, 0); buffertools.fill(targetBuf, 0);
diffBuf.copy(targetBuf, 32-diffBuf.length); diffBuf.copy(targetBuf, 32 - diffBuf.length);
return targetBuf; return targetBuf;
}; };
@ -393,8 +393,8 @@ var encodeDiffBits = exports.encodeDiffBits = function encodeDiffBits(target) {
var compact = size << 24; var compact = size << 24;
if (size >= 1) compact |= mpiBuf[4] << 16; if (size >= 1) compact |= mpiBuf[4] << 16;
if (size >= 2) compact |= mpiBuf[5] << 8; if (size >= 2) compact |= mpiBuf[5] << 8;
if (size >= 3) compact |= mpiBuf[6] ; if (size >= 3) compact |= mpiBuf[6];
return compact; return compact;
}; };
@ -405,16 +405,20 @@ var encodeDiffBits = exports.encodeDiffBits = function encodeDiffBits(target) {
* This function calculates the maximum difficulty target divided by the given * This function calculates the maximum difficulty target divided by the given
* difficulty target. * difficulty target.
*/ */
var calcDifficulty = exports.calcDifficulty = function (target) { var calcDifficulty = exports.calcDifficulty = function(target) {
if (!Buffer.isBuffer(target)) { if (!Buffer.isBuffer(target)) {
target = decodeDiffBits(target); target = decodeDiffBits(target);
} }
var targetBigint = bignum.fromBuffer(target, {order: 'forward'}); var targetBigint = bignum.fromBuffer(target, {
var maxBigint = bignum.fromBuffer(MAX_TARGET, {order: 'forward'}); order: 'forward'
});
var maxBigint = bignum.fromBuffer(MAX_TARGET, {
order: 'forward'
});
return maxBigint.div(targetBigint).toNumber(); return maxBigint.div(targetBigint).toNumber();
}; };
var reverseBytes32 = exports.reverseBytes32 = function (data) { var reverseBytes32 = exports.reverseBytes32 = function(data) {
if (data.length % 4) { if (data.length % 4) {
throw new Error("Util.reverseBytes32(): Data length must be multiple of 4"); throw new Error("Util.reverseBytes32(): Data length must be multiple of 4");
} }

Loading…
Cancel
Save