From b0a2c9e9444162b3453e3722451c87ba190810ff Mon Sep 17 00:00:00 2001
From: Rache Bartmoss <bartmoss@tutanota.com>
Date: Fri, 4 Sep 2020 14:49:54 +0200
Subject: [PATCH] Exposed transaction signing to Wasm, bugfix

---
 Cargo.lock                                    | 28 +++++----------
 morpheus-core-ffi/Cargo.toml                  |  2 +-
 morpheus-core-ffi/src/lib.rs                  |  2 +-
 morpheus-core-wasm/Cargo.toml                 |  4 +--
 morpheus-core-wasm/src/hydra/mod.rs           |  1 +
 morpheus-core-wasm/src/hydra/sign.rs          | 36 +++++++++++++++++++
 morpheus-core-wasm/src/hydra/tx.rs            |  1 +
 morpheus-core/Cargo.toml                      |  2 +-
 morpheus-core/src/crypto/hd/hydra/private.rs  |  2 +-
 morpheus-core/src/hydra/mod.rs                |  2 +-
 .../src/hydra/{crypto.rs => sign.rs}          |  0
 morpheus-core/src/hydra/txtype/hyd_core.rs    |  6 +++-
 morpheus-core/src/hydra/txtype/mod.rs         |  2 +-
 morpheus-sdk/Cargo.toml                       |  2 +-
 14 files changed, 60 insertions(+), 30 deletions(-)
 create mode 100644 morpheus-core-wasm/src/hydra/sign.rs
 rename morpheus-core/src/hydra/{crypto.rs => sign.rs} (100%)

