@@ -1336,6 +1336,12 @@ export function ecdsa(
13361336 throw new Error ( `invalid signature ${ title } : out of range 1..Point.Fn.ORDER` ) ;
13371337 return num ;
13381338 }
1339+ function validateSigLength ( bytes : Uint8Array , format : ECDSASigFormat ) {
1340+ validateSigFormat ( format ) ;
1341+ const size = lengths . signature ! ;
1342+ const sizer = format === 'compact' ? size : format === 'recovered' ? size + 1 : undefined ;
1343+ return abytes ( bytes , sizer , `${ format } signature` ) ;
1344+ }
13391345
13401346 /**
13411347 * ECDSA signature with its (r, s) properties. Supports compact, recovered & DER representations.
@@ -1352,22 +1358,18 @@ export function ecdsa(
13521358 }
13531359
13541360 static fromBytes ( bytes : Uint8Array , format : ECDSASigFormat = defaultSigOpts_format ) : Signature {
1355- validateSigFormat ( format ) ;
1356- const size = lengths . signature ! ;
1361+ validateSigLength ( bytes , format )
13571362 let recid : number | undefined ;
13581363 if ( format === 'der' ) {
13591364 const { r, s } = DER . toSig ( abytes ( bytes ) ) ;
13601365 return new Signature ( r , s ) ;
13611366 }
13621367 if ( format === 'recovered' ) {
1363- abytes ( bytes , size + 1 ) ;
13641368 recid = bytes [ 0 ] ;
1365-
13661369 format = 'compact' ;
13671370 bytes = bytes . subarray ( 1 ) ;
13681371 }
1369- abytes ( bytes , size ) ;
1370- const L = size / 2 ;
1372+ const L = Fn . BYTES ;
13711373 const r = bytes . subarray ( 0 , L ) ;
13721374 const s = bytes . subarray ( L , L * 2 ) ;
13731375 return new Signature ( Fn . fromBytes ( r ) , Fn . fromBytes ( s ) , recid ) ;
0 commit comments