Skip to content

Commit

Permalink
Implement more functions in napi-rs wrapper
Browse files Browse the repository at this point in the history
Signed-off-by: Patrik Stas <[email protected]>
  • Loading branch information
Patrik-Stas committed Dec 29, 2022
1 parent 9cf64ef commit 397f043
Show file tree
Hide file tree
Showing 27 changed files with 1,040 additions and 1,097 deletions.
99 changes: 97 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion wrappers/node-napi-rs/api-node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ doctest = false
libvcx = { path = "../../../libvcx" }
log = "0.4.16"
napi = { version = "=2.9.1", default-features = false, features = [ "async" ] }
napi-derive = { version = "=2.9.1" }
napi-derive = { version = "=2.9.3" }

[build-dependencies]
napi-build = "2.0.1"
47 changes: 47 additions & 0 deletions wrappers/node-napi-rs/api-node/src/api/agency_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use napi_derive::napi;

use vcx::api_vcx::api_global::agency_client;
use vcx::api_vcx::api_global::settings::enable_mocks;
use vcx::api_vcx::api_handle::mediated_connection::update_message_status;
use vcx::aries_vcx::agency_client::configuration::{AgencyClientConfig, AgentProvisionConfig};
use vcx::aries_vcx::agency_client::messages::update_message::UIDsByConn;
use vcx::aries_vcx::agency_client::MessageStatusCode;
use vcx::aries_vcx::utils::test_logger::LibvcxDefaultLogger;
use vcx::errors::error::{LibvcxError, LibvcxErrorKind};
use vcx::serde_json;

use crate::error::to_napi_err;

#[napi]
pub fn create_agency_client_for_main_wallet(config: String) -> napi::Result<()> {
let config = serde_json::from_str::<AgencyClientConfig>(&config)
.map_err(|err| LibvcxError::from_msg(LibvcxErrorKind::InvalidJson, format!("Serialization error: {:?}", err)))
.map_err(to_napi_err)?;
agency_client::create_agency_client_for_main_wallet(&config).map_err(to_napi_err)?;
Ok(())
}

#[napi]
pub async fn provision_cloud_agent(config: String) -> napi::Result<()> {
let config = serde_json::from_str::<AgentProvisionConfig>(&config)
.map_err(|err| LibvcxError::from_msg(LibvcxErrorKind::InvalidJson, format!("Serialization error: {:?}", err)))
.map_err(to_napi_err)?;
agency_client::provision_cloud_agent(&config).await.map_err(to_napi_err)?;
Ok(())
}

// todo: can we accept Vec<String> instead of Stringified JSON in place of uids_by_conns?
#[napi]
pub async fn messages_update_status(status_code: String, uids_by_conns: String) -> napi::Result<()> {
let status_code = serde_json::from_str::<MessageStatusCode>(&status_code)
.map_err(|err| LibvcxError::from_msg(LibvcxErrorKind::InvalidJson, format!("Serialization error: {:?}", err)))
.map_err(to_napi_err)?;
let uids_by_conns = serde_json::from_str::<Vec<UIDsByConn>>(&uids_by_conns)
.map_err(|err| LibvcxError::from_msg(LibvcxErrorKind::InvalidJson, format!("Serialization error: {:?}", err)))
.map_err(to_napi_err)?;

agency_client::agency_update_messages(status_code, uids_by_conns)
.await
.map_err(to_napi_err)?;
Ok(())
}
60 changes: 60 additions & 0 deletions wrappers/node-napi-rs/api-node/src/api/credential_definition.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use napi_derive::napi;

use vcx::api_vcx::api_handle::{credential_def, schema};
use crate::error::to_napi_err;

#[napi]
fn schema_deserialize(serialized: String) -> napi::Result<u32> {
schema::from_string(&serialized).map_err(to_napi_err)
}

#[napi]
async fn credentialdef_create_v2_(source_id: String, schema_id: String, tag: String, support_revocation: bool,) -> napi::Result<u32> {
credential_def::create(source_id, schema_id, tag, support_revocation)
.await
.map_err(to_napi_err)
}

#[napi]
async fn credentialdef_publish(handle: u32) -> napi::Result<()> {
credential_def::publish(handle)
.await
.map_err(to_napi_err)
}

#[napi]
fn credentialdef_deserialize(serialized: String) -> napi::Result<u32> {
credential_def::from_string(&serialized)
.map_err(to_napi_err)
}

#[napi]
fn credentialdef_release(handle: u32) -> napi::Result<()> {
credential_def::release(handle)
.map_err(to_napi_err)
}

#[napi]
fn credentialdef_serialize(handle: u32) -> napi::Result<String> {
credential_def::to_string(handle)
.map_err(to_napi_err)
}

