@@ -20,7 +20,10 @@ export class VMInstance {
20
20
public bech32 : BechLib ;
21
21
public debugMsgs : string [ ] = [ ] ;
22
22
23
- constructor ( public backend : IBackend , public readonly gasLimit ?: number | undefined ) {
23
+ constructor (
24
+ public backend : IBackend ,
25
+ public readonly gasLimit ?: number | undefined
26
+ ) {
24
27
this . bech32 = bech32 ;
25
28
}
26
29
@@ -60,13 +63,13 @@ export class VMInstance {
60
63
}
61
64
62
65
public allocate ( size : number ) : Region {
63
- let { allocate, memory} = this . exports ;
66
+ let { allocate, memory } = this . exports ;
64
67
let regPtr = allocate ( size ) ;
65
68
return new Region ( memory , regPtr ) ;
66
69
}
67
70
68
71
public deallocate ( region : Region ) : void {
69
- let { deallocate} = this . exports ;
72
+ let { deallocate } = this . exports ;
70
73
deallocate ( region . ptr ) ;
71
74
}
72
75
@@ -93,41 +96,36 @@ export class VMInstance {
93
96
return region ;
94
97
}
95
98
96
- public allocate_none ( ) : Region {
97
- const none = new Uint8Array ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ) ;
98
- return this . allocate_bytes ( none ) ;
99
- }
100
-
101
99
public instantiate ( env : Env , info : MessageInfo , msg : object ) : Region {
102
- let { instantiate} = this . exports ;
100
+ let { instantiate } = this . exports ;
103
101
let args = [ env , info , msg ] . map ( ( x ) => this . allocate_json ( x ) . ptr ) ;
104
102
let result = instantiate ( ...args ) ;
105
103
return this . region ( result ) ;
106
104
}
107
105
108
106
public execute ( env : Env , info : MessageInfo , msg : object ) : Region {
109
- let { execute} = this . exports ;
107
+ let { execute } = this . exports ;
110
108
let args = [ env , info , msg ] . map ( ( x ) => this . allocate_json ( x ) . ptr ) ;
111
109
let result = execute ( ...args ) ;
112
110
return this . region ( result ) ;
113
111
}
114
112
115
113
public query ( env : Env , msg : object ) : Region {
116
- let { query} = this . exports ;
114
+ let { query } = this . exports ;
117
115
let args = [ env , msg ] . map ( ( x ) => this . allocate_json ( x ) . ptr ) ;
118
116
let result = query ( ...args ) ;
119
117
return this . region ( result ) ;
120
118
}
121
119
122
120
public migrate ( env : Env , msg : object ) : Region {
123
- let { migrate} = this . exports ;
121
+ let { migrate } = this . exports ;
124
122
let args = [ env , msg ] . map ( ( x ) => this . allocate_json ( x ) . ptr ) ;
125
123
let result = migrate ( ...args ) ;
126
124
return this . region ( result ) ;
127
125
}
128
126
129
127
public reply ( env : Env , msg : object ) : Region {
130
- let { reply} = this . exports ;
128
+ let { reply } = this . exports ;
131
129
let args = [ env , msg ] . map ( ( x ) => this . allocate_json ( x ) . ptr ) ;
132
130
let result = reply ( ...args ) ;
133
131
return this . region ( result ) ;
@@ -178,9 +176,9 @@ export class VMInstance {
178
176
}
179
177
180
178
secp256k1_verify (
181
- hash_ptr : number ,
182
- signature_ptr : number ,
183
- pubkey_ptr : number
179
+ hash_ptr : number ,
180
+ signature_ptr : number ,
181
+ pubkey_ptr : number
184
182
) : number {
185
183
let hash = this . region ( hash_ptr ) ;
186
184
let signature = this . region ( signature_ptr ) ;
@@ -189,19 +187,21 @@ export class VMInstance {
189
187
}
190
188
191
189
secp256k1_recover_pubkey (
192
- hash_ptr : number ,
193
- signature_ptr : number ,
194
- recover_param : number
190
+ hash_ptr : number ,
191
+ signature_ptr : number ,
192
+ recover_param : number
195
193
) : bigint {
196
194
let hash = this . region ( hash_ptr ) ;
197
195
let signature = this . region ( signature_ptr ) ;
198
- return BigInt ( this . do_secp256k1_recover_pubkey ( hash , signature , recover_param ) . ptr ) ;
196
+ return BigInt (
197
+ this . do_secp256k1_recover_pubkey ( hash , signature , recover_param ) . ptr
198
+ ) ;
199
199
}
200
200
201
201
ed25519_verify (
202
- message_ptr : number ,
203
- signature_ptr : number ,
204
- pubkey_ptr : number
202
+ message_ptr : number ,
203
+ signature_ptr : number ,
204
+ pubkey_ptr : number
205
205
) : number {
206
206
let message = this . region ( message_ptr ) ;
207
207
let signature = this . region ( signature_ptr ) ;
@@ -210,9 +210,9 @@ export class VMInstance {
210
210
}
211
211
212
212
ed25519_batch_verify (
213
- messages_ptr : number ,
214
- signatures_ptr : number ,
215
- public_keys_ptr : number
213
+ messages_ptr : number ,
214
+ signatures_ptr : number ,
215
+ public_keys_ptr : number
216
216
) : number {
217
217
let messages = this . region ( messages_ptr ) ;
218
218
let signatures = this . region ( signatures_ptr ) ;
@@ -243,12 +243,14 @@ export class VMInstance {
243
243
let value : Uint8Array | null = this . backend . storage . get ( key . data ) ;
244
244
245
245
if ( key . str . length > MAX_LENGTH_DB_KEY ) {
246
- throw new Error ( `Key length ${ key . str . length } exceeds maximum length ${ MAX_LENGTH_DB_KEY } ` ) ;
246
+ throw new Error (
247
+ `Key length ${ key . str . length } exceeds maximum length ${ MAX_LENGTH_DB_KEY } `
248
+ ) ;
247
249
}
248
250
249
251
if ( value === null ) {
250
252
console . warn ( `db_read: key not found: ${ key . str } ` ) ;
251
- return this . allocate_none ( ) ;
253
+ return this . region ( 0 ) ;
252
254
}
253
255
254
256
return this . allocate_bytes ( value ) ;
@@ -272,7 +274,11 @@ export class VMInstance {
272
274
}
273
275
274
276
do_db_scan ( start : Region , end : Region , order : number ) : Region {
275
- const iteratorId : Uint8Array = this . backend . storage . scan ( start . data , end . data , order ) ;
277
+ const iteratorId : Uint8Array = this . backend . storage . scan (
278
+ start . data ,
279
+ end . data ,
280
+ order
281
+ ) ;
276
282
277
283
let region = this . allocate ( iteratorId . length ) ;
278
284
region . write ( iteratorId ) ;
@@ -284,16 +290,17 @@ export class VMInstance {
284
290
const record : Record | null = this . backend . storage . next ( iterator_id . data ) ;
285
291
286
292
if ( record === null ) {
287
- return this . allocate_none ( ) ;
293
+ return this . allocate_bytes ( Uint8Array . from ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ) ) ;
288
294
}
289
295
290
- return this . allocate_bytes ( new Uint8Array (
291
- [
292
- ...record . key ,
293
- ...toByteArray ( record . key . length , 4 ) ,
294
- ...record . value ,
295
- ...toByteArray ( record . value . length , 4 )
296
- ] ) ) ;
296
+ return this . allocate_bytes (
297
+ new Uint8Array ( [
298
+ ...record . key ,
299
+ ...toByteArray ( record . key . length , 4 ) ,
300
+ ...record . value ,
301
+ ...toByteArray ( record . value . length , 4 ) ,
302
+ ] )
303
+ ) ;
297
304
}
298
305
299
306
do_addr_humanize ( source : Region , destination : Region ) : Region {
@@ -333,16 +340,16 @@ export class VMInstance {
333
340
}
334
341
335
342
const canonical = this . bech32 . fromWords (
336
- this . bech32 . decode ( source . str ) . words
343
+ this . bech32 . decode ( source . str ) . words
337
344
) ;
338
345
339
346
if ( canonical . length === 0 ) {
340
347
throw new Error ( 'Invalid address.' ) ;
341
348
}
342
349
343
350
const human = this . bech32 . encode (
344
- this . backend . backend_api . bech32_prefix ,
345
- this . bech32 . toWords ( canonical )
351
+ this . backend . backend_api . bech32_prefix ,
352
+ this . bech32 . toWords ( canonical )
346
353
) ;
347
354
if ( human !== source . str ) {
348
355
throw new Error ( 'Invalid address.' ) ;
@@ -354,9 +361,9 @@ export class VMInstance {
354
361
// Returns 0 on verification success, 1 on verification failure
355
362
do_secp256k1_verify ( hash : Region , signature : Region , pubkey : Region ) : number {
356
363
const isValidSignature = ecdsaVerify (
357
- signature . data ,
358
- hash . data ,
359
- pubkey . data
364
+ signature . data ,
365
+ hash . data ,
366
+ pubkey . data
360
367
) ;
361
368
362
369
if ( isValidSignature ) {
@@ -367,20 +374,25 @@ export class VMInstance {
367
374
}
368
375
369
376
do_secp256k1_recover_pubkey (
370
- msgHash : Region ,
371
- signature : Region ,
372
- recover_param : number
377
+ msgHash : Region ,
378
+ signature : Region ,
379
+ recover_param : number
373
380
) : Region {
374
- const pub = ecdsaRecover ( signature . data , recover_param , msgHash . data , false ) ;
381
+ const pub = ecdsaRecover (
382
+ signature . data ,
383
+ recover_param ,
384
+ msgHash . data ,
385
+ false
386
+ ) ;
375
387
return this . allocate_bytes ( pub ) ;
376
388
}
377
389
378
390
// Verifies a message against a signature with a public key, using the ed25519 EdDSA scheme.
379
391
// Returns 0 on verification success, 1 on verification failure
380
392
do_ed25519_verify (
381
- message : Region ,
382
- signature : Region ,
383
- pubkey : Region
393
+ message : Region ,
394
+ signature : Region ,
395
+ pubkey : Region
384
396
) : number {
385
397
if ( message . length > MAX_LENGTH_ED25519_MESSAGE ) return 1 ;
386
398
if ( signature . length > MAX_LENGTH_ED25519_SIGNATURE ) return 1 ;
@@ -405,34 +417,50 @@ export class VMInstance {
405
417
// using the ed25519 EdDSA scheme.
406
418
// Returns 0 on verification success (all batches verify correctly), 1 on verification failure
407
419
do_ed25519_batch_verify (
408
- messages_ptr : Region ,
409
- signatures_ptr : Region ,
410
- public_keys_ptr : Region
420
+ messages_ptr : Region ,
421
+ signatures_ptr : Region ,
422
+ public_keys_ptr : Region
411
423
) : number {
412
424
let messages = decodeSections ( messages_ptr . data ) ;
413
425
let signatures = decodeSections ( signatures_ptr . data ) ;
414
426
let publicKeys = decodeSections ( public_keys_ptr . data ) ;
415
427
416
- if ( messages . length === signatures . length && messages . length === publicKeys . length ) {
428
+ if (
429
+ messages . length === signatures . length &&
430
+ messages . length === publicKeys . length
431
+ ) {
417
432
// Do nothing, we're good to go
418
- } else if ( messages . length === 1 && signatures . length == publicKeys . length ) {
433
+ } else if (
434
+ messages . length === 1 &&
435
+ signatures . length == publicKeys . length
436
+ ) {
419
437
const repeated = [ ] ;
420
438
for ( let i = 0 ; i < signatures . length ; i ++ ) {
421
439
repeated . push ( ...messages ) ;
422
440
}
423
441
messages = repeated ;
424
- } else if ( publicKeys . length === 1 && messages . length == signatures . length ) {
442
+ } else if (
443
+ publicKeys . length === 1 &&
444
+ messages . length == signatures . length
445
+ ) {
425
446
const repeated = [ ] ;
426
447
for ( let i = 0 ; i < messages . length ; i ++ ) {
427
448
repeated . push ( ...publicKeys ) ;
428
449
}
429
450
publicKeys = repeated ;
430
451
} else {
431
- throw new Error ( 'Lengths of messages, signatures and public keys do not match.' ) ;
452
+ throw new Error (
453
+ 'Lengths of messages, signatures and public keys do not match.'
454
+ ) ;
432
455
}
433
456
434
- if ( messages . length !== signatures . length || messages . length !== publicKeys . length ) {
435
- throw new Error ( 'Lengths of messages, signatures and public keys do not match.' ) ;
457
+ if (
458
+ messages . length !== signatures . length ||
459
+ messages . length !== publicKeys . length
460
+ ) {
461
+ throw new Error (
462
+ 'Lengths of messages, signatures and public keys do not match.'
463
+ ) ;
436
464
}
437
465
438
466
for ( let i = 0 ; i < messages . length ; i ++ ) {
@@ -476,7 +504,9 @@ export class VMInstance {
476
504
}
477
505
}
478
506
479
- function decodeSections ( data : Uint8Array | number [ ] ) : ( number [ ] | Uint8Array ) [ ] {
507
+ function decodeSections (
508
+ data : Uint8Array | number [ ]
509
+ ) : ( number [ ] | Uint8Array ) [ ] {
480
510
let result : ( number [ ] | Uint8Array ) [ ] = [ ] ;
481
511
let remainingLen = data . length ;
482
512
0 commit comments