bn.js bindings for Rust & WebAssembly with primitive-types support

Related tags

WebAssembly bn.rs
Overview

bn.rs

bn.js bindings for Rust & WebAssembly with primitive-types support


Write Rust code that uses BN

use std::str::FromStr;

use primitive_types::{H160, U128};
use wasm_bindgen::prelude::*;

use bn_rs::BN;

#[wasm_bindgen]
pub fn sum(a: BN, b: BN) -> Result<BN, JsValue> {
    // `BNError` implements `Into<JsValue>`, so we can use `?` here
    let a = u128::try_from(a)?; // std uints are supported
    let b: U128 = b.try_into()?; // primitive-types uints supported too

    let result = a + b.as_u128();

    Ok(result.into())
}

#[wasm_bindgen]
pub fn is_dead(hash: BN) -> Result<bool, JsValue> {
    let hash = H160::try_from(hash)?; // primitive-types hashes supported too
    let dead = H160::from_str("0x000000000000000000000000000000000000dead").unwrap();

    Ok(hash == dead)
}

Call it from JavaScript

import {sum, is_dead} from './pkg'
import BN from 'bn.js'

// Initialize bn.js numbers
const a = new BN(2, 10)
const b = new BN(2, 10)

// Call Rust code with BN passed and returned
console.log(sum(a, b)) // == new BN(4, 10)
console.log(is_dead(new BN('dead', 'hex'))) // == true

Run example

$ cd example
$ yarn
$ yarn start
You might also like...
WebAssembly serialization/deserialization in rust

parity-wasm Low-level WebAssembly format library. Documentation Rust WebAssembly format serializing/deserializing Add to Cargo.toml [dependencies] par

This is a webpack loader that loads Rust code as a WebAssembly module

rust-native-wasm-loader This is a webpack loader that loads Rust code as a WebAssembly module. It uses the native Rust support for compiling to wasm32

Zaplib is an open-source library for speeding up web applications using Rust and WebAssembly.

⚡ Zaplib Zaplib is an open-source library for speeding up web applications using Rust and WebAssembly. It lets you write high-performance code in Rust

A template for kick starting a Rust and WebAssembly project using wasm-pack.

A template for kick starting a Rust and WebAssembly project using wasm-pack.

Client for integrating private analytics in fast and reliable libraries and apps using Rust and WebAssembly

TelemetryDeck Client Client for integrating private analytics in fast and reliable libraries and apps using Rust and WebAssembly The library provides

A simple Rust and WebAssembly real-time implementation of the Vigénere Cipher utilizing the Sycamore reactive library.
A simple Rust and WebAssembly real-time implementation of the Vigénere Cipher utilizing the Sycamore reactive library.

WebAssembly Vigenère Cipher A simple Rust and WebAssembly real-time implementation of the Vigenère Cipher utilizing the Sycamore reactive library, Tru

A prototype WebAssembly linker using module linking.

WebAssembly Module Linker Please note: this is an experimental project. wasmlink is a prototype WebAssembly module linker that can link together a mod

WebAssembly modules that use Azure services

This is an experimental repository containing WebAssembly modules running on top of WAGI (WebAssembly Gateway Interface, which allows you to run WebAssembly WASI binaries as HTTP handlers) and using Azure services.

WebAssembly Service Porter
WebAssembly Service Porter

WebAssembly Service Porter.

Sealed boxes implementation for Rust/WebAssembly.

Sealed boxes for Rust/WebAssembly This Rust crate provides libsodium sealed boxes for WebAssembly. Usage: // Recipient: create a new key pair let reci

Frank Denis 16 Aug 28, 2022
WebAssembly on Rust is a bright future in making application runs at the Edge or on the Serverless technologies.

WebAssembly Tour WebAssembly on Rust is a bright future in making application runs at the Edge or on the Serverless technologies. We spend a lot of ti

Thang Chung 129 Dec 28, 2022
A console and web-based Gomoku written in Rust and WebAssembly

?? rust-gomoku A console and web-based Gomoku written in Rust and WebAssembly Getting started with cargo & npm Install required program, run # install

namkyu1999 2 Jan 4, 2022
darkforest is a console and web-based Roguelike written in Rust and WebAssembly.

darkforest darkforest is a console and web-based Roguelike written in Rust and WebAssembly. Key Features TBA Quick Start TBA How To Contribute Contrib

Chris Ohk 5 Oct 5, 2021
A Rust ESP stack trace decoder that can also runs in your browser thanks to WebAssembly

ESP Stack Trace Decoder A Rust ESP stack trace decoder that can also runs in your browser thanks to WebAssembly. It is composed of a ⌨️ Rust library,

Maxime BORGES 20 Oct 5, 2022
Simple file sharing with client-side encryption, powered by Rust and WebAssembly

Hako Simple file sharing with client-side encryption, powered by Rust and WebAssembly Not feature-packed, but basic functionalities are just working.

Jaehyeon Park 30 Nov 25, 2022
A handy calculator, based on Rust and WebAssembly.

qubit ?? Visit Website To Use Calculator Example ?? Visit Website To Use Calculator 2 + 2

Abhimanyu Sharma 55 Dec 26, 2022
A simple compile-to-WebAssembly language rewritten in Rust

chasm A very simple compile-to-WebAssembly language You can play with chasm online. This is a rewrite in Rust of the compiler for the language chasm.

null 11 Nov 27, 2022
Stylist is a CSS-in-Rust styling solution for WebAssembly Applications.

Stylist Stylist is a CSS-in-Rust styling solution for WebAssembly Applications. This is a fork of css-in-rust. Install Add the following to your Cargo

Kaede Hoshikawa 190 Dec 30, 2022
Rust WebGL2 wrapper with a focus on making high-performance WebAssembly graphics code easier to write and maintain

Limelight Limelight is a WebGL2 wrapper with a focus on making high-performance WebAssembly graphics code easier to write and maintain. demo.mov live

drifting in space 27 Dec 30, 2022