Browse Source

implement Script.buildDDataOut()

patch-2
Manuel Araoz 10 years ago
parent
commit
4bca5316ea
  1. 39
      lib/script.js
  2. 15
      test/script.js

39
lib/script.js

@ -196,21 +196,27 @@ Script.prototype.isPublicKeyHashIn = function() {
this.chunks[0].buf.length >= 0x47 &&
this.chunks[0].buf.length <= 0x49 &&
this.chunks[1].buf &&
PublicKey.isValid(this.chunks[1].buf));
/*
(
// compressed public key
(this.chunks[1].buf[0] === 0x03 && this.chunks[1].buf.length === 0x21) ||
(
(this.chunks[1].buf[0] === 0x02 || this.chunks[1].buf[0] === 0x03) &&
this.chunks[1].buf.length === 0x21) ||
// uncompressed public key
(this.chunks[1].buf[0] === 0x04 && this.chunks[1].buf.length === 0x41))
);
*/
};
/**
* @returns true if this is a public key output script
*/
Script.prototype.isPublicKeyOut = function() {
console.log(this.toString());
return this.chunks.length === 2 &&
Buffer.isBuffer(this.chunks[0].buf) &&
this.chunks[0].buf.length === 0x41 &&
PublicKey.isValid(this.chunks[0].buf) &&
this.chunks[1] === Opcode('OP_CHECKSIG').toNumber();
};
@ -404,13 +410,15 @@ Script.prototype._addOpcode = function(opcode, prepend) {
Script.prototype._addBuffer = function(buf, prepend) {
var opcodenum;
var len = buf.length;
if (buf.length > 0 && buf.length < Opcode.map.OP_PUSHDATA1) {
opcodenum = buf.length;
} else if (buf.length < Math.pow(2, 8)) {
if (len === 0) {
return;
} else if (len > 0 && len < Opcode.map.OP_PUSHDATA1) {
opcodenum = len;
} else if (len < Math.pow(2, 8)) {
opcodenum = Opcode.map.OP_PUSHDATA1;
} else if (buf.length < Math.pow(2, 16)) {
} else if (len < Math.pow(2, 16)) {
opcodenum = Opcode.map.OP_PUSHDATA2;
} else if (buf.length < Math.pow(2, 32)) {
} else if (len < Math.pow(2, 32)) {
opcodenum = Opcode.map.OP_PUSHDATA4;
} else {
throw new Error('You can\'t push that much data');
@ -433,7 +441,7 @@ Script.prototype._addBuffer = function(buf, prepend) {
Script.buildMultisigOut = function(pubkeys, m) {
var s = new Script();
s.add(Opcode.smallInt(m));
for (var i = 0; i<pubkeys.length; i++) {
for (var i = 0; i < pubkeys.length; i++) {
var pubkey = pubkeys[i];
s.add(pubkey.toBuffer());
}
@ -464,16 +472,23 @@ Script.buildPublicKeyHashOut = function(to) {
* public key
*/
Script.buildPublicKeyOut = function(pubkey) {
console.log(pubkey);
return new Script();
var s = new Script();
s.add(pubkey.toBuffer())
.add(Opcode('OP_CHECKSIG'));
return s;
};
/**
* @returns a new OP_RETURN script with data
*/
Script.buildDataOut = function(data) {
console.log(data);
return new Script();
if (typeof data === 'string') {
data = new Buffer(data);
}
var s = new Script();
s.add(Opcode('OP_RETURN'))
.add(data);
return s;
};
/**

15
test/script.js

@ -327,6 +327,9 @@ describe('Script', function() {
describe('#add and #prepend', function() {
it('should add these ops', function() {
Script().add(Opcode('OP_RETURN')).add(new Buffer('')).toString().should.equal('OP_RETURN');
});
it('should add these ops', function() {
Script().add('OP_CHECKMULTISIG').toString().should.equal('OP_CHECKMULTISIG');
Script().add('OP_1').add('OP_2').toString().should.equal('OP_1 OP_2');
@ -377,7 +380,7 @@ describe('Script', function() {
});
});
describe.only('new methods', function() {
describe('new methods', function() {
describe('#buildMultisigOut', function() {
var pubkey_hexs = [
'022df8750480ad5b26950b25c7ba79d3e37d75f640f8e5d9bcd5b150a0f85014da',
@ -427,13 +430,13 @@ describe('Script', function() {
var pubkey = new PublicKey('022df8750480ad5b26950b25c7ba79d3e37d75f640f8e5d9bcd5b150a0f85014da');
var s = Script.buildPublicKeyOut(pubkey);
should.exist(s);
s.toString().should.equal('9674af7395592ec5d91573aa8d6557de55f60147 OP_CHECKSIG');
s.toString().should.equal('33 0x022df8750480ad5b26950b25c7ba79d3e37d75f640f8e5d9bcd5b150a0f85014da OP_CHECKSIG');
s.isPublicKeyOut().should.equal(true);
});
});
describe('#buildDataOut', function() {
it('should create script from empty data', function() {
var data = new Buffer();
var data = new Buffer('');
var s = Script.buildDataOut(data);
should.exist(s);
s.toString().should.equal('OP_RETURN');
@ -443,14 +446,14 @@ describe('Script', function() {
var data = new Buffer('bacacafe0102030405', 'hex');
var s = Script.buildDataOut(data);
should.exist(s);
s.toString().should.equal('OP_RETURN bacacafe0102030405');
s.toString().should.equal('OP_RETURN 9 0xbacacafe0102030405');
s.isDataOut().should.equal(true);
});
it('should create script from string', function() {
var data = 'hello world';
var data = 'hello world!!!';
var s = Script.buildDataOut(data);
should.exist(s);
s.toString().should.equal('OP_RETURN 68656c6c6f20776f726c64212121');
s.toString().should.equal('OP_RETURN 14 0x68656c6c6f20776f726c64212121');
s.isDataOut().should.equal(true);
});
});

Loading…
Cancel
Save