npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nois

v2.0.0

Published

The Nois standard library

Downloads

8

Readme

Nois standard library

nois on crates.io nois on docs.rs

Use this library to integrate your app with the nois proxy.

Storing the proxy address

#[entry_point]
pub fn instantiate(
    deps: DepsMut,
    _env: Env,
    _info: MessageInfo,
    msg: InstantiateMsg,
) -> Result<Response, ContractError> {
    let nois_proxy_addr = deps
        .api
        .addr_validate(&msg.nois_proxy)
        .map_err(|_| ContractError::InvalidProxyAddress)?;
    NOIS_PROXY.save(deps.storage, &nois_proxy_addr)?;
    Ok(Response::new()
        .add_attribute("action", "instantiate")
        .add_attribute("nois_proxy", msg.nois_proxy))
}

Sending a request

use nois::ProxyExecuteMsg;

pub fn execute_estimate_pi(
    deps: DepsMut,
    _env: Env,
    _info: MessageInfo,
    job_id: String,
) -> Result<Response, ContractError> {
    let nois_proxy = NOIS_PROXY.load(deps.storage)?; // Nois proxy address stored in init

    let res = Response::new().add_message(WasmMsg::Execute {
        contract_addr: nois_proxy.into(),
        msg: to_binary(&ProxyExecuteMsg::GetNextRandomness { job_id })?,
        funds: vec![],
    });
    Ok(res)
}

Processing the callback

Create a ExecuteMsg enum case called Receive

use cosmwasm_schema::{cw_serde, QueryResponses};

use nois::NoisCallback;

#[cw_serde]
pub enum ExecuteMsg {
    // ...

    NoisReceive {
        callback: NoisCallback,
    },
}

and use it:

#[entry_point]
pub fn execute(
    deps: DepsMut,
    env: Env,
    info: MessageInfo,
    msg: ExecuteMsg,
) -> Result<Response, ContractError> {
    match msg {
        // ...

        ExecuteMsg::NoisReceive { callback } => execute_nois_receive(deps, env, info, callback),
    }
}

// ...

pub fn execute_nois_receive(
    deps: DepsMut,
    _env: Env,
    _info: MessageInfo,
    callback: NoisCallback,
) -> Result<Response, ContractError> {
    let proxy = NOIS_PROXY.load(deps.storage)?;
    ensure_eq!(info.sender, proxy, ContractError::UnauthorizedReceive);

    let NoisCallback { job_id, randomness, .. } = callback;
    let randomness: [u8; 32] = randomness
        .to_array()
        .map_err(|_| ContractError::InvalidRandomness)?;

    // use randomness 🎉
}

Build for JavaScript

The Nois Toolbox can be compiled to JavaScript via WebAssembly. This way you can simulate the outputs for every randomness value. The results match exactly those of CosmWasm contracts using the same tools.

In order to keep the JS/Wasm interface simple, there is a wrapper in the module lib/js which takes randomness inputs in hex format and uses types and error handling that plays well with JS. JS/Wasm bindings are created using wasm-bindgen.

The JS does not match 100% the contract implementation. The differences are documented here.

| Contract function | JS function | Status | Note | | ------------------------------ | ---------------------- | -------- | -------------------------------------------------------------------- | | nois::coinflip | coinflip | ✅ Ready | Returns string instead of enum | | nois::roll_dice | roll_dice | ✅ Ready | | | nois::int_in_range | int_in_range | ✅ Ready | Only supports safe integer range | | nois::ints_in_range | ints_in_range | ✅ Ready | Only supports safe integer range | | nois::pick | pick | ✅ Ready | | | nois::select_from_weighted | select_from_weighted | ✅ Ready | | | nois::random_decimal | random_decimal | ✅ Ready | Encodes result Decimal as string | | nois::sub_randomness | sub_randomness | ✅ Ready | Takes a count argument and returns an Array instead of an iterator | | nois::shuffle | shuffle | ✅ Ready | |

Installation

We need this:

$ cargo install wasm-pack -f
$ wasm-pack --version
wasm-pack 0.10.3

For Node.js

This creates a CommonJS module that is loaded synchonously.

$ wasm-pack build --target nodejs -- --features js
$ node
> const { coinflip, roll_dice, random_decimal, sub_randomness, int_in_range, ints_in_range, pick, select_from_weighted, shuffle } = require('./pkg/nois');

// Round 2497992

> coinflip("c59f098f3c12b8c36ed81f5c17660c72414a1ed63467888a374af529a205c584")
'tails'

// Round 2497994

> coinflip("2267ba7356c01a58e405d4194a31bddc3fd3eb1f0a86758f7a609ba8a47420ba")
'heads'
> roll_dice("2267ba7356c01a58e405d4194a31bddc3fd3eb1f0a86758f7a609ba8a47420ba")
6
> random_decimal("2267ba7356c01a58e405d4194a31bddc3fd3eb1f0a86758f7a609ba8a47420ba")
'0.126047856387596389'
> sub_randomness("2267ba7356c01a58e405d4194a31bddc3fd3eb1f0a86758f7a609ba8a47420ba", 3)
[
  'ac7b151d67cd4263520b16e450e6d1fc01750dab80b5d8b7cdc4075c99daf80a',
  '33622b0865f1ab35142e3e63a91c25cf89311b04b9540ca15e49413a4a114ce1',
  'f08927af18d4995c28f15f07e4038407f32d966087771314b9e64b6a33a9101c'
]
> int_in_range("2267ba7356c01a58e405d4194a31bddc3fd3eb1f0a86758f7a609ba8a47420ba", 5, 9)
9
> ints_in_range("2267ba7356c01a58e405d4194a31bddc3fd3eb1f0a86758f7a609ba8a47420ba", 20, 0, 1)
[
  1, 1, 1, 0, 0, 1, 1,
  0, 0, 1, 1, 1, 1, 1,
  0, 0, 0, 0, 0, 1
]
> shuffle("2267ba7356c01a58e405d4194a31bddc3fd3eb1f0a86758f7a609ba8a47420ba", [1, 2, 3, "a", "b", true])
[ 2, 'a', 1, 'b', 3, true ]
> pick("2267ba7356c01a58e405d4194a31bddc3fd3eb1f0a86758f7a609ba8a47420ba", 4, [1, 2, 3, "a", "b", true])
[ 'a', 'b', 3, true ]
> select_from_weighted("2267ba7356c01a58e405d4194a31bddc3fd3eb1f0a86758f7a609ba8a47420ba", [["red", 20], ["blue", 70]])
'blue'

For browsers and other JS environments

You need to change the target in order to get a suiteable package. E.g.

$ wasm-pack build --target web -- --features js
$ ls ./pkg

for browsers. Please refer to the wasm-bindgen handbook to learn more about targets.