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);
|
||||
};
|
||||
|
||||
/**
|
||||
* 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
|
||||
* string into a witness object. _Must_
|
||||
@ -546,24 +572,7 @@ Witness.fromRaw = function fromRaw(data, enc) {
|
||||
*/
|
||||
|
||||
Witness.fromString = function fromString(items) {
|
||||
var i, result;
|
||||
|
||||
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);
|
||||
return new Witness().fromString(items);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -2677,23 +2686,25 @@ Script.fromAddress = function fromAddress(address) {
|
||||
*/
|
||||
|
||||
Script.prototype.fromCommitment = function fromCommitment(hash, flags) {
|
||||
var code = [];
|
||||
var p;
|
||||
|
||||
if (!flags)
|
||||
flags = 'mined by bcoin';
|
||||
|
||||
if (typeof flags === 'string')
|
||||
flags = new Buffer(flags, 'utf8');
|
||||
|
||||
assert(!flags || Buffer.isBuffer(flags));
|
||||
|
||||
p = new BufferWriter();
|
||||
p.writeU32BE(0xaa21a9ed);
|
||||
p.writeHash(hash);
|
||||
|
||||
return this.fromArray([
|
||||
opcodes.OP_RETURN,
|
||||
p.render(),
|
||||
flags
|
||||
]);
|
||||
code.push(opcodes.OP_RETURN);
|
||||
code.push(p.render());
|
||||
|
||||
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
|
||||
* string into a script object.
|
||||
* Inject properties from bitcoind test string.
|
||||
* @private
|
||||
* @param {String} items - Script string.
|
||||
* @returns {Script}
|
||||
* @throws Parse error.
|
||||
*/
|
||||
|
||||
Script.fromString = function fromString(code) {
|
||||
Script.prototype.fromString = function fromString(code) {
|
||||
var i, op, symbol, p;
|
||||
|
||||
if (typeof code !== 'string')
|
||||
return new Script();
|
||||
assert(typeof code === 'string');
|
||||
|
||||
code = code.trim();
|
||||
|
||||
if (code.length === 0)
|
||||
return new Script();
|
||||
return this;
|
||||
|
||||
code = code.split(/\s+/);
|
||||
|
||||
p = new BufferWriter();
|
||||
|
||||
for (i = 0; i < code.length; i++) {
|
||||
@ -4074,7 +4084,19 @@ Script.fromString = function fromString(code) {
|
||||
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) {
|
||||
// ["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 output = data[1] ? data[1].trim() : data[1] || '';
|
||||
var flags = data[2] ? data[2].trim().split(/,\s*/) : [];
|
||||
@ -288,7 +288,7 @@ describe('Script', function() {
|
||||
|
||||
comments += ' (' + expected + ')';
|
||||
|
||||
if (witness)
|
||||
if (witness.length !== 0)
|
||||
amount = witness.pop() * 100000000;
|
||||
|
||||
witness = bcoin.witness.fromString(witness);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user