-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make vm serializable #33
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 {}; |
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')); | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
||
|
@@ -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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
public async build(wasmByteCode: ArrayBuffer) { | ||
|
@@ -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; | ||
|
@@ -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; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ | |
"downlevelIteration": true, | ||
"outDir": "dist", | ||
"rootDir": "src", | ||
"skipLibCheck": true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See https://www.typescriptlang.org/tsconfig#skipLibCheck for details.
Otherwise, we get these two errors due to webpack:
|
||
"sourceMap": true, | ||
"strict": true, | ||
"strictFunctionTypes": true, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We want to store this globally (and lazy-initialise it) because constructing it each time is slow - on my year-old macbook, about 500ms.