github.com/ethersphere/bee/v2@v2.2.0/pkg/settlement/swap/chequebook/cheque_test.go (about) 1 // Copyright 2020 The Swarm Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package chequebook_test 6 7 import ( 8 "bytes" 9 10 "encoding/hex" 11 "math/big" 12 "testing" 13 14 "github.com/ethereum/go-ethereum/common" 15 "github.com/ethersphere/bee/v2/pkg/crypto" 16 "github.com/ethersphere/bee/v2/pkg/crypto/eip712" 17 signermock "github.com/ethersphere/bee/v2/pkg/crypto/mock" 18 "github.com/ethersphere/bee/v2/pkg/settlement/swap/chequebook" 19 ) 20 21 func TestSignCheque(t *testing.T) { 22 t.Parallel() 23 24 chequebookAddress := common.HexToAddress("0x8d3766440f0d7b949a5e32995d09619a7f86e632") 25 beneficiaryAddress := common.HexToAddress("0xb8d424e9662fe0837fb1d728f1ac97cebb1085fe") 26 signature := common.Hex2Bytes("abcd") 27 cumulativePayout := big.NewInt(10) 28 chainId := int64(1) 29 cheque := &chequebook.Cheque{ 30 Chequebook: chequebookAddress, 31 Beneficiary: beneficiaryAddress, 32 CumulativePayout: cumulativePayout, 33 } 34 35 signer := signermock.New( 36 signermock.WithSignTypedDataFunc(func(data *eip712.TypedData) ([]byte, error) { 37 38 if data.Message["beneficiary"].(string) != beneficiaryAddress.Hex() { 39 t.Fatal("signing cheque with wrong beneficiary") 40 } 41 42 if data.Message["chequebook"].(string) != chequebookAddress.Hex() { 43 t.Fatal("signing cheque for wrong chequebook") 44 } 45 46 if data.Message["cumulativePayout"].(string) != cumulativePayout.String() { 47 t.Fatal("signing cheque with wrong cumulativePayout") 48 } 49 50 return signature, nil 51 }), 52 ) 53 54 chequeSigner := chequebook.NewChequeSigner(signer, chainId) 55 56 result, err := chequeSigner.Sign(cheque) 57 if err != nil { 58 t.Fatal(err) 59 } 60 61 if !bytes.Equal(result, signature) { 62 t.Fatalf("returned wrong signature. wanted %x, got %x", signature, result) 63 } 64 } 65 66 func TestSignChequeIntegration(t *testing.T) { 67 t.Parallel() 68 69 chequebookAddress := common.HexToAddress("0xfa02D396842E6e1D319E8E3D4D870338F791AA25") 70 beneficiaryAddress := common.HexToAddress("0x98E6C644aFeB94BBfB9FF60EB26fc9D83BBEcA79") 71 cumulativePayout := big.NewInt(500) 72 chainId := int64(1) 73 74 data, err := hex.DecodeString("634fb5a872396d9693e5c9f9d7233cfa93f395c093371017ff44aa9ae6564cdd") 75 if err != nil { 76 t.Fatal(err) 77 } 78 79 privKey, err := crypto.DecodeSecp256k1PrivateKey(data) 80 if err != nil { 81 t.Fatal(err) 82 } 83 84 signer := crypto.NewDefaultSigner(privKey) 85 86 cheque := &chequebook.Cheque{ 87 Chequebook: chequebookAddress, 88 Beneficiary: beneficiaryAddress, 89 CumulativePayout: cumulativePayout, 90 } 91 92 chequeSigner := chequebook.NewChequeSigner(signer, chainId) 93 94 result, err := chequeSigner.Sign(cheque) 95 if err != nil { 96 t.Fatal(err) 97 } 98 99 // computed using ganache 100 expectedSignature, err := hex.DecodeString("171b63fc598ae2c7987f4a756959dadddd84ccd2071e7b5c3aa3437357be47286125edc370c344a163ba7f4183dfd3611996274a13e4b3496610fc00c0e2fc421c") 101 if err != nil { 102 t.Fatal(err) 103 } 104 105 if !bytes.Equal(result, expectedSignature) { 106 t.Fatalf("returned wrong signature. wanted %x, got %x", expectedSignature, result) 107 } 108 }