working on Transaction verification and Script tests

This commit is contained in:
Manuel Araoz 2014-02-28 16:32:10 -03:00
parent ade6f36c34
commit 40ee699453
7 changed files with 842 additions and 754 deletions

View File

@ -280,6 +280,30 @@ function spec(b) {
return this.buffer;
};
Script.fromStringContent = function(s) {
var chunks = [];
var split = s.split(' ');
console.log(split);
for (var i=0; i<split.length; i++) {
var word = split[i];
if (word.length > 2 && word.substring(0,2) === '0x') {
chunks.push(new Buffer(word.substring(2,word.length), 'hex'));
} else {
var integer = parseInt(word);
if (isNaN(integer)) {
chunks.push(Opcode.map['OP_'+word]);
} else {
var hexi = integer.toString(16);
if (hexi.length %2 === 1) hexi = '0'+hexi;
console.log(hexi);
chunks.push(new Buffer(hexi,'hex'));
}
}
}
return Script.fromChunks(chunks);
};
Script.prototype.getStringContent = function (truncate, maxEl)
{
if (truncate === null) {
@ -290,26 +314,26 @@ function spec(b) {
maxEl = 15;
}
var script = '';
var s = '';
for (var i = 0, l = this.chunks.length; i < l; i++) {
var chunk = this.chunks[i];
if (i > 0) {
script += " ";
s += ' ';
}
if (Buffer.isBuffer(chunk)) {
script += "0x"+util.formatBuffer(chunk, truncate ? null : 0);
s += '0x'+util.formatBuffer(chunk, truncate ? null : 0);
} else {
script += Opcode.reverseMap[chunk];
s += Opcode.reverseMap[chunk];
}
if (maxEl && i > maxEl) {
script += " ...";
s += ' ...';
break;
}
}
return script;
return s;
};
Script.prototype.toString = function (truncate, maxEl)

File diff suppressed because it is too large Load Diff

View File

@ -63,7 +63,8 @@
"browserify-bignum": "git://github.com/maraoz/browserify-bignum.git",
"browserify-buffertools": "~1.0.2",
"chai": "~1.9.0",
"brfs": "~1.0.0"
"brfs": "~1.0.0",
"async": "~0.2.10"
},
"license": "MIT"
}

View File

@ -1,8 +1,8 @@
[
["0x01 0x0b", "11 EQUAL", "push 1 byte"],
["0x02 0x417a", "'Az' EQUAL"],
["0x02 0x417a", "0x417a EQUAL"],
["0x4b 0x417a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a",
"'Azzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz' EQUAL", "push 75 bytes"],
"0x417a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a EQUAL", "push 75 bytes"],
["0x4c 0x01 0x07","7 EQUAL", "0x4c is OP_PUSHDATA1"],
["0x4d 0x0100 0x08","8 EQUAL", "0x4d is OP_PUSHDATA2"],
@ -11,7 +11,7 @@
["0x4c 0x00","0 EQUAL"],
["0x4d 0x0000","0 EQUAL"],
["0x4e 0x00000000","0 EQUAL"],
["0x4f 1000 ADD","999 EQUAL"],
["0x4f 0x03e8 ADD","0x03e7 EQUAL"],
["0", "IF 0x50 ENDIF 1", "0x50 is reserved (ok if not executed)"],
["0x51", "0x5f ADD 0x60 EQUAL", "0x51 through 0x60 push 1 through 16 onto stack"],
["1","NOP"],
@ -53,8 +53,8 @@
["1 1", "VERIFY"],
["10 0 11 TOALTSTACK DROP FROMALTSTACK", "ADD 21 EQUAL"],
["'gavin_was_here' TOALTSTACK 11 FROMALTSTACK", "'gavin_was_here' EQUALVERIFY 11 EQUAL"],
["10 0 11 TOALTSTACK DROP FROMALTSTACK", "ADD 0x15 EQUAL"],
["0x676176696e5f7761735f68657265 TOALTSTACK 11 FROMALTSTACK", "0x676176696e5f7761735f68657265 EQUALVERIFY 11 EQUAL"],
["0 IFDUP", "DEPTH 1 EQUALVERIFY 0 EQUAL"],
["1 IFDUP", "DEPTH 2 EQUALVERIFY 1 EQUALVERIFY 1 EQUAL"],

