/
request_offledger.go
365 lines (301 loc) · 8.97 KB
/
request_offledger.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package isc
import (
"errors"
"fmt"
"io"
"math/big"
"time"
"github.com/ethereum/go-ethereum/core/types"
"github.com/minio/blake2b-simd"
iotago "github.com/iotaledger/iota.go/v3"
"github.com/iotaledger/wasp/packages/cryptolib"
"github.com/iotaledger/wasp/packages/hashing"
"github.com/iotaledger/wasp/packages/kv/dict"
"github.com/iotaledger/wasp/packages/util/rwutil"
)
type offLedgerSignature struct {
publicKey *cryptolib.PublicKey
signature []byte
}
type OffLedgerRequestData struct {
allowance *Assets
chainID ChainID
contract Hname
entryPoint Hname
gasBudget uint64
nonce uint64
params dict.Dict
signature offLedgerSignature
}
var (
_ Request = new(OffLedgerRequestData)
_ OffLedgerRequest = new(OffLedgerRequestData)
_ UnsignedOffLedgerRequest = new(OffLedgerRequestData)
_ Calldata = new(OffLedgerRequestData)
_ Features = new(OffLedgerRequestData)
)
type ImpersonatedOffLedgerRequestData struct {
req *OffLedgerRequestData
address *iotago.Ed25519Address
}
func NewImpersonatedOffLedgerRequest(request OffLedgerRequest) ImpersonatedOffLedgerRequest {
gasBudget, _ := request.GasBudget()
return &ImpersonatedOffLedgerRequestData{
req: &OffLedgerRequestData{
allowance: request.Allowance().Clone(),
chainID: request.ChainID(),
contract: request.CallTarget().Contract,
entryPoint: request.CallTarget().EntryPoint,
gasBudget: gasBudget,
nonce: request.Nonce(),
params: request.Params().Clone(),
signature: offLedgerSignature{
signature: make([]byte, 0),
publicKey: cryptolib.NewEmptyPublicKey(),
},
},
}
}
func (r *ImpersonatedOffLedgerRequestData) WithSenderAddress(address *iotago.Ed25519Address) OffLedgerRequest {
r.address = address
return r
}
func (r *ImpersonatedOffLedgerRequestData) Allowance() *Assets {
return r.req.Allowance()
}
func (r *ImpersonatedOffLedgerRequestData) Assets() *Assets {
return r.req.Assets()
}
func (r *ImpersonatedOffLedgerRequestData) CallTarget() CallTarget {
return r.req.CallTarget()
}
func (r *ImpersonatedOffLedgerRequestData) GasBudget() (gas uint64, isEVM bool) {
return r.req.GasBudget()
}
func (r *ImpersonatedOffLedgerRequestData) ID() RequestID {
return r.req.ID()
}
func (r *ImpersonatedOffLedgerRequestData) NFT() *NFT {
return r.req.NFT()
}
func (r *ImpersonatedOffLedgerRequestData) Params() dict.Dict {
return r.req.Params()
}
func (r *ImpersonatedOffLedgerRequestData) SenderAccount() AgentID {
return NewAgentID(r.address)
}
func (r *ImpersonatedOffLedgerRequestData) TargetAddress() iotago.Address {
return r.req.TargetAddress()
}
func (r *ImpersonatedOffLedgerRequestData) TxValue() *big.Int {
return r.req.TxValue()
}
func (r *ImpersonatedOffLedgerRequestData) EVMCallData() *EVMCallData {
return r.req.EVMCallData()
}
func (r *ImpersonatedOffLedgerRequestData) Bytes() []byte {
return r.req.Bytes()
}
func (r *ImpersonatedOffLedgerRequestData) IsOffLedger() bool {
return r.req.IsOffLedger()
}
func (r *ImpersonatedOffLedgerRequestData) String() string {
return r.req.String()
}
func (r *ImpersonatedOffLedgerRequestData) Read(reader io.Reader) error {
return r.req.Read(reader)
}
func (r *ImpersonatedOffLedgerRequestData) Write(w io.Writer) error {
return r.req.Write(w)
}
func (r *ImpersonatedOffLedgerRequestData) ChainID() ChainID {
return r.req.ChainID()
}
func (r *ImpersonatedOffLedgerRequestData) Nonce() uint64 {
return r.req.Nonce()
}
func (r *ImpersonatedOffLedgerRequestData) VerifySignature() error {
return r.req.VerifySignature()
}
func (r *ImpersonatedOffLedgerRequestData) EVMTransaction() *types.Transaction {
return r.req.EVMTransaction()
}
func NewOffLedgerRequest(
chainID ChainID,
contract, entryPoint Hname,
params dict.Dict,
nonce uint64,
gasBudget uint64,
) UnsignedOffLedgerRequest {
return &OffLedgerRequestData{
chainID: chainID,
contract: contract,
entryPoint: entryPoint,
params: params,
nonce: nonce,
allowance: NewEmptyAssets(),
gasBudget: gasBudget,
}
}
func (req *OffLedgerRequestData) Read(r io.Reader) error {
rr := rwutil.NewReader(r)
req.readEssence(rr)
req.signature.publicKey = cryptolib.NewEmptyPublicKey()
rr.Read(req.signature.publicKey)
req.signature.signature = rr.ReadBytes()
return rr.Err
}
func (req *OffLedgerRequestData) TxValue() *big.Int {
return big.NewInt(0)
}
func (req *OffLedgerRequestData) EVMCallData() *EVMCallData {
return nil
}
func (req *OffLedgerRequestData) Write(w io.Writer) error {
ww := rwutil.NewWriter(w)
req.writeEssence(ww)
ww.Write(req.signature.publicKey)
ww.WriteBytes(req.signature.signature)
return ww.Err
}
func (req *OffLedgerRequestData) readEssence(rr *rwutil.Reader) {
rr.ReadKindAndVerify(rwutil.Kind(requestKindOffLedgerISC))
rr.Read(&req.chainID)
rr.Read(&req.contract)
rr.Read(&req.entryPoint)
req.params = dict.New()
rr.Read(&req.params)
req.nonce = rr.ReadAmount64()
req.gasBudget = rr.ReadGas64()
req.allowance = NewEmptyAssets()
rr.Read(req.allowance)
}
func (req *OffLedgerRequestData) writeEssence(ww *rwutil.Writer) {
ww.WriteKind(rwutil.Kind(requestKindOffLedgerISC))
ww.Write(&req.chainID)
ww.Write(&req.contract)
ww.Write(&req.entryPoint)
ww.Write(&req.params)
ww.WriteAmount64(req.nonce)
ww.WriteGas64(req.gasBudget)
ww.Write(req.allowance)
}
// Allowance from the sender's account to the target smart contract. Nil mean no Allowance
func (req *OffLedgerRequestData) Allowance() *Assets {
return req.allowance
}
// Assets is attached assets to the UTXO. Nil for off-ledger
func (req *OffLedgerRequestData) Assets() *Assets {
return nil
}
func (req *OffLedgerRequestData) Bytes() []byte {
return rwutil.WriteToBytes(req)
}
func (req *OffLedgerRequestData) CallTarget() CallTarget {
return CallTarget{
Contract: req.contract,
EntryPoint: req.entryPoint,
}
}
func (req *OffLedgerRequestData) ChainID() ChainID {
return req.chainID
}
func (req *OffLedgerRequestData) EssenceBytes() []byte {
ww := rwutil.NewBytesWriter()
req.writeEssence(ww)
return ww.Bytes()
}
func (req *OffLedgerRequestData) messageToSign() []byte {
ret := blake2b.Sum256(req.EssenceBytes())
return ret[:]
}
func (req *OffLedgerRequestData) Expiry() (time.Time, iotago.Address) {
return time.Time{}, nil
}
func (req *OffLedgerRequestData) GasBudget() (gasBudget uint64, isEVM bool) {
return req.gasBudget, false
}
// ID returns request id for this request
// index part of request id is always 0 for off ledger requests
// note that request needs to have been signed before this value is
// considered valid
func (req *OffLedgerRequestData) ID() (requestID RequestID) {
return NewRequestID(iotago.TransactionID(hashing.HashData(req.Bytes())), 0)
}
func (req *OffLedgerRequestData) IsOffLedger() bool {
return true
}
func (req *OffLedgerRequestData) NFT() *NFT {
return nil
}
// Nonce incremental nonce used for replay protection
func (req *OffLedgerRequestData) Nonce() uint64 {
return req.nonce
}
func (req *OffLedgerRequestData) Params() dict.Dict {
return req.params
}
func (req *OffLedgerRequestData) ReturnAmount() (uint64, bool) {
return 0, false
}
func (req *OffLedgerRequestData) SenderAccount() AgentID {
return NewAgentID(req.signature.publicKey.AsEd25519Address())
}
// Sign signs the essence
func (req *OffLedgerRequestData) Sign(key cryptolib.VariantKeyPair) OffLedgerRequest {
req.signature = offLedgerSignature{
publicKey: key.GetPublicKey(),
signature: key.SignBytes(req.messageToSign()),
}
return req
}
func (req *OffLedgerRequestData) String() string {
return fmt.Sprintf("offLedgerRequestData::{ ID: %s, sender: %s, target: %s, entrypoint: %s, Params: %s, nonce: %d }",
req.ID().String(),
req.SenderAccount().String(),
req.contract.String(),
req.entryPoint.String(),
req.Params().String(),
req.nonce,
)
}
func (req *OffLedgerRequestData) TargetAddress() iotago.Address {
return req.chainID.AsAddress()
}
func (req *OffLedgerRequestData) TimeLock() time.Time {
return time.Time{}
}
func (req *OffLedgerRequestData) Timestamp() time.Time {
// no request TX, return zero time
return time.Time{}
}
// VerifySignature verifies essence signature
func (req *OffLedgerRequestData) VerifySignature() error {
if !req.signature.publicKey.Verify(req.messageToSign(), req.signature.signature) {
return errors.New("invalid signature")
}
return nil
}
func (req *OffLedgerRequestData) WithAllowance(allowance *Assets) UnsignedOffLedgerRequest {
req.allowance = allowance.Clone()
return req
}
func (req *OffLedgerRequestData) WithGasBudget(gasBudget uint64) UnsignedOffLedgerRequest {
req.gasBudget = gasBudget
return req
}
func (req *OffLedgerRequestData) WithNonce(nonce uint64) UnsignedOffLedgerRequest {
req.nonce = nonce
return req
}
// WithSender can be used to estimate gas, without a signature
func (req *OffLedgerRequestData) WithSender(sender *cryptolib.PublicKey) UnsignedOffLedgerRequest {
req.signature = offLedgerSignature{
publicKey: sender,
signature: []byte{},
}
return req
}
func (*OffLedgerRequestData) EVMTransaction() *types.Transaction {
return nil
}