This commit is contained in:
Christopher Jeffrey 2016-03-28 19:42:05 -07:00
parent 484db3568a
commit ea4d0dee03
5 changed files with 483 additions and 381 deletions

View File

@ -13,7 +13,7 @@ var constants = bcoin.protocol.constants;
var network = bcoin.protocol.network; var network = bcoin.protocol.network;
var Script = bcoin.script; var Script = bcoin.script;
var Witness = bcoin.script.witness; var Witness = bcoin.script.witness;
var opc = constants.opcodes; var opcodes = constants.opcodes;
/** /**
* MTX * MTX
@ -189,7 +189,7 @@ MTX.prototype.scriptInput = function scriptInput(index, addr) {
redeemScript = addr.program.encode(); redeemScript = addr.program.encode();
vector = input.witness.items; vector = input.witness.items;
dummy = new Buffer([]); dummy = new Buffer([]);
assert(addr.program.code[0] === opc['0'], 'Non-zero version passed to address.'); assert(addr.program.code[0] === opcodes.OP_0, 'Non-zero version passed to address.');
if (addr.program.code[1].length === 32) { if (addr.program.code[1].length === 32) {
// P2WSH nested within pay-to-scripthash // P2WSH nested within pay-to-scripthash
// (it had to be this complicated, didn't it?) // (it had to be this complicated, didn't it?)
@ -206,7 +206,7 @@ MTX.prototype.scriptInput = function scriptInput(index, addr) {
redeemScript = addr.script.encode(); redeemScript = addr.script.encode();
vector = input.script.code; vector = input.script.code;
prev = addr.script; prev = addr.script;
dummy = opc['0']; dummy = opcodes.OP_0;
} else { } else {
return false; return false;
} }
@ -215,7 +215,7 @@ MTX.prototype.scriptInput = function scriptInput(index, addr) {
vector = input.witness.items; vector = input.witness.items;
dummy = new Buffer([]); dummy = new Buffer([]);
if (prev.code[0] !== opc['0']) if (prev.code[0] !== opcodes.OP_0)
return false; return false;
if (prev.code[1].length === 32) { if (prev.code[1].length === 32) {
@ -238,7 +238,7 @@ MTX.prototype.scriptInput = function scriptInput(index, addr) {
} else { } else {
// Wow, a normal output! Praise be to Jengus and Gord. // Wow, a normal output! Praise be to Jengus and Gord.
vector = input.script.code; vector = input.script.code;
dummy = opc['0']; dummy = opcodes.OP_0;
} }
if (prev.isPubkey()) { if (prev.isPubkey()) {
@ -360,7 +360,7 @@ MTX.prototype.signInput = function signInput(index, addr, type) {
vector = input.script.code; vector = input.script.code;
len = vector.length; len = vector.length;
dummy = opc['0']; dummy = opcodes.OP_0;
version = 0; version = 0;
// We need to grab the redeem script when // We need to grab the redeem script when

View File

@ -48,145 +48,143 @@ exports.filterFlags = {
}; };
exports.opcodes = { exports.opcodes = {
// 'false': 0x00, OP_FALSE: 0x00,
'0': 0x00, OP_0: 0x00,
pushdata1: 0x4c, OP_PUSHDATA1: 0x4c,
pushdata2: 0x4d, OP_PUSHDATA2: 0x4d,
pushdata4: 0x4e, OP_PUSHDATA4: 0x4e,
'1negate': 0x4f, OP_1NEGATE: 0x4f,
reserved: 0x50, OP_RESERVED: 0x50,
// 'true': 0x51, OP_TRUE: 0x51,
'1': 0x51, OP_1: 0x51,
'2': 0x52, OP_2: 0x52,
'3': 0x53, OP_3: 0x53,
'4': 0x54, OP_4: 0x54,
'5': 0x55, OP_5: 0x55,
'6': 0x56, OP_6: 0x56,
'7': 0x57, OP_7: 0x57,
'8': 0x58, OP_8: 0x58,
'9': 0x59, OP_9: 0x59,
'10': 0x5a, OP_10: 0x5a,
'11': 0x5b, OP_11: 0x5b,
'12': 0x5c, OP_12: 0x5c,
'13': 0x5d, OP_13: 0x5d,
'14': 0x5e, OP_14: 0x5e,
'15': 0x5f, OP_15: 0x5f,
'16': 0x60, OP_16: 0x60,
nop: 0x61, OP_NOP: 0x61,
ver: 0x62, OP_VER: 0x62,
'if': 0x63, OP_IF: 0x63,
notif: 0x64, OP_NOTIF: 0x64,
verif: 0x65, OP_VERIF: 0x65,
vernotif: 0x66, OP_VERNOTIF: 0x66,
'else': 0x67, OP_ELSE: 0x67,
endif: 0x68, OP_ENDIF: 0x68,
verify: 0x69, OP_VERIFY: 0x69,
'return': 0x6a, OP_RETURN: 0x6a,
toaltstack: 0x6b, OP_TOALTSTACK: 0x6b,
fromaltstack: 0x6c, OP_FROMALTSTACK: 0x6c,
'2drop': 0x6d, OP_2DROP: 0x6d,
'2dup': 0x6e, OP_2DUP: 0x6e,
'3dup': 0x6f, OP_3DUP: 0x6f,
'2over': 0x70, OP_2OVER: 0x70,
'2rot': 0x71, OP_2ROT: 0x71,
'2swap': 0x72, OP_2SWAP: 0x72,
ifdup: 0x73, OP_IFDUP: 0x73,
depth: 0x74, OP_DEPTH: 0x74,
drop: 0x75, OP_DROP: 0x75,
dup: 0x76, OP_DUP: 0x76,
nip: 0x77, OP_NIP: 0x77,
over: 0x78, OP_OVER: 0x78,
pick: 0x79, OP_PICK: 0x79,
roll: 0x7a, OP_ROLL: 0x7a,
rot: 0x7b, OP_ROT: 0x7b,
swap: 0x7c, OP_SWAP: 0x7c,
tuck: 0x7d, OP_TUCK: 0x7d,
cat: 0x7e, OP_CAT: 0x7e,
substr: 0x7f, OP_SUBSTR: 0x7f,
left: 0x80, OP_LEFT: 0x80,
right: 0x81, OP_RIGHT: 0x81,
size: 0x82, OP_SIZE: 0x82,
invert: 0x83, OP_INVERT: 0x83,
and: 0x84, OP_AND: 0x84,
or: 0x85, OP_OR: 0x85,
xor: 0x86, OP_XOR: 0x86,
equal: 0x87, OP_EQUAL: 0x87,
equalverify: 0x88, OP_EQUALVERIFY: 0x88,
reserved1: 0x89, OP_RESERVED1: 0x89,
reserved2: 0x8a, OP_RESERVED2: 0x8a,
'1add': 0x8b, OP_1ADD: 0x8b,
'1sub': 0x8c, OP_1SUB: 0x8c,
'2mul': 0x8d, OP_2MUL: 0x8d,
'2div': 0x8e, OP_2DIV: 0x8e,
negate: 0x8f, OP_NEGATE: 0x8f,
abs: 0x90, OP_ABS: 0x90,
not: 0x91, OP_NOT: 0x91,
'0notequal': 0x92, OP_0NOTEQUAL: 0x92,
add: 0x93, OP_ADD: 0x93,
sub: 0x94, OP_SUB: 0x94,
mul: 0x95, OP_MUL: 0x95,
div: 0x96, OP_DIV: 0x96,
mod: 0x97, OP_MOD: 0x97,
lshift: 0x98, OP_LSHIFT: 0x98,
rshift: 0x99, OP_RSHIFT: 0x99,
booland: 0x9a, OP_BOOLAND: 0x9a,
boolor: 0x9b, OP_BOOLOR: 0x9b,
numequal: 0x9c, OP_NUMEQUAL: 0x9c,
numequalverify: 0x9d, OP_NUMEQUALVERIFY: 0x9d,
numnotequal: 0x9e, OP_NUMNOTEQUAL: 0x9e,
lessthan: 0x9f, OP_LESSTHAN: 0x9f,
greaterthan: 0xa0, OP_GREATERTHAN: 0xa0,
lessthanorequal: 0xa1, OP_LESSTHANOREQUAL: 0xa1,
greaterthanorequal: 0xa2, OP_GREATERTHANOREQUAL: 0xa2,
min: 0xa3, OP_MIN: 0xa3,
max: 0xa4, OP_MAX: 0xa4,
within: 0xa5, OP_WITHIN: 0xa5,
ripemd160: 0xa6, OP_RIPEMD160: 0xa6,
sha1: 0xa7, OP_SHA1: 0xa7,
sha256: 0xa8, OP_SHA256: 0xa8,
hash160: 0xa9, OP_HASH160: 0xa9,
hash256: 0xaa, OP_HASH256: 0xaa,
codeseparator: 0xab, OP_CODESEPARATOR: 0xab,
checksig: 0xac, OP_CHECKSIG: 0xac,
checksigverify: 0xad, OP_CHECKSIGVERIFY: 0xad,
checkmultisig: 0xae, OP_CHECKMULTISIG: 0xae,
checkmultisigverify: 0xaf, OP_CHECKMULTISIGVERIFY: 0xaf,
// 'eval': 0xb0, OP_EVAL: 0xb0,
nop1: 0xb0, OP_NOP1: 0xb0,
// nop2: 0xb1, OP_NOP2: 0xb1,
checklocktimeverify: 0xb1, OP_CHECKLOCKTIMEVERIFY: 0xb1,
// nop3: 0xb2, OP_NOP3: 0xb2,
checksequenceverify: 0xb2, OP_CHECKSEQUENCEVERIFY: 0xb2,
nop4: 0xb3, OP_NOP4: 0xb3,
nop5: 0xb4, OP_NOP5: 0xb4,
nop6: 0xb5, OP_NOP6: 0xb5,
nop7: 0xb6, OP_NOP7: 0xb6,
nop8: 0xb7, OP_NOP8: 0xb7,
nop9: 0xb8, OP_NOP9: 0xb8,
nop10: 0xb9, OP_NOP10: 0xb9,
pubkeyhash: 0xfd, OP_PUBKEYHASH: 0xfd,
pubkey: 0xfe, OP_PUBKEY: 0xfe,
invalidopcode: 0xff OP_INVALIDOPCODE: 0xff
}; };
exports.opcodesByVal = new Array(256); exports.opcodesByVal = new Array(256);
Object.keys(exports.opcodes).forEach(function(name) { Object.keys(exports.opcodes).forEach(function(name) {
var val = exports.opcodes[name]; var val = exports.opcodes[name];
// if (val === 0x00 || (val >= 0x51 && val <= 0x60))
// name = +name;
exports.opcodesByVal[val] = name; exports.opcodesByVal[val] = name;
}); });

File diff suppressed because it is too large Load Diff

View File

@ -3,7 +3,7 @@ var bcoin = require('../');
var constants = bcoin.protocol.constants; var constants = bcoin.protocol.constants;
var utils = bcoin.utils; var utils = bcoin.utils;
var assert = utils.assert; var assert = utils.assert;
var opc = constants.opcodes; var opcodes = constants.opcodes;
describe('Wallet', function() { describe('Wallet', function() {
process.env.BCOIN_DB = 'memdown'; process.env.BCOIN_DB = 'memdown';
@ -31,7 +31,7 @@ describe('Wallet', function() {
// Coinbase // Coinbase
var t1 = bcoin.mtx().addOutput(w, 50000).addOutput(w, 10000); // 10000 instead of 1000 var t1 = bcoin.mtx().addOutput(w, 50000).addOutput(w, 10000); // 10000 instead of 1000
var prev = new bcoin.script([w.publicKey, opc['checksig']]); var prev = new bcoin.script([w.publicKey, opcodes.OP_CHECKSIG]);
var dummyInput = { var dummyInput = {
prevout: { prevout: {
hash: constants.oneHash, hash: constants.oneHash,

View File

@ -2,7 +2,7 @@ var assert = require('assert');
var bcoin = require('../'); var bcoin = require('../');
var Script = bcoin.script; var Script = bcoin.script;
var Stack = bcoin.script.stack; var Stack = bcoin.script.stack;
var opc = bcoin.protocol.constants.opcodes; var opcodes = bcoin.protocol.constants.opcodes;
describe('Script', function() { describe('Script', function() {
it('should encode/decode script', function() { it('should encode/decode script', function() {
@ -20,7 +20,7 @@ describe('Script', function() {
assert.equal( assert.equal(
bcoin.utils.toHex(decoded[1]), bcoin.utils.toHex(decoded[1]),
'101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f'); '101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f');
assert.equal(decoded[2], opc['checksig']); assert.equal(decoded[2], opcodes.OP_CHECKSIG);
var dst = bcoin.script.encode(decoded); var dst = bcoin.script.encode(decoded);
assert.equal(bcoin.utils.toHex(dst), src); assert.equal(bcoin.utils.toHex(dst), src);
@ -50,40 +50,79 @@ describe('Script', function() {
}); });
it('should handle if statements correctly', function () { it('should handle if statements correctly', function () {
var inputScript = new Script([opc['1'], opc['2']]); var inputScript = new Script([opcodes.OP_1, opcodes.OP_2]);
var prevOutScript = new Script([opc['2'], opc.equal, opc['if'], opc[3], opc['else'], opc[4], opc.endif, opc[5]]); var prevOutScript = new Script([
opcodes.OP_2,
opcodes.OP_EQUAL,
opcodes.OP_IF,
opcodes.OP_3,
opcodes.OP_ELSE,
opcodes.OP_4,
opcodes.OP_ENDIF,
opcodes.OP_5
]);
var stack = new Stack(); var stack = new Stack();
inputScript.execute(stack); inputScript.execute(stack);
var res = prevOutScript.execute(stack); var res = prevOutScript.execute(stack);
assert(res); assert(res);
assert.deepEqual(stack.slice(), [[1], [3], [5]]); assert.deepEqual(stack.slice(), [[1], [3], [5]]);
var inputScript = new Script([opc[1], opc[2]]); var inputScript = new Script([opcodes.OP_1, opcodes.OP_2]);
var prevOutScript = new Script([opc[9], opc['equal'], opc['if'], opc[3], opc['else'], opc[4], opc['endif'], opc[5]]); var prevOutScript = new Script([
opcodes.OP_9,
opcodes.OP_EQUAL,
opcodes.OP_IF,
opcodes.OP_3,
opcodes.OP_ELSE,
opcodes.OP_4,
opcodes.OP_ENDIF,
opcodes.OP_5
]);
var stack = new Stack(); var stack = new Stack();
inputScript.execute(stack); inputScript.execute(stack);
var res = prevOutScript.execute(stack); var res = prevOutScript.execute(stack);
assert(res); assert(res);
assert.deepEqual(stack.slice(), [[1], [4], [5]]); assert.deepEqual(stack.slice(), [[1], [4], [5]]);
var inputScript = new Script([opc[1], opc[2]]); var inputScript = new Script([opcodes.OP_1, opcodes.OP_2]);
var prevOutScript = new Script([opc[2], opc['equal'], opc['if'], opc[3], opc['endif'], opc[5]]); var prevOutScript = new Script([
opcodes.OP_2,
opcodes.OP_EQUAL,
opcodes.OP_IF,
opcodes.OP_3,
opcodes.OP_ENDIF,
opcodes.OP_5
]);
var stack = new Stack(); var stack = new Stack();
inputScript.execute(stack); inputScript.execute(stack);
var res = prevOutScript.execute(stack); var res = prevOutScript.execute(stack);
assert(res); assert(res);
assert.deepEqual(stack.slice(), [[1], [3], [5]]); assert.deepEqual(stack.slice(), [[1], [3], [5]]);
var inputScript = new Script([opc[1], opc[2]]); var inputScript = new Script([opcodes.OP_1, opcodes.OP_2]);
var prevOutScript = new Script([opc[9], opc['equal'], opc['if'], opc[3], opc['endif'], opc[5]]); var prevOutScript = new Script([
opcodes.OP_9,
opcodes.OP_EQUAL,
opcodes.OP_IF,
opcodes.OP_3,
opcodes.OP_ENDIF,
opcodes.OP_5
]);
var stack = new Stack(); var stack = new Stack();
inputScript.execute(stack); inputScript.execute(stack);
var res = prevOutScript.execute(stack); var res = prevOutScript.execute(stack);
assert(res); assert(res);
assert.deepEqual(stack.slice(), [[1], [5]]); assert.deepEqual(stack.slice(), [[1], [5]]);
var inputScript = new Script([opc[1], opc[2]]); var inputScript = new Script([opcodes.OP_1, opcodes.OP_2]);
var prevOutScript = new Script([opc[9], opc['equal'], opc['notif'], opc[3], opc['endif'], opc[5]]); var prevOutScript = new Script([
opcodes.OP_9,
opcodes.OP_EQUAL,
opcodes.OP_NOTIF,
opcodes.OP_3,
opcodes.OP_ENDIF,
opcodes.OP_5
]);
var stack = new Stack(); var stack = new Stack();
inputScript.execute(stack); inputScript.execute(stack);
var res = prevOutScript.execute(stack); var res = prevOutScript.execute(stack);