1
1
import { recoverPublicKey , Signature , verify } from '@noble/secp256k1' ;
2
2
import { bytesToHex , hexToBigInt } from 'micro-stacks/common' ;
3
- import { hashMessage } from './encoding' ;
3
+ import { hashMessage , LEGACY_CHAIN_PREFIX_BYTES } from './encoding' ;
4
4
5
5
import { getStructuredDataHashes , makeStructuredDataHash } from './structured-message' ;
6
6
import { StructuredSignatureRequestOptions } from './types' ;
@@ -55,13 +55,14 @@ export const recoverSignature = (options: { signature: string; mode?: 'vrs' | 'r
55
55
} ;
56
56
} ;
57
57
58
- export const verifyMessageSignature = ( options : {
58
+ export const _verifyMessageSignature = ( options : {
59
59
// string = message, bytes = hash
60
60
message : string | Uint8Array ;
61
61
signature : string ;
62
62
publicKey ?: string ;
63
63
stxAddress ?: string ;
64
64
mode ?: 'vrs' | 'rsv' ;
65
+ prefix ?: Uint8Array ;
65
66
} ) => {
66
67
if ( ! options . publicKey && ! options . stxAddress )
67
68
throw Error (
@@ -72,7 +73,7 @@ export const verifyMessageSignature = (options: {
72
73
let publicKey = options . publicKey ;
73
74
74
75
try {
75
- const hash = typeof message === 'string' ? hashMessage ( message ) : message ;
76
+ const hash = typeof message === 'string' ? hashMessage ( message , options . prefix ) : message ;
76
77
const { signature, recoveryBytes } = recoverSignature ( {
77
78
signature : options . signature ,
78
79
mode,
@@ -109,13 +110,14 @@ export const verifyMessageSignature = (options: {
109
110
}
110
111
} ;
111
112
112
- export const verifyStructuredMessageSignature = ( options : {
113
+ const _verifyStructuredMessageSignature = ( options : {
113
114
message : StructuredSignatureRequestOptions [ 'message' ] ;
114
115
domain : StructuredSignatureRequestOptions [ 'domain' ] ;
115
116
signature : string ;
116
117
publicKey ?: string ;
117
118
stxAddress ?: string ;
118
119
mode ?: 'vrs' | 'rsv' ;
120
+ prefix ?: Uint8Array ;
119
121
} ) => {
120
122
if ( ! options . publicKey && ! options . stxAddress )
121
123
throw Error (
@@ -129,11 +131,47 @@ export const verifyStructuredMessageSignature = (options: {
129
131
130
132
const hashBytes = makeStructuredDataHash ( domain , message ) ;
131
133
132
- return verifyMessageSignature ( {
134
+ return _verifyMessageSignature ( {
133
135
message : hashBytes ,
134
136
signature : options . signature ,
135
137
publicKey : options . publicKey ,
136
138
stxAddress : options . stxAddress ,
137
139
mode : options . mode ,
140
+ prefix : options . prefix ,
138
141
} ) ;
139
142
} ;
143
+
144
+ export const verifyMessageSignature = ( options : {
145
+ // string = message, bytes = hash
146
+ message : string | Uint8Array ;
147
+ signature : string ;
148
+ publicKey ?: string ;
149
+ stxAddress ?: string ;
150
+ mode ?: 'vrs' | 'rsv' ;
151
+ } ) => {
152
+ const isValid = _verifyMessageSignature ( options ) ;
153
+ return isValid
154
+ ? true
155
+ : _verifyMessageSignature ( {
156
+ ...options ,
157
+ prefix : LEGACY_CHAIN_PREFIX_BYTES ,
158
+ } ) ;
159
+ } ;
160
+
161
+ export const verifyStructuredMessageSignature = ( options : {
162
+ message : StructuredSignatureRequestOptions [ 'message' ] ;
163
+ domain : StructuredSignatureRequestOptions [ 'domain' ] ;
164
+ signature : string ;
165
+ publicKey ?: string ;
166
+ stxAddress ?: string ;
167
+ mode ?: 'vrs' | 'rsv' ;
168
+ } ) => {
169
+ const isValid = _verifyStructuredMessageSignature ( options ) ;
170
+ return (
171
+ isValid ??
172
+ _verifyStructuredMessageSignature ( {
173
+ ...options ,
174
+ prefix : LEGACY_CHAIN_PREFIX_BYTES ,
175
+ } )
176
+ ) ;
177
+ } ;
0 commit comments