@@ -55,9 +55,10 @@ interface SignTransactionResponse extends BaseLedgerResponse {
55
55
}
56
56
57
57
interface SignMessageResponse extends BaseLedgerResponse {
58
- field : string | null ;
59
- scalar : string | null ;
60
- raw_signature ?: string | null ;
58
+ field : string | null ;
59
+ scalar : string | null ;
60
+ raw_signature ?: string | null ;
61
+ signed_message ?: string | null ;
61
62
}
62
63
63
64
@@ -245,33 +246,49 @@ export class MinaApp extends BaseApp {
245
246
}
246
247
}
247
248
248
- async signMessage ( account : number , networkId : number , message : string ) : Promise < SignMessageResponse > {
249
- if ( message . length === 0 ) {
250
- return {
251
- field : null ,
252
- scalar : null ,
253
- raw_signature : null ,
254
- returnCode : "-6" ,
255
- message : "Message is empty" ,
256
- } ;
257
- }
258
- if ( message . length > 255 ) {
259
- return {
260
- field : null ,
261
- scalar : null ,
262
- raw_signature : null ,
263
- returnCode : "-7" ,
264
- message : "Message too long" ,
265
- } ;
266
- }
267
- try {
268
- const accountHex = Buffer . from ( account . toString ( 16 ) . padStart ( 8 , '0' ) , 'hex' ) ;
269
- const networkIdHex = Buffer . from ( networkId . toString ( 16 ) . padStart ( 2 , '0' ) , 'hex' ) ;
270
- const messageHex = Buffer . from ( message , 'utf8' ) ;
271
- // Calculate total buffer length
272
- const totalLength = accountHex . length + networkIdHex . length + messageHex . length ;
273
- // Create buffer with total length
274
- const dataTx = Buffer . concat ( [ accountHex , networkIdHex , messageHex ] , totalLength ) ;
249
+ async signMessage (
250
+ account : number ,
251
+ networkId : number ,
252
+ message : string ,
253
+ ) : Promise < SignMessageResponse > {
254
+ if ( message . length === 0 ) {
255
+ return {
256
+ field : null ,
257
+ scalar : null ,
258
+ raw_signature : null ,
259
+ signed_message : null ,
260
+ returnCode : "-6" ,
261
+ message : "Message is empty" ,
262
+ } ;
263
+ }
264
+ if ( message . length > 255 ) {
265
+ return {
266
+ field : null ,
267
+ scalar : null ,
268
+ raw_signature : null ,
269
+ signed_message : null ,
270
+ returnCode : "-7" ,
271
+ message : "Message too long" ,
272
+ } ;
273
+ }
274
+ try {
275
+ const accountHex = Buffer . from (
276
+ account . toString ( 16 ) . padStart ( 8 , "0" ) ,
277
+ "hex" ,
278
+ ) ;
279
+ const networkIdHex = Buffer . from (
280
+ networkId . toString ( 16 ) . padStart ( 2 , "0" ) ,
281
+ "hex" ,
282
+ ) ;
283
+ const messageHex = Buffer . from ( message , "utf8" ) ;
284
+ // Calculate total buffer length
285
+ const totalLength =
286
+ accountHex . length + networkIdHex . length + messageHex . length ;
287
+ // Create buffer with total length
288
+ const dataTx = Buffer . concat (
289
+ [ accountHex , networkIdHex , messageHex ] ,
290
+ totalLength ,
291
+ ) ;
275
292
276
293
const responseBuffer = await this . transport . send (
277
294
this . CLA ,
@@ -281,28 +298,47 @@ export class MinaApp extends BaseApp {
281
298
dataTx
282
299
) ;
283
300
284
- const response = processResponse ( responseBuffer )
285
- const signature = response . readBytes ( response . length ( ) ) . toString ( "hex" ) ;
286
- const sigLength = signature . length ;
287
- const field_extracted = signature . substring ( 0 , sigLength / 2 ) ;
288
- const scalar_extracted = signature . substring ( sigLength / 2 , sigLength ) ;
289
-
290
- return {
291
- field : BigInt ( "0x" + field_extracted ) . toString ( ) ,
292
- scalar : BigInt ( "0x" + scalar_extracted ) . toString ( ) ,
293
- raw_signature : signature ,
294
- returnCode : "9000"
295
- } ;
296
- } catch ( e ) {
297
- const respError = processErrorResponse ( e )
298
- return {
299
- field : null ,
300
- scalar : null ,
301
- raw_signature : null ,
302
- returnCode : respError . returnCode . toString ( ) ,
303
- message : respError . errorMessage ,
304
- } ;
305
- }
301
+ const response = processResponse ( responseBuffer ) ;
302
+
303
+ // Validate minimum buffer length (64 bytes for signature + 1 byte for message length)
304
+ if ( response . length ( ) < 65 ) {
305
+ throw new Error ( "Response buffer too short" ) ;
306
+ }
307
+
308
+ // First read the signature (64 bytes)
309
+ const signature = response . readBytes ( 64 ) . toString ( "hex" ) ;
310
+ const sigLength = signature . length ;
311
+ const field_extracted = signature . substring ( 0 , sigLength / 2 ) ;
312
+ const scalar_extracted = signature . substring ( sigLength / 2 , sigLength ) ;
313
+
314
+ // Then read the message length (1 byte)
315
+ const messageLength = response . readBytes ( 1 ) . readUInt8 ( ) ;
316
+
317
+ // Validate remaining buffer length against message length
318
+ if ( response . length ( ) < messageLength ) {
319
+ throw new Error ( "Response buffer too short for message" ) ;
320
+ }
321
+
322
+ // Finally read the message
323
+ const returnedMessage = response . readBytes ( messageLength ) . toString ( 'utf8' ) ;
324
+
325
+ return {
326
+ field : BigInt ( "0x" + field_extracted ) . toString ( ) ,
327
+ scalar : BigInt ( "0x" + scalar_extracted ) . toString ( ) ,
328
+ raw_signature : signature ,
329
+ signed_message : returnedMessage ,
330
+ returnCode : "9000" ,
331
+ } ;
332
+ } catch ( e ) {
333
+ const respError = processErrorResponse ( e ) ;
334
+ return {
335
+ field : null ,
336
+ scalar : null ,
337
+ raw_signature : null ,
338
+ signed_message : null ,
339
+ returnCode : respError . returnCode . toString ( ) ,
340
+ message : respError . errorMessage ,
341
+ } ;
306
342
}
307
343
308
344
0 commit comments