-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmsg_server_receive_message.go
173 lines (147 loc) · 6.05 KB
/
msg_server_receive_message.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
// Copyright 2024 Circle Internet Group, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
package keeper
import (
"bytes"
"context"
"strings"
"cosmossdk.io/errors"
"cosmossdk.io/math"
"github.com/circlefin/noble-cctp/x/cctp/types"
fiattokenfactorytypes "github.com/circlefin/noble-fiattokenfactory/x/fiattokenfactory/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/bech32"
)
var zeroByteArray = []byte{ // 32 bytes
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
}
func (k msgServer) ReceiveMessage(goCtx context.Context, msg *types.MsgReceiveMessage) (*types.MsgReceiveMessageResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
sendingReceivingPaused, found := k.GetSendingAndReceivingMessagesPaused(ctx)
if found && sendingReceivingPaused.Paused {
return nil, errors.Wrap(types.ErrReceiveMessage, "sending and receiving messages are paused")
}
// Validate each signature in the attestation
publicKeys := k.GetAllAttesters(ctx)
if len(publicKeys) == 0 {
return nil, errors.Wrap(types.ErrReceiveMessage, "no attesters found")
}
signatureThreshold, found := k.GetSignatureThreshold(ctx)
if !found {
return nil, errors.Wrap(types.ErrReceiveMessage, "signature threshold not found")
}
if err := VerifyAttestationSignatures(msg.Message, msg.Attestation, publicKeys, signatureThreshold.Amount); err != nil {
return nil, errors.Wrapf(types.ErrReceiveMessage, "unable to verify signatures: %s", err)
}
// parse message
message, err := new(types.Message).Parse(msg.Message)
if err != nil {
return nil, err
}
// validate domain
if message.DestinationDomain != types.NobleDomainId {
return nil, errors.Wrapf(types.ErrReceiveMessage, "incorrect destination domain: %d", message.DestinationDomain)
}
// validate destination caller
if !bytes.Equal(message.DestinationCaller, zeroByteArray) {
bech32Prefix := sdk.GetConfig().GetBech32AccountAddrPrefix()
destinationCaller, err := bech32.ConvertAndEncode(bech32Prefix, message.DestinationCaller[12:])
if err != nil {
return nil, errors.Wrapf(types.ErrReceiveMessage, "unable to encode destination caller %s: %s", msg.From, err)
}
if destinationCaller != msg.From {
return nil, errors.Wrapf(types.ErrReceiveMessage, "incorrect destination caller: %s, sender: %s", destinationCaller, msg.From)
}
}
// validate version
if message.Version != types.NobleMessageVersion {
return nil, errors.Wrapf(types.ErrReceiveMessage, "incorrect message version. expected: %d, found: %d", types.NobleMessageVersion, message.Version)
}
// validate nonce is available
// note: we use the domain/nonce combo instead of a hash
usedNonce := types.Nonce{SourceDomain: message.SourceDomain, Nonce: message.Nonce}
found = k.GetUsedNonce(ctx, usedNonce)
if found {
return nil, errors.Wrapf(types.ErrReceiveMessage, "nonce already used")
}
// mark nonce as used
k.SetUsedNonce(ctx, usedNonce)
// verify and parse BurnMessage
if bytes.Equal(message.Recipient, types.PaddedModuleAddress) { // then mint
burningMintingPaused, found := k.GetBurningAndMintingPaused(ctx)
if found && burningMintingPaused.Paused {
return nil, errors.Wrap(types.ErrReceiveMessage, "cctp burning and minting is paused")
}
burnMessage, err := new(types.BurnMessage).Parse(message.MessageBody)
if err != nil {
return nil, err
}
if burnMessage.Version != types.MessageBodyVersion {
return nil, errors.Wrap(types.ErrReceiveMessage, "invalid message body version")
}
// look up Noble mint token from corresponding source domain/token
tokenPair, found := k.GetTokenPair(ctx, message.SourceDomain, burnMessage.BurnToken)
if !found {
return nil, errors.Wrap(types.ErrReceiveMessage, "corresponding noble mint token not found")
}
remoteTokenMessenger, found := k.GetRemoteTokenMessenger(ctx, message.SourceDomain)
if !found {
return nil, errors.Wrapf(types.ErrReceiveMessage, "could not retrieve remote token messenger for domain %d", message.SourceDomain)
}
if !bytes.Equal(message.Sender, remoteTokenMessenger.Address) {
return nil, errors.Wrap(types.ErrReceiveMessage, "message sender is not the remote token messenger")
}
// get mint recipient as noble address
bech32Prefix := sdk.GetConfig().GetBech32AccountAddrPrefix()
mintRecipient, err := sdk.Bech32ifyAddressBytes(bech32Prefix, burnMessage.MintRecipient[12:])
if err != nil {
return nil, errors.Wrapf(types.ErrReceiveMessage, "error bech32 encoding mint recipient address: %s", err)
}
msgMint := fiattokenfactorytypes.MsgMint{
From: types.ModuleAddress.String(),
Address: mintRecipient,
Amount: sdk.Coin{
Denom: strings.ToLower(tokenPair.LocalToken),
Amount: math.NewIntFromBigInt(burnMessage.Amount.BigInt()),
},
}
_, err = k.fiattokenfactory.Mint(ctx, &msgMint)
if err != nil {
return nil, errors.Wrapf(err, "error during minting: %s", err)
}
mintEvent := types.MintAndWithdraw{
MintRecipient: burnMessage.MintRecipient,
Amount: burnMessage.Amount,
MintToken: strings.ToLower(tokenPair.LocalToken),
}
err = ctx.EventManager().EmitTypedEvent(&mintEvent)
if err != nil {
return nil, errors.Wrapf(err, "error emitting mint event: %s", err)
}
}
event := types.MessageReceived{
Caller: msg.From,
SourceDomain: message.SourceDomain,
Nonce: message.Nonce,
Sender: message.Sender,
MessageBody: message.MessageBody,
}
err = ctx.EventManager().EmitTypedEvent(&event)
return &types.MsgReceiveMessageResponse{Success: true}, err
}