forked from noble-assets/noble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cctp_receive_message_test.go
233 lines (179 loc) · 6.96 KB
/
cctp_receive_message_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package interchaintest_test
import (
"bytes"
"context"
"crypto/ecdsa"
"crypto/elliptic"
"encoding/hex"
"sort"
"testing"
"time"
cosmossdk_io_math "cosmossdk.io/math"
cctptypes "github.com/circlefin/noble-cctp/x/cctp/types"
sdkclient "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/bech32"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/noble-assets/noble/v7/cmd"
"github.com/strangelove-ventures/interchaintest/v4"
"github.com/strangelove-ventures/interchaintest/v4/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v4/testreporter"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)
// run `make local-image`to rebuild updated binary before running test
func TestCCTP_ReceiveMessage(t *testing.T) {
if testing.Short() {
t.Skip()
}
t.Parallel()
ctx := context.Background()
rep := testreporter.NewNopReporter()
eRep := rep.RelayerExecReporter(t)
client, network := interchaintest.DockerSetup(t)
var gw genesisWrapper
nv := 1
nf := 0
cf := interchaintest.NewBuiltinChainFactory(zaptest.NewLogger(t), []*interchaintest.ChainSpec{
nobleChainSpec(ctx, &gw, "noble-1", nv, nf, true, false, true, false),
})
chains, err := cf.Chains(t.Name())
require.NoError(t, err)
gw.chain = chains[0].(*cosmos.CosmosChain)
noble := gw.chain
ic := interchaintest.NewInterchain().AddChain(noble)
require.NoError(t, ic.Build(ctx, eRep, interchaintest.InterchainBuildOptions{
TestName: t.Name(),
Client: client,
NetworkID: network,
// BlockDatabaseFile: interchaintest.DefaultBlockDatabaseFilepath(),
SkipPathCreation: false,
}))
t.Cleanup(func() {
_ = ic.Close()
})
nobleChainCfg := noble.Config()
cmd.SetPrefixes(nobleChainCfg.Bech32Prefix)
attesters := make([]*ecdsa.PrivateKey, 2)
msgs := make([]sdk.Msg, 2)
// attester - ECDSA public key (Circle will own these keys for mainnet)
for i := range attesters {
p, err := crypto.GenerateKey() // private key
require.NoError(t, err)
attesters[i] = p
pubKey := elliptic.Marshal(p.PublicKey, p.PublicKey.X, p.PublicKey.Y) //public key
attesterPub := hex.EncodeToString(pubKey)
// Adding an attester to protocal
msgs[i] = &cctptypes.MsgEnableAttester{
From: gw.fiatTfRoles.Owner.FormattedAddress(),
Attester: attesterPub,
}
}
broadcaster := cosmos.NewBroadcaster(t, noble)
broadcaster.ConfigureClientContextOptions(func(clientContext sdkclient.Context) sdkclient.Context {
return clientContext.WithBroadcastMode(flags.BroadcastBlock)
})
t.Log("preparing to submit add public keys tx")
burnToken := make([]byte, 32)
copy(burnToken[12:], common.FromHex("0x07865c6E87B9F70255377e024ace6630C1Eaa37F"))
msgs = append(msgs, &cctptypes.MsgLinkTokenPair{
From: gw.fiatTfRoles.Owner.FormattedAddress(),
RemoteDomain: 0,
RemoteToken: burnToken,
LocalToken: denomMetadataUsdc.Base,
})
tokenMessenger := make([]byte, 32)
copy(tokenMessenger[12:], common.FromHex("0xBd3fa81B58Ba92a82136038B25aDec7066af3155"))
msgs = append(msgs, &cctptypes.MsgAddRemoteTokenMessenger{
From: gw.fiatTfRoles.Owner.FormattedAddress(),
DomainId: 0,
Address: tokenMessenger,
})
bCtx, bCancel := context.WithTimeout(ctx, 20*time.Second)
defer bCancel()
tx, err := cosmos.BroadcastTx(
bCtx,
broadcaster,
gw.fiatTfRoles.Owner,
msgs...,
)
require.NoError(t, err, "error submitting add public keys tx")
require.Zero(t, tx.Code, "cctp add pub keys transaction failed: %s - %s - %s", tx.Codespace, tx.RawLog, tx.Data)
t.Logf("Submitted add public keys tx: %s", tx.TxHash)
_, bCancel = context.WithTimeout(ctx, 20*time.Second)
defer bCancel()
nobleValidator := noble.Validators[0]
cctpModuleAccount := authtypes.NewModuleAddress(cctptypes.ModuleName).String()
_, err = nobleValidator.ExecTx(ctx, gw.fiatTfRoles.MasterMinter.KeyName(),
"fiat-tokenfactory", "configure-minter-controller", gw.fiatTfRoles.MinterController.FormattedAddress(), cctpModuleAccount, "-b", "block",
)
require.NoError(t, err, "failed to execute configure minter controller tx")
_, err = nobleValidator.ExecTx(ctx, gw.fiatTfRoles.MinterController.KeyName(),
"fiat-tokenfactory", "configure-minter", cctpModuleAccount, "1000000"+denomMetadataUsdc.Base, "-b", "block",
)
require.NoError(t, err, "failed to execute configure minter tx")
const receiver = "9B6CA0C13EB603EF207C4657E1E619EF531A4D27" //account
receiverBz, err := hex.DecodeString(receiver)
require.NoError(t, err)
nobleReceiver, err := bech32.ConvertAndEncode(nobleChainCfg.Bech32Prefix, receiverBz)
require.NoError(t, err)
burnRecipientPadded := append([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, receiverBz...)
// someone burned USDC on Ethereum -> Mint on Noble
depositForBurn := cctptypes.BurnMessage{
BurnToken: burnToken,
MintRecipient: burnRecipientPadded,
Amount: cosmossdk_io_math.NewInt(1000000),
MessageSender: burnRecipientPadded,
}
depositForBurnBz, err := depositForBurn.Bytes()
require.NoError(t, err)
emptyDestinationCaller := make([]byte, 32)
wrappedDepositForBurn := cctptypes.Message{
Version: 0,
SourceDomain: 0,
DestinationDomain: 4, // Noble is 4
Nonce: 0, // dif per message
Sender: tokenMessenger,
Recipient: cctptypes.PaddedModuleAddress,
DestinationCaller: emptyDestinationCaller,
MessageBody: depositForBurnBz,
}
wrappedDepositForBurnBz, err := wrappedDepositForBurn.Bytes()
require.NoError(t, err)
digestBurn := crypto.Keccak256(wrappedDepositForBurnBz) // hashed message is the key to the attestation
attestationBurn := make([]byte, 0, len(attesters)*65) //65 byte
// CCTP requires attestations to have signatures sorted by address
sort.Slice(attesters, func(i, j int) bool {
return bytes.Compare(
crypto.PubkeyToAddress(attesters[i].PublicKey).Bytes(),
crypto.PubkeyToAddress(attesters[j].PublicKey).Bytes(),
) < 0
})
for i := range attesters {
sig, err := crypto.Sign(digestBurn, attesters[i])
require.NoError(t, err)
attestationBurn = append(attestationBurn, sig...)
}
t.Logf("Attested to messages: %s", tx.TxHash)
bCtx, bCancel = context.WithTimeout(ctx, 20*time.Second)
defer bCancel()
tx, err = cosmos.BroadcastTx(
bCtx,
broadcaster,
gw.fiatTfRoles.Owner,
&cctptypes.MsgReceiveMessage{ //note: all messages that go to noble go through MsgReceiveMessage
From: gw.fiatTfRoles.Owner.FormattedAddress(),
Message: wrappedDepositForBurnBz,
Attestation: attestationBurn,
},
)
require.NoError(t, err, "error submitting cctp burn recv tx")
require.Zerof(t, tx.Code, "cctp burn recv transaction failed: %s - %s - %s", tx.Codespace, tx.RawLog, tx.Data)
t.Logf("CCTP burn message successfully received: %s", tx.TxHash)
balance, err := noble.GetBalance(ctx, nobleReceiver, denomMetadataUsdc.Base)
require.NoError(t, err)
require.Equal(t, int64(1000000), balance)
}