Skip to content

Commit

Permalink
Expose more APIs via napi-rs
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 30, 2022
1 parent e2b5e83 commit bc7984a
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 25 deletions.
10 changes: 1 addition & 9 deletions libvcx/src/api_c/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -858,17 +858,9 @@ pub extern "C" fn vcx_get_service_from_ledger(

trace!("vcx_get_service_from_ledger(command_handle: {})", command_handle,);

let institution_did = match Did::new(&target_did) {
Ok(did) => did,
Err(err) => {
error!("Error parsing value {} as DID, err: {}", target_did, err.to_string());
return LibvcxError::from_msg(LibvcxErrorKind::InvalidDid, err.to_string()).into();
}
};

execute_async::<BoxFuture<'static, Result<(), ()>>>(
async move {
match ledger_get_service(&institution_did).await {
match ledger_get_service(&target_did).await {
Ok(service) => {
trace!(
"vcx_get_service_from_ledger_cb(command_handle: {}, rc: {})",
Expand Down
4 changes: 2 additions & 2 deletions libvcx/src/api_c/vcx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use aries_vcx::indy::wallet::IssuerConfig;
use aries_vcx::utils::version_constants;

use crate::api_c::types::CommandHandle;
use crate::api_vcx::api_global::agency_client::agency_update_agent_webhook;
use crate::api_vcx::api_global::agency_client::update_webhook_url;
use crate::api_vcx::api_global::ledger::{ledger_get_txn_author_agreement, ledger_set_txn_author_agreement};

use crate::api_vcx::api_global::agency_client::create_agency_client_for_main_wallet;
Expand Down Expand Up @@ -338,7 +338,7 @@ pub extern "C" fn vcx_update_webhook_url(

execute_async::<BoxFuture<'static, Result<(), ()>>>(
async move {
match agency_update_agent_webhook(&notification_webhook_url[..]).await {
match update_webhook_url(&notification_webhook_url[..]).await {
Ok(()) => {
trace!(
"vcx_update_webhook_url_cb(command_handle: {}, rc: {})",
Expand Down
2 changes: 1 addition & 1 deletion libvcx/src/api_vcx/api_global/agency_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub async fn agency_update_messages(
})
}

pub async fn agency_update_agent_webhook(webhook_url: &str) -> LibvcxResult<()> {
pub async fn update_webhook_url(webhook_url: &str) -> LibvcxResult<()> {
let client = get_main_agency_client()?;
client.update_agent_webhook(webhook_url).await?;
Ok(())
Expand Down
5 changes: 3 additions & 2 deletions libvcx/src/api_vcx/api_global/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ pub async fn ledger_write_endpoint_legacy(
Ok(service)
}

pub async fn ledger_get_service(did: &Did) -> LibvcxResult<AriesService> {
pub async fn ledger_get_service(target_did: &str) -> LibvcxResult<AriesService> {
let target_did = Did::new(&target_did)?;
let profile = get_main_profile()?;
map_ariesvcx_result(get_service(&profile, did).await)
map_ariesvcx_result(get_service(&profile, &target_did).await)
}

pub async fn ledger_get_txn_author_agreement() -> LibvcxResult<String> {
Expand Down
7 changes: 7 additions & 0 deletions wrappers/node-napi-rs/api-node/src/api/agency_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ use vcx::serde_json::json;

use crate::error::to_napi_err;

#[napi]
pub async fn update_webhook_url(webhook_url: String) -> napi::Result<()> {
agency_client::update_webhook_url(&webhook_url)
.await
.map_err(to_napi_err)
}

#[napi]
pub fn create_agency_client_for_main_wallet(config: String) -> napi::Result<()> {
let config = serde_json::from_str::<AgencyClientConfig>(&config)
Expand Down
8 changes: 8 additions & 0 deletions wrappers/node-napi-rs/api-node/src/api/agent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::error::to_napi_err;
use napi_derive::napi;
use vcx::api_vcx::api_handle::mediated_connection;

#[napi]
pub fn generate_public_invitation(public_did: String, label: String) -> napi::Result<String> {
mediated_connection::generate_public_invitation(&public_did, &label).map_err(to_napi_err)
}
37 changes: 34 additions & 3 deletions wrappers/node-napi-rs/api-node/src/api/ledger.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use napi_derive::napi;

use crate::error::to_napi_err;
use vcx::api_vcx::api_global::ledger::{ledger_get_txn_author_agreement, ledger_set_txn_author_agreement};
use vcx::api_vcx::api_global::ledger;
use vcx::serde_json::json;

#[napi]
async fn get_ledger_author_agreement() -> napi::Result<String> {
let res = ledger_get_txn_author_agreement().await.map_err(to_napi_err)?;
let res = ledger::ledger_get_txn_author_agreement().await.map_err(to_napi_err)?;
Ok(res)
}

Expand All @@ -18,5 +19,35 @@ fn set_active_txn_author_agreement_meta(
acc_mech_type: String,
time_of_acceptance: u32,
) -> napi::Result<()> {
ledger_set_txn_author_agreement(text, version, hash, acc_mech_type, time_of_acceptance as u64).map_err(to_napi_err)
ledger::ledger_set_txn_author_agreement(text, version, hash, acc_mech_type, time_of_acceptance as u64)
.map_err(to_napi_err)
}

#[napi]
async fn create_service(
target_did: String,
recipient_keys: Vec<String>,
routing_keys: Vec<String>,
endpoint: String,
) -> napi::Result<String> {
let res = ledger::ledger_write_endpoint_legacy(&target_did, recipient_keys, routing_keys, endpoint)
.await
.map_err(to_napi_err)?;
Ok(json!(res).to_string())
}

#[napi]
async fn get_service_from_ledger(target_did: String) -> napi::Result<String> {
let res = ledger::ledger_get_service(&target_did).await.map_err(to_napi_err)?;
Ok(json!(res).to_string())
}

#[napi]
async fn get_verkey_from_ledger(did: String) -> napi::Result<String> {
ledger::get_verkey_from_ledger(&did).await.map_err(to_napi_err)
}

#[napi]
async fn get_ledger_txn(seq_no: i32, submitter_did: Option<String>) -> napi::Result<String> {
ledger::get_ledger_txn(seq_no, submitter_did).await.map_err(to_napi_err)
}
1 change: 1 addition & 0 deletions wrappers/node-napi-rs/api-node/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod agency_client;
pub mod agent;
pub mod credential;
pub mod credential_definition;
pub mod disclosed_proof;
Expand Down
66 changes: 58 additions & 8 deletions wrappers/node-napi-rs/api-node/src/api/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use napi::bindgen_prelude::Buffer;
use napi_derive::napi;
use vcx::api_vcx::api_global::settings::settings_init_issuer_config;
use vcx::api_vcx::api_global::wallet::{
close_main_wallet, create_main_wallet, open_as_main_wallet, wallet_configure_issuer,
};
use vcx::aries_vcx::indy::wallet::{IssuerConfig, WalletConfig};
use vcx::api_vcx::api_global::{ledger, wallet};
use vcx::aries_vcx::indy::wallet::{IssuerConfig, RestoreWalletConfigs, WalletConfig};
use vcx::errors::error::{LibvcxError, LibvcxErrorKind};
use vcx::serde_json;
use vcx::serde_json::json;
Expand All @@ -15,7 +14,7 @@ pub async fn wallet_open_as_main(wallet_config: String) -> napi::Result<i32> {
let wallet_config = serde_json::from_str::<WalletConfig>(&wallet_config)
.map_err(|err| LibvcxError::from_msg(LibvcxErrorKind::InvalidJson, format!("Serialization error: {:?}", err)))
.map_err(to_napi_err)?;
let handle = open_as_main_wallet(&wallet_config).await.map_err(to_napi_err)?;
let handle = wallet::open_as_main_wallet(&wallet_config).await.map_err(to_napi_err)?;
Ok(handle.0)
}

Expand All @@ -24,12 +23,12 @@ pub async fn wallet_create_main(wallet_config: String) -> napi::Result<()> {
let wallet_config = serde_json::from_str::<WalletConfig>(&wallet_config)
.map_err(|err| LibvcxError::from_msg(LibvcxErrorKind::InvalidJson, format!("Serialization error: {:?}", err)))
.map_err(to_napi_err)?;
create_main_wallet(&wallet_config).await.map_err(to_napi_err)
wallet::create_main_wallet(&wallet_config).await.map_err(to_napi_err)
}

#[napi]
pub async fn wallet_close_main() -> napi::Result<()> {
close_main_wallet().await.map_err(to_napi_err)
wallet::close_main_wallet().await.map_err(to_napi_err)
}

#[napi]
Expand All @@ -42,6 +41,57 @@ pub async fn vcx_init_issuer_config(config: String) -> napi::Result<()> {

#[napi]
pub async fn configure_issuer_wallet(enterprise_seed: String) -> napi::Result<String> {
let res = wallet_configure_issuer(&enterprise_seed).await.map_err(to_napi_err)?;
let res = wallet::wallet_configure_issuer(&enterprise_seed)
.await
.map_err(to_napi_err)?;
Ok(json!(res).to_string())
}

#[napi]
pub async fn unpack(data: Buffer) -> napi::Result<String> {
let res = wallet::wallet_unpack_message_to_string(&data.to_vec())
.await
.map_err(to_napi_err)?;
Ok(json!(res).to_string())
}

#[napi]
pub async fn create_pairwise_info() -> napi::Result<String> {
let res = wallet::wallet_create_pairwise_did().await.map_err(to_napi_err)?;
Ok(json!(res).to_string())
}

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

#[napi]
pub async fn wallet_export(path: String, backup_key: String) -> napi::Result<()> {
wallet::export_main_wallet(&path, &backup_key)
.await
.map_err(to_napi_err)
}

#[napi]
pub async fn get_verkey_from_wallet(did: String) -> napi::Result<String> {
wallet::key_for_local_did(&did).await.map_err(to_napi_err)
}

#[napi]
pub async fn rotate_verkey(did: String) -> napi::Result<()> {
ledger::rotate_verkey(&did).await.map_err(to_napi_err)
}

#[napi]
pub async fn rotate_verkey_start(did: String) -> napi::Result<String> {
wallet::replace_did_keys_start(&did).await.map_err(to_napi_err)
}

#[napi]
pub async fn rotate_verkey_apply(did: String, temp_vk: String) -> napi::Result<()> {
wallet::rotate_verkey_apply(&did, &temp_vk).await.map_err(to_napi_err)
}
14 changes: 14 additions & 0 deletions wrappers/node-napi-rs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

/* auto-generated by NAPI-RS */

export function updateWebhookUrl(webhookUrl: string): Promise<void>
export function createAgencyClientForMainWallet(config: string): void
export function provisionCloudAgent(config: string): Promise<string>
export function messagesUpdateStatus(statusCode: string, uidsByConns: string): Promise<void>
Expand Down Expand Up @@ -63,6 +64,10 @@ export function issuerCredentialRelease(credentialHandle: number): void
export function issuerCredentialGetThreadId(credentialHandle: number): string
export function getLedgerAuthorAgreement(): Promise<string>
export function setActiveTxnAuthorAgreementMeta(text: string | undefined | null, version: string | undefined | null, hash: string | undefined | null, accMechType: string, timeOfAcceptance: number): void
export function createService(targetDid: string, recipientKeys: Array<string>, routingKeys: Array<string>, endpoint: string): Promise<string>
export function getServiceFromLedger(targetDid: string): Promise<string>
export function getVerkeyFromLedger(did: string): Promise<string>
export function getLedgerTxn(seqNo: number, submitterDid?: string | undefined | null): Promise<string>
export function initDefaultLogger(pattern?: string | undefined | null): void
export function mediatedConnectionGeneratePublicInvite(publicDid: string, label: string): string
export function mediatedConnectionGetPwDid(handle: number): string
Expand Down Expand Up @@ -155,3 +160,12 @@ export function walletCreateMain(walletConfig: string): Promise<void>
export function walletCloseMain(): Promise<void>
export function vcxInitIssuerConfig(config: string): Promise<void>
export function configureIssuerWallet(enterpriseSeed: string): Promise<string>
export function unpack(data: Buffer): Promise<string>
export function createPairwiseInfo(): Promise<string>
export function walletImport(config: string): Promise<void>
export function walletExport(path: string, backupKey: string): Promise<void>
export function getVerkeyFromWallet(did: string): Promise<string>
export function rotateVerkey(did: string): Promise<void>
export function rotateVerkeyStart(did: string): Promise<string>
export function rotateVerkeyApply(did: string, tempVk: string): Promise<void>
export function generatePublicInvitation(publicDid: string, label: string): string

0 comments on commit bc7984a

Please sign in to comment.