forked from harmony-one/harmony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
construction_create.go
267 lines (253 loc) · 9.93 KB
/
construction_create.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package services
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/coinbase/rosetta-sdk-go/types"
"github.com/ethereum/go-ethereum/rlp"
"github.com/pkg/errors"
hmyTypes "github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/rosetta/common"
stakingTypes "github.com/harmony-one/harmony/staking/types"
)
const (
// SignedPayloadLength ..
SignedPayloadLength = 65
)
// WrappedTransaction is a wrapper for a transaction that includes all relevant
// data to parse a transaction.
type WrappedTransaction struct {
RLPBytes []byte `json:"rlp_bytes"`
IsStaking bool `json:"is_staking"`
From *types.AccountIdentifier `json:"from"`
EstimatedGasUsed uint64 `json:"estimated_gas_used"`
}
// unpackWrappedTransactionFromString ..
func unpackWrappedTransactionFromString(
str string,
) (*WrappedTransaction, hmyTypes.PoolTransaction, *types.Error) {
wrappedTransaction := &WrappedTransaction{}
if err := json.Unmarshal([]byte(str), wrappedTransaction); err != nil {
return nil, nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": errors.WithMessage(err, "unknown wrapped transaction format").Error(),
})
}
if wrappedTransaction.RLPBytes == nil {
return nil, nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": "RLP encoded transactions are not found in wrapped transaction",
})
}
if wrappedTransaction.From == nil {
return nil, nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": "from/sender not found in wrapped transaction",
})
}
var tx hmyTypes.PoolTransaction
stream := rlp.NewStream(bytes.NewBuffer(wrappedTransaction.RLPBytes), 0)
if wrappedTransaction.IsStaking {
stakingTx := &stakingTypes.StakingTransaction{}
if err := stakingTx.DecodeRLP(stream); err != nil {
return nil, nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": errors.WithMessage(err, "rlp encoding error for staking transaction").Error(),
})
}
tx = stakingTx
} else {
plainTx := &hmyTypes.Transaction{}
if err := plainTx.DecodeRLP(stream); err != nil {
return nil, nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": errors.WithMessage(err, "rlp encoding error for plain transaction").Error(),
})
}
tx = plainTx
}
return wrappedTransaction, tx, nil
}
// ConstructionPayloads implements the /construction/payloads endpoint.
func (s *ConstructAPI) ConstructionPayloads(
ctx context.Context, request *types.ConstructionPayloadsRequest,
) (*types.ConstructionPayloadsResponse, *types.Error) {
if err := assertValidNetworkIdentifier(request.NetworkIdentifier, s.hmy.ShardID); err != nil {
return nil, err
}
if request.Metadata == nil {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": "require metadata",
})
}
metadata := &ConstructMetadata{}
if err := metadata.UnmarshalFromInterface(request.Metadata); err != nil {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": errors.WithMessage(err, "invalid metadata").Error(),
})
}
if request.PublicKeys == nil || len(request.PublicKeys) != 1 {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": "require sender public key only",
})
}
senderAddr, rosettaError := getAddressFromPublicKey(request.PublicKeys[0])
if rosettaError != nil {
return nil, rosettaError
}
senderID, rosettaError := newAccountIdentifier(*senderAddr)
if rosettaError != nil {
return nil, rosettaError
}
components, rosettaError := GetOperationComponents(request.Operations)
if rosettaError != nil {
return nil, rosettaError
}
if components.From == nil {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": "sender address is not found for given operations",
})
}
if types.Hash(senderID) != types.Hash(components.From) {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": "sender account identifier from operations does not match account identifier from public key",
})
}
unsignedTx, rosettaError := ConstructTransaction(components, metadata, s.hmy.ShardID)
if rosettaError != nil {
return nil, rosettaError
}
payload, rosettaError := s.getSigningPayload(unsignedTx, senderID)
if rosettaError != nil {
return nil, rosettaError
}
buf := &bytes.Buffer{}
if err := unsignedTx.EncodeRLP(buf); err != nil {
return nil, common.NewError(common.CatchAllError, map[string]interface{}{
"message": err.Error(),
})
}
wrappedTxMarshalledBytes, err := json.Marshal(WrappedTransaction{
RLPBytes: buf.Bytes(),
From: senderID,
EstimatedGasUsed: metadata.GasLimit,
IsStaking: components.IsStaking(),
})
if err != nil {
return nil, common.NewError(common.CatchAllError, map[string]interface{}{
"message": err.Error(),
})
}
return &types.ConstructionPayloadsResponse{
UnsignedTransaction: string(wrappedTxMarshalledBytes),
Payloads: []*types.SigningPayload{payload},
}, nil
}
// getSigningPayload ..
func (s *ConstructAPI) getSigningPayload(
tx hmyTypes.PoolTransaction, senderAccountID *types.AccountIdentifier,
) (*types.SigningPayload, *types.Error) {
payload := &types.SigningPayload{
AccountIdentifier: senderAccountID,
SignatureType: common.SignatureType,
}
switch tx.(type) {
case *stakingTypes.StakingTransaction:
payload.Bytes = s.stakingSigner.Hash(tx.(*stakingTypes.StakingTransaction)).Bytes()
case *hmyTypes.Transaction:
payload.Bytes = s.signer.Hash(tx.(*hmyTypes.Transaction)).Bytes()
default:
return nil, common.NewError(common.CatchAllError, map[string]interface{}{
"message": "constructed unknown or unsupported transaction",
})
}
return payload, nil
}
// ConstructionCombine implements the /construction/combine endpoint.
func (s *ConstructAPI) ConstructionCombine(
ctx context.Context, request *types.ConstructionCombineRequest,
) (*types.ConstructionCombineResponse, *types.Error) {
if err := assertValidNetworkIdentifier(request.NetworkIdentifier, s.hmy.ShardID); err != nil {
return nil, err
}
wrappedTransaction, tx, rosettaError := unpackWrappedTransactionFromString(request.UnsignedTransaction)
if rosettaError != nil {
return nil, rosettaError
}
if request.Signatures == nil || len(request.Signatures) != 1 {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": "require exactly 1 signature",
})
}
sig := request.Signatures[0]
if sig.SignatureType != common.SignatureType {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": fmt.Sprintf("invalid transaction type, currently only support %v", common.SignatureType),
})
}
sigAddress, rosettaError := getAddressFromPublicKey(sig.PublicKey)
if rosettaError != nil {
return nil, rosettaError
}
sigAccountID, rosettaError := newAccountIdentifier(*sigAddress)
if rosettaError != nil {
return nil, rosettaError
}
if wrappedTransaction.From == nil || types.Hash(wrappedTransaction.From) != types.Hash(sigAccountID) {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": "signer public key does not match unsigned transaction's sender",
})
}
txPayload, rosettaError := s.getSigningPayload(tx, sigAccountID)
if rosettaError != nil {
return nil, rosettaError
}
if sig.SigningPayload == nil || types.Hash(sig.SigningPayload) != types.Hash(txPayload) {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": "transaction signing payload does not match given signing payload",
})
}
var err error
var signedTx hmyTypes.PoolTransaction
if len(sig.Bytes) != SignedPayloadLength {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": fmt.Sprintf("invalid signature byte length, require len %v got len %v",
SignedPayloadLength, len(sig.Bytes)),
})
}
if stakingTx, ok := tx.(*stakingTypes.StakingTransaction); ok && wrappedTransaction.IsStaking {
signedTx, err = stakingTx.WithSignature(s.stakingSigner, sig.Bytes)
} else if plainTx, ok := tx.(*hmyTypes.Transaction); ok && !wrappedTransaction.IsStaking {
signedTx, err = plainTx.WithSignature(s.signer, sig.Bytes)
} else {
return nil, common.NewError(common.CatchAllError, map[string]interface{}{
"message": "invalid/inconsistent type or unknown transaction type stored in wrapped transaction",
})
}
if err != nil {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": errors.WithMessage(err, "unable to apply signature to transaction").Error(),
})
}
senderAddress, err := signedTx.SenderAddress()
if err != nil {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": errors.WithMessage(err, "unable to get sender address with signed transaction").Error(),
})
}
if *sigAddress != senderAddress {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": "signature address does not match signed transaction sender address",
})
}
buf := &bytes.Buffer{}
if err := signedTx.EncodeRLP(buf); err != nil {
return nil, common.NewError(common.CatchAllError, map[string]interface{}{
"message": err.Error(),
})
}
wrappedTransaction.RLPBytes = buf.Bytes()
wrappedTxMarshalledBytes, err := json.Marshal(wrappedTransaction)
if err != nil {
return nil, common.NewError(common.CatchAllError, map[string]interface{}{
"message": err.Error(),
})
}
return &types.ConstructionCombineResponse{SignedTransaction: string(wrappedTxMarshalledBytes)}, nil
}