script.fromString.
This commit is contained in:
parent
9fe97dc855
commit
8541843e14
@ -535,6 +535,32 @@ Witness.fromRaw = function fromRaw(data, enc) {
|
|||||||
return new Witness().fromRaw(data);
|
return new Witness().fromRaw(data);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inject items from string.
|
||||||
|
* @private
|
||||||
|
* @param {String|String[]} items
|
||||||
|
*/
|
||||||
|
|
||||||
|
Witness.prototype.fromString = function fromString(items) {
|
||||||
|
var i;
|
||||||
|
|
||||||
|
if (!Array.isArray(items)) {
|
||||||
|
assert(typeof items === 'string');
|
||||||
|
|
||||||
|
items = items.trim();
|
||||||
|
|
||||||
|
if (items.length === 0)
|
||||||
|
return this;
|
||||||
|
|
||||||
|
items = items.split(/\s+/);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < items.length; i++)
|
||||||
|
this.items.push(new Buffer(items[i], 'hex'));
|
||||||
|
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse a test script/array
|
* Parse a test script/array
|
||||||
* string into a witness object. _Must_
|
* string into a witness object. _Must_
|
||||||
@ -546,24 +572,7 @@ Witness.fromRaw = function fromRaw(data, enc) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Witness.fromString = function fromString(items) {
|
Witness.fromString = function fromString(items) {
|
||||||
var i, result;
|
return new Witness().fromString(items);
|
||||||
|
|
||||||
if (!Array.isArray(items)) {
|
|
||||||
if (typeof items !== 'string')
|
|
||||||
return new Witness();
|
|
||||||
|
|
||||||
items = items.trim().split(/\s+/);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (items.length === 0)
|
|
||||||
return new Witness();
|
|
||||||
|
|
||||||
result = new Array(items.length);
|
|
||||||
|
|
||||||
for (i = 0; i < items.length; i++)
|
|
||||||
result[i] = new Buffer(items[i], 'hex');
|
|
||||||
|
|
||||||
return new Witness(result);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -2677,23 +2686,25 @@ Script.fromAddress = function fromAddress(address) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Script.prototype.fromCommitment = function fromCommitment(hash, flags) {
|
Script.prototype.fromCommitment = function fromCommitment(hash, flags) {
|
||||||
|
var code = [];
|
||||||
var p;
|
var p;
|
||||||
|
|
||||||
if (!flags)
|
|
||||||
flags = 'mined by bcoin';
|
|
||||||
|
|
||||||
if (typeof flags === 'string')
|
if (typeof flags === 'string')
|
||||||
flags = new Buffer(flags, 'utf8');
|
flags = new Buffer(flags, 'utf8');
|
||||||
|
|
||||||
|
assert(!flags || Buffer.isBuffer(flags));
|
||||||
|
|
||||||
p = new BufferWriter();
|
p = new BufferWriter();
|
||||||
p.writeU32BE(0xaa21a9ed);
|
p.writeU32BE(0xaa21a9ed);
|
||||||
p.writeHash(hash);
|
p.writeHash(hash);
|
||||||
|
|
||||||
return this.fromArray([
|
code.push(opcodes.OP_RETURN);
|
||||||
opcodes.OP_RETURN,
|
code.push(p.render());
|
||||||
p.render(),
|
|
||||||
flags
|
if (flags)
|
||||||
]);
|
code.push(flags);
|
||||||
|
|
||||||
|
return this.fromArray(code);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -4021,25 +4032,24 @@ Script.getWitnessSigops = function getWitnessSigops(input, output, witness, flag
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse a bitcoind test script
|
* Inject properties from bitcoind test string.
|
||||||
* string into a script object.
|
* @private
|
||||||
* @param {String} items - Script string.
|
* @param {String} items - Script string.
|
||||||
* @returns {Script}
|
|
||||||
* @throws Parse error.
|
* @throws Parse error.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Script.fromString = function fromString(code) {
|
Script.prototype.fromString = function fromString(code) {
|
||||||
var i, op, symbol, p;
|
var i, op, symbol, p;
|
||||||
|
|
||||||
if (typeof code !== 'string')
|
assert(typeof code === 'string');
|
||||||
return new Script();
|
|
||||||
|
|
||||||
code = code.trim();
|
code = code.trim();
|
||||||
|
|
||||||
if (code.length === 0)
|
if (code.length === 0)
|
||||||
return new Script();
|
return this;
|
||||||
|
|
||||||
code = code.split(/\s+/);
|
code = code.split(/\s+/);
|
||||||
|
|
||||||
p = new BufferWriter();
|
p = new BufferWriter();
|
||||||
|
|
||||||
for (i = 0; i < code.length; i++) {
|
for (i = 0; i < code.length; i++) {
|
||||||
@ -4074,7 +4084,19 @@ Script.fromString = function fromString(code) {
|
|||||||
p.writeU8(opcodes[symbol]);
|
p.writeU8(opcodes[symbol]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Script(p.render());
|
return this.fromRaw(p.render());
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse a bitcoind test script
|
||||||
|
* string into a script object.
|
||||||
|
* @param {String} items - Script string.
|
||||||
|
* @returns {Script}
|
||||||
|
* @throws Parse error.
|
||||||
|
*/
|
||||||
|
|
||||||
|
Script.fromString = function fromString(code) {
|
||||||
|
return new Script().fromString(code);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -272,7 +272,7 @@ describe('Script', function() {
|
|||||||
|
|
||||||
scripts.forEach(function(data) {
|
scripts.forEach(function(data) {
|
||||||
// ["Format is: [[wit...]?, scriptSig, scriptPubKey, flags, expected_scripterror, ... comments]"],
|
// ["Format is: [[wit...]?, scriptSig, scriptPubKey, flags, expected_scripterror, ... comments]"],
|
||||||
var witness = Array.isArray(data[0]) ? data.shift() : null;
|
var witness = Array.isArray(data[0]) ? data.shift() : [];
|
||||||
var input = data[0] ? data[0].trim() : data[0] || '';
|
var input = data[0] ? data[0].trim() : data[0] || '';
|
||||||
var output = data[1] ? data[1].trim() : data[1] || '';
|
var output = data[1] ? data[1].trim() : data[1] || '';
|
||||||
var flags = data[2] ? data[2].trim().split(/,\s*/) : [];
|
var flags = data[2] ? data[2].trim().split(/,\s*/) : [];
|
||||||
@ -288,7 +288,7 @@ describe('Script', function() {
|
|||||||
|
|
||||||
comments += ' (' + expected + ')';
|
comments += ' (' + expected + ')';
|
||||||
|
|
||||||
if (witness)
|
if (witness.length !== 0)
|
||||||
amount = witness.pop() * 100000000;
|
amount = witness.pop() * 100000000;
|
||||||
|
|
||||||
witness = bcoin.witness.fromString(witness);
|
witness = bcoin.witness.fromString(witness);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user