Skip to content

Commit 6ccf7ff

Browse files
lukedawilsonLuke Wilson
andauthored
Make vm serializable (#33)
* Make vm serializable * Fix webpack ts issue * Support manual workflow triggering * Bumped version to 0.1.13 [ci-skip]" Co-authored-by: Luke Wilson <[email protected]>
1 parent e62e217 commit 6ccf7ff

File tree

7 files changed

+1344
-1462
lines changed

7 files changed

+1344
-1462
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@terran-one/cosmwasm-vm-js",
3-
"version": "0.1.12",
3+
"version": "0.1.13",
44
"license": "MIT",
55
"author": "TerranOne",
66
"main": "dist/index.js",

src/global.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { eddsa as ellipticEddsa } from "elliptic";
2+
3+
declare global {
4+
var _eddsa: ellipticEddsa; // we use a global to prevent serialization issues for the calling class
5+
function eddsa(): ellipticEddsa;
6+
}
7+
8+
export {};

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
import { eddsa } from 'elliptic';
2+
13
export * from './memory';
24
export * from './backend';
35
export * from './instance';
46
export * from './environment';
7+
8+
global.eddsa = () => global._eddsa || (global._eddsa = new eddsa('ed25519'));

src/instance.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import { bech32, BechLib } from 'bech32';
33
import { Region } from './memory';
44
import { ecdsaRecover, ecdsaVerify } from 'secp256k1';
5-
import { eddsa } from 'elliptic';
65
import { IBackend, Record } from './backend';
76
import { toByteArray } from './helpers/byte-array';
87

@@ -16,12 +15,10 @@ export class VMInstance {
1615
public instance?: WebAssembly.Instance;
1716
public backend: IBackend;
1817
public bech32: BechLib;
19-
public eddsa: eddsa;
2018

2119
constructor(backend: IBackend) {
2220
this.backend = backend;
2321
this.bech32 = bech32;
24-
this.eddsa = new eddsa('ed25519');
2522
}
2623

2724
public async build(wasmByteCode: ArrayBuffer) {
@@ -371,10 +368,10 @@ export class VMInstance {
371368
const sig = Buffer.from(signature.data).toString('hex');
372369
const pub = Buffer.from(pubkey.data).toString('hex');
373370
const msg = Buffer.from(message.data).toString('hex');
374-
const _signature = this.eddsa.makeSignature(sig);
375-
const _pubkey = this.eddsa.keyFromPublic(pub);
371+
const _signature = global.eddsa().makeSignature(sig);
372+
const _pubkey = global.eddsa().keyFromPublic(pub);
376373

377-
const isValidSignature = this.eddsa.verify(msg, _signature, _pubkey);
374+
const isValidSignature = global.eddsa().verify(msg, _signature, _pubkey);
378375

379376
if (isValidSignature) {
380377
return 0;
@@ -410,10 +407,10 @@ export class VMInstance {
410407
const _sig = Buffer.from(sig).toString('hex');
411408
const _pub = Buffer.from(pub).toString('hex');
412409
const _msg = Buffer.from(msg).toString('hex');
413-
const _signature = this.eddsa.makeSignature(_sig);
414-
const _pubkey = this.eddsa.keyFromPublic(_pub);
410+
const _signature = global.eddsa().makeSignature(_sig);
411+
const _pubkey = global.eddsa().keyFromPublic(_pub);
415412

416-
const isValidSignature = this.eddsa.verify(_msg, _signature, _pubkey);
413+
const isValidSignature = global.eddsa().verify(_msg, _signature, _pubkey);
417414

418415
if (!isValidSignature) {
419416
return 1;

test/vm.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { readFileSync } from 'fs';
22
import { BasicKVIterStorage, VMInstance } from '../src';
33
import { BasicBackendApi, BasicQuerier, IBackend } from '../src/backend';
4+
import { writeData } from './common/test-vm';
5+
import * as testData from './common/test-data';
46

57
const wasmByteCode = readFileSync('testdata/v1.0/cosmwasm_vm_test.wasm');
68
const backend: IBackend = {
@@ -64,4 +66,33 @@ describe('CosmWasmVM', () => {
6466
};
6567
expect(region.json).toEqual(actual);
6668
});
69+
70+
it('serializes', async () => {
71+
// Arrange
72+
await vm.build(wasmByteCode);
73+
vm.instantiate(mockEnv, mockInfo, { count: 20 });
74+
75+
// Act
76+
const json = JSON.stringify(vm);
77+
78+
// Assert
79+
expect(json).toBeDefined();
80+
});
81+
82+
it('serializes after edda usage', async () => {
83+
// Arrange
84+
await vm.build(wasmByteCode);
85+
vm.instantiate(mockEnv, mockInfo, { count: 20 });
86+
87+
const hashPtr = writeData(vm, testData.EDDSA_MSG_HEX);
88+
const sigPtr = writeData(vm, testData.EDDSA_SIG_HEX);
89+
const pubkeyPtr = writeData(vm, testData.EDDSA_PUBKEY_HEX);
90+
vm.do_ed25519_verify(hashPtr, sigPtr, pubkeyPtr);
91+
92+
// Act
93+
const json = JSON.stringify(vm);
94+
95+
// Assert
96+
expect(json).toBeDefined();
97+
});
6798
});

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"downlevelIteration": true,
3131
"outDir": "dist",
3232
"rootDir": "src",
33+
"skipLibCheck": true,
3334
"sourceMap": true,
3435
"strict": true,
3536
"strictFunctionTypes": true,

0 commit comments

Comments
 (0)