Opcode('*').toNumber() -> Opcode.*

This commit is contained in:
Manuel Araoz 2014-12-11 16:19:11 -03:00
parent bb8373ead7
commit d3b761fc7c
1 changed files with 31 additions and 31 deletions

View File

@ -55,14 +55,14 @@ Script.fromBuffer = function(buffer) {
var opcodenum = br.readUInt8(); var opcodenum = br.readUInt8();
var len, buf; var len, buf;
if (opcodenum > 0 && opcodenum < Opcode.map.OP_PUSHDATA1) { if (opcodenum > 0 && opcodenum < Opcode.OP_PUSHDATA1) {
len = opcodenum; len = opcodenum;
script.chunks.push({ script.chunks.push({
buf: br.read(len), buf: br.read(len),
len: len, len: len,
opcodenum: opcodenum opcodenum: opcodenum
}); });
} else if (opcodenum === Opcode.map.OP_PUSHDATA1) { } else if (opcodenum === Opcode.OP_PUSHDATA1) {
len = br.readUInt8(); len = br.readUInt8();
buf = br.read(len); buf = br.read(len);
script.chunks.push({ script.chunks.push({
@ -70,7 +70,7 @@ Script.fromBuffer = function(buffer) {
len: len, len: len,
opcodenum: opcodenum opcodenum: opcodenum
}); });
} else if (opcodenum === Opcode.map.OP_PUSHDATA2) { } else if (opcodenum === Opcode.OP_PUSHDATA2) {
len = br.readUInt16LE(); len = br.readUInt16LE();
buf = br.read(len); buf = br.read(len);
script.chunks.push({ script.chunks.push({
@ -78,7 +78,7 @@ Script.fromBuffer = function(buffer) {
len: len, len: len,
opcodenum: opcodenum opcodenum: opcodenum
}); });
} else if (opcodenum === Opcode.map.OP_PUSHDATA4) { } else if (opcodenum === Opcode.OP_PUSHDATA4) {
len = br.readUInt32LE(); len = br.readUInt32LE();
buf = br.read(len); buf = br.read(len);
script.chunks.push({ script.chunks.push({
@ -104,15 +104,15 @@ Script.prototype.toBuffer = function() {
var opcodenum = chunk.opcodenum; var opcodenum = chunk.opcodenum;
bw.writeUInt8(chunk.opcodenum); bw.writeUInt8(chunk.opcodenum);
if (chunk.buf) { if (chunk.buf) {
if (opcodenum < Opcode.map.OP_PUSHDATA1) { if (opcodenum < Opcode.OP_PUSHDATA1) {
bw.write(chunk.buf); bw.write(chunk.buf);
} else if (opcodenum === Opcode.map.OP_PUSHDATA1) { } else if (opcodenum === Opcode.OP_PUSHDATA1) {
bw.writeUInt8(chunk.len); bw.writeUInt8(chunk.len);
bw.write(chunk.buf); bw.write(chunk.buf);
} else if (opcodenum === Opcode.map.OP_PUSHDATA2) { } else if (opcodenum === Opcode.OP_PUSHDATA2) {
bw.writeUInt16LE(chunk.len); bw.writeUInt16LE(chunk.len);
bw.write(chunk.buf); bw.write(chunk.buf);
} else if (opcodenum === Opcode.map.OP_PUSHDATA4) { } else if (opcodenum === Opcode.OP_PUSHDATA4) {
bw.writeUInt32LE(chunk.len); bw.writeUInt32LE(chunk.len);
bw.write(chunk.buf); bw.write(chunk.buf);
} }
@ -138,7 +138,7 @@ Script.fromString = function(str) {
if (typeof opcodenum === 'undefined') { if (typeof opcodenum === 'undefined') {
opcodenum = parseInt(token); opcodenum = parseInt(token);
if (opcodenum > 0 && opcodenum < Opcode.map.OP_PUSHDATA1) { if (opcodenum > 0 && opcodenum < Opcode.OP_PUSHDATA1) {
script.chunks.push({ script.chunks.push({
buf: new Buffer(tokens[i + 1].slice(2), 'hex'), buf: new Buffer(tokens[i + 1].slice(2), 'hex'),
len: opcodenum, len: opcodenum,
@ -148,9 +148,9 @@ Script.fromString = function(str) {
} else { } else {
throw new Error('Invalid script: ' + JSON.stringify(str)); throw new Error('Invalid script: ' + JSON.stringify(str));
} }
} else if (opcodenum === Opcode.map.OP_PUSHDATA1 || } else if (opcodenum === Opcode.OP_PUSHDATA1 ||
opcodenum === Opcode.map.OP_PUSHDATA2 || opcodenum === Opcode.OP_PUSHDATA2 ||
opcodenum === Opcode.map.OP_PUSHDATA4) { opcodenum === Opcode.OP_PUSHDATA4) {
if (tokens[i + 2].slice(0, 2) !== '0x') { if (tokens[i + 2].slice(0, 2) !== '0x') {
throw new Error('Pushdata data must start with 0x'); throw new Error('Pushdata data must start with 0x');
} }
@ -228,7 +228,7 @@ Script.prototype.isPublicKeyOut = function() {
return this.chunks.length === 2 && return this.chunks.length === 2 &&
bufferUtil.isBuffer(this.chunks[0].buf) && bufferUtil.isBuffer(this.chunks[0].buf) &&
PublicKey.isValid(this.chunks[0].buf) && PublicKey.isValid(this.chunks[0].buf) &&
this.chunks[1].opcodenum === Opcode('OP_CHECKSIG').toNumber(); this.chunks[1].opcodenum === Opcode.OP_CHECKSIG;
}; };
/** /**
@ -246,10 +246,10 @@ Script.prototype.isPublicKeyIn = function() {
*/ */
Script.prototype.isScriptHashOut = function() { Script.prototype.isScriptHashOut = function() {
return this.chunks.length === 3 && return this.chunks.length === 3 &&
this.chunks[0].opcodenum === Opcode('OP_HASH160').toNumber() && this.chunks[0].opcodenum === Opcode.OP_HASH160 &&
this.chunks[1].buf && this.chunks[1].buf &&
this.chunks[1].buf.length === 20 && this.chunks[1].buf.length === 20 &&
this.chunks[2].opcodenum === Opcode('OP_EQUAL').toNumber(); this.chunks[2].opcodenum === Opcode.OP_EQUAL;
}; };
/** /**
@ -283,7 +283,7 @@ Script.prototype.isMultisigOut = function() {
return obj.buf && bufferUtil.isBuffer(obj.buf); return obj.buf && bufferUtil.isBuffer(obj.buf);
}) && }) &&
Opcode.isSmallIntOp(this.chunks[this.chunks.length - 2].opcodenum) && Opcode.isSmallIntOp(this.chunks[this.chunks.length - 2].opcodenum) &&
this.chunks[this.chunks.length - 1].opcodenum === Opcode.map.OP_CHECKMULTISIG); this.chunks[this.chunks.length - 1].opcodenum === Opcode.OP_CHECKMULTISIG);
}; };
@ -305,7 +305,7 @@ Script.prototype.isMultisigIn = function() {
*/ */
Script.prototype.isDataOut = function() { Script.prototype.isDataOut = function() {
return this.chunks.length >= 1 && return this.chunks.length >= 1 &&
this.chunks[0].opcodenum === Opcode('OP_RETURN').toNumber() && this.chunks[0].opcodenum === Opcode.OP_RETURN &&
(this.chunks.length === 1 || (this.chunks.length === 1 ||
(this.chunks.length === 2 && (this.chunks.length === 2 &&
this.chunks[1].buf && this.chunks[1].buf &&
@ -319,7 +319,7 @@ Script.prototype.isDataOut = function() {
*/ */
Script.prototype.isPushOnly = function() { Script.prototype.isPushOnly = function() {
return _.every(this.chunks, function(chunk) { return _.every(this.chunks, function(chunk) {
return chunk.opcodenum <= Opcode.map.OP_16; return chunk.opcodenum <= Opcode.OP_16;
}); });
}; };
@ -438,14 +438,14 @@ Script.prototype._addBuffer = function(buf, prepend) {
var len = buf.length; var len = buf.length;
if (len === 0) { if (len === 0) {
return; return;
} else if (len > 0 && len < Opcode.map.OP_PUSHDATA1) { } else if (len > 0 && len < Opcode.OP_PUSHDATA1) {
opcodenum = len; opcodenum = len;
} else if (len < Math.pow(2, 8)) { } else if (len < Math.pow(2, 8)) {
opcodenum = Opcode.map.OP_PUSHDATA1; opcodenum = Opcode.OP_PUSHDATA1;
} else if (len < Math.pow(2, 16)) { } else if (len < Math.pow(2, 16)) {
opcodenum = Opcode.map.OP_PUSHDATA2; opcodenum = Opcode.OP_PUSHDATA2;
} else if (len < Math.pow(2, 32)) { } else if (len < Math.pow(2, 32)) {
opcodenum = Opcode.map.OP_PUSHDATA4; opcodenum = Opcode.OP_PUSHDATA4;
} else { } else {
throw new Error('You can\'t push that much data'); throw new Error('You can\'t push that much data');
} }
@ -494,7 +494,7 @@ Script.buildMultisigOut = function(pubkeys, m, opts) {
s.add(pubkey.toBuffer()); s.add(pubkey.toBuffer());
} }
s.add(Opcode.smallInt(pubkeys.length)); s.add(Opcode.smallInt(pubkeys.length));
s.add(Opcode('OP_CHECKMULTISIG')); s.add(Opcode.OP_CHECKMULTISIG);
return s; return s;
}; };
@ -510,11 +510,11 @@ Script.buildPublicKeyHashOut = function(to) {
to = new Address(to); to = new Address(to);
} }
var s = new Script(); var s = new Script();
s.add(Opcode('OP_DUP')) s.add(Opcode.OP_DUP)
.add(Opcode('OP_HASH160')) .add(Opcode.OP_HASH160)
.add(to.hashBuffer) .add(to.hashBuffer)
.add(Opcode('OP_EQUALVERIFY')) .add(Opcode.OP_EQUALVERIFY)
.add(Opcode('OP_CHECKSIG')); .add(Opcode.OP_CHECKSIG);
return s; return s;
}; };
@ -525,7 +525,7 @@ Script.buildPublicKeyHashOut = function(to) {
Script.buildPublicKeyOut = function(pubkey) { Script.buildPublicKeyOut = function(pubkey) {
var s = new Script(); var s = new Script();
s.add(pubkey.toBuffer()) s.add(pubkey.toBuffer())
.add(Opcode('OP_CHECKSIG')); .add(Opcode.OP_CHECKSIG);
return s; return s;
}; };
@ -538,7 +538,7 @@ Script.buildDataOut = function(data) {
data = new Buffer(data); data = new Buffer(data);
} }
var s = new Script(); var s = new Script();
s.add(Opcode('OP_RETURN')) s.add(Opcode.OP_RETURN)
.add(data); .add(data);
return s; return s;
}; };
@ -549,9 +549,9 @@ Script.buildDataOut = function(data) {
*/ */
Script.buildScriptHashOut = function(script) { Script.buildScriptHashOut = function(script) {
var s = new Script(); var s = new Script();
s.add(Opcode('OP_HASH160')) s.add(Opcode.OP_HASH160)
.add(Hash.sha256ripemd160(script.toBuffer())) .add(Hash.sha256ripemd160(script.toBuffer()))
.add(Opcode('OP_EQUAL')); .add(Opcode.OP_EQUAL);
return s; return s;
}; };