Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Patrik Stas <patrik.stas@absa.africa>
  • Loading branch information
Patrik-Stas committed Dec 29, 2022
1 parent 2aaf5f9 commit 5b51a87
Show file tree
Hide file tree
Showing 19 changed files with 365 additions and 690 deletions.
4 changes: 2 additions & 2 deletions wrappers/node-napi-rs/api-node/src/api/mediated_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub async fn mediated_connection_send_handshake_reuse(handle: u32, oob_msg: Stri
}

#[napi]
pub async fn mediated_connection_update_state_with_message(handle: u32, message: String) -> napi::Result<()> {
pub async fn mediated_connection_update_state_with_message(handle: u32, message: String) -> napi::Result<u32> {
mediated_connection::update_state_with_message(handle, &message)
.await
.map_err(to_napi_err)
Expand All @@ -113,7 +113,7 @@ pub async fn mediated_connection_handle_message(handle: u32, message: String) ->
}

#[napi]
pub async fn mediated_connection_update_state(handle: u32) -> napi::Result<()> {
pub async fn mediated_connection_update_state(handle: u32) -> napi::Result<u32> {
mediated_connection::update_state(handle)
.await
.map_err(to_napi_err)
Expand Down
9 changes: 7 additions & 2 deletions wrappers/node-napi-rs/api-node/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use vcx::aries_vcx::errors::error::AriesVcxError;
use vcx::errors::error::LibvcxError;
use vcx::serde_json::json;

pub fn to_napi_err(err: LibvcxError) -> napi::Error {
error!("{}", err.to_string());
napi::Error::new(napi::Status::Unknown, format!("{:?}", Into::<u32>::into(err.kind())))
let reason = json!({
"vcxErrKind": err.kind().to_string(),
"vcxErrCode": u32::from(err.kind()),
"vcxErrMessage": err.msg,
}).to_string();
napi::Error::new(napi::Status::GenericFailure, format!("vcx_err_json:{reason}"))
}

pub fn ariesvcx_to_napi_err(err: AriesVcxError) -> napi::Error {
Expand Down
4 changes: 2 additions & 2 deletions wrappers/node-napi-rs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ export function mediatedConnectionCreateWithConnectionRequest(request: string, a
export function mediatedConnectionSendMessage(handle: number, msg: string): Promise<void>
export function mediatedConnectionCreateWithConnectionRequestV2(request: string, pwInfo: string): Promise<number>
export function mediatedConnectionSendHandshakeReuse(handle: number, oobMsg: string): Promise<void>
export function mediatedConnectionUpdateStateWithMessage(handle: number, message: string): Promise<void>
export function mediatedConnectionUpdateStateWithMessage(handle: number, message: string): Promise<number>
export function mediatedConnectionHandleMessage(handle: number, message: string): Promise<void>
export function mediatedConnectionUpdateState(handle: number): Promise<void>
export function mediatedConnectionUpdateState(handle: number): Promise<number>
export function mediatedConnectionDeleteConnection(handle: number): Promise<void>
export function mediatedConnectionConnect(handle: number): Promise<string | null>
export function mediatedConnectionSerialize(handle: number): string
Expand Down
74 changes: 28 additions & 46 deletions wrappers/node/src/api/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,72 +4,54 @@ import { Callback } from 'ffi-napi';
import { VCXInternalError } from '../errors';
import { rustAPI } from '../rustlib';
import { createFFICallbackPromise } from '../utils/ffi-helpers';
import { VCXInternalErrorNapirs } from '../errors-napirs';

export function initThreadpool (config: object) {
const rc = rustAPI().vcx_init_threadpool(JSON.stringify(config))
export function initThreadpool(config: object) {
const rc = rustAPI().vcx_init_threadpool(JSON.stringify(config));
if (rc !== 0) {
throw new VCXInternalError(rc)
throw new VCXInternalError(rc);
}
}

export function createAgencyClientForMainWallet (config: object): void {
export function createAgencyClientForMainWallet(config: object): void {
try {
ffiNapi.createAgencyClientForMainWallet(JSON.stringify(config))
ffiNapi.createAgencyClientForMainWallet(JSON.stringify(config));
} catch (err: any) {
throw new VCXInternalError(err)
throw new VCXInternalError(err);
}
}

export async function initIssuerConfig (config: object): Promise<void> {
export async function initIssuerConfig(config: object): Promise<void> {
try {
return await createFFICallbackPromise<void>(
(resolve, reject, cb) => {
const rc = rustAPI().vcx_init_issuer_config(0, JSON.stringify(config), cb)
if (rc) {
reject(rc)
}
},
(resolve, reject) => Callback(
'void',
['uint32', 'uint32'],
(xhandle: number, err: number) => {
if (err) {
reject(err)
return
}
resolve()
})
)
return await ffiNapi.vcxInitIssuerConfig(JSON.stringify(config));
} catch (err: any) {
throw new VCXInternalError(err)
throw new VCXInternalErrorNapirs(err);
}
}

export async function openMainPool (config: object): Promise<void> {
export async function openMainPool(config: object): Promise<void> {
try {
return await createFFICallbackPromise<void>(
(resolve, reject, cb) => {
const rc = rustAPI().vcx_open_main_pool(0, JSON.stringify(config), cb)
if (rc) {
reject(rc)
}
},
(resolve, reject) => Callback(
'void',
['uint32', 'uint32'],
(xhandle: number, err: number) => {
if (err) {
reject(err)
return
}
resolve()
})
)
(resolve, reject, cb) => {
const rc = rustAPI().vcx_open_main_pool(0, JSON.stringify(config), cb);
if (rc) {
reject(rc);
}
},
(resolve, reject) =>
Callback('void', ['uint32', 'uint32'], (xhandle: number, err: number) => {
if (err) {
reject(err);
return;
}
resolve();
}),
);
} catch (err: any) {
throw new VCXInternalError(err)
throw new VCXInternalError(err);
}
}

export function enableMocks(): void {
return ffiNapi.enableMocks()
return ffiNapi.enableMocks();
}
26 changes: 13 additions & 13 deletions wrappers/node/src/api/issuer-credential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Connection, IConnectionData } from './mediated-connection';
import { CredentialDef } from './credential-def';
import { RevocationRegistry } from './revocation-registry';
import { VCXBaseWithState1 } from './vcx-base-with-state-1';
import { VCXInternalError1 } from '../errors-1';
import { VCXInternalErrorNapirs } from '../errors-napirs';


/**
Expand Down Expand Up @@ -83,7 +83,7 @@ export class IssuerCredential extends VCXBaseWithState1<IIssuerCredentialData, I
connection._setHandle(await ffiNapi.issuerCredentialCreate(sourceId));
return connection;
} catch (err: any) {
throw new VCXInternalError1(err);
throw new VCXInternalErrorNapirs(err);
}
}

Expand All @@ -93,7 +93,7 @@ export class IssuerCredential extends VCXBaseWithState1<IIssuerCredentialData, I
try {
return super._deserialize(IssuerCredential, serializedData);
} catch (err: any) {
throw new VCXInternalError1(err);
throw new VCXInternalErrorNapirs(err);
}
}

Expand All @@ -115,23 +115,23 @@ export class IssuerCredential extends VCXBaseWithState1<IIssuerCredentialData, I
message,
);
} catch (err: any) {
throw new VCXInternalError1(err);
throw new VCXInternalErrorNapirs(err);
}
}

public async sendOfferV2(connection: Connection): Promise<void> {
try {
return await ffiNapi.issuerCredentialSendOfferV2(this.handle, connection.handle);
} catch (err: any) {
throw new VCXInternalError1(err);
throw new VCXInternalErrorNapirs(err);
}
}

public async markCredentialOfferMsgSent(): Promise<void> {
try {
return await ffiNapi.issuerCredentialMarkOfferMsgSent(this.handle);
} catch (err: any) {
throw new VCXInternalError1(err);
throw new VCXInternalErrorNapirs(err);
}
}

Expand All @@ -150,31 +150,31 @@ export class IssuerCredential extends VCXBaseWithState1<IIssuerCredentialData, I
comment || '',
);
} catch (err: any) {
throw new VCXInternalError1(err);
throw new VCXInternalErrorNapirs(err);
}
}

public async getCredentialOfferMsg(): Promise<string> {
try {
return await ffiNapi.issuerCredentialGetOfferMsg(this.handle);
} catch (err: any) {
throw new VCXInternalError1(err);
throw new VCXInternalErrorNapirs(err);
}
}

public async getThreadId(): Promise<string> {
try {
return await ffiNapi.issuerCredentialGetThreadId(this.handle);
} catch (err: any) {
throw new VCXInternalError1(err);
throw new VCXInternalErrorNapirs(err);
}
}

public async sendCredential(connection: Connection): Promise<number> {
try {
return await ffiNapi.issuerCredentialSendCredential(this.handle, connection.handle);
} catch (err: any) {
throw new VCXInternalError1(err);
throw new VCXInternalErrorNapirs(err);
}
}

Expand All @@ -186,23 +186,23 @@ export class IssuerCredential extends VCXBaseWithState1<IIssuerCredentialData, I
try {
return await ffiNapi.issuerCredentialRevokeLocal(this.handle);
} catch (err: any) {
throw new VCXInternalError1(err);
throw new VCXInternalErrorNapirs(err);
}
}

public async isRevokable(): Promise<boolean> {
try {
return await ffiNapi.issuerCredentialIsRevokable(this.handle);
} catch (err: any) {
throw new VCXInternalError1(err);
throw new VCXInternalErrorNapirs(err);
}
}

public async getRevRegId(): Promise<string> {
try {
return await ffiNapi.issuerCredentialGetRevRegId(this.handle);
} catch (err: any) {
throw new VCXInternalError1(err);
throw new VCXInternalErrorNapirs(err);
}
}

Expand Down
Loading

0 comments on commit 5b51a87

Please sign in to comment.