Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion crypto-wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 5 additions & 10 deletions crypto-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ edition = "2021"
[lib]
crate-type = ["cdylib", "rlib"]

[features]
default = ["console_error_panic_hook"]

[dependencies]
# iroha_crypto = { path = "../.iroha/crates/iroha_crypto", default-features = false, features = ["rand"] }
iroha_crypto = { git = "https://github.com/hyperledger-iroha/iroha.git", rev = "daa2d50fbe288aed1231c6f45c853698435477d6", default-features = false, features = ["rand"] }
Expand All @@ -21,18 +18,16 @@ serde = { version = "1", default-features = false, features = ["derive"] }
serde-wasm-bindgen = "0.6"
hex = { version = "0.4", default-features = false, features = ["alloc"] }

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1", optional = true }

# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
# compared to the default allocator's ~10K. It is slower than the default
# allocator, however.
#
# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now.
wee_alloc = { version = "0.4.5", optional = true }
wee_alloc = { version = "0.4.5" }

[profile.release]
opt-level = "z"
lto = true

[dev-dependencies]
wasm-bindgen-test = "0.3.13"
Expand Down
11 changes: 6 additions & 5 deletions crypto-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@

extern crate alloc;

extern crate wee_alloc;

// Use `wee_alloc` as the global allocator.
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

mod utils;

use alloc::string::ToString;
Expand Down Expand Up @@ -327,8 +333,3 @@ impl TryFrom<Result<(), iroha_crypto::error::Error>> for VerifyResultJs {
Ok(Self { obj: js_value })
}
}

#[wasm_bindgen(start)]
pub fn main_js() {
utils::set_panic_hook();
}
11 changes: 0 additions & 11 deletions crypto-wasm/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,6 @@ use core::fmt::Display;

use wasm_bindgen::prelude::*;

pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}

pub trait JsErrorResultExt<T> {
fn wrap_js_error(self) -> Result<T, JsError>;
}
Expand Down
22 changes: 18 additions & 4 deletions etc/task-prep-crypto-wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ const PREP_DIR = resolveFromRoot('prep/crypto-wasm')
console.log(PROJECT_DIR)
const CRATE_NAME = 'iroha_crypto_wasm'

const GENERATED_FILES = new Set([`.d.ts`, '.internal.js', '.js'].map((x) => `${CRATE_NAME}${x}`))

async function buildWasm() {
const outDir = PREP_DIR
console.log(' ' + colors.yellow(`empty ${pathRel(PREP_DIR)}`))
await emptyDir(PREP_DIR)

$.logStep(`Building with @deno/wasmbuild...`)
await $`deno run -A jsr:@deno/wasmbuild --out ${outDir}`.cwd(PROJECT_DIR)
await $`deno run -A jsr:@deno/wasmbuild@0.19.1 --out ${outDir} --inline`.cwd(PROJECT_DIR)
console.log(` ${colors.yellow(`write ${pathRel(outDir)}`)}`)
}

Expand All @@ -29,27 +31,39 @@ async function checkBuildReady() {
assert(i.isFile)
files.add(i.name)
}
assertEquals(files, new Set([`.d.ts`, '.internal.js', '.js', '.wasm'].map((x) => `${CRATE_NAME}${x}`)))
assertEquals(files, GENERATED_FILES)
return true
} catch (err) {
$.logWarn('Error whiule checking build artifacts:', err)
return false
}
}

function patchWasmJsWrapCode(code: string): string {
return code.replace(/(__wbg_set_wasm\(wasm.exports\))/, '$1\nwasm.exports.__wbindgen_start()')
}

async function patchWasmJsWrap(file: string) {
const content = await Deno.readTextFile(file)
const patched = patchWasmJsWrapCode(content)
await Deno.writeTextFile(file, patched)
}

async function copyOutputs() {
const targetDir = resolveFromRoot('packages/core/crypto/wasm')
const files = [`${CRATE_NAME}.d.ts`, `${CRATE_NAME}.wasm`, `${CRATE_NAME}.internal.js`]

console.log(' ' + colors.yellow(`empty ${pathRel(targetDir)}`))
await emptyDir(targetDir)

for (const i of files) {
for (const i of GENERATED_FILES) {
const src = resolveFromRoot(PREP_DIR, i)
const dest = path.join(targetDir, i)
console.log(` ${colors.yellow(`write ${pathRel(dest)}`)}`)
await copy(src, dest)
}

// https://github.com/denoland/wasmbuild/issues/156
await patchWasmJsWrap(path.join(targetDir, `${CRATE_NAME}.js`))
}

const args = parseArgs(Deno.args, {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/crypto/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import { enumCodec, GenCodec, structCodec } from '../codec.ts'
import * as scale from '@scale-codec/core'
import { assert } from '@std/assert/assert'
import { BytesVec } from '../data-model/primitives.ts'
import * as wasm from './wasm.js'
import * as wasm from './wasm/iroha_crypto_wasm.js'

export { Bytes }
export type VerifyResult = wasm.VerifyResult
Expand Down
2 changes: 1 addition & 1 deletion packages/core/crypto/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { decodeHex, encodeHex } from '@std/encoding/hex'
import type { Bytes as WasmBytes } from './wasm.js'
import type { Bytes as WasmBytes } from './wasm/iroha_crypto_wasm.js'

/**
* Helper to work with binary data passed into the WASM
Expand Down
51 changes: 0 additions & 51 deletions packages/core/crypto/wasm.js

This file was deleted.