-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch.go
135 lines (117 loc) · 4.98 KB
/
batch.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
package types
import (
"math/big"
"strings"
sdkerrors "github.com/ci123chain/ci123chain/pkg/abci/types/errors"
"github.com/ethereum/go-ethereum/accounts/abi"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
// GetCheckpoint gets the checkpoint signature from the given outgoing tx batch
func (b OutgoingTxBatch) GetCheckpoint(gravityIDstring string) ([]byte, error) {
abi, err := abi.JSON(strings.NewReader(OutgoingBatchTxCheckpointABIJSON))
if err != nil {
return nil, sdkerrors.Wrap(err, "bad ABI definition in code")
}
// the contract argument is not a arbitrary length array but a fixed length 32 byte
// array, therefore we have to utf8 encode the string (the default in this case) and
// then copy the variable length encoded data into a fixed length array. This function
// will panic if gravityId is too long to fit in 32 bytes
gravityID, err := strToFixByteArray(gravityIDstring)
if err != nil {
panic(err)
}
// Create the methodName argument which salts the signature
methodNameBytes := []uint8("transactionBatch")
var batchMethodName [32]uint8
copy(batchMethodName[:], methodNameBytes[:])
// Run through the elements of the batch and serialize them
txAmounts := make([]*big.Int, len(b.Transactions))
txDestinations := make([]gethcommon.Address, len(b.Transactions))
txFees := make([]*big.Int, len(b.Transactions))
for i, tx := range b.Transactions {
txAmounts[i] = tx.Erc20Token.Amount.BigInt()
txDestinations[i] = gethcommon.HexToAddress(tx.DestAddress)
txFees[i] = tx.Erc20Fee.Amount.BigInt()
}
// the methodName needs to be the same as the 'name' above in the checkpointAbiJson
// but other than that it's a constant that has no impact on the output. This is because
// it gets encoded as a function name which we must then discard.
abiEncodedBatch, err := abi.Pack("submitBatch",
gravityID,
batchMethodName,
txAmounts,
txDestinations,
txFees,
big.NewInt(int64(b.BatchNonce)),
gethcommon.HexToAddress(b.TokenContract),
big.NewInt(int64(b.BatchTimeout)),
)
// this should never happen outside of test since any case that could crash on encoding
// should be filtered above.
if err != nil {
return nil, sdkerrors.Wrap(err, "packing checkpoint")
}
// we hash the resulting encoded bytes discarding the first 4 bytes these 4 bytes are the constant
// method name 'checkpoint'. If you where to replace the checkpoint constant in this code you would
// then need to adjust how many bytes you truncate off the front to get the output of abi.encode()
return crypto.Keccak256Hash(abiEncodedBatch[4:]).Bytes(), nil
}
// GetCheckpoint gets the checkpoint signature from the given outgoing tx batch
func (c OutgoingLogicCall) GetCheckpoint(gravityIDstring string) ([]byte, error) {
abi, err := abi.JSON(strings.NewReader(OutgoingLogicCallABIJSON))
if err != nil {
return nil, sdkerrors.Wrap(err, "bad ABI definition in code")
}
// Create the methodName argument which salts the signature
methodNameBytes := []uint8("logicCall")
var logicCallMethodName [32]uint8
copy(logicCallMethodName[:], methodNameBytes[:])
// the contract argument is not a arbitrary length array but a fixed length 32 byte
// array, therefore we have to utf8 encode the string (the default in this case) and
// then copy the variable length encoded data into a fixed length array. This function
// will panic if gravityId is too long to fit in 32 bytes
gravityID, err := strToFixByteArray(gravityIDstring)
if err != nil {
panic(err)
}
// Run through the elements of the logic call and serialize them
transferAmounts := make([]*big.Int, len(c.Transfers))
transferTokenContracts := make([]gethcommon.Address, len(c.Transfers))
feeAmounts := make([]*big.Int, len(c.Fees))
feeTokenContracts := make([]gethcommon.Address, len(c.Fees))
for i, tx := range c.Transfers {
transferAmounts[i] = tx.Amount.BigInt()
transferTokenContracts[i] = gethcommon.HexToAddress(tx.Contract)
}
for i, tx := range c.Fees {
feeAmounts[i] = tx.Amount.BigInt()
feeTokenContracts[i] = gethcommon.HexToAddress(tx.Contract)
}
payload := make([]byte, len(c.Payload))
copy(payload, c.Payload)
var invalidationId [32]byte
copy(invalidationId[:], c.InvalidationId[:])
// the methodName needs to be the same as the 'name' above in the checkpointAbiJson
// but other than that it's a constant that has no impact on the output. This is because
// it gets encoded as a function name which we must then discard.
abiEncodedCall, err := abi.Pack("checkpoint",
gravityID,
logicCallMethodName,
transferAmounts,
transferTokenContracts,
feeAmounts,
feeTokenContracts,
gethcommon.HexToAddress(c.LogicContractAddress),
payload,
big.NewInt(int64(c.Timeout)),
invalidationId,
big.NewInt(int64(c.InvalidationNonce)),
)
// this should never happen outside of test since any case that could crash on encoding
// should be filtered above.
if err != nil {
return nil, sdkerrors.Wrap(err, "packing checkpoint")
}
return crypto.Keccak256Hash(abiEncodedCall[4:]).Bytes(), nil
}