diff --git a/Cargo.lock b/Cargo.lock
index d7af84a..db81908 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -705,8 +705,7 @@ dependencies = [
 
 [[package]]
 name = "iop-keyvault"
-version = "0.0.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
+version = "0.0.4"
 dependencies = [
  "blake2 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "digest 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -729,11 +728,10 @@ dependencies = [
 
 [[package]]
 name = "iop-keyvault-wasm"
-version = "0.0.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
+version = "0.0.4"
 dependencies = [
  "failure 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
- "iop-keyvault 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "iop-keyvault 0.0.4",
  "log 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)",
  "serde_json 1.0.56 (registry+https://github.com/rust-lang/crates.io-index)",
  "wasm-bindgen 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -749,7 +747,7 @@ dependencies = [
  "chrono 0.4.15 (registry+https://github.com/rust-lang/crates.io-index)",
  "failure 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
  "hex 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
- "iop-keyvault 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "iop-keyvault 0.0.4",
  "jni 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "jwt-compact 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "log 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -772,7 +770,7 @@ dependencies = [
  "android_logger 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "chrono 0.4.15 (registry+https://github.com/rust-lang/crates.io-index)",
  "failure 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
- "iop-keyvault 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "iop-keyvault 0.0.4",
  "iop-morpheus-core 0.0.2",
  "jni 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "log 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -785,8 +783,8 @@ name = "iop-morpheus-core-wasm"
 version = "0.0.2"
 dependencies = [
  "failure 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
- "iop-keyvault 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
- "iop-keyvault-wasm 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "iop-keyvault 0.0.4",
+ "iop-keyvault-wasm 0.0.4",
  "iop-morpheus-core 0.0.2",
  "js-sys 0.3.42 (registry+https://github.com/rust-lang/crates.io-index)",
  "log 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -805,7 +803,7 @@ dependencies = [
  "failure 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
  "futures 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
  "hyper 0.13.7 (registry+https://github.com/rust-lang/crates.io-index)",
- "iop-keyvault 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "iop-keyvault 0.0.4",
  "iop-morpheus-core 0.0.2",
  "jni 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
  "log 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -1848,14 +1846,6 @@ dependencies = [
  "synstructure 0.12.4 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
-[[patch.unused]]
-name = "iop-keyvault"
-version = "0.0.4"
-
-[[patch.unused]]
-name = "iop-keyvault-wasm"
-version = "0.0.4"
-
 [metadata]
 "checksum addr2line 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1b6a2d3371669ab3ca9797670853d61402b03d0b4b9ebf33d677dfa720203072"
 "checksum adler 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ee2a4ec343196209d6594e19543ae87a39f96d5534d7174822a3ad825dd6ed7e"
@@ -1944,8 +1934,6 @@ version = "0.0.4"
 "checksum indexmap 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5b88cd59ee5f71fea89a62248fc8f387d44400cefe05ef548466d61ced9029a7"
 "checksum inventory 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "621b50c176968fd3b0bd71f821a28a0ea98db2b5aea966b2fbb8bd1b7d310328"
 "checksum inventory-impl 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "f99a4111304bade76468d05beab3487c226e4fe4c4de1c4e8f006e815762db73"
-"checksum iop-keyvault 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2f83449cc9dbce5907a5f0c8fc79e1a43d23f36b95dec961dd149176162305ba"
-"checksum iop-keyvault-wasm 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "49007a7aea3815650caccfdf44886f5b2e2fdbf673ed1d23b31ac5b7e6331576"
 "checksum iovec 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b2b3ea6ff95e175473f8ffe6a7eb7c00d054240321b84c57051175fe3c1e075e"
 "checksum itoa 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6"
 "checksum jni 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)" = "36bcc950632e48b86da402c5c077590583da5ac0d480103611d5374e7c967a3c"
diff --git a/morpheus-core-ffi/Cargo.toml b/morpheus-core-ffi/Cargo.toml
index f9e9bf3..9a8d33e 100644
--- a/morpheus-core-ffi/Cargo.toml
+++ b/morpheus-core-ffi/Cargo.toml
@@ -13,7 +13,7 @@ crate-type = ["rlib", "cdylib"]
 [dependencies]
 chrono = "*"
 failure = "*"
-iop-keyvault = "0.0.2"
+iop-keyvault = "0.0.4"
 iop-morpheus-core = "0.0.2"
 log = "*"
 serde_json = { version = "*", features = ["preserve_order"] }
diff --git a/morpheus-core-ffi/src/lib.rs b/morpheus-core-ffi/src/lib.rs
index eefe161..a19a0f4 100644
--- a/morpheus-core-ffi/src/lib.rs
+++ b/morpheus-core-ffi/src/lib.rs
@@ -34,7 +34,7 @@ use iop_morpheus_core::{
         sign::*,
     },
     data::{claim::*, did::*, diddoc::*, present::*, validation::*},
-    hydra::{crypto::HydraSigner, transaction::TransactionData},
+    hydra::{sign::HydraSigner, transaction::TransactionData},
 };
 
 use crate::ffi::{convert, *};
diff --git a/morpheus-core-wasm/Cargo.toml b/morpheus-core-wasm/Cargo.toml
index 3ed5860..062d70e 100644
--- a/morpheus-core-wasm/Cargo.toml
+++ b/morpheus-core-wasm/Cargo.toml
@@ -11,8 +11,8 @@ crate-type = ["cdylib", "rlib"]
 
 [dependencies]
 failure = "*"
-iop-keyvault = "0.0.2"
-iop-keyvault-wasm = "0.0.2"
+iop-keyvault = "0.0.4"
+iop-keyvault-wasm = "0.0.4"
 iop-morpheus-core = "0.0.2"
 js-sys = "*"
 log = "*"
diff --git a/morpheus-core-wasm/src/hydra/mod.rs b/morpheus-core-wasm/src/hydra/mod.rs
index c85514a..3065742 100644
--- a/morpheus-core-wasm/src/hydra/mod.rs
+++ b/morpheus-core-wasm/src/hydra/mod.rs
@@ -2,6 +2,7 @@ mod parameters;
 mod plugin;
 mod private;
 mod public;
+mod sign;
 mod tx;
 
 pub use parameters::*;
diff --git a/morpheus-core-wasm/src/hydra/sign.rs b/morpheus-core-wasm/src/hydra/sign.rs
new file mode 100644
index 0000000..3295b8f
--- /dev/null
+++ b/morpheus-core-wasm/src/hydra/sign.rs
@@ -0,0 +1,36 @@
+use super::*;
+
+use iop_keyvault::secp256k1::SecpPrivateKey;
+use iop_morpheus_core::hydra::sign::HydraSigner;
+
+#[wasm_bindgen(js_name = HydraSigner)]
+pub struct JsHydraSigner {
+    inner: SecpPrivateKey,
+}
+
+#[wasm_bindgen(js_class = HydraSigner)]
+impl JsHydraSigner {
+    #[wasm_bindgen(constructor)]
+    pub fn new(inner: JsSecpPrivateKey) -> JsHydraSigner {
+        inner.inner().to_owned().into()
+    }
+
+    #[wasm_bindgen(js_name = signHydraTransaction)]
+    pub fn sign_hydra_transaction(&self, transaction: &JsValue) -> Result<JsValue, JsValue> {
+        let mut tx = transaction.into_serde().map_err_to_js()?;
+        self.inner.sign_hydra_transaction(&mut tx).map_err_to_js()?;
+        JsValue::from_serde(&tx).map_err_to_js()
+    }
+}
+
+impl From<SecpPrivateKey> for JsHydraSigner {
+    fn from(inner: SecpPrivateKey) -> Self {
+        Self { inner }
+    }
+}
+
+impl Wraps<SecpPrivateKey> for JsHydraSigner {
+    fn inner(&self) -> &SecpPrivateKey {
+        &self.inner
+    }
+}
diff --git a/morpheus-core-wasm/src/hydra/tx.rs b/morpheus-core-wasm/src/hydra/tx.rs
index 81a1839..a3aa777 100644
--- a/morpheus-core-wasm/src/hydra/tx.rs
+++ b/morpheus-core-wasm/src/hydra/tx.rs
@@ -46,6 +46,7 @@ impl JsHydraTxBuilder {
         self.create_vote_tx(delegate, sender_pubkey, nonce, hyd_core::Transaction::unvote)
     }
 
+    #[wasm_bindgen(js_name = registerDelegate)]
     pub fn register_delegate(
         &self, sender_pubkey: &JsSecpPublicKey, delegate_name: &str, nonce: u64,
     ) -> Result<JsValue, JsValue> {
diff --git a/morpheus-core/Cargo.toml b/morpheus-core/Cargo.toml
index b2c53a8..0ee7e37 100644
--- a/morpheus-core/Cargo.toml
+++ b/morpheus-core/Cargo.toml
@@ -9,7 +9,7 @@ edition = "2018"
 anyhow = "*"
 chrono = { version = "*", features = ["wasmbind"] }
 failure = "*"
-iop-keyvault = "0.0.2"
+iop-keyvault = "0.0.4"
 jwt-compact = { version = "*", default-features = false }
 log = "*"
 multibase = "0.8"
diff --git a/morpheus-core/src/crypto/hd/hydra/private.rs b/morpheus-core/src/crypto/hd/hydra/private.rs
index c214fdb..b8d449e 100644
--- a/morpheus-core/src/crypto/hd/hydra/private.rs
+++ b/morpheus-core/src/crypto/hd/hydra/private.rs
@@ -1,6 +1,6 @@
 use super::*;
 
-use crate::hydra::crypto::HydraSigner;
+use crate::hydra::sign::HydraSigner;
 
 pub struct Private {
     state: Box<dyn State<PublicState>>,
diff --git a/morpheus-core/src/hydra/mod.rs b/morpheus-core/src/hydra/mod.rs
index cac75eb..c22aaf1 100644
--- a/morpheus-core/src/hydra/mod.rs
+++ b/morpheus-core/src/hydra/mod.rs
@@ -1,5 +1,5 @@
-pub mod crypto;
 pub mod serializer;
+pub mod sign;
 pub mod transaction;
 pub mod txtype;
 
diff --git a/morpheus-core/src/hydra/crypto.rs b/morpheus-core/src/hydra/sign.rs
similarity index 100%
rename from morpheus-core/src/hydra/crypto.rs
rename to morpheus-core/src/hydra/sign.rs
diff --git a/morpheus-core/src/hydra/txtype/hyd_core.rs b/morpheus-core/src/hydra/txtype/hyd_core.rs
index d93c3ec..58d004a 100644
--- a/morpheus-core/src/hydra/txtype/hyd_core.rs
+++ b/morpheus-core/src/hydra/txtype/hyd_core.rs
@@ -195,7 +195,11 @@ impl Aip29Transaction for Transaction {
 
         let mut tx_data: TransactionData = self.common_fields.to_data();
         tx_data.set_type(crate::hydra::txtype::TransactionType::Core(self.tx_type));
-        tx_data.asset = Some(crate::hydra::txtype::Asset::Core(self.asset.to_owned()));
+        tx_data.asset = if self.asset.is_none() {
+            None
+        } else {
+            Some(crate::hydra::txtype::Asset::Core(self.asset.to_owned()))
+        };
         tx_data.recipient_id = self.recipient_id.as_ref().map(|addr| addr.to_p2pkh_addr(prefix));
         tx_data.fee = self.common_fields.calculate_fee(self).to_string();
         tx_data
diff --git a/morpheus-core/src/hydra/txtype/mod.rs b/morpheus-core/src/hydra/txtype/mod.rs
index a5de78e..a3e9a76 100644
--- a/morpheus-core/src/hydra/txtype/mod.rs
+++ b/morpheus-core/src/hydra/txtype/mod.rs
@@ -101,7 +101,7 @@ mod test {
     use crate::data::{auth::Authentication, did::Did, diddoc::Right};
     use crate::hydra::txtype::morpheus::OperationAttempt;
     use crate::hydra::{
-        crypto::HydraSigner,
+        sign::HydraSigner,
         transaction::{TransactionData, TxBatch},
         txtype::{hyd_core, morpheus, Aip29Transaction, CommonTransactionFields},
     };
diff --git a/morpheus-sdk/Cargo.toml b/morpheus-sdk/Cargo.toml
index 88e313e..e78cd07 100644
--- a/morpheus-sdk/Cargo.toml
+++ b/morpheus-sdk/Cargo.toml
@@ -16,7 +16,7 @@ async-trait = "*"
 failure = "*"
 futures = "*"
 hyper = "*"
-iop-keyvault = "0.0.2"
+iop-keyvault = "0.0.4"
 iop-morpheus-core = "0.0.2"
 log = "*"
 multihash = "*"