From 64e0659e0eaa49427e6253e6f8c5bf3e697d0b8e Mon Sep 17 00:00:00 2001 From: Wigy Date: Tue, 28 Jul 2020 08:59:15 +0000 Subject: [PATCH] Add FFI calls required for the Dart tutorials --- morpheus-core-ffi/src/hydra/private.rs | 2 -- morpheus-core-ffi/src/hydra_signer.rs | 16 +++++++++++ morpheus-core-ffi/src/lib.rs | 3 +++ morpheus-core-ffi/src/morpheus/public_kind.rs | 11 ++++++++ morpheus-core-ffi/src/tx.rs | 27 ++++++++++++++++++- 5 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 morpheus-core-ffi/src/hydra_signer.rs diff --git a/morpheus-core-ffi/src/hydra/private.rs b/morpheus-core-ffi/src/hydra/private.rs index 42fb096..9ad9674 100644 --- a/morpheus-core-ffi/src/hydra/private.rs +++ b/morpheus-core-ffi/src/hydra/private.rs @@ -1,7 +1,5 @@ use super::*; -use iop_morpheus_core::hydra::transaction::TransactionData; - #[no_mangle] pub extern "C" fn delete_HydraPrivate(private: *mut Private) { delete(private) diff --git a/morpheus-core-ffi/src/hydra_signer.rs b/morpheus-core-ffi/src/hydra_signer.rs new file mode 100644 index 0000000..03ac95a --- /dev/null +++ b/morpheus-core-ffi/src/hydra_signer.rs @@ -0,0 +1,16 @@ +use super::*; + +#[no_mangle] +pub extern "C" fn SecpPrivateKey_sign_hydra_tx( + sk: *mut SecpPrivateKey, unsigned_tx: *const raw::c_char, +) -> CPtrResult { + let sk = unsafe { convert::borrow_in(sk) }; + let fun = || { + let tx_str = unsafe { convert::str_in(unsigned_tx)? }; + let mut tx_data: TransactionData = serde_json::from_str(tx_str)?; + sk.sign_hydra_transaction(&mut tx_data)?; + let signed_tx_str = serde_json::to_string(&tx_data)?; + Ok(convert::string_out(signed_tx_str)) + }; + cresult(fun()) +} diff --git a/morpheus-core-ffi/src/lib.rs b/morpheus-core-ffi/src/lib.rs index 14e802c..506f740 100644 --- a/morpheus-core-ffi/src/lib.rs +++ b/morpheus-core-ffi/src/lib.rs @@ -1,4 +1,5 @@ #![allow(clippy::not_unsafe_ptr_arg_deref)] +#![allow(non_snake_case)] mod bip39; mod bip44; @@ -6,6 +7,7 @@ mod crypto; mod did; mod ffi; mod hydra; +mod hydra_signer; mod morpheus; mod multicipher; mod secp; @@ -30,6 +32,7 @@ use iop_morpheus_core::{ sign::*, }, data::{claim::*, did::*, diddoc::*, present::*, validation::*}, + hydra::{crypto::HydraSigner, transaction::TransactionData}, }; use crate::ffi::{convert, *}; diff --git a/morpheus-core-ffi/src/morpheus/public_kind.rs b/morpheus-core-ffi/src/morpheus/public_kind.rs index 9e21569..a0d8d51 100644 --- a/morpheus-core-ffi/src/morpheus/public_kind.rs +++ b/morpheus-core-ffi/src/morpheus/public_kind.rs @@ -36,3 +36,14 @@ pub extern "C" fn MorpheusPublicKind_key( }; cresult(fun()) } + +#[no_mangle] +pub extern "C" fn MorpheusPublicKind_did(kind: *const PublicKind, idx: i32) -> CPtrResult { + let kind = unsafe { convert::borrow_in(kind) }; + let fun = || { + let pk = kind.key(idx)?; + let did = Did::from(pk.key_id()); + Ok(convert::move_out(did)) + }; + cresult(fun()) +} diff --git a/morpheus-core-ffi/src/tx.rs b/morpheus-core-ffi/src/tx.rs index 75dbf09..52deafd 100644 --- a/morpheus-core-ffi/src/tx.rs +++ b/morpheus-core-ffi/src/tx.rs @@ -1,7 +1,9 @@ use super::*; use iop_keyvault::Networks; -use iop_morpheus_core::hydra::txtype::{hyd_core, Aip29Transaction, CommonTransactionFields}; +use iop_morpheus_core::hydra::txtype::{ + hyd_core, morpheus, Aip29Transaction, CommonTransactionFields, +}; #[no_mangle] pub extern "C" fn TxBuilder_hydraTransferTx( @@ -26,3 +28,26 @@ pub extern "C" fn TxBuilder_hydraTransferTx( }; cresult(fun()) } + +#[no_mangle] +pub extern "C" fn TxBuilder_morpheusTx( + network: *const raw::c_char, sender_public_key: *const raw::c_char, + attempts: *const raw::c_char, nonce: u64, +) -> CPtrResult { + let fun = || { + let network = unsafe { convert::str_in(network)? }; + let attempts = unsafe { convert::str_in(attempts)? }; + let sender_public_key = unsafe { convert::str_in(sender_public_key)? }; + let op_attempts: Vec = serde_json::from_str(attempts)?; + let common_fields = CommonTransactionFields { + network: Networks::by_name(network)?, + sender_public_key: sender_public_key.to_owned(), + nonce, + ..Default::default() + }; + let transfer_tx = morpheus::Transaction::new(common_fields, op_attempts); + let tx_str = serde_json::to_string(&transfer_tx.to_data())?; + Ok(convert::string_out(tx_str)) + }; + cresult(fun()) +}