/
payloads.go
175 lines (154 loc) · 5.59 KB
/
payloads.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
// Copyright 2022 Coinbase, Inc.
//
// 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.
package construction
import (
"bytes"
"context"
"encoding/json"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/coinbase/rosetta-geth-sdk/client"
sdkTypes "github.com/coinbase/rosetta-geth-sdk/types"
"github.com/coinbase/rosetta-sdk-go/parser"
"github.com/coinbase/rosetta-sdk-go/types"
"github.com/ethereum/go-ethereum/common"
EthTypes "github.com/ethereum/go-ethereum/core/types"
)
// ConstructionPayloads implements /construction/payloads endpoint
//
// Payloads is called with an array of operations and the response from /construction/Metadata.
// It returns an unsigned Transaction blob and a collection of payloads that must
// be signed by particular AccountIdentifiers using a certain SignatureType.
// The array of operations provided in Transaction construction often times can
// not specify all "effects" of a Transaction (consider invoked transactions in Ethereum).
// However, they can deterministically specify the "intent" of the Transaction,
// which is sufficient for construction. For this reason, parsing the corresponding
// Transaction in the Data API (when it lands on chain) will contain a superset of
// whatever operations were provided during construction.
func (s *APIService) ConstructionPayloads(
ctx context.Context,
req *types.ConstructionPayloadsRequest) (*types.ConstructionPayloadsResponse, *types.Error) {
isContractCall := false
if _, ok := req.Metadata["method_signature"]; ok {
isContractCall = true
}
operationDescriptions, err := s.CreateOperationDescription(req.Operations, isContractCall)
if err != nil {
return nil, sdkTypes.WrapErr(sdkTypes.ErrInvalidInput, err)
}
descriptions := &parser.Descriptions{
OperationDescriptions: operationDescriptions,
ErrUnmatched: true,
}
matches, err := parser.MatchOperations(descriptions, req.Operations)
if err != nil {
return nil, sdkTypes.WrapErr(sdkTypes.ErrInternalError, err)
}
var metadata client.Metadata
if err := client.UnmarshalJSONMap(req.Metadata, &metadata); err != nil {
return nil, sdkTypes.WrapErr(sdkTypes.ErrInternalError, err)
}
toOp, amount := matches[1].First()
toAddress := toOp.Account.Address
nonce := metadata.Nonce
gasPrice := metadata.GasPrice
gasLimit := metadata.GasLimit
gasTipCap := metadata.GasTipCap
gasFeeCap := metadata.GasFeeCap
chainID := s.config.ChainConfig.ChainID
fromOp, _ := matches[0].First()
fromAddress := fromOp.Account.Address
fromCurrency := fromOp.Amount.Currency
// Address validation
from, err := client.ChecksumAddress(fromAddress)
if err != nil {
return nil, sdkTypes.WrapErr(sdkTypes.ErrInvalidInput, fmt.Errorf("%s is not a valid address: %w", fromAddress, err))
}
to, err := client.ChecksumAddress(toAddress)
if err != nil {
return nil, sdkTypes.WrapErr(sdkTypes.ErrInvalidInput, fmt.Errorf("%s is not a valid address: %w", toAddress, err))
}
var transferData []byte
var sendToAddress common.Address
switch {
case isContractCall:
// Generic contract call logic
contractData, err := hexutil.Decode(metadata.ContractData)
if err != nil {
return nil, sdkTypes.WrapErr(sdkTypes.ErrInvalidInput, err)
}
data, err := ConstructContractCallDataGeneric(metadata.MethodSignature, metadata.MethodArgs)
if err != nil {
return nil, sdkTypes.WrapErr(sdkTypes.ErrInvalidInput, err)
}
res := bytes.Compare(data, contractData)
if res != 0 {
return nil, sdkTypes.WrapErr(
sdkTypes.ErrInvalidInput,
fmt.Errorf("invalid conctract data"),
)
}
transferData = contractData
sendToAddress = common.HexToAddress(to)
case types.Hash(fromCurrency) == types.Hash(s.config.RosettaCfg.Currency):
// Native currency logic
transferData = []byte{}
sendToAddress = common.HexToAddress(to)
default:
// ERC20 logic
contract, ok := fromCurrency.Metadata[client.ContractAddressMetadata].(string)
if !ok {
return nil, sdkTypes.WrapErr(
sdkTypes.ErrInvalidInput,
fmt.Errorf(
"%s currency doesn't have a contract address in Metadata",
fromCurrency.Symbol,
),
)
}
transferData = client.GenerateErc20TransferData(toAddress, amount)
sendToAddress = common.HexToAddress(contract)
amount = big.NewInt(0)
}
// Construct SigningPayload
unsignedTx := &client.Transaction{
From: from,
To: sendToAddress.Hex(),
Value: amount,
Data: transferData,
Nonce: nonce,
GasPrice: gasPrice,
GasLimit: gasLimit,
GasTipCap: gasTipCap,
GasFeeCap: gasFeeCap,
ChainID: chainID,
Currency: fromCurrency,
}
unsignedEthTx := EthTransaction(unsignedTx)
signer := EthTypes.LatestSignerForChainID(chainID)
payload := &types.SigningPayload{
AccountIdentifier: &types.AccountIdentifier{Address: from},
Bytes: signer.Hash(unsignedEthTx).Bytes(),
SignatureType: types.EcdsaRecovery,
}
unsignedTxJSON, err := json.Marshal(unsignedTx)
if err != nil {
return nil, sdkTypes.WrapErr(sdkTypes.ErrInvalidInput, err)
}
return &types.ConstructionPayloadsResponse{
UnsignedTransaction: string(unsignedTxJSON),
Payloads: []*types.SigningPayload{payload},
}, nil
}