@@ -47,11 +47,28 @@ Object.defineProperty(exports, '__esModule', { value: true });
4747exports . p2ms = p2ms ;
4848const networks_js_1 = require ( '../networks.cjs' ) ;
4949const bscript = __importStar ( require ( '../script.cjs' ) ) ;
50+ const scriptNumber = __importStar ( require ( '../script_number.cjs' ) ) ;
5051const types_js_1 = require ( '../types.cjs' ) ;
5152const lazy = __importStar ( require ( './lazy.cjs' ) ) ;
5253const v = __importStar ( require ( 'valibot' ) ) ;
5354const OPS = bscript . OPS ;
5455const OP_INT_BASE = OPS . OP_RESERVED ; // OP_1 - 1
56+ function encodeSmallOrScriptNum ( n ) {
57+ return n <= 16 ? OP_INT_BASE + n : scriptNumber . encode ( n ) ;
58+ }
59+ function decodeSmallOrScriptNum ( chunk ) {
60+ if ( typeof chunk === 'number' ) {
61+ const val = chunk - OP_INT_BASE ;
62+ if ( val < 1 || val > 16 )
63+ throw new TypeError ( `Invalid opcode: expected OP_1–OP_16, got ${ chunk } ` ) ;
64+ return val ;
65+ } else return scriptNumber . decode ( chunk ) ;
66+ }
67+ function isSmallOrScriptNum ( chunk ) {
68+ if ( typeof chunk === 'number' )
69+ return chunk - OP_INT_BASE >= 1 && chunk - OP_INT_BASE <= 16 ;
70+ else return Number . isInteger ( scriptNumber . decode ( chunk ) ) ;
71+ }
5572// input: OP_0 [signatures ...]
5673// output: m [pubKeys ...] n OP_CHECKMULTISIG
5774/**
@@ -104,8 +121,9 @@ function p2ms(a, opts) {
104121 if ( decoded ) return ;
105122 decoded = true ;
106123 chunks = bscript . decompile ( output ) ;
107- o . m = chunks [ 0 ] - OP_INT_BASE ;
108- o . n = chunks [ chunks . length - 2 ] - OP_INT_BASE ;
124+ if ( chunks . length < 3 ) throw new TypeError ( 'Output is invalid' ) ;
125+ o . m = decodeSmallOrScriptNum ( chunks [ 0 ] ) ;
126+ o . n = decodeSmallOrScriptNum ( chunks [ chunks . length - 2 ] ) ;
109127 o . pubkeys = chunks . slice ( 1 , - 2 ) ;
110128 }
111129 lazy . prop ( o , 'output' , ( ) => {
@@ -114,9 +132,9 @@ function p2ms(a, opts) {
114132 if ( ! a . pubkeys ) return ;
115133 return bscript . compile (
116134 [ ] . concat (
117- OP_INT_BASE + a . m ,
135+ encodeSmallOrScriptNum ( a . m ) ,
118136 a . pubkeys ,
119- OP_INT_BASE + o . n ,
137+ encodeSmallOrScriptNum ( o . n ) ,
120138 OPS . OP_CHECKMULTISIG ,
121139 ) ,
122140 ) ;
@@ -155,13 +173,13 @@ function p2ms(a, opts) {
155173 if ( opts . validate ) {
156174 if ( a . output ) {
157175 decode ( a . output ) ;
158- v . parse ( v . number ( ) , chunks [ 0 ] , { message : 'Output is invalid' } ) ;
159- v . parse ( v . number ( ) , chunks [ chunks . length - 2 ] , {
160- message : 'Output is invalid' ,
161- } ) ;
176+ if ( ! isSmallOrScriptNum ( chunks [ 0 ] ) )
177+ throw new TypeError ( 'Output is invalid' ) ;
178+ if ( ! isSmallOrScriptNum ( chunks [ chunks . length - 2 ] ) )
179+ throw new TypeError ( 'Output is invalid' ) ;
162180 if ( chunks [ chunks . length - 1 ] !== OPS . OP_CHECKMULTISIG )
163181 throw new TypeError ( 'Output is invalid' ) ;
164- if ( o . m <= 0 || o . n > 16 || o . m > o . n || o . n !== chunks . length - 3 )
182+ if ( o . m <= 0 || o . n > 20 || o . m > o . n || o . n !== chunks . length - 3 )
165183 throw new TypeError ( 'Output is invalid' ) ;
166184 if ( ! o . pubkeys . every ( x => ( 0 , types_js_1 . isPoint ) ( x ) ) )
167185 throw new TypeError ( 'Output is invalid' ) ;
0 commit comments