File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed
modules/sdk-coin-flrp/src/lib Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -338,6 +338,38 @@ export class Utils implements BaseUtils {
338338 flareIdString ( value : string ) : Id {
339339 return new Id ( Buffer . from ( value , 'hex' ) ) ;
340340 }
341+
342+ /**
343+ * FlareJS wrapper to recover signature
344+ * @param network
345+ * @param message
346+ * @param signature
347+ * @return recovered public key
348+ */
349+ recoverySignature ( network : FlareNetwork , message : Buffer , signature : Buffer ) : Buffer {
350+ try {
351+ // Hash the message first - must match the hash used in signing
352+ const messageHash = createHash ( 'sha256' ) . update ( message ) . digest ( ) ;
353+
354+ // Extract recovery parameter and signature
355+ if ( signature . length !== 65 ) {
356+ throw new Error ( 'Invalid signature length - expected 65 bytes (64 bytes signature + 1 byte recovery)' ) ;
357+ }
358+
359+ const recoveryParam = signature [ 64 ] ;
360+ const sigOnly = signature . slice ( 0 , 64 ) ;
361+
362+ // Recover public key using the provided recovery parameter
363+ const recovered = ecc . recoverPublicKey ( messageHash , sigOnly , recoveryParam , true ) ;
364+ if ( ! recovered ) {
365+ throw new Error ( 'Failed to recover public key' ) ;
366+ }
367+
368+ return Buffer . from ( recovered ) ;
369+ } catch ( error ) {
370+ throw new Error ( `Failed to recover signature: ${ error } ` ) ;
371+ }
372+ }
341373}
342374
343375const utils = new Utils ( ) ;
You can’t perform that action at this time.
0 commit comments