Skip to content

Commit

Permalink
Make vm serializable (#33)
Browse files Browse the repository at this point in the history
* Make vm serializable

* Fix webpack ts issue

* Support manual workflow triggering

* Bumped version to 0.1.13 [ci-skip]"

Co-authored-by: Luke Wilson <luke.wilson@intelligenthack.com>
  • Loading branch information
lukedawilson and Luke Wilson authored Sep 26, 2022
1 parent e62e217 commit 6ccf7ff
Show file tree
Hide file tree
Showing 7 changed files with 1,344 additions and 1,462 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@terran-one/cosmwasm-vm-js",
"version": "0.1.12",
"version": "0.1.13",
"license": "MIT",
"author": "TerranOne",
"main": "dist/index.js",
Expand Down
8 changes: 8 additions & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { eddsa as ellipticEddsa } from "elliptic";

declare global {
var _eddsa: ellipticEddsa; // we use a global to prevent serialization issues for the calling class
function eddsa(): ellipticEddsa;
}

export {};
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { eddsa } from 'elliptic';

export * from './memory';
export * from './backend';
export * from './instance';
export * from './environment';

global.eddsa = () => global._eddsa || (global._eddsa = new eddsa('ed25519'));
15 changes: 6 additions & 9 deletions src/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import { bech32, BechLib } from 'bech32';
import { Region } from './memory';
import { ecdsaRecover, ecdsaVerify } from 'secp256k1';
import { eddsa } from 'elliptic';
import { IBackend, Record } from './backend';
import { toByteArray } from './helpers/byte-array';

Expand All @@ -16,12 +15,10 @@ export class VMInstance {
public instance?: WebAssembly.Instance;
public backend: IBackend;
public bech32: BechLib;
public eddsa: eddsa;

constructor(backend: IBackend) {
this.backend = backend;
this.bech32 = bech32;
this.eddsa = new eddsa('ed25519');
}

public async build(wasmByteCode: ArrayBuffer) {
Expand Down Expand Up @@ -371,10 +368,10 @@ export class VMInstance {
const sig = Buffer.from(signature.data).toString('hex');
const pub = Buffer.from(pubkey.data).toString('hex');
const msg = Buffer.from(message.data).toString('hex');
const _signature = this.eddsa.makeSignature(sig);
const _pubkey = this.eddsa.keyFromPublic(pub);
const _signature = global.eddsa().makeSignature(sig);
const _pubkey = global.eddsa().keyFromPublic(pub);

const isValidSignature = this.eddsa.verify(msg, _signature, _pubkey);
const isValidSignature = global.eddsa().verify(msg, _signature, _pubkey);

if (isValidSignature) {
return 0;
Expand Down Expand Up @@ -410,10 +407,10 @@ export class VMInstance {
const _sig = Buffer.from(sig).toString('hex');
const _pub = Buffer.from(pub).toString('hex');
const _msg = Buffer.from(msg).toString('hex');
const _signature = this.eddsa.makeSignature(_sig);
const _pubkey = this.eddsa.keyFromPublic(_pub);
const _signature = global.eddsa().makeSignature(_sig);
const _pubkey = global.eddsa().keyFromPublic(_pub);

const isValidSignature = this.eddsa.verify(_msg, _signature, _pubkey);
const isValidSignature = global.eddsa().verify(_msg, _signature, _pubkey);

if (!isValidSignature) {
return 1;
Expand Down
31 changes: 31 additions & 0 deletions test/vm.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { readFileSync } from 'fs';
import { BasicKVIterStorage, VMInstance } from '../src';
import { BasicBackendApi, BasicQuerier, IBackend } from '../src/backend';
import { writeData } from './common/test-vm';
import * as testData from './common/test-data';

const wasmByteCode = readFileSync('testdata/v1.0/cosmwasm_vm_test.wasm');
const backend: IBackend = {
Expand Down Expand Up @@ -64,4 +66,33 @@ describe('CosmWasmVM', () => {
};
expect(region.json).toEqual(actual);
});

it('serializes', async () => {
// Arrange
await vm.build(wasmByteCode);
vm.instantiate(mockEnv, mockInfo, { count: 20 });

// Act
const json = JSON.stringify(vm);

// Assert
expect(json).toBeDefined();
});

it('serializes after edda usage', async () => {
// Arrange
await vm.build(wasmByteCode);
vm.instantiate(mockEnv, mockInfo, { count: 20 });

const hashPtr = writeData(vm, testData.EDDSA_MSG_HEX);
const sigPtr = writeData(vm, testData.EDDSA_SIG_HEX);
const pubkeyPtr = writeData(vm, testData.EDDSA_PUBKEY_HEX);
vm.do_ed25519_verify(hashPtr, sigPtr, pubkeyPtr);

// Act
const json = JSON.stringify(vm);

// Assert
expect(json).toBeDefined();
});
});
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"downlevelIteration": true,
"outDir": "dist",
"rootDir": "src",
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"strictFunctionTypes": true,
Expand Down
Loading

0 comments on commit 6ccf7ff

Please sign in to comment.