@@ -65,7 +65,7 @@ func TestValidate(t *testing.T) {
65
65
Nonce : 20 ,
66
66
}
67
67
68
- message := msg .GetBytesToSign ()
68
+ message := msg .GetBytesToSign ("arkeo" )
69
69
msg .Signature , _ , err = kb .Sign ("whatever" , message , signing .SignMode_SIGN_MODE_DIRECT )
70
70
require .NoError (t , err )
71
71
require .NoError (t , s .HandlerClaimContractIncome (ctx , & msg ))
@@ -127,7 +127,7 @@ func TestHandlePayAsYouGo(t *testing.T) {
127
127
Nonce : 20 ,
128
128
}
129
129
130
- message := msg .GetBytesToSign ()
130
+ message := msg .GetBytesToSign ("arkeo" )
131
131
msg .Signature , _ , err = kb .Sign ("whatever" , message , signing .SignMode_SIGN_MODE_DIRECT )
132
132
require .NoError (t , err )
133
133
@@ -143,7 +143,7 @@ func TestHandlePayAsYouGo(t *testing.T) {
143
143
Nonce : 21 ,
144
144
}
145
145
146
- message = msg .GetBytesToSign ()
146
+ message = msg .GetBytesToSign ("arkeo" )
147
147
msg .Signature , _ , err = kb .Sign ("whatever" , message , signing .SignMode_SIGN_MODE_DIRECT )
148
148
require .NoError (t , err )
149
149
@@ -157,7 +157,7 @@ func TestHandlePayAsYouGo(t *testing.T) {
157
157
msg .Nonce = 25
158
158
159
159
// update signature for message
160
- message = msg .GetBytesToSign ()
160
+ message = msg .GetBytesToSign ("arkeo" )
161
161
msg .Signature , _ , err = kb .Sign ("whatever" , message , signing .SignMode_SIGN_MODE_DIRECT )
162
162
require .NoError (t , err )
163
163
@@ -174,7 +174,7 @@ func TestHandlePayAsYouGo(t *testing.T) {
174
174
msg .Nonce = contract .Deposit .Int64 () / contract .Rate .Amount .Int64 () * 1000000000000
175
175
176
176
// update signature for message
177
- message = msg .GetBytesToSign ()
177
+ message = msg .GetBytesToSign ("arkeo" )
178
178
msg .Signature , _ , err = kb .Sign ("whatever" , message , signing .SignMode_SIGN_MODE_DIRECT )
179
179
require .NoError (t , err )
180
180
@@ -311,7 +311,7 @@ func TestClaimContractIncomeHandler(t *testing.T) {
311
311
Nonce : 20 ,
312
312
}
313
313
314
- message := msg .GetBytesToSign ()
314
+ message := msg .GetBytesToSign ("arkeo" )
315
315
msg .Signature , _ , err = kb .Sign ("whatever" , message , signing .SignMode_SIGN_MODE_DIRECT )
316
316
require .NoError (t , err )
317
317
@@ -324,7 +324,7 @@ func TestClaimContractIncomeHandler(t *testing.T) {
324
324
// bad nonce
325
325
msg .Nonce = 0
326
326
327
- message = msg .GetBytesToSign ()
327
+ message = msg .GetBytesToSign ("arkeo" )
328
328
msg .Signature , _ , err = kb .Sign ("whatever" , message , signing .SignMode_SIGN_MODE_DIRECT )
329
329
require .NoError (t , err )
330
330
@@ -381,3 +381,65 @@ func TestClaimContractIncomeHandlerSignatureVerification(t *testing.T) {
381
381
err = s .HandlerClaimContractIncome (ctx , & msg )
382
382
require .Error (t , err , types .ErrClaimContractIncomeInvalidSignature )
383
383
}
384
+
385
+ func TestSignatureReplay (t * testing.T ) {
386
+ var err error
387
+ // Setup keeper, context, and staking
388
+ ctx , k , sk := SetupKeeperWithStaking (t )
389
+ ctx = ctx .WithBlockHeight (20 )
390
+
391
+ s := newMsgServer (k , sk )
392
+
393
+ // Set up necessary codec and keyring for signing.
394
+ interfaceRegistry := codectypes .NewInterfaceRegistry ()
395
+ std .RegisterInterfaces (interfaceRegistry )
396
+ module .NewBasicManager ().RegisterInterfaces (interfaceRegistry )
397
+ types .RegisterInterfaces (interfaceRegistry )
398
+ cdc := codec .NewProtoCodec (interfaceRegistry )
399
+
400
+ pubkey := types .GetRandomPubKey ()
401
+ acc := types .GetRandomBech32Addr ()
402
+ service := common .BTCService
403
+ kb := cKeys .NewInMemory (cdc )
404
+ info , _ , err := kb .NewMnemonic ("whatever" , cKeys .English , `m/44'/931'/0'/0/0` , "" , hd .Secp256k1 )
405
+ require .NoError (t , err )
406
+ pk , err := info .GetPubKey ()
407
+ require .NoError (t , err )
408
+ client , err := common .NewPubKeyFromCrypto (pk )
409
+ require .NoError (t , err )
410
+ rate , err := cosmos .ParseCoin ("10uarkeo" )
411
+ require .NoError (t , err )
412
+
413
+ contract := types .NewContract (pubkey , service , client )
414
+ contract .Duration = 100
415
+ contract .Rate = rate
416
+ contract .Height = 10
417
+ contract .Nonce = 0
418
+ contract .Type = types .ContractType_PAY_AS_YOU_GO
419
+ contract .Deposit = cosmos .NewInt (contract .Duration * contract .Rate .Amount .Int64 ())
420
+ contract .Id = 1
421
+ require .NoError (t , k .SetContract (ctx , contract ))
422
+
423
+ require .NoError (t , k .MintToModule (ctx , types .ReserveName , getCoin (common .Tokens (10000 * 100 * 2 ))))
424
+ require .NoError (t , k .SendFromModuleToModule (ctx , types .ReserveName , types .ContractName , getCoins (1000 * 100 )))
425
+
426
+ // Create a claim message
427
+ msg := types.MsgClaimContractIncome {
428
+ ContractId : contract .Id ,
429
+ Creator : acc .String (),
430
+ Nonce : 20 ,
431
+ }
432
+
433
+ // Sign the message using chain ID "arkeo"
434
+ messageBytes := msg .GetBytesToSign ("arkeo" )
435
+ msg .Signature , _ , err = kb .Sign ("whatever" , messageBytes , signing .SignMode_SIGN_MODE_DIRECT )
436
+ require .NoError (t , err )
437
+
438
+ require .NoError (t , s .HandlerClaimContractIncome (ctx , & msg ))
439
+
440
+ ctxReplay := ctx .WithChainID ("otherchain" )
441
+ msg .Nonce = 21
442
+ err = s .HandlerClaimContractIncome (ctxReplay , & msg )
443
+ require .Error (t , err )
444
+ require .Contains (t , err .Error (), "signature mismatch" , "expected signature mismatch error when using a replayed signature on a different chain" )
445
+ }
0 commit comments