forked from deroproject/derohe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdvm_functions.go
628 lines (516 loc) · 24.2 KB
/
dvm_functions.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
// Copyright 2017-2018 DERO Project. All rights reserved.
// Use of this source code in any form is governed by RESEARCH license.
// license can be found in the LICENSE file.
// GPG: 0F39 E425 8C65 3947 702A 8234 08B2 0360 A03A 9DE8
//
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package dvm
import "fmt"
import "go/ast"
import "strconv"
import "strings"
import "crypto/sha256"
import "encoding/hex"
import "golang.org/x/crypto/sha3"
import "github.com/blang/semver/v4"
import "github.com/deroproject/derohe/rpc"
import "github.com/deroproject/derohe/cryptography/crypto"
// this files defines external functions which can be called in DVM
// for example to load and store data from the blockchain and other basic functions
// random number generator is the basis
// however, further investigation is needed if we would like to enable users to use pederson commitments
// they can be used like
// original SC developers delivers a pederson commitment to SC as external oracle
// after x users have played lottery, dev reveals the commitment using which the winner is finalised
// this needs more investigation
// also, more investigation is required to enable predetermined external oracles
type DVM_FUNCTION_PTR_UINT64 func(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result uint64)
type DVM_FUNCTION_PTR_STRING func(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result string)
type DVM_FUNCTION_PTR_ANY func(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result interface{})
var func_table = map[string][]func_data{}
type func_data struct {
Range semver.Range
ComputeCost int64
StorageCost int64
PtrU DVM_FUNCTION_PTR_UINT64
PtrS DVM_FUNCTION_PTR_STRING
Ptr DVM_FUNCTION_PTR_ANY
}
func init() {
func_table["version"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 1000, StorageCost: 0, PtrU: dvm_version}}
func_table["load"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 5000, StorageCost: 0, Ptr: dvm_load}}
func_table["exists"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 5000, StorageCost: 0, PtrU: dvm_exists}}
func_table["store"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 10000, StorageCost: 0, PtrU: dvm_store}}
func_table["delete"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 3000, StorageCost: 0, PtrU: dvm_delete}}
func_table["mapexists"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 1000, StorageCost: 0, PtrU: dvm_mapexists}}
func_table["mapget"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 1000, StorageCost: 0, Ptr: dvm_mapget}}
func_table["mapstore"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 1000, StorageCost: 0, PtrU: dvm_mapstore}}
func_table["mapdelete"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 1000, StorageCost: 0, PtrU: dvm_mapdelete}}
func_table["random"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 2500, StorageCost: 0, PtrU: dvm_random}}
func_table["scid"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 2000, StorageCost: 0, PtrS: dvm_scid}}
func_table["blid"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 2000, StorageCost: 0, PtrS: dvm_blid}}
func_table["txid"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 2000, StorageCost: 0, PtrS: dvm_txid}}
func_table["dero"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 10000, StorageCost: 0, PtrS: dvm_dero}}
func_table["block_height"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 2000, StorageCost: 0, PtrU: dvm_block_height}}
func_table["block_timestamp"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 2500, StorageCost: 0, PtrU: dvm_block_timestamp}}
func_table["signer"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 5000, StorageCost: 0, PtrS: dvm_signer}}
func_table["update_sc_code"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 5000, StorageCost: 0, PtrU: dvm_update_sc_code}}
func_table["is_address_valid"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 50000, StorageCost: 0, PtrU: dvm_is_address_valid}}
func_table["address_raw"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 60000, StorageCost: 0, PtrS: dvm_address_raw}}
func_table["address_string"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 50000, StorageCost: 0, PtrS: dvm_address_string}}
func_table["send_dero_to_address"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 70000, StorageCost: 0, PtrU: dvm_send_dero_to_address}}
func_table["send_asset_to_address"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 90000, StorageCost: 0, PtrU: dvm_send_asset_to_address}}
func_table["derovalue"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 10000, StorageCost: 0, PtrU: dvm_derovalue}}
func_table["assetvalue"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 10000, StorageCost: 0, PtrU: dvm_assetvalue}}
func_table["atoi"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 5000, StorageCost: 0, PtrU: dvm_atoi}}
func_table["itoa"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 5000, StorageCost: 0, PtrS: dvm_itoa}}
func_table["sha256"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 25000, StorageCost: 0, PtrS: dvm_sha256}}
func_table["sha3256"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 25000, StorageCost: 0, PtrS: dvm_sha3256}}
func_table["keccak256"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 25000, StorageCost: 0, PtrS: dvm_keccak256}}
func_table["hex"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 10000, StorageCost: 0, PtrS: dvm_hex}}
func_table["hexdecode"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 10000, StorageCost: 0, PtrS: dvm_hexdecode}}
func_table["min"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 5000, StorageCost: 0, PtrU: dvm_min}}
func_table["max"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 5000, StorageCost: 0, PtrU: dvm_max}}
func_table["strlen"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 20000, StorageCost: 0, PtrU: dvm_strlen}}
func_table["substr"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 20000, StorageCost: 0, PtrS: dvm_substr}}
func_table["panic"] = []func_data{func_data{Range: semver.MustParseRange(">=0.0.0"), ComputeCost: 10000, StorageCost: 0, PtrU: dvm_panic}}
}
// this will handle all internal functions which may be required/necessary to expand DVM functionality
func (dvm *DVM_Interpreter) Handle_Internal_Function(expr *ast.CallExpr, func_name string) (handled bool, result interface{}) {
if func_data_array, ok := func_table[strings.ToLower(func_name)]; ok {
for _, f := range func_data_array {
if f.Range(dvm.Version) {
dvm.State.ConsumeGas(f.ComputeCost)
if f.PtrU != nil {
return f.PtrU(dvm, expr)
} else if f.PtrS != nil {
return f.PtrS(dvm, expr)
} else {
return f.Ptr(dvm, expr)
}
}
}
panic("function doesnot match any version")
}
//panic("function does not exist")
return false, nil // function does not exist
}
// the load/store functions are sandboxed and thus cannot affect any other SC storage
// loads a variable from store
func (dvm *DVM_Interpreter) Load(key Variable) interface{} {
var found uint64
result := dvm.State.Store.Load(DataKey{SCID: dvm.State.Chain_inputs.SCID, Key: key}, &found)
switch result.Type {
case Uint64:
return result.ValueUint64
case String:
return result.ValueString
default:
panic("Unhandled data_type")
}
}
// whether a variable exists in store or not
func (dvm *DVM_Interpreter) Exists(key Variable) uint64 {
var found uint64
dvm.State.Store.Load(DataKey{SCID: dvm.State.Chain_inputs.SCID, Key: key}, &found)
return found
}
func (dvm *DVM_Interpreter) Store(key Variable, value Variable) {
dvm.State.Store.Store(DataKey{SCID: dvm.State.Chain_inputs.SCID, Key: key}, value)
}
func (dvm *DVM_Interpreter) Delete(key Variable) {
dvm.State.Store.Delete(DataKey{SCID: dvm.State.Chain_inputs.SCID, Key: key})
}
// we should migrate to generics ASAP
func convertdatatovariable(datai interface{}) Variable {
switch k := datai.(type) {
case uint64:
return Variable{Type: Uint64, ValueUint64: k}
case string:
return Variable{Type: String, ValueString: k}
default:
panic("This variable cannot be loaded")
}
}
// checks whether necessary number of arguments have been provided
func checkargscount(expected, actual int) {
if expected != actual {
panic("incorrect number of arguments")
}
}
func dvm_version(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result uint64) {
checkargscount(1, len(expr.Args)) // check number of arguments
if version_str, ok := dvm.eval(expr.Args[0]).(string); !ok {
panic("unsupported version format")
} else {
dvm.Version = semver.MustParse(version_str)
}
return true, uint64(1)
}
func dvm_load(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result interface{}) {
checkargscount(1, len(expr.Args)) // check number of arguments
key := dvm.eval(expr.Args[0])
return true, dvm.Load(convertdatatovariable(key))
}
func dvm_exists(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result uint64) {
checkargscount(1, len(expr.Args)) // check number of arguments
key := dvm.eval(expr.Args[0]) // evaluate the argument and use the result
return true, dvm.Exists(convertdatatovariable(key))
}
func dvm_store(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result uint64) {
checkargscount(2, len(expr.Args)) // check number of arguments
key := convertdatatovariable(dvm.eval(expr.Args[0]))
value := convertdatatovariable(dvm.eval(expr.Args[1]))
dvm.Store(key, value)
return true, 1
}
func dvm_delete(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result uint64) {
checkargscount(1, len(expr.Args)) // check number of arguments
key := convertdatatovariable(dvm.eval(expr.Args[0]))
dvm.Delete(key)
return true, uint64(1)
}
func dvm_mapexists(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result uint64) {
checkargscount(1, len(expr.Args)) // check number of arguments
key := convertdatatovariable(dvm.eval(expr.Args[0])) // evaluate the argument and use the result
if _, ok := dvm.State.RamStore[key]; ok {
return true, uint64(1)
}
return true, uint64(0)
}
func dvm_mapget(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result interface{}) {
checkargscount(1, len(expr.Args)) // check number of arguments
key := convertdatatovariable(dvm.eval(expr.Args[0])) // evaluate the argument and use the result
v := dvm.State.RamStore[key]
if v.Type == Uint64 {
return true, v.ValueUint64
} else if v.Type == String {
return true, v.ValueString
} else {
panic("This variable cannot be obtained")
}
}
func dvm_mapstore(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result uint64) {
checkargscount(2, len(expr.Args)) // check number of arguments
key := convertdatatovariable(dvm.eval(expr.Args[0])) // evaluate the argument and use the result
value := convertdatatovariable(dvm.eval(expr.Args[1])) // evaluate the argument and use the result
dvm.State.RamStore[key] = value
return true, uint64(1)
}
func dvm_mapdelete(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result uint64) {
checkargscount(1, len(expr.Args)) // check number of arguments
key := convertdatatovariable(dvm.eval(expr.Args[0])) // evaluate the argument and use the result
delete(dvm.State.RamStore, key)
return true, uint64(1)
}
func dvm_random(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result uint64) {
if len(expr.Args) >= 2 {
panic("RANDOM function expects 0 or 1 number as parameter")
}
if len(expr.Args) == 0 { // expression without limit
return true, dvm.State.RND.Random()
}
range_eval := dvm.eval(expr.Args[0])
switch k := range_eval.(type) {
case uint64:
return true, dvm.State.RND.Random_MAX(k)
default:
panic("This variable cannot be randomly generated")
}
}
func dvm_scid(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result string) {
checkargscount(0, len(expr.Args)) // check number of arguments
return true, string(dvm.State.Chain_inputs.SCID[:])
}
func dvm_blid(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result string) {
checkargscount(0, len(expr.Args)) // check number of arguments
return true, string(dvm.State.Chain_inputs.BLID[:])
}
func dvm_txid(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result string) {
checkargscount(0, len(expr.Args)) // check number of arguments
return true, string(dvm.State.Chain_inputs.TXID[:])
}
func dvm_dero(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result string) {
checkargscount(0, len(expr.Args)) // check number of arguments
var zerohash crypto.Hash
return true, string(zerohash[:])
}
func dvm_block_height(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result uint64) {
checkargscount(0, len(expr.Args)) // check number of arguments
return true, dvm.State.Chain_inputs.BL_HEIGHT
}
/*
func dvm_block_topoheight(dvm *DVM_Interpreter, expr *ast.CallExpr)(handled bool, result interface{}){
if len(expr.Args) != 0 {
panic("BLOCK_HEIGHT function expects 0 parameters")
}
return true, dvm.State.Chain_inputs.BL_TOPOHEIGHT
}
*/
func dvm_block_timestamp(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result uint64) {
checkargscount(0, len(expr.Args)) // check number of arguments
return true, dvm.State.Chain_inputs.BL_TIMESTAMP
}
func dvm_signer(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result string) {
checkargscount(0, len(expr.Args)) // check number of arguments
return true, dvm.State.Chain_inputs.Signer
}
func dvm_update_sc_code(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result uint64) {
checkargscount(1, len(expr.Args)) // check number of arguments
code_eval := dvm.eval(expr.Args[0])
switch k := code_eval.(type) {
case string:
dvm.State.Store.Store(DataKey{Key: Variable{Type: String, ValueString: "C"}}, Variable{Type: String, ValueString: k}) // TODO verify code authenticity how
return true, uint64(1)
default:
return true, uint64(0)
}
}
func dvm_is_address_valid(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result uint64) {
checkargscount(1, len(expr.Args)) // check number of arguments
addr_eval := dvm.eval(expr.Args[0])
switch k := addr_eval.(type) {
case string:
addr_raw := new(crypto.Point)
if err := addr_raw.DecodeCompressed([]byte(k)); err == nil {
return true, uint64(1)
}
return true, uint64(0) // fallthrough not supported in type switch
default:
return true, uint64(0)
}
}
func dvm_address_raw(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result string) {
checkargscount(1, len(expr.Args)) // check number of arguments
addr_eval := dvm.eval(expr.Args[0])
switch k := addr_eval.(type) {
case string:
if addr, err := rpc.NewAddress(k); err == nil {
return true, string(addr.Compressed())
}
return true, "" // fallthrough not supported in type switch
default:
return true, ""
}
}
func dvm_address_string(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result string) {
checkargscount(1, len(expr.Args)) // check number of arguments
addr_eval := dvm.eval(expr.Args[0])
switch k := addr_eval.(type) {
case string:
p := new(crypto.Point)
if err := p.DecodeCompressed([]byte(k)); err == nil {
addr := rpc.NewAddressFromKeys(p)
return true, addr.String()
}
return true, "" // fallthrough not supported in type switch
default:
return true, ""
}
}
func dvm_send_dero_to_address(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result uint64) {
checkargscount(2, len(expr.Args)) // check number of arguments
addr_eval := dvm.eval(expr.Args[0])
amount_eval := dvm.eval(expr.Args[1])
if err := new(crypto.Point).DecodeCompressed([]byte(addr_eval.(string))); err != nil {
panic("address must be valid DERO network address")
}
if _, ok := amount_eval.(uint64); !ok {
panic("amount must be valid uint64")
}
if amount_eval.(uint64) == 0 {
return true, amount_eval.(uint64)
}
var zerohash crypto.Hash
dvm.State.Store.SendExternal(dvm.State.Chain_inputs.SCID, zerohash, addr_eval.(string), amount_eval.(uint64)) // add record for external transfer
return true, amount_eval.(uint64)
}
func dvm_send_asset_to_address(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result uint64) {
checkargscount(3, len(expr.Args)) // check number of arguments
addr_eval := dvm.eval(expr.Args[0])
amount_eval := dvm.eval(expr.Args[1])
asset_eval := dvm.eval(expr.Args[2])
if err := new(crypto.Point).DecodeCompressed([]byte(addr_eval.(string))); err != nil {
panic("address must be valid DERO network address")
}
if _, ok := amount_eval.(uint64); !ok {
panic("amount must be valid uint64")
}
if _, ok := asset_eval.(string); !ok {
panic("asset must be valid string")
}
//fmt.Printf("sending asset %x (%d) to address %x\n", asset_eval.(string), amount_eval.(uint64),[]byte(addr_eval.(string)))
if amount_eval.(uint64) == 0 {
return true, amount_eval.(uint64)
}
if len(asset_eval.(string)) != 32 {
panic("asset must be valid string of 32 byte length")
}
var asset crypto.Hash
copy(asset[:], ([]byte(asset_eval.(string))))
dvm.State.Store.SendExternal(dvm.State.Chain_inputs.SCID, asset, addr_eval.(string), amount_eval.(uint64)) // add record for external transfer
return true, amount_eval.(uint64)
}
func dvm_derovalue(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result uint64) {
checkargscount(0, len(expr.Args)) // check number of arguments
return true, dvm.State.Assets[dvm.State.SCIDZERO]
}
func dvm_assetvalue(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result uint64) {
checkargscount(1, len(expr.Args)) // check number of arguments
asset_eval := dvm.eval(expr.Args[0])
if _, ok := asset_eval.(string); !ok {
panic("asset must be valid string")
}
if len(asset_eval.(string)) != 32 {
panic("asset must be valid string of 32 byte length")
}
var asset crypto.Hash
copy(asset[:], ([]byte(asset_eval.(string))))
return true, dvm.State.Assets[asset]
}
func dvm_itoa(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result string) {
checkargscount(1, len(expr.Args)) // check number of arguments
asset_eval := dvm.eval(expr.Args[0])
if _, ok := asset_eval.(uint64); !ok {
panic("itoa argument must be valid uint64")
}
return true, fmt.Sprintf("%d", asset_eval.(uint64))
}
func dvm_atoi(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result uint64) {
checkargscount(1, len(expr.Args)) // check number of arguments
asset_eval := dvm.eval(expr.Args[0])
if _, ok := asset_eval.(string); !ok {
panic("atoi argument must be valid string")
}
if u, err := strconv.ParseUint(asset_eval.(string), 10, 64); err != nil {
panic(err)
} else {
return true, u
}
}
func dvm_strlen(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result uint64) {
checkargscount(1, len(expr.Args)) // check number of arguments
asset_eval := dvm.eval(expr.Args[0])
if _, ok := asset_eval.(string); !ok {
panic("atoi argument must be valid string")
}
return true, uint64(len([]byte(asset_eval.(string))))
}
func substr(input string, start uint64, length uint64) string {
asbytes := []byte(input)
if start >= uint64(len(asbytes)) {
return ""
}
if start+length > uint64(len(asbytes)) {
length = uint64(len(asbytes)) - start
}
return string(asbytes[start : start+length])
}
func dvm_substr(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result string) {
checkargscount(3, len(expr.Args)) // check number of arguments
input_eval := dvm.eval(expr.Args[0])
if _, ok := input_eval.(string); !ok {
panic("input argument must be valid string")
}
offset_eval := dvm.eval(expr.Args[1])
if _, ok := offset_eval.(uint64); !ok {
panic("input argument must be valid uint64")
}
length_eval := dvm.eval(expr.Args[2])
if _, ok := length_eval.(uint64); !ok {
panic("input argument must be valid uint64")
}
return true, substr(input_eval.(string), offset_eval.(uint64), length_eval.(uint64))
}
func dvm_sha256(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result string) {
checkargscount(1, len(expr.Args)) // check number of arguments
input_eval := dvm.eval(expr.Args[0])
if _, ok := input_eval.(string); !ok {
panic("input argument must be valid string")
}
hash := sha256.Sum256([]byte(input_eval.(string)))
return true, string(hash[:])
}
func dvm_sha3256(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result string) {
checkargscount(1, len(expr.Args)) // check number of arguments
input_eval := dvm.eval(expr.Args[0])
if _, ok := input_eval.(string); !ok {
panic("input argument must be valid string")
}
hash := sha3.Sum256([]byte(input_eval.(string)))
return true, string(hash[:])
}
func dvm_keccak256(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result string) {
checkargscount(1, len(expr.Args)) // check number of arguments
input_eval := dvm.eval(expr.Args[0])
if _, ok := input_eval.(string); !ok {
panic("input argument must be valid string")
}
h1 := sha3.NewLegacyKeccak256()
h1.Write([]byte(input_eval.(string)))
hash := h1.Sum(nil)
return true, string(hash[:])
}
func dvm_hex(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result string) {
checkargscount(1, len(expr.Args)) // check number of arguments
input_eval := dvm.eval(expr.Args[0])
if _, ok := input_eval.(string); !ok {
panic("input argument must be valid string")
}
return true, hex.EncodeToString([]byte(input_eval.(string)))
}
func dvm_hexdecode(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result string) {
checkargscount(1, len(expr.Args)) // check number of arguments
input_eval := dvm.eval(expr.Args[0])
if _, ok := input_eval.(string); !ok {
panic("input argument must be valid string")
}
if b, err := hex.DecodeString(input_eval.(string)); err != nil {
panic(err)
} else {
return true, string(b)
}
}
func dvm_min(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result uint64) {
checkargscount(2, len(expr.Args)) // check number of arguments
a1 := dvm.eval(expr.Args[0])
if _, ok := a1.(uint64); !ok {
panic("input argument must be uint64")
}
a2 := dvm.eval(expr.Args[1])
if _, ok := a1.(uint64); !ok {
panic("input argument must be uint64")
}
if a1.(uint64) < a2.(uint64) {
return true, a1.(uint64)
}
return true, a2.(uint64)
}
func dvm_max(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result uint64) {
checkargscount(2, len(expr.Args)) // check number of arguments
a1 := dvm.eval(expr.Args[0])
if _, ok := a1.(uint64); !ok {
panic("input argument must be uint64")
}
a2 := dvm.eval(expr.Args[1])
if _, ok := a1.(uint64); !ok {
panic("input argument must be uint64")
}
if a1.(uint64) > a2.(uint64) {
return true, a1.(uint64)
}
return true, a2.(uint64)
}
func dvm_panic(dvm *DVM_Interpreter, expr *ast.CallExpr) (handled bool, result uint64) {
panic("panic function called")
return true, uint64(0)
}