forked from aptos-labs/aptos-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rawTransaction.go
326 lines (268 loc) · 8.92 KB
/
rawTransaction.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package aptos
import (
"fmt"
"github.com/aptos-labs/aptos-go-sdk/bcs"
"github.com/aptos-labs/aptos-go-sdk/crypto"
"golang.org/x/crypto/sha3"
)
//region RawTransaction
var rawTransactionPrehash []byte
const rawTransactionPrehashStr = "APTOS::RawTransaction"
// RawTransactionPrehash Return the sha3-256 prehash for RawTransaction
// Do not write to the []byte returned
func RawTransactionPrehash() []byte {
// Cache the prehash
if rawTransactionPrehash == nil {
b32 := sha3.Sum256([]byte(rawTransactionPrehashStr))
out := make([]byte, len(b32))
copy(out, b32[:])
rawTransactionPrehash = out
return out
}
return rawTransactionPrehash
}
type RawTransactionImpl interface {
bcs.Struct
// SigningMessage creates a raw signing message for the transaction
// Note that this should only be used externally if signing transactions outside the SDK. Otherwise, use Sign.
SigningMessage() (message []byte, err error)
// Sign signs a transaction and returns the associated AccountAuthenticator, it will underneath sign the SigningMessage
Sign(signer crypto.Signer) (*crypto.AccountAuthenticator, error)
}
// RawTransaction representation of a transaction's parts prior to signing
// Implements crypto.MessageSigner, crypto.Signer, bcs.Struct
type RawTransaction struct {
Sender AccountAddress
SequenceNumber uint64
Payload TransactionPayload
MaxGasAmount uint64
GasUnitPrice uint64
// ExpirationTimestampSeconds is seconds since Unix epoch
ExpirationTimestampSeconds uint64
ChainId uint8
}
func (txn *RawTransaction) SignedTransaction(sender crypto.Signer) (*SignedTransaction, error) {
auth, err := txn.Sign(sender)
if err != nil {
return nil, err
}
return txn.SignedTransactionWithAuthenticator(auth)
}
// SignedTransactionWithAuthenticator signs the sender only signed transaction
func (txn *RawTransaction) SignedTransactionWithAuthenticator(auth *crypto.AccountAuthenticator) (*SignedTransaction, error) {
txnAuth, err := NewTransactionAuthenticator(auth)
if err != nil {
return nil, err
}
return &SignedTransaction{
Transaction: txn,
Authenticator: txnAuth,
}, nil
}
//region RawTransaction bcs.Struct
func (txn *RawTransaction) MarshalBCS(ser *bcs.Serializer) {
txn.Sender.MarshalBCS(ser)
ser.U64(txn.SequenceNumber)
txn.Payload.MarshalBCS(ser)
ser.U64(txn.MaxGasAmount)
ser.U64(txn.GasUnitPrice)
ser.U64(txn.ExpirationTimestampSeconds)
ser.U8(txn.ChainId)
}
func (txn *RawTransaction) UnmarshalBCS(des *bcs.Deserializer) {
txn.Sender.UnmarshalBCS(des)
txn.SequenceNumber = des.U64()
txn.Payload.UnmarshalBCS(des)
txn.MaxGasAmount = des.U64()
txn.GasUnitPrice = des.U64()
txn.ExpirationTimestampSeconds = des.U64()
txn.ChainId = des.U8()
}
//endregion
//region RawTransaction MessageSigner
// SigningMessage generates the bytes needed to be signed by a signer
func (txn *RawTransaction) SigningMessage() (message []byte, err error) {
txnBytes, err := bcs.Serialize(txn)
if err != nil {
return
}
prehash := RawTransactionPrehash()
message = make([]byte, len(prehash)+len(txnBytes))
copy(message, prehash)
copy(message[len(prehash):], txnBytes)
return message, nil
}
//endregion
//region RawTransaction Signer
func (txn *RawTransaction) Sign(signer crypto.Signer) (authenticator *crypto.AccountAuthenticator, err error) {
message, err := txn.SigningMessage()
if err != nil {
return
}
return signer.Sign(message)
}
//endregion
//endregion
//region RawTransactionWithData
var rawTransactionWithDataPrehash []byte
const rawTransactionWithDataPrehashStr = "APTOS::RawTransactionWithData"
// RawTransactionWithDataPrehash Return the sha3-256 prehash for RawTransactionWithData
// Do not write to the []byte returned
func RawTransactionWithDataPrehash() []byte {
// Cache the prehash
if rawTransactionWithDataPrehash == nil {
b32 := sha3.Sum256([]byte(rawTransactionWithDataPrehashStr))
out := make([]byte, len(b32))
copy(out, b32[:])
rawTransactionPrehash = out
return out
}
return rawTransactionPrehash
}
type RawTransactionWithDataVariant uint32
const (
MultiAgentRawTransactionWithDataVariant RawTransactionWithDataVariant = 0
MultiAgentWithFeePayerRawTransactionWithDataVariant RawTransactionWithDataVariant = 1
)
type RawTransactionWithDataImpl interface {
bcs.Struct
}
// TODO: make a function to make creating this easier
type RawTransactionWithData struct {
Variant RawTransactionWithDataVariant
Inner RawTransactionWithDataImpl
}
func (txn *RawTransactionWithData) SetFeePayer(
feePayer AccountAddress,
) bool {
if txn.Variant == MultiAgentWithFeePayerRawTransactionWithDataVariant {
inner := txn.Inner.(*MultiAgentWithFeePayerRawTransactionWithData)
inner.FeePayer = &feePayer
return true
} else {
return false
}
}
func (txn *RawTransactionWithData) ToMultiAgentSignedTransaction(
sender *crypto.AccountAuthenticator,
additionalSigners []crypto.AccountAuthenticator,
) (*SignedTransaction, bool) {
if txn.Variant != MultiAgentRawTransactionWithDataVariant {
return nil, false
}
multiAgent := txn.Inner.(*MultiAgentRawTransactionWithData)
return &SignedTransaction{
Transaction: multiAgent.RawTxn,
Authenticator: &TransactionAuthenticator{
Variant: TransactionAuthenticatorMultiAgent,
Auth: &MultiAgentTransactionAuthenticator{
Sender: sender,
SecondarySignerAddresses: multiAgent.SecondarySigners,
SecondarySigners: additionalSigners,
},
},
}, true
}
func (txn *RawTransactionWithData) ToFeePayerSignedTransaction(
sender *crypto.AccountAuthenticator,
feePayerAuthenticator *crypto.AccountAuthenticator,
additionalSigners []crypto.AccountAuthenticator,
) (*SignedTransaction, bool) {
if txn.Variant != MultiAgentWithFeePayerRawTransactionWithDataVariant {
return nil, false
}
feePayerTxn := txn.Inner.(*MultiAgentWithFeePayerRawTransactionWithData)
return &SignedTransaction{
Transaction: feePayerTxn.RawTxn,
Authenticator: &TransactionAuthenticator{
Variant: TransactionAuthenticatorFeePayer,
Auth: &FeePayerTransactionAuthenticator{
Sender: sender,
SecondarySignerAddresses: feePayerTxn.SecondarySigners,
SecondarySigners: additionalSigners,
FeePayer: feePayerTxn.FeePayer,
FeePayerAuthenticator: feePayerAuthenticator,
},
},
}, true
}
//region RawTransactionWithData Signer
func (txn *RawTransactionWithData) Sign(signer crypto.Signer) (authenticator *crypto.AccountAuthenticator, err error) {
message, err := txn.SigningMessage()
if err != nil {
return
}
return signer.Sign(message)
}
//endregion
//region RawTransactionWithData MessageSigner
func (txn *RawTransactionWithData) SigningMessage() (message []byte, err error) {
txnBytes, err := bcs.Serialize(txn)
if err != nil {
return
}
prehash := RawTransactionWithDataPrehash()
message = make([]byte, len(prehash)+len(txnBytes))
copy(message, prehash)
copy(message[len(prehash):], txnBytes)
return message, nil
}
//endregion
//region RawTransactionWithData bcs.Struct
func (txn *RawTransactionWithData) MarshalBCS(ser *bcs.Serializer) {
ser.Uleb128(uint32(txn.Variant))
ser.Struct(txn.Inner)
}
func (txn *RawTransactionWithData) UnmarshalBCS(des *bcs.Deserializer) {
txn.Variant = RawTransactionWithDataVariant(des.Uleb128())
switch txn.Variant {
case MultiAgentRawTransactionWithDataVariant:
txn.Inner = &MultiAgentRawTransactionWithData{}
case MultiAgentWithFeePayerRawTransactionWithDataVariant:
txn.Inner = &MultiAgentWithFeePayerRawTransactionWithData{}
default:
des.SetError(fmt.Errorf("unknown RawTransactionWithData variant %d", txn.Variant))
return
}
des.Struct(txn.Inner)
}
//endregion
//endregion
//region MultiAgentRawTransactionWithData
type MultiAgentRawTransactionWithData struct {
RawTxn *RawTransaction
SecondarySigners []AccountAddress
}
//region MultiAgentRawTransactionWithData bcs.Struct
func (txn *MultiAgentRawTransactionWithData) MarshalBCS(ser *bcs.Serializer) {
ser.Struct(txn.RawTxn)
bcs.SerializeSequence(txn.SecondarySigners, ser)
}
func (txn *MultiAgentRawTransactionWithData) UnmarshalBCS(des *bcs.Deserializer) {
txn.RawTxn = &RawTransaction{}
des.Struct(txn.RawTxn)
txn.SecondarySigners = bcs.DeserializeSequence[AccountAddress](des)
}
//endregion
//endregion
//region MultiAgentWithFeePayerRawTransactionWithData
type MultiAgentWithFeePayerRawTransactionWithData struct {
RawTxn *RawTransaction
SecondarySigners []AccountAddress
FeePayer *AccountAddress
}
//region MultiAgentWithFeePayerRawTransactionWithData bcs.Struct
func (txn *MultiAgentWithFeePayerRawTransactionWithData) MarshalBCS(ser *bcs.Serializer) {
ser.Struct(txn.RawTxn)
bcs.SerializeSequence(txn.SecondarySigners, ser)
ser.Struct(txn.FeePayer)
}
func (txn *MultiAgentWithFeePayerRawTransactionWithData) UnmarshalBCS(des *bcs.Deserializer) {
txn.RawTxn = &RawTransaction{}
des.Struct(txn.RawTxn)
txn.SecondarySigners = bcs.DeserializeSequence[AccountAddress](des)
txn.FeePayer = &AccountAddress{}
des.Struct(txn.FeePayer)
}
//endregion
//endregion