Skip to content

Commit

Permalink
Rename wasm module, write readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
pietgeursen committed Jan 15, 2021
1 parent 7c023b7 commit 8275f62
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 120 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ members = [
"bamboo-rs-core-test",
"bamboo-rs-cli",
"generate-test-vectors",
"bamboo-rs-wasm"
"bamboo-wasm"
]

exclude = [
Expand Down
114 changes: 0 additions & 114 deletions bamboo-rs-wasm/README.md

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions bamboo-rs-wasm/Cargo.toml → bamboo-wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bamboo-rs-wasm"
version = "0.5.0"
name = "bamboo-wasm"
version = "0.5.1"
authors = ["Piet Geursen <[email protected]>"]
edition = "2018"

Expand Down
File renamed without changes.
98 changes: 98 additions & 0 deletions bamboo-wasm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# @bamboo-logs/bamboo-wasm

`publish`, `verify` and `decode` bamboo entries from javascript / typescript.

## About

This is a low level module. It's unlikely you want this. See [@bamboo-logs/bamboo-log](https://www.npmjs.com/package/@bamboo-logs/bamboo-log) for a module that wraps these functions and provides an easier to use API.

## Example

Publish and verify our first entry!

```js
const {publish, verify, decode, KeyPair} = require('@bamboo-logs/bamboo-wasm')

// Create a new cryptographic keypair for signing our entries
const keyPair = new KeyPair()

// The payload for our first entry
const payload = Buffer.from("Hello World!")

// The bamboo log id that we'll publish to. See the bamboo spec for more.
// Note that logId is a BigInt because it can be up to 2^64 -1.
const logId = 0n

// Is this the entry the last entry for this logId?
const isEndOfFeed = false

// Publish our first entry!
const entryBytes = publish(keyPair.publicKeyBytes(), keyPair.secretKeyBytes(), logId, payload, isEndOfFeed)

// Decode the entry bytes as an `Entry`
const entry = decode(entryBytes)

console.log(entry)

try {
// verify throws (with a useful exception) if the entry is invalid
verify(entryBytes, payload)
console.log("Entry was valid")
}catch(e){ }

```

Outputs:

( Array contents omitted for clarity )

```
{
entryHash: Uint8Array(66) [ ... ],
payloadHash: Uint8Array(66) [ ... ],
lipmaaLinkHash: undefined,
backLinkHash: undefined,
signature: Uint8Array(64) [ ... ],
author: Uint8Array(32) [ ... ],
isEndOfFeed: false,
logId: 0n,
payloadSize: 12n,
sequence: 1n
}
Entry was valid
```

## Api

See the Typescript types in `index.d.ts`

### NPM

[Published on npm](https://www.npmjs.com/package/@bamboo-logs/bamboo-wasm)

### 🛠️ Build with `wasm-pack build`

```
wasm-pack build -t nodejs --scope bamboo-logs --release --out-name index
```

### 🔬 Test in Headless Browsers with `wasm-pack test`

```
wasm-pack test --headless --firefox
```

### 🎁 Publish to NPM with `wasm-pack publish`

```
wasm-pack publish
```

## 🔋 Batteries Included

* [`wasm-bindgen`](https://github.com/rustwasm/wasm-bindgen) for communicating
between WebAssembly and JavaScript.
* [`console_error_panic_hook`](https://github.com/rustwasm/console_error_panic_hook)
for logging panic messages to the developer console.
* [`wee_alloc`](https://github.com/rustwasm/wee_alloc), an allocator optimized
for small code size.
14 changes: 11 additions & 3 deletions bamboo-rs-wasm/src/lib.rs → bamboo-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ pub fn verify(

#[wasm_bindgen]
pub fn publish(
out: &mut [u8],
public_key: &[u8],
secret_key: &[u8],
log_id: u64,
Expand All @@ -147,7 +146,11 @@ pub fn publish(
last_seq_num: Option<u64>,
lipmaa_entry_vec: Option<Vec<u8>>,
backlink_vec: Option<Vec<u8>>,
) -> Result<usize, JsValue> {
) -> Result<Vec<u8>, JsValue> {

let mut out = Vec::with_capacity(bamboo_rs_core::entry::MAX_ENTRY_SIZE);
out.resize(bamboo_rs_core::entry::MAX_ENTRY_SIZE, 0);

let public_key =
PublicKey::from_bytes(public_key).map_err(|e| JsValue::from_str(&e.to_string()))?;

Expand All @@ -160,7 +163,7 @@ pub fn publish(
};

publish_entry(
out,
&mut out[..],
Some(&key_pair),
log_id,
payload,
Expand All @@ -170,6 +173,11 @@ pub fn publish(
backlink_vec.as_ref().map(|vec| vec.as_slice()),
)
.map_err(|err| JsValue::from_str(&err.to_string()))
.map(|entry_size| {
out.truncate(entry_size);
out
})

}

//TODO: keygen.
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 8275f62

Please sign in to comment.