MCPcopy Index your code
hub / github.com/bcoin-org/bcoin / fromString

Method fromString

lib/script/script.js:3015–3069  ·  view source on GitHub ↗

* Inject properties from bitcoind test string. * @private * @param {String} items - Script string. * @throws Parse error.

(code)

Source from the content-addressed store, hash-verified

3013 */
3014
3015 fromString(code) {
3016 assert(typeof code === 'string');
3017
3018 code = code.trim();
3019
3020 if (code.length === 0)
3021 return this;
3022
3023 const items = code.split(/\s+/);
3024 const bw = bio.write();
3025
3026 for (const item of items) {
3027 let symbol = item;
3028
3029 if (symbol.charCodeAt(0) & 32)
3030 symbol = symbol.toUpperCase();
3031
3032 if (!/^OP_/.test(symbol))
3033 symbol = `OP_${symbol}`;
3034
3035 const value = opcodes[symbol];
3036
3037 if (value == null) {
3038 if (item[0] === '\'') {
3039 assert(item[item.length - 1] === '\'', 'Invalid string.');
3040 const str = item.slice(1, -1);
3041 const op = Opcode.fromString(str);
3042 bw.writeBytes(op.toRaw());
3043 continue;
3044 }
3045
3046 if (/^-?\d+$/.test(item)) {
3047 const num = ScriptNum.fromString(item, 10);
3048 const op = Opcode.fromNum(num);
3049 bw.writeBytes(op.toRaw());
3050 continue;
3051 }
3052
3053 assert(item.indexOf('0x') === 0, 'Unknown opcode.');
3054
3055 const hex = item.substring(2);
3056 const data = Buffer.from(hex, 'hex');
3057
3058 assert(data.length === hex.length / 2, 'Invalid hex string.');
3059
3060 bw.writeBytes(data);
3061
3062 continue;
3063 }
3064
3065 bw.writeU8(value);
3066 }
3067
3068 return this.fromRaw(bw.render());
3069 }
3070
3071 /**
3072 * Parse a bitcoind test script

Callers 5

fromAddressMethod · 0.45
setStringMethod · 0.45
pushStringMethod · 0.45
unshiftStringMethod · 0.45
insertStringMethod · 0.45

Calls 8

fromRawMethod · 0.95
fromMethod · 0.80
renderMethod · 0.80
writeMethod · 0.45
testMethod · 0.45
toRawMethod · 0.45
fromNumMethod · 0.45
indexOfMethod · 0.45

Tested by

no test coverage detected