@@ -4,15 +4,14 @@ import {
4
4
Clause ,
5
5
HexUInt ,
6
6
networkInfo ,
7
+ Secp256k1 ,
7
8
Transaction ,
8
9
type TransactionBody ,
9
10
VET
10
11
} from '@vechain/sdk-core' ;
11
- import {
12
- type FetchHttpClient ,
13
- type HttpPath ,
14
- type HttpQuery
15
- } from '../../../src' ;
12
+ import { type FetchHttpClient , type HttpPath } from '../../../src' ;
13
+
14
+ import { secp256k1 as nc_secp256k1 } from '@noble/curves/secp256k1' ;
16
15
17
16
const mockHttpClient = < T > ( response : T ) : FetchHttpClient => {
18
17
return {
@@ -110,7 +109,7 @@ describe('unit tests', () => {
110
109
'/transactions' as unknown as HttpPath ,
111
110
{
112
111
query : HexUInt . of ( tx . encoded ) . toString ( )
113
- } as unknown as HttpQuery
112
+ }
114
113
) ;
115
114
expect ( await txResult . json ( ) ) . toEqual ( mockTxResponse ) ;
116
115
@@ -120,4 +119,73 @@ describe('unit tests', () => {
120
119
) ;
121
120
expect ( await txReceipt . json ( ) ) . toEqual ( mockTxReceiptResponse ) ;
122
121
} ) ;
122
+
123
+ test ( 'verify signatures' , ( ) => {
124
+ const latestBlock = {
125
+ id : '0x0000000000000000000000000000000000000000000000000000000000000123'
126
+ } ;
127
+ const gasToPay = { totalGas : 21000 } ;
128
+ const gasPayerPublicKey = Secp256k1 . derivePublicKey (
129
+ gasPayer . privateKey . bytes ,
130
+ false
131
+ ) ;
132
+ const txA = Transaction . of ( {
133
+ chainTag : networkInfo . solo . chainTag ,
134
+ blockRef : latestBlock ?. id . slice ( 0 , 18 ) ?? '0x0' ,
135
+ expiration : 0 ,
136
+ clauses,
137
+ gasPriceCoef : 0 ,
138
+ gas : gasToPay . totalGas ,
139
+ dependsOn : null ,
140
+ nonce : 1 ,
141
+ reserved : {
142
+ features : 1 // set the transaction to be delegated
143
+ }
144
+ } ) ;
145
+ const as = txA . signAsSender ( sender . privateKey . bytes ) ;
146
+ const ap = as . signAsGasPayer ( sender . address , gasPayer . privateKey . bytes ) ;
147
+ const sigmaA = nc_secp256k1 . Signature . fromCompact (
148
+ ap . signature ?. slice ( - 65 ) . slice ( 0 , 64 ) as Uint8Array
149
+ ) ;
150
+ const hashA = ap . getTransactionHash ( sender . address ) . bytes ;
151
+ const isVerifiedA = nc_secp256k1 . verify (
152
+ sigmaA ,
153
+ hashA ,
154
+ gasPayerPublicKey
155
+ ) ;
156
+ expect ( isVerifiedA ) . toBe ( true ) ;
157
+
158
+ const txB = Transaction . of ( {
159
+ chainTag : networkInfo . solo . chainTag ,
160
+ blockRef : latestBlock ?. id . slice ( 0 , 18 ) ?? '0x0' ,
161
+ expiration : 0 ,
162
+ clauses,
163
+ gasPriceCoef : 0 ,
164
+ gas : gasToPay . totalGas ,
165
+ dependsOn : null ,
166
+ nonce : 2 ,
167
+ reserved : {
168
+ features : 1 // set the transaction to be delegated
169
+ }
170
+ } ) ;
171
+ const bs = txB . signAsSender ( sender . privateKey . bytes ) ;
172
+ const bp = bs . signAsGasPayer ( sender . address , gasPayer . privateKey . bytes ) ;
173
+ const sigmaB = nc_secp256k1 . Signature . fromCompact (
174
+ bp . signature ?. slice ( - 65 ) . slice ( 0 , 64 ) as Uint8Array
175
+ ) ;
176
+ const hashB = bp . getTransactionHash ( sender . address ) . bytes ;
177
+ const isVerifiedB = nc_secp256k1 . verify (
178
+ sigmaB ,
179
+ hashB ,
180
+ gasPayerPublicKey
181
+ ) ;
182
+ expect ( isVerifiedB ) . toBe ( true ) ;
183
+
184
+ const isVerifiedForge = nc_secp256k1 . verify (
185
+ sigmaA ,
186
+ hashB ,
187
+ gasPayerPublicKey
188
+ ) ;
189
+ expect ( isVerifiedForge ) . toBe ( false ) ;
190
+ } ) ;
123
191
} ) ;
0 commit comments