Skip to content

Commit

Permalink
Add some missing bindings to WASM and FFI
Browse files Browse the repository at this point in the history
  • Loading branch information
wigy-opensource-developer committed Jul 9, 2021
1 parent 7030c44 commit 863acb4
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
6 changes: 6 additions & 0 deletions keyvault-wasm/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ impl JsSecpKeyId {
let inner = SecpKeyId::from_p2pkh_addr(address, network).map_err_to_js()?;
Ok(inner.into())
}

#[wasm_bindgen(js_name=toAddress)]
pub fn to_p2pkh_addr(&self, network: &str) -> Result<String, JsValue> {
let network = Networks::by_name(network).map_err_to_js()?;
Ok(self.inner.to_p2pkh_addr(network.p2pkh_addr()))
}
}

impl From<SecpKeyId> for JsSecpKeyId {
Expand Down
2 changes: 1 addition & 1 deletion sdk-ffi/src/hydra/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub extern "C" fn HydraPrivate_key(
) -> CPtrResult<Bip44Key<Secp256k1>> {
let fun = || {
let private = unsafe { convert::borrow_mut_in(private) };
let key = private.key(idx)?;
let key = private.key_mut(idx)?;
Ok(convert::move_out(key))
};
cresult(fun())
Expand Down
34 changes: 34 additions & 0 deletions sdk-ffi/src/keyvault/secp/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,37 @@ use super::*;
pub extern "C" fn delete_SecpKeyId(secp_id: *mut SecpKeyId) {
delete(secp_id)
}

#[no_mangle]
pub extern "C" fn SecpKeyId_from_address(
address: *mut raw::c_char, network: *mut raw::c_char,
) -> CPtrResult<SecpKeyId> {
let fun = || {
let address = unsafe { convert::str_in(address)? };
let network = unsafe { convert::str_in(network)? };

let network = Networks::by_name(network)?;
let id = SecpKeyId::from_p2pkh_addr(address, network)?;

let id = convert::move_out(id);
Ok(id)
};
cresult(fun())
}

#[no_mangle]
pub extern "C" fn SecpKeyId_to_address(
secp_id: *const SecpKeyId, network: *const raw::c_char,
) -> CPtrResult<raw::c_char> {
let secp_id = unsafe { convert::borrow_in(secp_id) };
let fun = || {
let network = unsafe { convert::str_in(network)? };

let network = Networks::by_name(network)?;
let address = secp_id.to_p2pkh_addr(network.p2pkh_addr());

let address = convert::string_out(address);
Ok(address)
};
cresult(fun())
}
10 changes: 6 additions & 4 deletions sdk-ffi/src/keyvault/secp/pk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub extern "C" fn delete_SecpPublicKey(secp_pk: *mut SecpPublicKey) {
}

#[no_mangle]
pub extern "C" fn SecpPublicKey_fromString(hex: *mut raw::c_char) -> CPtrResult<SecpPublicKey> {
pub extern "C" fn SecpPublicKey_fromString(hex: *const raw::c_char) -> CPtrResult<SecpPublicKey> {
let fun = || {
let hex = unsafe { convert::str_in(hex) }?;
let secp_pk = SecpPublicKey::from_str(hex)?;
Expand All @@ -16,7 +16,9 @@ pub extern "C" fn SecpPublicKey_fromString(hex: *mut raw::c_char) -> CPtrResult<
}

#[no_mangle]
pub extern "C" fn SecpPublicKey_to_string(secp_pk: *mut SecpPublicKey) -> CPtrResult<raw::c_char> {
pub extern "C" fn SecpPublicKey_to_string(
secp_pk: *const SecpPublicKey,
) -> CPtrResult<raw::c_char> {
let secp_pk = unsafe { convert::borrow_in(secp_pk) };
let fun = || {
let hex = secp_pk.to_string();
Expand All @@ -26,13 +28,13 @@ pub extern "C" fn SecpPublicKey_to_string(secp_pk: *mut SecpPublicKey) -> CPtrRe
}

#[no_mangle]
pub extern "C" fn SecpPublicKey_key_id(secp_pk: *mut SecpPublicKey) -> *mut SecpKeyId {
pub extern "C" fn SecpPublicKey_key_id(secp_pk: *const SecpPublicKey) -> *mut SecpKeyId {
let secp_pk = unsafe { convert::borrow_in(secp_pk) };
convert::move_out(secp_pk.key_id())
}

#[no_mangle]
pub extern "C" fn SecpPublicKey_ark_key_id(secp_pk: *mut SecpPublicKey) -> *mut SecpKeyId {
pub extern "C" fn SecpPublicKey_ark_key_id(secp_pk: *const SecpPublicKey) -> *mut SecpKeyId {
let secp_pk = unsafe { convert::borrow_in(secp_pk) };
convert::move_out(secp_pk.ark_key_id())
}
Expand Down

0 comments on commit 863acb4

Please sign in to comment.