forked from maidsafe/autonomi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request maidsafe#2221 from b-zee/feat-wasm-bindings
feat: WASM bindings
- Loading branch information
Showing
8 changed files
with
470 additions
and
295 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<html> | ||
<head> | ||
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/> | ||
</head> | ||
<body> | ||
<!-- credits: https://rustwasm.github.io/docs/wasm-bindgen/examples/without-a-bundler.html --> | ||
<script type="module"> | ||
import init, { Client, getFundedWallet, logInit } from './pkg/autonomi.js'; | ||
|
||
async function run() { | ||
document.getElementById("btn-run").disabled = true; | ||
const peer_addr = document.getElementById('peer_id').value; | ||
|
||
await init(); | ||
|
||
logInit("sn_networking=debug,autonomi=trace"); | ||
|
||
console.log("connecting..."); | ||
const client = await new Client([peer_addr]); | ||
console.log("connected"); | ||
|
||
console.log("getting wallet..."); | ||
const wallet = getFundedWallet(); | ||
console.log("wallet retrieved"); | ||
|
||
const data = new Uint8Array([1, 2, 3]); | ||
console.log("our data: ", data); | ||
|
||
console.log("calculating cost..."); | ||
let result = await client.dataCost(data, wallet); | ||
console.log("calculated cost: ", result.toString()); | ||
|
||
console.log("putting..."); | ||
const dataAddr = await client.dataPut(data, wallet); | ||
console.log("put done: ", dataAddr.toString()); | ||
|
||
console.log("getting..."); | ||
const data_get = await client.dataGet(dataAddr); | ||
console.log("get done: ", data_get, " (original data: ", data, ")"); | ||
} | ||
|
||
document.getElementById ("btn-run").addEventListener("click", run, false); | ||
</script> | ||
|
||
<label for="peer_id">Peer ID: <input type="text" id="peer_id" /></label> | ||
<button id="btn-run">Run</button> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
use libp2p::Multiaddr; | ||
use wasm_bindgen::prelude::*; | ||
|
||
#[wasm_bindgen] | ||
pub struct Client(super::Client); | ||
|
||
#[wasm_bindgen] | ||
pub struct ChunkAddr(xor_name::XorName); | ||
|
||
#[wasm_bindgen] | ||
pub struct DataAddr(xor_name::XorName); | ||
#[wasm_bindgen] | ||
impl DataAddr { | ||
#[wasm_bindgen(js_name = toString)] | ||
pub fn to_string(&self) -> String { | ||
crate::client::address::addr_to_str(self.0) | ||
} | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub struct AttoTokens(sn_evm::AttoTokens); | ||
#[wasm_bindgen] | ||
impl AttoTokens { | ||
#[wasm_bindgen(js_name = toString)] | ||
pub fn to_string(&self) -> String { | ||
self.0.to_string() | ||
} | ||
} | ||
|
||
#[wasm_bindgen] | ||
impl Client { | ||
#[wasm_bindgen(constructor)] | ||
pub async fn connect(peers: Vec<String>) -> Result<Client, JsError> { | ||
let peers = peers | ||
.into_iter() | ||
.map(|peer| peer.parse()) | ||
.collect::<Result<Vec<Multiaddr>, _>>()?; | ||
|
||
let client = super::Client::connect(&peers).await?; | ||
|
||
Ok(Client(client)) | ||
} | ||
|
||
#[wasm_bindgen(js_name = chunkPut)] | ||
pub async fn chunk_put(&self, _data: Vec<u8>, _wallet: Wallet) -> Result<ChunkAddr, JsError> { | ||
async { unimplemented!() }.await | ||
} | ||
|
||
#[wasm_bindgen(js_name = chunkGet)] | ||
pub async fn chunk_get(&self, addr: ChunkAddr) -> Result<Vec<u8>, JsError> { | ||
let chunk = self.0.chunk_get(addr.0).await?; | ||
Ok(chunk.value().to_vec()) | ||
} | ||
|
||
#[wasm_bindgen(js_name = dataPut)] | ||
pub async fn data_put(&self, data: Vec<u8>, wallet: Wallet) -> Result<DataAddr, JsError> { | ||
let data = crate::Bytes::from(data); | ||
let xorname = self.0.data_put(data, &wallet.0).await?; | ||
Ok(DataAddr(xorname)) | ||
} | ||
|
||
#[wasm_bindgen(js_name = dataGet)] | ||
pub async fn data_get(&self, addr: DataAddr) -> Result<Vec<u8>, JsError> { | ||
let data = self.0.data_get(addr.0).await?; | ||
Ok(data.to_vec()) | ||
} | ||
|
||
#[wasm_bindgen(js_name = dataCost)] | ||
pub async fn data_cost(&self, data: Vec<u8>) -> Result<AttoTokens, JsValue> { | ||
let data = crate::Bytes::from(data); | ||
let cost = self.0.data_cost(data).await.map_err(JsError::from)?; | ||
|
||
Ok(AttoTokens(cost)) | ||
} | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub struct Wallet(evmlib::wallet::Wallet); | ||
|
||
/// Get a funded wallet for testing. This either uses a default private key or the `EVM_PRIVATE_KEY` | ||
/// environment variable that was used during the build process of this library. | ||
#[wasm_bindgen(js_name = getFundedWallet)] | ||
pub fn funded_wallet() -> Wallet { | ||
Wallet(test_utils::evm::get_funded_wallet()) | ||
} | ||
|
||
/// Enable tracing logging in the console. | ||
/// | ||
/// A level could be passed like `trace` or `warn`. Or set for a specific module/crate | ||
/// with `sn_networking=trace,autonomi=info`. | ||
#[wasm_bindgen(js_name = logInit)] | ||
pub fn log_init(directive: String) { | ||
use tracing_subscriber::prelude::*; | ||
|
||
console_error_panic_hook::set_once(); | ||
|
||
let fmt_layer = tracing_subscriber::fmt::layer() | ||
.with_ansi(false) // Only partially supported across browsers | ||
.without_time() // std::time is not available in browsers | ||
.with_writer(tracing_web::MakeWebConsoleWriter::new()); // write events to the console | ||
tracing_subscriber::registry() | ||
.with(fmt_layer) | ||
.with(tracing_subscriber::EnvFilter::new(directive)) | ||
.init(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters