Skip to content

Commit

Permalink
Add FFI calls required for the Dart tutorials
Browse files Browse the repository at this point in the history
  • Loading branch information
wigy-opensource-developer committed Jul 28, 2020
1 parent 3d509a0 commit 64e0659
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
2 changes: 0 additions & 2 deletions morpheus-core-ffi/src/hydra/private.rs
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
16 changes: 16 additions & 0 deletions morpheus-core-ffi/src/hydra_signer.rs
Original file line number Diff line number Diff line change
@@ -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<raw::c_char> {
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())
}
3 changes: 3 additions & 0 deletions morpheus-core-ffi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#![allow(clippy::not_unsafe_ptr_arg_deref)]
#![allow(non_snake_case)]

mod bip39;
mod bip44;
mod crypto;
mod did;
mod ffi;
mod hydra;
mod hydra_signer;
mod morpheus;
mod multicipher;
mod secp;
Expand All @@ -30,6 +32,7 @@ use iop_morpheus_core::{
sign::*,
},
data::{claim::*, did::*, diddoc::*, present::*, validation::*},
hydra::{crypto::HydraSigner, transaction::TransactionData},
};

use crate::ffi::{convert, *};
Expand Down
11 changes: 11 additions & 0 deletions morpheus-core-ffi/src/morpheus/public_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Did> {
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())
}
27 changes: 26 additions & 1 deletion morpheus-core-ffi/src/tx.rs
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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<raw::c_char> {
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<morpheus::OperationAttempt> = 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())
}

0 comments on commit 64e0659

Please sign in to comment.