Skip to content
This repository has been archived by the owner on Apr 15, 2024. It is now read-only.

Commit

Permalink
cargo: updated iota-core and bee- deps
Browse files Browse the repository at this point in the history
wasm: added wasm-client cargo feature and enabled corresponding wasm
features in cargo deps

binding/wasm: updated bindings, notably Author and Subscriber now use
Rc<Ref<Api>> objects internally as async methods can only be 'self' and
not '&mut self', see
rustwasm/wasm-bindgen#1858 for discussion.
  • Loading branch information
semenov-vladyslav committed Dec 7, 2020
1 parent 4315c8f commit 69c3c47
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 93 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async = ["iota-streams-app/async", "iota-streams-app-channels/async"]
tangle = ["iota-streams-app/tangle", "iota-streams-app-channels/tangle"]
sync-client = ["iota-streams-app/sync-client", "iota-streams-app-channels/sync-client"]
async-client = ["iota-streams-app/async-client", "iota-streams-app-channels/async-client"]
wasmbind = ["iota-streams-app/wasmbind", "iota-streams-app-channels/wasmbind"]
wasm-client = ["iota-streams-app/wasm-client", "iota-streams-app-channels/wasm-client"]

[dependencies]
iota-streams-core = { version = "0.3.0", path = "iota-streams-core", default-features = false }
Expand Down
4 changes: 1 addition & 3 deletions bindings/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,5 @@ wasm-bindgen-futures = "0.4"
console_error_panic_hook = "0.1.6"

serde = { version = "1.0", features = ["derive"] }
js-sys = "0.3.46"
#chrono = { version = "0.4", default-features = false, features = ["wasmbind"] }

