Skip to content

Commit

Permalink
add sigma-tree wrapping types for WASM bindings;
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat committed Jun 4, 2020
1 parent 04f61e7 commit 185045f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 25 deletions.
67 changes: 49 additions & 18 deletions sigma-tree-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,72 @@

use sigma_tree::chain;

mod misc;
mod utils;

pub use misc::*;

// use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;

/// TODO: wrap sigma-tree type
#[wasm_bindgen]
pub struct Address(String);

#[wasm_bindgen]
impl Address {
/// Decode(base58) address
pub fn from_str(str: String) -> Address {
Address(str)
}
}

/// TODO: wrap sigma-tree type
#[wasm_bindgen]
pub struct PrivateKey(String);

/// TODO: explain wasm_bindgen limitations (supported types)
#[wasm_bindgen]
impl PrivateKey {
/// Decode from string
pub fn from_str(str: String) -> PrivateKey {
PrivateKey(str)
}
}

/// Transaction inputs
#[wasm_bindgen]
pub struct TxInputs(Vec<chain::ErgoBoxCandidate>);

#[wasm_bindgen]
impl TxInputs {
/// parse ErgoBoxCandidate from json
pub fn from_boxes(_boxes: Box<[JsValue]>) -> TxInputs {
// box in boxes.into_iter() {
// let _box: chain::ErgoBoxCandidate = jbox.into_serde().unwrap();
// }
TxInputs(vec![])
}
}

/// TODO: copy docs from ErgoBox
#[wasm_bindgen]
pub struct Transaction(chain::Transaction);

#[wasm_bindgen]
impl Transaction {
/// JSON representation
pub fn to_json(&self) -> Result<JsValue, JsValue> {
JsValue::from_serde(&self.0).map_err(|e| JsValue::from_str(&format!("{}", e)))
}
}

// TODO: explain wasm_bindgen limitations (supported types)
/// TODO: add doc
#[wasm_bindgen]
pub fn signed_p2pk_tx(
inputs: Box<[JsValue]>,
pub fn signed_p2pk_transaction(
_inputs: TxInputs,
_current_height: u32,
_recipient: Address,
_send_change_to: Address,
_sk: PrivateKey,
) -> Result<JsValue, JsValue> {
for jbox in inputs.into_iter() {
let _box: chain::ErgoBoxCandidate = jbox.into_serde().unwrap();
}

) -> Result<Transaction, JsValue> {
// TODO: create and sign a transaction

let tx = chain::Transaction {
inputs: vec![],
data_inputs: vec![],
outputs: vec![],
};
JsValue::from_serde(&tx).map_err(|e| JsValue::from_str(&format!("{}", e)))
todo!()
}
7 changes: 0 additions & 7 deletions sigma-tree-wasm/src/misc.rs

This file was deleted.

0 comments on commit 185045f

Please sign in to comment.