/
msg-bridge.go
227 lines (187 loc) · 6.07 KB
/
msg-bridge.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
package msg
import (
"encoding/json"
"fmt"
"math/big"
sdk "github.com/Sotatek-huytran2/go-sdk/common/types"
)
const (
RouteBridge = "bridge"
BindMsgType = "crossBind"
UnbindMsgType = "crossUnbind"
TransferOutMsgType = "crossTransferOut"
)
const (
MaxSymbolLength = 32
)
// SmartChainAddress defines a standard smart chain address
type SmartChainAddress [20]byte
// NewSmartChainAddress is a constructor function for SmartChainAddress
func NewSmartChainAddress(addr string) SmartChainAddress {
// we don't want to return error here, ethereum also do the same thing here
hexBytes, _ := HexDecode(addr)
var address SmartChainAddress
address.SetBytes(hexBytes)
return address
}
func (addr *SmartChainAddress) SetBytes(b []byte) {
if len(b) > len(addr) {
b = b[len(b)-20:]
}
copy(addr[20-len(b):], b)
}
func (addr SmartChainAddress) IsEmpty() bool {
addrValue := big.NewInt(0)
addrValue.SetBytes(addr[:])
return addrValue.Cmp(big.NewInt(0)) == 0
}
// Route should return the name of the module
func (addr SmartChainAddress) String() string {
return HexAddress(addr[:])
}
// MarshalJSON marshals the ethereum address to JSON
func (addr SmartChainAddress) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("\"%v\"", addr.String())), nil
}
// UnmarshalJSON unmarshals an smart chain address
func (addr *SmartChainAddress) UnmarshalJSON(input []byte) error {
hexBytes, err := HexDecode(string(input[1 : len(input)-1]))
if err != nil {
return err
}
addr.SetBytes(hexBytes)
return nil
}
type BindStatus int8
type BindMsg struct {
From sdk.AccAddress `json:"from"`
Symbol string `json:"symbol"`
Amount int64 `json:"amount"`
ContractAddress SmartChainAddress `json:"contract_address"`
ContractDecimals int8 `json:"contract_decimals"`
ExpireTime int64 `json:"expire_time"`
}
func NewBindMsg(from sdk.AccAddress, symbol string, amount int64, contractAddress SmartChainAddress, contractDecimals int8, expireTime int64) BindMsg {
return BindMsg{
From: from,
Amount: amount,
Symbol: symbol,
ContractAddress: contractAddress,
ContractDecimals: contractDecimals,
ExpireTime: expireTime,
}
}
func (msg BindMsg) Route() string { return RouteBridge }
func (msg BindMsg) Type() string { return BindMsgType }
func (msg BindMsg) String() string {
return fmt.Sprintf("Bind{%v#%s#%d$%s#%d#%d}", msg.From, msg.Symbol, msg.Amount, msg.ContractAddress.String(), msg.ContractDecimals, msg.ExpireTime)
}
func (msg BindMsg) GetInvolvedAddresses() []sdk.AccAddress { return msg.GetSigners() }
func (msg BindMsg) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{msg.From} }
func (msg BindMsg) ValidateBasic() error {
if len(msg.From) != sdk.AddrLen {
return fmt.Errorf("address length should be %d", sdk.AddrLen)
}
if len(msg.Symbol) == 0 {
return fmt.Errorf("symbol should not be empty")
}
if msg.Amount <= 0 {
return fmt.Errorf("amount should be larger than 0")
}
if msg.ContractAddress.IsEmpty() {
return fmt.Errorf("contract address should not be empty")
}
if msg.ContractDecimals < 0 {
return fmt.Errorf("decimal should be no less than 0")
}
if msg.ExpireTime <= 0 {
return fmt.Errorf("expire time should be larger than 0")
}
return nil
}
func (msg BindMsg) GetSignBytes() []byte {
b, err := json.Marshal(msg) // XXX: ensure some canonical form
if err != nil {
panic(err)
}
return b
}
type TransferOutMsg struct {
From sdk.AccAddress `json:"from"`
To SmartChainAddress `json:"to"`
Amount sdk.Coin `json:"amount"`
ExpireTime int64 `json:"expire_time"`
}
func NewTransferOutMsg(from sdk.AccAddress, to SmartChainAddress, amount sdk.Coin, expireTime int64) TransferOutMsg {
return TransferOutMsg{
From: from,
To: to,
Amount: amount,
ExpireTime: expireTime,
}
}
func (msg TransferOutMsg) Route() string { return RouteBridge }
func (msg TransferOutMsg) Type() string { return TransferOutMsgType }
func (msg TransferOutMsg) String() string {
return fmt.Sprintf("TransferOut{%v#%s#%s#%d}", msg.From, msg.To.String(), msg.Amount.String(), msg.ExpireTime)
}
func (msg TransferOutMsg) GetInvolvedAddresses() []sdk.AccAddress { return msg.GetSigners() }
func (msg TransferOutMsg) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{msg.From} }
func (msg TransferOutMsg) ValidateBasic() error {
if len(msg.From) != sdk.AddrLen {
return fmt.Errorf("address length should be %d", sdk.AddrLen)
}
if msg.To.IsEmpty() {
return fmt.Errorf("to address should not be empty")
}
if !msg.Amount.IsPositive() {
return fmt.Errorf("amount should be positive")
}
if msg.ExpireTime <= 0 {
return fmt.Errorf("expire time should be larger than 0")
}
return nil
}
func (msg TransferOutMsg) GetSignBytes() []byte {
b, err := json.Marshal(msg) // XXX: ensure some canonical form
if err != nil {
panic(err)
}
return b
}
type UnbindMsg struct {
From sdk.AccAddress `json:"from"`
Symbol string `json:"symbol"`
}
func NewUnbindMsg(from sdk.AccAddress, symbol string) UnbindMsg {
return UnbindMsg{
From: from,
Symbol: symbol,
}
}
func (msg UnbindMsg) Route() string { return RouteBridge }
func (msg UnbindMsg) Type() string { return UnbindMsgType }
func (msg UnbindMsg) String() string {
return fmt.Sprintf("Unbind{%v#%s}", msg.From, msg.Symbol)
}
func (msg UnbindMsg) GetInvolvedAddresses() []sdk.AccAddress { return msg.GetSigners() }
func (msg UnbindMsg) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{msg.From} }
func (msg UnbindMsg) ValidateBasic() error {
if len(msg.From) != sdk.AddrLen {
return fmt.Errorf("address length should be %d", sdk.AddrLen)
}
if len(msg.Symbol) == 0 {
return fmt.Errorf("symbol should not be empty")
}
if len(msg.Symbol) > MaxSymbolLength {
return fmt.Errorf("symbol length should not be larger than %d", MaxSymbolLength)
}
return nil
}
func (msg UnbindMsg) GetSignBytes() []byte {
b, err := json.Marshal(msg) // XXX: ensure some canonical form
if err != nil {
panic(err)
}
return b
}