-
Notifications
You must be signed in to change notification settings - Fork 82
/
encoding.go
72 lines (64 loc) · 3.01 KB
/
encoding.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
package crossdomain
import (
"math/big"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
)
var (
// NonceMask represents a mask used to extract version bytes from the nonce
NonceMask, _ = new(big.Int).SetString("0000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16)
// relayMessage0ABI represents the v0 relay message encoding
relayMessage0ABI = "[{\"inputs\":[{\"internalType\":\"address\",\"name\":\"_target\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"_sender\",\"type\":\"address\"},{\"internalType\":\"bytes\",\"name\":\"_message\",\"type\":\"bytes\"},{\"internalType\":\"uint256\",\"name\":\"_messageNonce\",\"type\":\"uint256\"}],\"name\":\"relayMessage\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]"
// relayMessage1ABI represents the v1 relay message encoding
relayMessage1ABI = "[{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"_nonce\",\"type\":\"uint256\"},{\"internalType\":\"address\",\"name\":\"_sender\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"_target\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"_value\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"_minGasLimit\",\"type\":\"uint256\"},{\"internalType\":\"bytes\",\"name\":\"_message\",\"type\":\"bytes\"}],\"name\":\"relayMessage\",\"outputs\":[],\"stateMutability\":\"payable\",\"type\":\"function\"}]"
// relayMessage0 represents the ABI of relay message v0
relayMessage0 abi.ABI
// relayMessage1 represents the ABI of relay message v1
relayMessage1 abi.ABI
)
// Create the required ABIs
func init() {
var err error
relayMessage0, err = abi.JSON(strings.NewReader(relayMessage0ABI))
if err != nil {
panic(err)
}
relayMessage1, err = abi.JSON(strings.NewReader(relayMessage1ABI))
if err != nil {
panic(err)
}
}
// EncodeCrossDomainMessageV0 will encode the calldata for
// "relayMessage(address,address,bytes,uint256)",
func EncodeCrossDomainMessageV0(
target common.Address,
sender common.Address,
message []byte,
nonce *big.Int,
) ([]byte, error) {
return relayMessage0.Pack("relayMessage", target, sender, message, nonce)
}
// EncodeCrossDomainMessageV1 will encode the calldata for
// "relayMessage(uint256,address,address,uint256,uint256,bytes)",
func EncodeCrossDomainMessageV1(
nonce *big.Int,
sender common.Address,
target common.Address,
value *big.Int,
gasLimit *big.Int,
data []byte,
) ([]byte, error) {
return relayMessage1.Pack("relayMessage", nonce, sender, target, value, gasLimit, data)
}
// DecodeVersionedNonce will decode the version that is encoded in the nonce
func DecodeVersionedNonce(versioned *big.Int) (*big.Int, *big.Int) {
nonce := new(big.Int).And(versioned, NonceMask)
version := new(big.Int).Rsh(versioned, 240)
return nonce, version
}
// EncodeVersionedNonce will encode the version into the nonce
func EncodeVersionedNonce(nonce, version *big.Int) *big.Int {
shifted := new(big.Int).Lsh(version, 240)
return new(big.Int).Or(nonce, shifted)
}