Skip to content

Commit

Permalink
add confirm and pending transactions to wallet-js
Browse files Browse the repository at this point in the history
still thinking if this is a good way of exposing collections, specially
because I don't know how the memory is handled
  • Loading branch information
ecioppettini committed Oct 28, 2020
1 parent 62886f5 commit 125b4e9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
3 changes: 2 additions & 1 deletion bindings/wallet-core/src/lib.rs
Expand Up @@ -12,7 +12,8 @@ pub use self::{
};
pub use ::wallet::Settings;
pub use chain_impl_mockchain::{
fragment::FragmentId,
value::Value,
vote::{Choice, Options},
vote::{Choice, Options, PayloadType},
};
pub use vote::{PayloadTypeConfig, VOTE_PLAN_ID_LENGTH};
1 change: 1 addition & 0 deletions bindings/wallet-js/Cargo.toml
Expand Up @@ -25,6 +25,7 @@ rand_chacha = "0.2.2"
symmetric-cipher = {path = "../../symmetric-cipher"}
wallet-core = {path = "../wallet-core"}
wasm-bindgen = "0.2"
js-sys = "0.3.40"

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
Expand Down
48 changes: 48 additions & 0 deletions bindings/wallet-js/src/lib.rs
@@ -1,8 +1,10 @@
use js_sys::Array;
use rand_chacha::rand_core::SeedableRng;
use rand_chacha::ChaCha20Rng;
use std::convert::TryInto;

use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast as _;

mod utils;

Expand Down Expand Up @@ -51,6 +53,16 @@ impl_public_key!(Ed25519Public, chain_crypto::Ed25519);
#[wasm_bindgen]
pub struct Ed25519Signature(chain_crypto::Signature<Box<[u8]>, chain_crypto::Ed25519>);

#[wasm_bindgen]
pub struct FragmentId(wallet_core::FragmentId);

/// this is used only for giving the Array a type in the typescript generated notation
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(typescript_type = "Array<FragmentId>")]
pub type FragmentIds;
}

#[wasm_bindgen]
impl Wallet {
/// retrieve a wallet from the given mnemonics and password
Expand Down Expand Up @@ -148,6 +160,28 @@ impl Wallet {
)
.map_err(|e| JsValue::from(e.to_string()))
}

/// use this function to confirm a transaction has been properly received
///
/// This function will automatically update the state of the wallet
///
pub fn confirm_transaction(&mut self, fragment: &FragmentId) {
self.0.confirm_transaction(fragment.0);
}

/// get the list of pending transaction ids, which can be used to query
/// the status and then using `confirm_transaction` as needed.
///
pub fn pending_transactions(&self) -> FragmentIds {
self.0
.pending_transactions()
.iter()
.cloned()
.map(FragmentId)
.map(JsValue::from)
.collect::<Array>()
.unchecked_into::<FragmentIds>()
}
}

#[wasm_bindgen]
Expand Down Expand Up @@ -319,3 +353,17 @@ macro_rules! impl_secret_key {
}
};
}

impl FragmentId {
pub fn new_from_bytes(bytes: &[u8]) -> Result<FragmentId, JsValue> {
let array: [u8; std::mem::size_of::<wallet_core::FragmentId>()] = bytes
.try_into()
.map_err(|_| JsValue::from_str("Invalid fragment id"))?;

Ok(FragmentId(array.into()))
}

pub fn to_bytes(&self) -> Vec<u8> {
self.0.as_bytes().to_vec()
}
}

0 comments on commit 125b4e9

Please sign in to comment.