View File

@ -9,6 +9,7 @@ var ScriptModule = bitcore.Script;
var Address = bitcore.Address.class();
var networks = bitcore.networks;
var Script;
var test_data = require('./testdata');
describe('Script', function() {
it('should initialze the main object', function() {
@ -82,4 +83,17 @@ describe('Script', function() {
script.chunks[0].should.equal(0);
});
});
test_data.dataScriptValid.forEach(function(datum) {
if (datum.length < 2) throw new Error('Invalid test data');
var human = datum[1];
it('should parse script from human readable ' + human, function() {
Script.fromStringContent(human).getStringContent(false, null).should.equal(human);
});
});
});

View File

@ -12,6 +12,7 @@ var Out;
var Script = bitcore.Script.class();
var buffertools = require('buffertools');
var test_data = require('./testdata');
var async = require('async');
describe('Transaction', function() {
it('should initialze the main object', function() {
@ -37,28 +38,38 @@ describe('Transaction', function() {
// ... where all scripts are stringified scripts.
test_data.dataTxValid.forEach(function(datum) {
if (datum.length === 3) {
it('valid tx=' + datum[1], function() {
it.skip('valid tx=' + datum[1], function(done) {
var inputs = datum[0];
var mapprevOutScriptPubKeys = {};
var ins = [];
var map = {};
inputs.forEach(function(vin) {
var hash = vin[0];
var index = vin[1];
var scriptPubKey = vin[2];
var input = new In({
s: scriptPubKey,
q: 0xffffffff,
oTxHash: hash,
oIndex: index
});
//mapprevOutScriptPubKeys[input] = new Script(scriptPubKey);
ins.push(input);
var scriptPubKey = new Script(new Buffer(vin[2]));
map[[hash, index]] = scriptPubKey;//Script.fromStringContent(scriptPubKey);
console.log(scriptPubKey.getStringContent());
console.log('********************************');
});
var raw = new Buffer(datum[1], 'hex');
var tx = new Transaction();
tx.parse(raw);
buffertools.toHex(tx.serialize()).should.equal(buffertools.toHex(raw));
var i = 0;
var stx = tx.getStandardizedObject();
async.eachSeries(tx.ins,
function(txin, next) {
var scriptPubKey = map[[stx.in[i].prev_out.hash, stx.in[i].prev_out.n]];
i += 1;
next();
},
function(err) {
should.not.exist(err);
done();
}
);
});
}
});

View File

@ -7,9 +7,13 @@ var dataInvalid = JSON.parse(fs.readFileSync('test/data/base58_keys_invalid.json
var dataEncodeDecode = JSON.parse(fs.readFileSync('test/data/base58_encode_decode.json'));
var dataTxValid = JSON.parse(fs.readFileSync('test/data/tx_valid.json'));
var dataTxInvalid = JSON.parse(fs.readFileSync('test/data/tx_invalid.json'));
var dataScriptValid = JSON.parse(fs.readFileSync('test/data/script_valid.json'));
var dataScriptInvalid = JSON.parse(fs.readFileSync('test/data/script_invalid.json'));
module.exports.dataValid = dataValid;
module.exports.dataInvalid = dataInvalid;
module.exports.dataEncodeDecode = dataEncodeDecode;
module.exports.dataTxValid = dataTxValid;
module.exports.dataTxInvalid = dataTxInvalid;
module.exports.dataScriptValid = dataScriptValid;
module.exports.dataScriptInvalid = dataScriptInvalid;