iota-streams = {path = "../../", default-features = false, features = ["tangle", "wasmbind", "async-client"]}#
iota-streams = {path = "../../", default-features = false, features = ["tangle", "wasm-client"]}#
23 changes: 16 additions & 7 deletions bindings/wasm/examples/web.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import("../pkg/index.js").then((streams) => {
import("../pkg/index.js").then(async (streams) => {

let options = new streams.SendTrytesOptions(1, 9, true, 1);
let seed = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
let auth = new streams.Author("https://nodes.devnet.iota.org:443", seed, options, false);

let seed = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
let options = new streams.SendTrytesOptionsW(1, 14, true, 1);
let auth = new streams.AuthorW("https://nodes.devnet.iota.org:443", seed, options, false);
console.log("channel address: ", auth.channel_address());
console.log("multi branching: ", auth.is_multi_branching());
console.log("timestamp: ", auth.timestamp());
console.log("announce: ", auth.auth_send_announce());
let ann_link = await auth.send_announce();
console.log("announced at: ", ann_link);

let options2 = new streams.SendTrytesOptions(1, 9, true, 1);
let seed2 = "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBA";
let sub = new streams.Subscriber("https://nodes.devnet.iota.org:443", seed2, options2, false);

//console.log("sub channel address: ", sub.channel_address());
//await sub.receive_announcement(ann_link);
//console.log("sub channel address: ", sub.channel_address());

auth.free();
//auth.free();
});
99 changes: 58 additions & 41 deletions bindings/wasm/src/author/authorw.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use wasm_bindgen::prelude::*;
//use wasm_bindgen_futures::*;

use crate::types::*;

Expand All @@ -10,9 +11,9 @@ use iota_streams::{
},
app_channels::{
api::tangle::{
Author,
Transport,
Address,
Author as ApiAuthor,
Transport as _,
Address as ApiAddress,
ChannelAddress,
},
},
Expand All @@ -28,75 +29,91 @@ use core::cell::RefCell;
use iota_streams::{
app::transport::{
TransportOptions,
tangle::client::{SendTrytesOptions, Client, },
tangle::client::{Client, },
},
core::prelude::{ String, },
core::prelude::{ String, ToString, },
};

#[wasm_bindgen]
pub struct AuthorW {
author: Author<Rc<RefCell<Client>>>,
pub struct Author {
author: Rc<RefCell<ApiAuthor<Rc<RefCell<Client>>>>>,
}

#[wasm_bindgen]
impl AuthorW {
impl Author {
#[wasm_bindgen(constructor)]
pub fn new(node: String, seed: String, options: SendTrytesOptionsW, multi_branching: bool) -> AuthorW {
pub fn new(node: String, seed: String, options: SendTrytesOptions, multi_branching: bool) -> Author {
let node = "https://nodes.devnet.iota.org:443";
let mut client = Client::new_from_url(&node);
client.set_send_options(options.into());
let transport = Rc::new(RefCell::new(client));

let client = Client::new_from_url(&node);

let mut transport = Rc::new(RefCell::new(client));

transport.set_send_options(SendTrytesOptions {
depth: options.depth,
min_weight_magnitude: options.min_weight_magnitude,
local_pow: options.local_pow,
threads: options.threads,
});

let author = Author::new(&seed, "utf-8", PAYLOAD_BYTES, multi_branching, transport);
let author = Rc::new(RefCell::new(ApiAuthor::new(
&seed, "utf-8", PAYLOAD_BYTES, multi_branching, transport)));
Author { author }
}

AuthorW { author: author }
pub fn channel_address(&self) -> Result<String> {
to_result(self.author.borrow()
.channel_address()
.map(|addr| addr.to_string())
.ok_or("channel not created")
)
}

pub fn timestamp(&self) -> f64 {
js_sys::Date::new_0().value_of()
//chrono::Utc::now().timestamp_millis() as f64
pub fn is_multi_branching(&self) -> Result<bool> {
Ok(self.author.borrow().is_multi_branching())
}

pub fn channel_address(&self) -> Result<String, JsValue> {
let ch_addr = self.author.channel_address().unwrap();
Ok(ch_addr.to_string().to_owned())
pub fn get_public_key(&self) -> Result<String> {
Ok("pk".to_owned())
}

pub fn is_multi_branching(&self) -> Result<bool, JsValue> {
Ok(self.author.is_multi_branching())
#[wasm_bindgen(catch)]
pub async fn send_announce(self) -> Result<String> {
self.author.borrow_mut().send_announce()
.await.map_or_else(
|err| Err(JsValue::from_str(&err.to_string())),
|addr| Ok(addr.to_string().to_owned()))
}

pub fn get_public_key(&self) -> Result<String, JsValue> {
Ok("pk".to_owned())
/*
#[wasm_bindgen(catch)]
pub async fn auth_send_announce2(this: Rc<RefCell<Author>>) -> Result<String, JsValue> {
this.borrow_mut()
.author.send_announce()
.await.map_or_else(
|err| Err(JsValue::from_str(&err.to_string())),
|addr| Ok(addr.to_string().to_owned()))
}
pub fn auth_send_announce(&mut self) -> Result<String, JsValue> {
let announce = self.author.send_announce().unwrap();
Ok(announce.to_string().to_owned())
#[wasm_bindgen(catch)]
pub fn auth_send_announce3(&mut self) -> Promise {
let ann = JsFuture::from(self.author.send_announce());
future_to_promise(async move {
ann.await.map_or_else(
|err| Err(JsValue::from_str(&err.to_string())),
|addr| Ok(addr.to_string().to_owned()))
})
}
*/

pub fn receive_subscribe(&mut self, link_to: AddressW) -> Result<(), JsValue> {
let addr = Address::from_str(&link_to.addr_id(), &link_to.msg_id()).unwrap();
pub fn receive_subscribe(&mut self, link_to: Address) -> Result<()> {
//let addr = link_to.try_into()?;
// Errors on missing functions from iota-core/iota-client/ureq/rustls/ring in the env
//self.author.receive_subscribe(&addr).unwrap();
Ok(())
}

pub fn send_keyload_for_everyone(&mut self, link_to: AddressW) -> Result<String, JsValue> {
/*
pub async fn send_keyload_for_everyone(&mut self, link_to: AddressW) -> Result<String, JsValue> {
let addr = Address::from_str(&link_to.addr_id(), &link_to.msg_id()).unwrap();
let keyload_id = self.author.send_keyload_for_everyone(&addr).unwrap();
Ok(keyload_id.0.to_string().to_owned())
self.author.send_keyload_for_everyone(&addr)
.await.map_or_else(
|err| Err(JsValue::from_str(&err.to_string())),
|addr| Ok(addr.0.to_string().to_owned()))
}
/*
// Keyload
// message_links_t
pub fn send_keyload(&self, link_to: AddressW, psk_ids_t *psk_ids, ke_pks_t ke_pks) -> Result<String, JsValue> {
Expand Down
51 changes: 30 additions & 21 deletions bindings/wasm/src/subscriber/subscriberw.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::convert::TryInto as _;
use wasm_bindgen::prelude::*;

use crate::types::*;
Expand All @@ -9,9 +10,8 @@ use iota_streams::{
},
app_channels::{
api::tangle::{
Subscriber,
Transport,
Address,
Subscriber as ApiSubscriber,
Transport as _,
ChannelAddress,
},
},
Expand All @@ -27,33 +27,42 @@ use core::cell::RefCell;
use iota_streams::{
app::transport::{
TransportOptions,
tangle::client::{SendTrytesOptions, Client, },
tangle::client::{Client, },
},
core::prelude::{ String, },
};

#[wasm_bindgen]
pub struct SubscriberW {
subscriber: Subscriber<Rc<RefCell<Client>>>,
pub struct Subscriber {
subscriber: Rc<RefCell<ApiSubscriber<Rc<RefCell<Client>>>>>,
}

#[wasm_bindgen]
impl SubscriberW {
#[wasm_bindgen(constructor)]
pub fn new(node: String, seed: String, options: SendTrytesOptionsW) -> SubscriberW {
let client = Client::new_from_url(&node);
impl Subscriber {
#[wasm_bindgen(constructor)]
pub fn new(node: String, seed: String, options: SendTrytesOptions) -> Subscriber {
let mut client = Client::new_from_url(&node);
client.set_send_options(options.into());
let transport = Rc::new(RefCell::new(client));

let mut transport = Rc::new(RefCell::new(client));
let subscriber = Rc::new(RefCell::new(ApiSubscriber::new(&seed, "utf-8", PAYLOAD_BYTES, transport)));
Subscriber { subscriber }
}

transport.set_send_options(SendTrytesOptions {
depth: options.depth,
min_weight_magnitude: options.min_weight_magnitude,
local_pow: options.local_pow,
threads: options.threads,
});
pub fn channel_address(&self) -> Result<String> {
to_result(self.subscriber.borrow()
.channel_address()
.map(|addr| addr.to_string())
.ok_or("channel not subscribed")
)
}

let subscriber = Subscriber::new(&seed, "utf-8", PAYLOAD_BYTES, transport);

SubscriberW { subscriber: subscriber }
}
/*
#[wasm_bindgen(catch)]
pub async fn receive_announcement(self, link: String) -> Result<()> {
to_result(self.subscriber.borrow_mut()
.receive_announcement(&ApiAddress::from_str(&link)?)
.await)
}
*/
}
52 changes: 44 additions & 8 deletions bindings/wasm/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,46 @@
use core::convert::TryFrom;
use wasm_bindgen::prelude::*;
use iota_streams::{
app::transport::{
tangle::client::{SendTrytesOptions as ApiSendTrytesOptions, },
},
app_channels::{
api::tangle::{
Address as ApiAddress,
},
},
core::prelude::{ String, ToString, },
};

pub type Result<T> = core::result::Result<T, JsValue>;
pub fn to_result<T, E: ToString>(r: core::result::Result<T, E>) -> Result<T> {
r.map_err(|e| JsValue::from_str(&e.to_string()))
}

#[wasm_bindgen]
pub struct SendTrytesOptionsW {
pub struct SendTrytesOptions {
pub depth: u8,
pub min_weight_magnitude: u8,
pub local_pow: bool,
pub threads: usize,
}

impl From<SendTrytesOptions> for ApiSendTrytesOptions {
fn from(options: SendTrytesOptions) -> Self {
Self {
depth: options.depth,
min_weight_magnitude: options.min_weight_magnitude,
local_pow: options.local_pow,
threads: options.threads,
}
}
}

#[wasm_bindgen]
impl SendTrytesOptionsW {
impl SendTrytesOptions {
#[wasm_bindgen(constructor)]
pub fn new(depth: u8, min_weight_magnitude: u8, local_pow: bool, threads: usize) -> SendTrytesOptionsW {
SendTrytesOptionsW {
pub fn new(depth: u8, min_weight_magnitude: u8, local_pow: bool, threads: usize) -> Self {
Self {
depth: depth,
min_weight_magnitude: min_weight_magnitude,
local_pow: local_pow,
Expand All @@ -22,13 +50,13 @@ impl SendTrytesOptionsW {
}

#[wasm_bindgen]
pub struct AddressW {
pub struct Address {
addr_id: String,
msg_id: String
msg_id: String,
}

#[wasm_bindgen]
impl AddressW {
impl Address {
#[wasm_bindgen(getter)]
pub fn addr_id(&self) -> String {
self.addr_id.clone()
Expand All @@ -48,4 +76,12 @@ impl AddressW {
pub fn set_msg_id(&mut self, msg_id: String) {
self.msg_id = msg_id;
}
}
}

impl TryFrom<Address> for ApiAddress {
type Error = JsValue;
fn try_from(addr: Address) -> Result<Self> {
ApiAddress::from_str(&addr.addr_id, &addr.msg_id)
.map_err(|()| JsValue::from_str("bad address"))
}
}
2 changes: 1 addition & 1 deletion iota-streams-app-channels/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async = ["iota-streams-app/async"]
tangle = ["iota-streams-app/tangle"]
sync-client = ["iota-streams-app/sync-client", "tangle", "std"]
async-client = ["iota-streams-app/async-client", "tangle", "std", "async"]
wasmbind = ["iota-streams-app/wasmbind"]
wasm-client = ["iota-streams-app/wasm-client", "tangle", "std", "async"]

[lib]
name = "iota_streams_app_channels"
Expand Down
11 changes: 6 additions & 5 deletions iota-streams-app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ async = ["async-trait", "atomic_refcell"]
tangle = ["chrono"]
# `iota-client` support is implemented as a feature (as opposed to a separate crate) in order to
# implement Transport for iota_client::Client.
sync-client = ["num_cpus", "smol", "iota-core", "tangle", "std"] #, "iota-constants"
async-client = ["num_cpus", "iota-core", "tangle", "std", "async"] #, "iota-constants"
wasmbind = ["iota-core/surf-wasm", "chrono/wasmbind"]#] #["wasm-timer"]
sync-client = ["num_cpus", "smol", "iota-core", "iota-core/ureq", "tangle", "std"] #, "iota-constants"
async-client = ["num_cpus", "iota-core", "iota-core/ureq", "tangle", "std", "async"] #, "iota-constants"
wasm-client = ["iota-core", "iota-core/surf-wasm", "chrono/wasmbind", "tangle", "async", "std"]

[lib]
name = "iota_streams_app"
Expand All @@ -42,8 +42,9 @@ atomic_refcell = { version = "0.1.6", optional = true }

# Dependencies for "client" feature
# `iota-core` crate is WIP with unstable API atm of writing.
#iota-core = { git = "https://github.com/iotaledger/iota.rs", branch = "iota-1.0", optional = true }
iota-core = { path = "../../iota.rs/iota-core/", optional = true, default-features = false }
# Branch `iota-1.0-surf` contains wasm-compat updates and is not merged atm.
iota-core = { path = "https://github.com/semenov-vladyslav/iota.rs", branch = "iota-1.0-surf", optional = true, default-features = false }
# Use same dependencies as `iota-core`
bee-transaction = { git = "https://github.com/Alex6323/bee-p.git", branch = "master"}#, rev = "02965e78a5cd43d0fb773771470ab3d891066562" }#
bee-crypto = { git = "https://github.com/iotaledger/bee.git", branch = "dev" }

Expand Down
3 changes: 0 additions & 3 deletions iota-streams-app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,3 @@ pub mod message;

/// Transport-related abstractions.
pub mod transport;

#[cfg(not(target_arch = "wasm32"))]
fn error() { "bad target arch" }
Loading

0 comments on commit 69c3c47

Please sign in to comment.