#[napi]
fn credentialdef_get_cred_def_id(handle: u32) -> napi::Result<String> {
credential_def::get_cred_def_id(handle)
.map_err(to_napi_err)
}

#[napi]
async fn credentialdef_update_state(handle: u32) -> napi::Result<u32> {
credential_def::update_state(handle)
.await
.map_err(to_napi_err)
}

#[napi]
fn credentialdef_get_state(handle: u32) -> napi::Result<u32> {
credential_def::get_state(handle)
.map_err(to_napi_err)
}
116 changes: 116 additions & 0 deletions wrappers/node-napi-rs/api-node/src/api/issuer_credential.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
use crate::error::to_napi_err;
use napi::Error;
use napi_derive::napi;
use vcx::api_vcx::api_handle::issuer_credential;
use vcx::aries_vcx::messages::a2a::A2AMessage;
use vcx::serde_json::json;

#[napi]
fn issuer_credential_deserialize(credential_data: String) -> napi::Result<u32> {
issuer_credential::from_string(&credential_data).map_err(to_napi_err)
}

#[napi]
fn issuer_credential_serialize(handle_credential: u32) -> napi::Result<String> {
issuer_credential::to_string(handle_credential).map_err(to_napi_err)
}

#[napi]
async fn issuer_credential_update_state_v2(handle_credential: u32, connection_handle: u32) -> napi::Result<u32> {
issuer_credential::update_state(handle_credential, None, connection_handle)
.await
.map_err(to_napi_err)
}

#[napi]
async fn issuer_credential_update_state_with_message_v2(
handle_credential: u32,
connection_handle: u32,
message: String,
) -> napi::Result<u32> {
issuer_credential::update_state(handle_credential, Some(&message), connection_handle)
.await
.map_err(to_napi_err)
}

#[napi]
fn issuer_credential_get_state(handle_credential: u32) -> napi::Result<u32> {
issuer_credential::get_state(handle_credential).map_err(to_napi_err)
}

#[napi]
fn issuer_credential_get_rev_reg_id(handle_credential: u32) -> napi::Result<String> {
issuer_credential::get_rev_reg_id(handle_credential).map_err(to_napi_err)
}

#[napi]
fn issuer_credential_create(source_id: String) -> napi::Result<u32> {
issuer_credential::issuer_credential_create(source_id).map_err(to_napi_err)
}

#[napi]
async fn issuer_credential_revoke_local(handle_credential: u32) -> napi::Result<()> {
issuer_credential::revoke_credential_local(handle_credential)
.await
.map_err(to_napi_err)
}

#[napi]
fn issuer_credential_is_revokable(handle_credential: u32) -> napi::Result<bool> {
issuer_credential::is_revokable(handle_credential).map_err(to_napi_err)
}

#[napi]
async fn issuer_credential_send_credential(handle_credential: u32, handle_connection: u32) -> napi::Result<u32> {
issuer_credential::send_credential(handle_credential, handle_connection)
.await
.map_err(to_napi_err)
}

#[napi]
async fn issuer_credential_send_offer_v2(handle_credential: u32, handle_connection: u32) -> napi::Result<()> {
issuer_credential::send_credential_offer_v2(handle_credential, handle_connection)
.await
.map_err(to_napi_err)?;
Ok(())
}

#[napi]
fn issuer_credential_mark_offer_msg_sent(handle_credential: u32) -> napi::Result<()> {
issuer_credential::mark_credential_offer_msg_sent(handle_credential).map_err(to_napi_err)
}

#[napi]
async fn issuer_credential_build_offer_msg_v2(
credential_handle: u32,
cred_def_handle: u32,
rev_reg_handle: u32,
credential_json: String,
comment: Option<String>,
) -> napi::Result<()> {
issuer_credential::build_credential_offer_msg_v2(
credential_handle,
cred_def_handle,
rev_reg_handle,
&credential_json,
comment.as_deref(),
)
.await
.map_err(to_napi_err)
}

#[napi]
fn issuer_credential_get_offer_msg(credential_handle: u32) -> napi::Result<String> {
let res: A2AMessage = issuer_credential::get_credential_offer_msg(credential_handle).map_err(to_napi_err)?;
Ok(json!(res).to_string())
}

#[napi]
fn issuer_credential_release(credential_handle: u32) -> napi::Result<()> {
issuer_credential::release(credential_handle).map_err(to_napi_err)
}

#[napi]
fn issuer_credential_get_thread_id(credential_handle: u32) -> napi::Result<String> {
issuer_credential::get_thread_id(credential_handle).map_err(to_napi_err)
}
Loading

0 comments on commit 397f043

Please sign in to comment.