Skip to content

Commit

Permalink
BREAKING: Separated MorpheusOperationSigner.sign and signWithKey in W…
Browse files Browse the repository at this point in the history
…ASM and FFI
  • Loading branch information
wigy-opensource-developer committed Jul 21, 2021
1 parent aa620d0 commit a5c1ddb
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 38 deletions.
5 changes: 4 additions & 1 deletion sdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ use iop_morpheus_proto::{
crypto::{jwt::*, sign::*},
data::*,
};
use iop_morpheus_sdk::vault::{Plugin, Private, PrivateKind, Public, PublicKind};
use iop_morpheus_sdk::vault::{
Plugin as MorpheusPlugin, Private as MorpheusPrivate, PrivateKind as MorpheusPrivateKind,
Public as MorpheusPublic, PublicKind as MorpheusPublicKind,
};
use iop_vault::{BoundPlugin, Vault};
use json_digest::*;

Expand Down
12 changes: 7 additions & 5 deletions sdk-ffi/src/morpheus/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;

pub struct CMorpheusPlugin {
pub(crate) plugin: BoundPlugin<Plugin, Public, Private>,
pub(crate) plugin: BoundPlugin<MorpheusPlugin, MorpheusPublic, MorpheusPrivate>,
}

#[no_mangle]
Expand All @@ -11,7 +11,7 @@ pub extern "C" fn MorpheusPlugin_init(
let vault = unsafe { convert::borrow_mut_in(vault) };
let mut fun = || {
let unlock_password = unsafe { convert::str_in(unlock_pwd)? };
Plugin::init(vault, unlock_password)?;
MorpheusPlugin::init(vault, unlock_password)?;
Ok(())
};
cresult_void(fun())
Expand All @@ -21,15 +21,17 @@ pub extern "C" fn MorpheusPlugin_init(
pub extern "C" fn MorpheusPlugin_get(vault: *mut Vault) -> CPtrResult<CMorpheusPlugin> {
let vault = unsafe { convert::borrow_mut_in(vault) };
let fun = || {
let plugin = Plugin::get(vault)?;
let plugin = MorpheusPlugin::get(vault)?;
let morpheus = CMorpheusPlugin { plugin };
Ok(convert::move_out(morpheus))
};
cresult(fun())
}

#[no_mangle]
pub extern "C" fn MorpheusPlugin_public_get(morpheus: *mut CMorpheusPlugin) -> CPtrResult<Public> {
pub extern "C" fn MorpheusPlugin_public_get(
morpheus: *mut CMorpheusPlugin,
) -> CPtrResult<MorpheusPublic> {
let morpheus = unsafe { convert::borrow_in(morpheus) };
let fun = || {
let public = morpheus.plugin.public()?;
Expand All @@ -41,7 +43,7 @@ pub extern "C" fn MorpheusPlugin_public_get(morpheus: *mut CMorpheusPlugin) -> C
#[no_mangle]
pub extern "C" fn MorpheusPlugin_private(
morpheus: *mut CMorpheusPlugin, unlock_pwd: *const raw::c_char,
) -> CPtrResult<Private> {
) -> CPtrResult<MorpheusPrivate> {
let morpheus = unsafe { convert::borrow_in(morpheus) };
let fun = || {
let unlock_password = unsafe { convert::str_in(unlock_pwd)? };
Expand Down
22 changes: 12 additions & 10 deletions sdk-ffi/src/morpheus/private.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use super::*;

#[no_mangle]
pub extern "C" fn delete_MorpheusPrivate(private: *mut Private) {
pub extern "C" fn delete_MorpheusPrivate(private: *mut MorpheusPrivate) {
delete(private)
}

#[no_mangle]
pub extern "C" fn MorpheusPrivate_personas_get(private: *mut Private) -> CPtrResult<PrivateKind> {
pub extern "C" fn MorpheusPrivate_personas_get(
private: *mut MorpheusPrivate,
) -> CPtrResult<MorpheusPrivateKind> {
let private = unsafe { convert::borrow_in(private) };
let fun = || {
let kind = private.personas()?;
Expand All @@ -16,14 +18,14 @@ pub extern "C" fn MorpheusPrivate_personas_get(private: *mut Private) -> CPtrRes
}

#[no_mangle]
pub extern "C" fn MorpheusPrivate_public_get(private: *mut Private) -> *mut Public {
pub extern "C" fn MorpheusPrivate_public_get(private: *mut MorpheusPrivate) -> *mut MorpheusPublic {
let private = unsafe { convert::borrow_in(private) };
convert::move_out(private.public())
}

#[no_mangle]
pub extern "C" fn MorpheusPrivate_key_by_pk(
private: *mut Private, pk: *mut MPublicKey,
private: *mut MorpheusPrivate, pk: *mut MPublicKey,
) -> CPtrResult<MorpheusPrivateKey> {
let private = unsafe { convert::borrow_in(private) };
let pk = unsafe { convert::borrow_in(pk) };
Expand All @@ -36,7 +38,7 @@ pub extern "C" fn MorpheusPrivate_key_by_pk(

#[no_mangle]
pub extern "C" fn MorpheusPrivate_sign_did_operations(
private: *mut Private, id: *mut MKeyId, message: *mut CSlice<u8>,
private: *mut MorpheusPrivate, id: *mut MKeyId, message: *mut CSlice<u8>,
) -> CPtrResult<Signed<Box<[u8]>>> {
let private = unsafe { convert::borrow_in(private) };
let id = unsafe { convert::borrow_in(id) };
Expand All @@ -53,7 +55,7 @@ pub extern "C" fn MorpheusPrivate_sign_did_operations(

#[no_mangle]
pub extern "C" fn MorpheusPrivate_sign_witness_request(
private: *mut Private, id: *mut MKeyId, request: *mut raw::c_char,
private: *mut MorpheusPrivate, id: *mut MKeyId, request: *mut raw::c_char,
) -> CPtrResult<Signed<serde_json::Value>> {
let private = unsafe { convert::borrow_in(private) };
let id = unsafe { convert::borrow_in(id) };
Expand All @@ -70,7 +72,7 @@ pub extern "C" fn MorpheusPrivate_sign_witness_request(

#[no_mangle]
pub extern "C" fn MorpheusPrivate_sign_witness_statement(
private: *mut Private, id: *mut MKeyId, statement: *mut raw::c_char,
private: *mut MorpheusPrivate, id: *mut MKeyId, statement: *mut raw::c_char,
) -> CPtrResult<Signed<serde_json::Value>> {
let private = unsafe { convert::borrow_in(private) };
let id = unsafe { convert::borrow_in(id) };
Expand All @@ -87,7 +89,7 @@ pub extern "C" fn MorpheusPrivate_sign_witness_statement(

#[no_mangle]
pub extern "C" fn MorpheusPrivate_sign_claim_presentation(
private: *mut Private, id: *mut MKeyId, presentation: *mut raw::c_char,
private: *mut MorpheusPrivate, id: *mut MKeyId, presentation: *mut raw::c_char,
) -> CPtrResult<Signed<serde_json::Value>> {
let private = unsafe { convert::borrow_in(private) };
let id = unsafe { convert::borrow_in(id) };
Expand All @@ -102,12 +104,12 @@ pub extern "C" fn MorpheusPrivate_sign_claim_presentation(
cresult(fun())
}

fn create_signer(private: &Private, id: &MKeyId) -> Result<PrivateKeySigner> {
fn create_signer(private: &MorpheusPrivate, id: &MKeyId) -> Result<PrivateKeySigner> {
let sk: MPrivateKey = key_by_id(private, id)?.private_key();
Ok(PrivateKeySigner::new(sk))
}

fn key_by_id(private: &Private, id: &MKeyId) -> Result<MorpheusPrivateKey> {
fn key_by_id(private: &MorpheusPrivate, id: &MKeyId) -> Result<MorpheusPrivateKey> {
let pk = private.public().key_by_id(id)?;
let morpheus_sk = private.key_by_pk(&pk)?;
Ok(morpheus_sk)
Expand Down
20 changes: 13 additions & 7 deletions sdk-ffi/src/morpheus/private_kind.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
use super::*;

#[no_mangle]
pub extern "C" fn delete_MorpheusPrivateKind(kind: *mut PrivateKind) {
pub extern "C" fn delete_MorpheusPrivateKind(kind: *mut MorpheusPrivateKind) {
delete(kind)
}

// TODO MorpheusPrivateKind_bip32_path_get and MorpheusPrivateKind_network_get

#[no_mangle]
pub extern "C" fn MorpheusPrivateKind_kind_get(kind: *mut PrivateKind) -> *mut raw::c_char {
pub extern "C" fn MorpheusPrivateKind_kind_get(kind: *mut MorpheusPrivateKind) -> *mut raw::c_char {
let kind = unsafe { convert::borrow_in(kind) };
let res = format!("{:?}", kind.path());
convert::string_out(res)
}

#[no_mangle]
pub extern "C" fn MorpheusPrivateKind_len_get(kind: *mut PrivateKind) -> CPtrResult<usize> {
pub extern "C" fn MorpheusPrivateKind_len_get(kind: *mut MorpheusPrivateKind) -> CPtrResult<usize> {
let kind = unsafe { convert::borrow_in(kind) };
let fun = || {
let len = kind.len()?;
Expand All @@ -25,7 +25,9 @@ pub extern "C" fn MorpheusPrivateKind_len_get(kind: *mut PrivateKind) -> CPtrRes
}

#[no_mangle]
pub extern "C" fn MorpheusPrivateKind_is_empty_get(kind: *mut PrivateKind) -> CPtrResult<u8> {
pub extern "C" fn MorpheusPrivateKind_is_empty_get(
kind: *mut MorpheusPrivateKind,
) -> CPtrResult<u8> {
let kind = unsafe { convert::borrow_in(kind) };
let fun = || {
let is_empty = kind.is_empty()?;
Expand All @@ -35,14 +37,16 @@ pub extern "C" fn MorpheusPrivateKind_is_empty_get(kind: *mut PrivateKind) -> CP
}

#[no_mangle]
pub extern "C" fn MorpheusPrivateKind_neuter(kind: *mut PrivateKind) -> *mut PublicKind {
pub extern "C" fn MorpheusPrivateKind_neuter(
kind: *mut MorpheusPrivateKind,
) -> *mut MorpheusPublicKind {
let kind = unsafe { convert::borrow_in(kind) };
convert::move_out(kind.neuter())
}

#[no_mangle]
pub extern "C" fn MorpheusPrivateKind_key(
kind: *mut PrivateKind, idx: i32,
kind: *mut MorpheusPrivateKind, idx: i32,
) -> CPtrResult<MorpheusPrivateKey> {
let kind = unsafe { convert::borrow_mut_in(kind) };
let mut fun = || {
Expand All @@ -53,7 +57,9 @@ pub extern "C" fn MorpheusPrivateKind_key(
}

#[no_mangle]
pub extern "C" fn MorpheusPrivateKind_did(kind: *mut PrivateKind, idx: i32) -> CPtrResult<Did> {
pub extern "C" fn MorpheusPrivateKind_did(
kind: *mut MorpheusPrivateKind, idx: i32,
) -> CPtrResult<Did> {
let kind = unsafe { convert::borrow_mut_in(kind) };
let mut fun = || {
let sk = kind.key_mut(idx)?;
Expand Down
8 changes: 5 additions & 3 deletions sdk-ffi/src/morpheus/public.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use super::*;

#[no_mangle]
pub extern "C" fn delete_MorpheusPublic(public: *mut Public) {
pub extern "C" fn delete_MorpheusPublic(public: *mut MorpheusPublic) {
delete(public)
}

#[no_mangle]
pub extern "C" fn MorpheusPublic_personas_get(public: *mut Public) -> CPtrResult<PublicKind> {
pub extern "C" fn MorpheusPublic_personas_get(
public: *mut MorpheusPublic,
) -> CPtrResult<MorpheusPublicKind> {
let public = unsafe { convert::borrow_in(public) };
let fun = || {
let personas = public.personas()?;
Expand All @@ -17,7 +19,7 @@ pub extern "C" fn MorpheusPublic_personas_get(public: *mut Public) -> CPtrResult

#[no_mangle]
pub extern "C" fn MorpheusPublic_key_by_id(
public: *mut Public, id: *mut MKeyId,
public: *mut MorpheusPublic, id: *mut MKeyId,
) -> CPtrResult<MPublicKey> {
let public = unsafe { convert::borrow_in(public) };
let id = unsafe { convert::borrow_in(id) };
Expand Down
12 changes: 7 additions & 5 deletions sdk-ffi/src/morpheus/public_kind.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use super::*;

#[no_mangle]
pub extern "C" fn delete_MorpheusPublicKind(kind: *mut PublicKind) {
pub extern "C" fn delete_MorpheusPublicKind(kind: *mut MorpheusPublicKind) {
delete(kind)
}

#[no_mangle]
pub extern "C" fn MorpheusPublicKind_len_get(kind: *mut PublicKind) -> CPtrResult<usize> {
pub extern "C" fn MorpheusPublicKind_len_get(kind: *mut MorpheusPublicKind) -> CPtrResult<usize> {
let kind = unsafe { convert::borrow_in(kind) };
let fun = || {
let len = kind.len()?;
Expand All @@ -16,7 +16,7 @@ pub extern "C" fn MorpheusPublicKind_len_get(kind: *mut PublicKind) -> CPtrResul
}

#[no_mangle]
pub extern "C" fn MorpheusPublicKind_is_empty_get(kind: *mut PublicKind) -> CPtrResult<u8> {
pub extern "C" fn MorpheusPublicKind_is_empty_get(kind: *mut MorpheusPublicKind) -> CPtrResult<u8> {
let kind = unsafe { convert::borrow_in(kind) };
let fun = || {
let is_empty = kind.is_empty()?;
Expand All @@ -27,7 +27,7 @@ pub extern "C" fn MorpheusPublicKind_is_empty_get(kind: *mut PublicKind) -> CPtr

#[no_mangle]
pub extern "C" fn MorpheusPublicKind_key(
kind: *mut PublicKind, idx: i32,
kind: *mut MorpheusPublicKind, idx: i32,
) -> CPtrResult<MPublicKey> {
let kind = unsafe { convert::borrow_mut_in(kind) };
let fun = || {
Expand All @@ -38,7 +38,9 @@ pub extern "C" fn MorpheusPublicKind_key(
}

#[no_mangle]
pub extern "C" fn MorpheusPublicKind_did(kind: *const PublicKind, idx: i32) -> CPtrResult<Did> {
pub extern "C" fn MorpheusPublicKind_did(
kind: *const MorpheusPublicKind, idx: i32,
) -> CPtrResult<Did> {
let kind = unsafe { convert::borrow_in(kind) };
let fun = || {
let pk = kind.key(idx)?;
Expand Down
31 changes: 26 additions & 5 deletions sdk-ffi/src/morpheus/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,41 @@ pub extern "C" fn MorpheusOperationSigner_add(
}

#[no_mangle]
pub extern "C" fn MorpheusOperationSigner_sign(
pub extern "C" fn MorpheusOperationSigner_sign_with_key(
signer: *mut MorpheusOperationSigner, private_key: *const MPrivateKey,
) -> CPtrResult<SignedOperation> {
let fun = || {
let signer = unsafe { convert::borrow_in(signer) };
let private_key = unsafe { convert::borrow_in(private_key) };
let signable_ops = SignableOperation::new(signer.operations.to_owned());
let signer = PrivateKeySigner::new(private_key.to_owned());
let signed_ops = signable_ops.sign(&signer)?;
let signed_ops = MorpheusOperationSigner_sign_inner(signer, private_key.to_owned())?;
Ok(convert::move_out(signed_ops))
};
cresult(fun())
}

#[no_mangle]
pub extern "C" fn MorpheusOperationSigner_sign(
signer: *mut MorpheusOperationSigner, public_key: *const MPublicKey,
morpheus_private: *const MorpheusPrivate,
) -> CPtrResult<SignedOperation> {
let fun = || {
let morpheus_private = unsafe { convert::borrow_in(morpheus_private) };
let public_key = unsafe { convert::borrow_in(public_key) };
let private_key = morpheus_private.key_by_pk(public_key)?.private_key();
let signed_ops = MorpheusOperationSigner_sign_inner(signer, private_key)?;
Ok(convert::move_out(signed_ops))
};
cresult(fun())
}

fn MorpheusOperationSigner_sign_inner(
signer: *mut MorpheusOperationSigner, private_key: MPrivateKey,
) -> Result<SignedOperation> {
let signer = unsafe { convert::borrow_in(signer) };
let signable_ops = SignableOperation::new(signer.operations.to_owned());
let signer = PrivateKeySigner::new(private_key);
signable_ops.sign(&signer)
}

#[no_mangle]
pub extern "C" fn delete_MorpheusSignedOperation(operation: *mut SignedOperation) {
delete(operation)
Expand Down
19 changes: 17 additions & 2 deletions sdk-wasm/src/morpheus/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,24 @@ impl JsMorpheusOperationSigner {
self.signables.push(signable.inner().to_owned())
}

pub fn sign(&self, private_key: &JsMPrivateKey) -> Result<JsMorpheusSignedOperation, JsValue> {
#[wasm_bindgen(js_name=signWithKey)]
pub fn sign_with_key(
&self, private_key: &JsMPrivateKey,
) -> Result<JsMorpheusSignedOperation, JsValue> {
self.sign_inner(private_key.inner().to_owned())
}

pub fn sign(
&self, public_key: JsMPublicKey, morpheus_private: &JsMorpheusPrivate,
) -> Result<JsMorpheusSignedOperation, JsValue> {
let private_key =
morpheus_private.inner().key_by_pk(&public_key.inner()).map_err_to_js()?;
self.sign_inner(private_key.private_key())
}

fn sign_inner(&self, private_key: MPrivateKey) -> Result<JsMorpheusSignedOperation, JsValue> {
let signable_ops = SignableOperation::new(self.signables.to_owned());
let signer = PrivateKeySigner::new(private_key.inner().to_owned());
let signer = PrivateKeySigner::new(private_key);
let signed = signable_ops.sign(&signer).map_err_to_js()?;
Ok(signed.into())
}
Expand Down

0 comments on commit a5c1ddb

Please sign in to comment.