|
| 1 | +'use strict'; |
| 2 | +Object.defineProperty(exports, '__esModule', { value: true }); |
| 3 | +exports.p2tr_ns = void 0; |
| 4 | +const ecc_lib_1 = require('../ecc_lib'); |
| 5 | +const networks_1 = require('../networks'); |
| 6 | +const bscript = require('../script'); |
| 7 | +const lazy = require('./lazy'); |
| 8 | +const OPS = bscript.OPS; |
| 9 | +const typef = require('typeforce'); |
| 10 | +function stacksEqual(a, b) { |
| 11 | + if (a.length !== b.length) return false; |
| 12 | + return a.every((x, i) => { |
| 13 | + return x.equals(b[i]); |
| 14 | + }); |
| 15 | +} |
| 16 | +// input: [signatures ...] |
| 17 | +// output: [pubKeys[0:n-1] OP_CHECKSIGVERIFY] pubKeys[n-1] OP_CHECKSIG |
| 18 | +function p2tr_ns(a, opts) { |
| 19 | + if ( |
| 20 | + !a.input && |
| 21 | + !a.output && |
| 22 | + !(a.pubkeys && a.pubkeys.length) && |
| 23 | + !a.signatures |
| 24 | + ) |
| 25 | + throw new TypeError('Not enough data'); |
| 26 | + opts = Object.assign({ validate: true }, opts || {}); |
| 27 | + function isAcceptableSignature(x) { |
| 28 | + if (Buffer.isBuffer(x)) |
| 29 | + return ( |
| 30 | + // empty signatures may be represented as empty buffers |
| 31 | + (opts && opts.allowIncomplete && x.length === 0) || |
| 32 | + bscript.isCanonicalSchnorrSignature(x) |
| 33 | + ); |
| 34 | + return !!(opts && opts.allowIncomplete && x === OPS.OP_0); |
| 35 | + } |
| 36 | + typef( |
| 37 | + { |
| 38 | + network: typef.maybe(typef.Object), |
| 39 | + output: typef.maybe(typef.Buffer), |
| 40 | + pubkeys: typef.maybe( |
| 41 | + typef.arrayOf((0, ecc_lib_1.getEccLib)().isXOnlyPoint), |
| 42 | + ), |
| 43 | + signatures: typef.maybe(typef.arrayOf(isAcceptableSignature)), |
| 44 | + input: typef.maybe(typef.Buffer), |
| 45 | + }, |
| 46 | + a, |
| 47 | + ); |
| 48 | + const network = a.network || networks_1.bitcoin; |
| 49 | + const o = { network }; |
| 50 | + const _chunks = lazy.value(() => { |
| 51 | + if (!a.output) return; |
| 52 | + return bscript.decompile(a.output); |
| 53 | + }); |
| 54 | + lazy.prop(o, 'output', () => { |
| 55 | + if (!a.pubkeys) return; |
| 56 | + return bscript.compile( |
| 57 | + [].concat( |
| 58 | + ...a.pubkeys.map((pk, i, pks) => [ |
| 59 | + pk, |
| 60 | + i === pks.length - 1 ? OPS.OP_CHECKSIG : OPS.OP_CHECKSIGVERIFY, |
| 61 | + ]), |
| 62 | + ), |
| 63 | + ); |
| 64 | + }); |
| 65 | + lazy.prop(o, 'n', () => { |
| 66 | + if (!o.pubkeys) return; |
| 67 | + return o.pubkeys.length; |
| 68 | + }); |
| 69 | + lazy.prop(o, 'pubkeys', () => { |
| 70 | + const chunks = _chunks(); |
| 71 | + if (!chunks) return; |
| 72 | + return chunks.filter((_, index) => index % 2 === 0); |
| 73 | + }); |
| 74 | + lazy.prop(o, 'signatures', () => { |
| 75 | + if (!a.input) return; |
| 76 | + return bscript.decompile(a.input).reverse(); |
| 77 | + }); |
| 78 | + lazy.prop(o, 'input', () => { |
| 79 | + if (!a.signatures) return; |
| 80 | + return bscript.compile([...a.signatures].reverse()); |
| 81 | + }); |
| 82 | + lazy.prop(o, 'witness', () => { |
| 83 | + if (!o.input) return; |
| 84 | + return []; |
| 85 | + }); |
| 86 | + lazy.prop(o, 'name', () => { |
| 87 | + if (!o.n) return; |
| 88 | + return `p2tr_ns(${o.n})`; |
| 89 | + }); |
| 90 | + // extended validation |
| 91 | + if (opts.validate) { |
| 92 | + const chunks = _chunks(); |
| 93 | + if (chunks) { |
| 94 | + if (chunks[chunks.length - 1] !== OPS.OP_CHECKSIG) |
| 95 | + throw new TypeError('Output ends with unexpected opcode'); |
| 96 | + if ( |
| 97 | + chunks |
| 98 | + .filter((_, index) => index % 2 === 1) |
| 99 | + .slice(0, -1) |
| 100 | + .some(op => op !== OPS.OP_CHECKSIGVERIFY) |
| 101 | + ) |
| 102 | + throw new TypeError('Output contains unexpected opcode'); |
| 103 | + if (o.n > 16 || o.n !== chunks.length / 2) |
| 104 | + throw new TypeError('Output contains too many pubkeys'); |
| 105 | + if (o.pubkeys.some(x => !(0, ecc_lib_1.getEccLib)().isXOnlyPoint(x))) |
| 106 | + throw new TypeError('Output contains invalid pubkey(s)'); |
| 107 | + if (a.pubkeys && !stacksEqual(a.pubkeys, o.pubkeys)) |
| 108 | + throw new TypeError('Pubkeys mismatch'); |
| 109 | + } |
| 110 | + if (a.pubkeys && a.pubkeys.length) { |
| 111 | + o.n = a.pubkeys.length; |
| 112 | + } |
| 113 | + if (a.signatures) { |
| 114 | + if (a.signatures.length < o.n) |
| 115 | + throw new TypeError('Not enough signatures provided'); |
| 116 | + if (a.signatures.length > o.n) |
| 117 | + throw new TypeError('Too many signatures provided'); |
| 118 | + } |
| 119 | + if (a.input) { |
| 120 | + if (!o.signatures.every(isAcceptableSignature)) |
| 121 | + throw new TypeError('Input has invalid signature(s)'); |
| 122 | + if (a.signatures && !stacksEqual(a.signatures, o.signatures)) |
| 123 | + throw new TypeError('Signature mismatch'); |
| 124 | + if (o.n !== o.signatures.length) |
| 125 | + throw new TypeError( |
| 126 | + `Signature count mismatch (n: ${o.n}, signatures.length: ${ |
| 127 | + o.signatures.length |
| 128 | + }`, |
| 129 | + ); |
| 130 | + } |
| 131 | + } |
| 132 | + return Object.assign(o, a); |
| 133 | +} |
| 134 | +exports.p2tr_ns = p2tr_ns; |
0 commit comments