* Inject properties from bitcoind test string. * @private * @param {String} items - Script string. * @throws Parse error.
(code)
| 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 |
no test coverage detected