p2panda-js
v0.8.1
Published
All the things a panda needs
Downloads
12
Readme
This library provides all tools required to write a client, node or even your
own protocol implementation for the p2panda
network. It is shipped both as
a Rust crate p2panda-rs
with WebAssembly bindings and a NPM package
p2panda-js
with TypeScript definitions running in NodeJS or any modern web
browser.
In the future
p2panda-js
will have full feature parity withp2panda-rs
to be able to write high-level client frameworks or node implementations in TypeScript. Until nowp2panda-js
provides basic methods to create, sign and encode data.
The core p2panda
specification
is fully functional but still under review so please be prepared for breaking API changes until we reachv1.0
. Currently no p2panda implementation has recieved a security audit.
Installation
To install p2panda-js
run:
npm i p2panda-js
Example
import { KeyPair, signAndEncodeEntry, encodeOperation } from 'p2panda-js';
// Id of the schema which describes the data we want to publish. This should
// already be known to the node we are publishing to.
const SCHEMA_ID =
'profile_0020c65567ae37efea293e34a9c7d13f8f2bf23dbdc3b5c7b9ab46293111c48fc78b';
// Generate new Ed25519 key pair.
const keyPair = new KeyPair();
// Add field data to "create" operation and encode it.
const operation = encodeOperation({
schemaId: SCHEMA_ID,
fields: {
username: 'Panda',
},
});
// Create Bamboo entry (append-only log data type) with operation as payload
// and encode it.
const entry = signAndEncodeEntry({ operation }, keyPair);
console.log(entry, operation);
Usage
p2panda-js
runs both in NodeJS and web browsers and comes as a ES, CommonJS
or UMD module. It can easily be integrated into Webpack, Rollup or other tools.
Since p2panda-js
contains WebAssembly code, it is necessary to initialise it
before using the methods in the Browser. This initialisation step is not
required in NodeJS contexts.
To make this step a little bit easier p2panda-js
inlines the WebAssembly code
as a base64 string which gets decoded automatically during initialisation. For
manual initialisation the package also comes with "slim" versions where you
need to provide a path to the ".wasm" file yourself, you can read about this
approach further below.
NodeJS
import { KeyPair } from 'p2panda-js';
const keyPair = new KeyPair();
console.log(keyPair.publicKey());
Browser
To quickly get started you can run p2panda-js
in any modern browser as an ES module like that:
<script type="module">
import { initWebAssembly, KeyPair } from 'https://cdn.jsdelivr.net/npm/[email protected]/lib/esm/index.min.js';
// This only needs to be done once before using all `p2panda-js` methods.
initWebAssembly().then(() => {
const keyPair = new KeyPair();
console.log(keyPair.publicKey());
});
</script>
Or use the "slim" version if you want to provide the ".wasm" file manually:
<script type="module">
import { initWebAssembly, KeyPair } from 'https://cdn.jsdelivr.net/npm/[email protected]/lib/esm-slim/index.min.js';
// Pass external .wasm file manually for smaller file sizes
const wasmFile = 'https://cdn.jsdelivr.net/npm/[email protected]/lib/p2panda.wasm';
initWebAssembly(wasmFile).then(() => {
const keyPair = new KeyPair();
console.log(keyPair.publicKey());
});
</script>
Bundlers
import { initWebAssembly, KeyPair } from 'p2panda-js';
// This only needs to be done once before using all `p2panda-js` methods.
await initWebAssembly();
const keyPair = new KeyPair();
console.log(keyPair.publicKey());
Manually load .wasm
Running p2panda-js
in the browser automatically inlines the WebAssembly
inside the JavaScript file, encoded as a base64 string. While this works for
most developers, it also doubles the size of the imported file. To avoid larger
payloads and decoding times you can load the .wasm
file manually by using a
"slim" version. For this you need to initialise the module by passing the path
to the file into initWebAssembly
:
// Import from `slim` module to manually initialise WebAssembly code
import { initWebAssembly, KeyPair } from 'p2panda-js/slim';
import wasm from 'p2panda-js/p2panda.wasm';
// When running p2panda in the browser, this method needs to run once
// before using all other `p2panda-js` methods
await initWebAssembly(wasm);
const keyPair = new KeyPair();
console.log(keyPair.publicKey());
Development
Dependencies
NodeJS
Rust
wasm-bindgen
wasm-opt
wasm32-unknown-unknown
compilation target forrustup
In order to develop with the current code base p2panda-js
needs to be
compiled from the p2panda-rs
code using wasm-bindgen
. This requires a
working Rust
environment to be setup and wasm-bindgen
to be installed.
wasm-opt
is only required to optimize the WebAssembly builds for production
via npm run build
. You can then run the following commands, the compilation
occurs during the testing and build phases:
# Install dependencies
npm install
# Check code formatting
npm run lint
# Run tests, requires `wasm-bindgen`
npm test
# Compile wasm and bundle js package, requires `wasm-bindgen` and `wasm-opt`
npm run build
Documentation
# Generate documentation
npm run docs
# Show documentation in browser
npx serve ./docs
License
GNU Affero General Public License v3.0 AGPL-3.0-or-later
Supported by
This project has received funding from the European Union’s Horizon 2020 research and innovation programme within the framework of the NGI-POINTER Project funded under grant agreement No 871528 and NGI-ASSURE No 957073