diff --git a/wrappers/javascript/indy-vdr-nodejs/src/NodeJSIndyVdr.ts b/wrappers/javascript/indy-vdr-nodejs/src/NodeJSIndyVdr.ts index b7aad68d..5dc0da71 100644 --- a/wrappers/javascript/indy-vdr-nodejs/src/NodeJSIndyVdr.ts +++ b/wrappers/javascript/indy-vdr-nodejs/src/NodeJSIndyVdr.ts @@ -17,6 +17,7 @@ import type { GetTransactionRequestOptions, GetValidatorInfoActionOptions, IndyVdr, + IndyVdrErrorObject, NymRequestOptions, PoolCreateOptions, PoolStatus, @@ -37,7 +38,6 @@ import type { import { handleInvalidNullResponse, IndyVdrError } from '@hyperledger/indy-vdr-shared' -import { handleError } from './error' import { deallocateCallback, allocateHandle, @@ -46,7 +46,7 @@ import { toNativeCallbackWithResponse, serializeArguments, } from './ffi' -import { nativeIndyVdr } from './library' +import { getNativeIndyVdr } from './library' function handleReturnPointer(returnValue: Buffer): Return { if (returnValue.address() === 0) { @@ -63,7 +63,7 @@ export class NodeJSIndyVdr implements IndyVdr { deallocateCallback(id) try { - handleError(errorCode) + this.handleError(errorCode) } catch (e) { reject(e) } @@ -84,7 +84,7 @@ export class NodeJSIndyVdr implements IndyVdr { deallocateCallback(id) try { - handleError(errorCode) + this.handleError(errorCode) } catch (e) { return reject(e) } @@ -105,42 +105,63 @@ export class NodeJSIndyVdr implements IndyVdr { }) } + private handleError(code: number) { + if (code === 0) return + + const nativeError = allocateString() + this.nativeIndyVdr.indy_vdr_get_current_error(nativeError) + + const indyVdrErrorObject = JSON.parse(nativeError.deref() as string) as IndyVdrErrorObject + + throw new IndyVdrError(indyVdrErrorObject) + } + + public get nativeIndyVdr() { + return getNativeIndyVdr() + } + public getCurrentError(): string { const error = allocateString() - handleError(nativeIndyVdr.indy_vdr_get_current_error(error)) + this.handleError(this.nativeIndyVdr.indy_vdr_get_current_error(error)) return handleReturnPointer(error) } public version(): string { - return nativeIndyVdr.indy_vdr_version() + return this.nativeIndyVdr.indy_vdr_version() } public setConfig(options: { config: Record }): void { const { config } = serializeArguments(options) - handleError(nativeIndyVdr.indy_vdr_set_config(config)) + this.handleError(this.nativeIndyVdr.indy_vdr_set_config(config)) } public setDefaultLogger(): void { - handleError(nativeIndyVdr.indy_vdr_set_default_logger()) + this.handleError(this.nativeIndyVdr.indy_vdr_set_default_logger()) } public setProtocolVersion(options: { version: number }): void { const { version } = serializeArguments(options) - handleError(nativeIndyVdr.indy_vdr_set_protocol_version(version)) + this.handleError(this.nativeIndyVdr.indy_vdr_set_protocol_version(version)) } public setSocksProxy(options: { socksProxy: string }): void { const { socksProxy } = serializeArguments(options) - handleError(nativeIndyVdr.indy_vdr_set_socks_proxy(socksProxy)) + this.handleError(this.nativeIndyVdr.indy_vdr_set_socks_proxy(socksProxy)) } public buildAcceptanceMechanismsRequest(options: AcceptanceMechanismsRequestOptions): number { const requestHandle = allocateHandle() const { version, aml, submitterDid, amlContext } = serializeArguments(options) - handleError( - nativeIndyVdr.indy_vdr_build_acceptance_mechanisms_request(submitterDid, aml, version, amlContext, requestHandle) + this.handleError( + this.nativeIndyVdr.indy_vdr_build_acceptance_mechanisms_request( + submitterDid, + aml, + version, + amlContext, + requestHandle + ) ) return handleReturnPointer(requestHandle) @@ -153,8 +174,8 @@ export class NodeJSIndyVdr implements IndyVdr { // We cannot handle this step in the serialization. Indy-vdr expects a -1 for an undefined timestamp const convertedTimestamp = timestamp ?? -1 - handleError( - nativeIndyVdr.indy_vdr_build_get_acceptance_mechanisms_request( + this.handleError( + this.nativeIndyVdr.indy_vdr_build_get_acceptance_mechanisms_request( submitterDid, convertedTimestamp, version, @@ -169,7 +190,9 @@ export class NodeJSIndyVdr implements IndyVdr { const requestHandle = allocateHandle() const { submitterDid, targetDid, raw, hash, enc } = serializeArguments(options) - handleError(nativeIndyVdr.indy_vdr_build_attrib_request(submitterDid, targetDid, hash, raw, enc, requestHandle)) + this.handleError( + this.nativeIndyVdr.indy_vdr_build_attrib_request(submitterDid, targetDid, hash, raw, enc, requestHandle) + ) return handleReturnPointer(requestHandle) } @@ -178,7 +201,9 @@ export class NodeJSIndyVdr implements IndyVdr { const requestHandle = allocateHandle() const { submitterDid, targetDid, raw, hash, enc } = serializeArguments(options) - handleError(nativeIndyVdr.indy_vdr_build_get_attrib_request(submitterDid, targetDid, raw, hash, enc, requestHandle)) + this.handleError( + this.nativeIndyVdr.indy_vdr_build_get_attrib_request(submitterDid, targetDid, raw, hash, enc, requestHandle) + ) return handleReturnPointer(requestHandle) } @@ -187,7 +212,9 @@ export class NodeJSIndyVdr implements IndyVdr { const requestHandle = allocateHandle() const { credentialDefinition, submitterDid } = serializeArguments(options) - handleError(nativeIndyVdr.indy_vdr_build_cred_def_request(submitterDid, credentialDefinition, requestHandle)) + this.handleError( + this.nativeIndyVdr.indy_vdr_build_cred_def_request(submitterDid, credentialDefinition, requestHandle) + ) return handleReturnPointer(requestHandle) } @@ -196,7 +223,9 @@ export class NodeJSIndyVdr implements IndyVdr { const requestHandle = allocateHandle() const { credentialDefinitionId, submitterDid } = serializeArguments(options) - handleError(nativeIndyVdr.indy_vdr_build_get_cred_def_request(submitterDid, credentialDefinitionId, requestHandle)) + this.handleError( + this.nativeIndyVdr.indy_vdr_build_get_cred_def_request(submitterDid, credentialDefinitionId, requestHandle) + ) return handleReturnPointer(requestHandle) } @@ -205,8 +234,8 @@ export class NodeJSIndyVdr implements IndyVdr { const requestHandle = allocateHandle() const { revocationRegistryId, submitterDid } = serializeArguments(options) - handleError( - nativeIndyVdr.indy_vdr_build_get_revoc_reg_def_request(submitterDid, revocationRegistryId, requestHandle) + this.handleError( + this.nativeIndyVdr.indy_vdr_build_get_revoc_reg_def_request(submitterDid, revocationRegistryId, requestHandle) ) return handleReturnPointer(requestHandle) @@ -216,8 +245,13 @@ export class NodeJSIndyVdr implements IndyVdr { const requestHandle = allocateHandle() const { revocationRegistryId, timestamp, submitterDid } = serializeArguments(options) - handleError( - nativeIndyVdr.indy_vdr_build_get_revoc_reg_request(submitterDid, revocationRegistryId, timestamp, requestHandle) + this.handleError( + this.nativeIndyVdr.indy_vdr_build_get_revoc_reg_request( + submitterDid, + revocationRegistryId, + timestamp, + requestHandle + ) ) return handleReturnPointer(requestHandle) @@ -229,8 +263,8 @@ export class NodeJSIndyVdr implements IndyVdr { const convertedFromTs = fromTs ?? -1 - handleError( - nativeIndyVdr.indy_vdr_build_get_revoc_reg_delta_request( + this.handleError( + this.nativeIndyVdr.indy_vdr_build_get_revoc_reg_delta_request( submitterDid, revocationRegistryId, convertedFromTs, @@ -246,8 +280,8 @@ export class NodeJSIndyVdr implements IndyVdr { const requestHandle = allocateHandle() const { submitterDid, revocationRegistryDefinitionV1: revocationRegistryDefinition } = serializeArguments(options) - handleError( - nativeIndyVdr.indy_vdr_build_revoc_reg_def_request(submitterDid, revocationRegistryDefinition, requestHandle) + this.handleError( + this.nativeIndyVdr.indy_vdr_build_revoc_reg_def_request(submitterDid, revocationRegistryDefinition, requestHandle) ) return handleReturnPointer(requestHandle) @@ -257,7 +291,7 @@ export class NodeJSIndyVdr implements IndyVdr { const requestHandle = allocateHandle() const { customRequest } = serializeArguments(options) - handleError(nativeIndyVdr.indy_vdr_build_custom_request(customRequest, requestHandle)) + this.handleError(this.nativeIndyVdr.indy_vdr_build_custom_request(customRequest, requestHandle)) return handleReturnPointer(requestHandle) } @@ -268,7 +302,9 @@ export class NodeJSIndyVdr implements IndyVdr { const requestHandle = allocateHandle() const { submitterDid } = serializeArguments(options) - handleError(nativeIndyVdr.indy_vdr_build_disable_all_txn_author_agreements_request(submitterDid, requestHandle)) + this.handleError( + this.nativeIndyVdr.indy_vdr_build_disable_all_txn_author_agreements_request(submitterDid, requestHandle) + ) return handleReturnPointer(requestHandle) } @@ -277,7 +313,7 @@ export class NodeJSIndyVdr implements IndyVdr { const requestHandle = allocateHandle() const { dest, submitterDid } = serializeArguments(options) - handleError(nativeIndyVdr.indy_vdr_build_get_nym_request(submitterDid, dest, requestHandle)) + this.handleError(this.nativeIndyVdr.indy_vdr_build_get_nym_request(submitterDid, dest, requestHandle)) return handleReturnPointer(requestHandle) } @@ -286,7 +322,7 @@ export class NodeJSIndyVdr implements IndyVdr { const requestHandle = allocateHandle() const { schemaId, submitterDid } = serializeArguments(options) - handleError(nativeIndyVdr.indy_vdr_build_get_schema_request(submitterDid, schemaId, requestHandle)) + this.handleError(this.nativeIndyVdr.indy_vdr_build_get_schema_request(submitterDid, schemaId, requestHandle)) return handleReturnPointer(requestHandle) } @@ -295,7 +331,9 @@ export class NodeJSIndyVdr implements IndyVdr { const requestHandle = allocateHandle() const { data, submitterDid } = serializeArguments(options) - handleError(nativeIndyVdr.indy_vdr_build_get_txn_author_agreement_request(submitterDid, data, requestHandle)) + this.handleError( + this.nativeIndyVdr.indy_vdr_build_get_txn_author_agreement_request(submitterDid, data, requestHandle) + ) return handleReturnPointer(requestHandle) } @@ -304,7 +342,7 @@ export class NodeJSIndyVdr implements IndyVdr { const requestHandle = allocateHandle() const { ledgerType, seqNo, submitterDid } = serializeArguments(options) - handleError(nativeIndyVdr.indy_vdr_build_get_txn_request(submitterDid, ledgerType, seqNo, requestHandle)) + this.handleError(this.nativeIndyVdr.indy_vdr_build_get_txn_request(submitterDid, ledgerType, seqNo, requestHandle)) return handleReturnPointer(requestHandle) } @@ -313,7 +351,7 @@ export class NodeJSIndyVdr implements IndyVdr { const requestHandle = allocateHandle() const { submitterDid } = serializeArguments(options) - handleError(nativeIndyVdr.indy_vdr_build_get_validator_info_request(submitterDid, requestHandle)) + this.handleError(this.nativeIndyVdr.indy_vdr_build_get_validator_info_request(submitterDid, requestHandle)) return handleReturnPointer(requestHandle) } @@ -322,7 +360,9 @@ export class NodeJSIndyVdr implements IndyVdr { const requestHandle = allocateHandle() const { dest, submitterDid, alias, role, verkey } = serializeArguments(options) - handleError(nativeIndyVdr.indy_vdr_build_nym_request(submitterDid, dest, verkey, alias, role, requestHandle)) + this.handleError( + this.nativeIndyVdr.indy_vdr_build_nym_request(submitterDid, dest, verkey, alias, role, requestHandle) + ) return handleReturnPointer(requestHandle) } @@ -332,8 +372,8 @@ export class NodeJSIndyVdr implements IndyVdr { const { revocationRegistryDefinitionId, revocationRegistryDefinitionType, revocationRegistryEntry, submitterDid } = serializeArguments(options) - handleError( - nativeIndyVdr.indy_vdr_build_revoc_reg_entry_request( + this.handleError( + this.nativeIndyVdr.indy_vdr_build_revoc_reg_entry_request( submitterDid, revocationRegistryDefinitionId, revocationRegistryDefinitionType, @@ -349,7 +389,7 @@ export class NodeJSIndyVdr implements IndyVdr { const requestHandle = allocateHandle() const { schema, submitterDid } = serializeArguments(options) - handleError(nativeIndyVdr.indy_vdr_build_schema_request(submitterDid, schema, requestHandle)) + this.handleError(this.nativeIndyVdr.indy_vdr_build_schema_request(submitterDid, schema, requestHandle)) return handleReturnPointer(requestHandle) } @@ -361,8 +401,8 @@ export class NodeJSIndyVdr implements IndyVdr { const convertedRatificationTs = ratificationTs ?? -1 const convertedRetirementTs = retirementTs ?? -1 - handleError( - nativeIndyVdr.indy_vdr_build_txn_author_agreement_request( + this.handleError( + this.nativeIndyVdr.indy_vdr_build_txn_author_agreement_request( submitterDid, text, version, @@ -379,7 +419,7 @@ export class NodeJSIndyVdr implements IndyVdr { const poolHandle = allocateHandle() const { parameters } = serializeArguments(options) - handleError(nativeIndyVdr.indy_vdr_pool_create(parameters, poolHandle)) + this.handleError(this.nativeIndyVdr.indy_vdr_pool_create(parameters, poolHandle)) return handleReturnPointer(poolHandle) } @@ -387,14 +427,14 @@ export class NodeJSIndyVdr implements IndyVdr { public async poolRefresh(options: { poolHandle: number }): Promise { const { poolHandle } = serializeArguments(options) - return this.promisify((cbPtr, id) => nativeIndyVdr.indy_vdr_pool_refresh(poolHandle, cbPtr, id)) + return this.promisify((cbPtr, id) => this.nativeIndyVdr.indy_vdr_pool_refresh(poolHandle, cbPtr, id)) } public async poolGetStatus(options: { poolHandle: number }): Promise { const { poolHandle } = serializeArguments(options) const poolStatus = await this.promisifyWithResponse((cbPtr, id) => - nativeIndyVdr.indy_vdr_pool_get_status(poolHandle, cbPtr, id) + this.nativeIndyVdr.indy_vdr_pool_get_status(poolHandle, cbPtr, id) ) return handleInvalidNullResponse(poolStatus) @@ -404,7 +444,7 @@ export class NodeJSIndyVdr implements IndyVdr { const { poolHandle } = serializeArguments(options) const transactions = await this.promisifyWithResponse( - (cbPtr, id) => nativeIndyVdr.indy_vdr_pool_get_transactions(poolHandle, cbPtr, id), + (cbPtr, id) => this.nativeIndyVdr.indy_vdr_pool_get_transactions(poolHandle, cbPtr, id), true ) @@ -415,7 +455,7 @@ export class NodeJSIndyVdr implements IndyVdr { const { poolHandle } = serializeArguments(options) const verifiers = await this.promisifyWithResponse((cbPtr, id) => - nativeIndyVdr.indy_vdr_pool_get_verifiers(poolHandle, cbPtr, id) + this.nativeIndyVdr.indy_vdr_pool_get_verifiers(poolHandle, cbPtr, id) ) return handleInvalidNullResponse(verifiers) @@ -425,7 +465,7 @@ export class NodeJSIndyVdr implements IndyVdr { const { requestHandle, poolHandle, nodes, timeout } = serializeArguments(options) const response = await this.promisifyWithResponse((cbPtr, id) => - nativeIndyVdr.indy_vdr_pool_submit_action(poolHandle, requestHandle, nodes, timeout, cbPtr, id) + this.nativeIndyVdr.indy_vdr_pool_submit_action(poolHandle, requestHandle, nodes, timeout, cbPtr, id) ) return handleInvalidNullResponse(response) @@ -435,7 +475,7 @@ export class NodeJSIndyVdr implements IndyVdr { const { requestHandle, poolHandle } = serializeArguments(options) const response = await this.promisifyWithResponse((cbPtr, id) => - nativeIndyVdr.indy_vdr_pool_submit_request(poolHandle, requestHandle, cbPtr, id) + this.nativeIndyVdr.indy_vdr_pool_submit_request(poolHandle, requestHandle, cbPtr, id) ) return handleInvalidNullResponse(response) @@ -444,15 +484,15 @@ export class NodeJSIndyVdr implements IndyVdr { public poolClose(options: { poolHandle: number }): void { const { poolHandle } = serializeArguments(options) - handleError(nativeIndyVdr.indy_vdr_pool_close(poolHandle)) + this.handleError(this.nativeIndyVdr.indy_vdr_pool_close(poolHandle)) } public prepareTxnAuthorAgreementAcceptance(options: PrepareTxnAuthorAgreementAcceptanceOptions): string { const output = allocateString() const { acceptanceMechanismType, time, taaDigest, text, version } = serializeArguments(options) - handleError( - nativeIndyVdr.indy_vdr_prepare_txn_author_agreement_acceptance( + this.handleError( + this.nativeIndyVdr.indy_vdr_prepare_txn_author_agreement_acceptance( text, version, taaDigest, @@ -468,14 +508,14 @@ export class NodeJSIndyVdr implements IndyVdr { public requestFree(options: { requestHandle: number }): void { const { requestHandle } = serializeArguments(options) - handleError(nativeIndyVdr.indy_vdr_request_free(requestHandle)) + this.handleError(this.nativeIndyVdr.indy_vdr_request_free(requestHandle)) } public requestGetBody>(options: { requestHandle: number }): T { const output = allocateString() const { requestHandle } = serializeArguments(options) - handleError(nativeIndyVdr.indy_vdr_request_get_body(requestHandle, output)) + this.handleError(this.nativeIndyVdr.indy_vdr_request_get_body(requestHandle, output)) const outputString = handleReturnPointer(output) return JSON.parse(outputString) as T @@ -485,7 +525,7 @@ export class NodeJSIndyVdr implements IndyVdr { const output = allocateString() const { requestHandle } = serializeArguments(options) - handleError(nativeIndyVdr.indy_vdr_request_get_signature_input(requestHandle, output)) + this.handleError(this.nativeIndyVdr.indy_vdr_request_get_signature_input(requestHandle, output)) return handleReturnPointer(output) } @@ -493,19 +533,19 @@ export class NodeJSIndyVdr implements IndyVdr { public requestSetEndorser(options: RequestSetEndorserOptions & { requestHandle: number }): void { const { endorser, requestHandle } = serializeArguments(options) - handleError(nativeIndyVdr.indy_vdr_request_set_endorser(requestHandle, endorser)) + this.handleError(this.nativeIndyVdr.indy_vdr_request_set_endorser(requestHandle, endorser)) } public requestSetMultiSignature(options: RequestSetMultiSignatureOptions & { requestHandle: number }): void { const { identifier, requestHandle, signature } = serializeArguments(options) - handleError(nativeIndyVdr.indy_vdr_request_set_multi_signature(requestHandle, identifier, signature)) + this.handleError(this.nativeIndyVdr.indy_vdr_request_set_multi_signature(requestHandle, identifier, signature)) } public requestSetSignature(options: RequestSetSignatureOptions & { requestHandle: number }): void { const { requestHandle, signature } = serializeArguments(options) - handleError(nativeIndyVdr.indy_vdr_request_set_signature(requestHandle, signature)) + this.handleError(this.nativeIndyVdr.indy_vdr_request_set_signature(requestHandle, signature)) } public requestSetTxnAuthorAgreementAcceptance( @@ -513,6 +553,6 @@ export class NodeJSIndyVdr implements IndyVdr { ): void { const { acceptance, requestHandle } = serializeArguments(options) - handleError(nativeIndyVdr.indy_vdr_request_set_txn_author_agreement_acceptance(requestHandle, acceptance)) + this.handleError(this.nativeIndyVdr.indy_vdr_request_set_txn_author_agreement_acceptance(requestHandle, acceptance)) } } diff --git a/wrappers/javascript/indy-vdr-nodejs/src/error.ts b/wrappers/javascript/indy-vdr-nodejs/src/error.ts deleted file mode 100644 index 18521c47..00000000 --- a/wrappers/javascript/indy-vdr-nodejs/src/error.ts +++ /dev/null @@ -1,17 +0,0 @@ -import type { IndyVdrErrorObject } from '@hyperledger/indy-vdr-shared' - -import { IndyVdrError } from '@hyperledger/indy-vdr-shared' - -import { allocateString } from './ffi' -import { nativeIndyVdr } from './library' - -export const handleError = (code: number) => { - if (code === 0) return - - const nativeError = allocateString() - nativeIndyVdr.indy_vdr_get_current_error(nativeError) - - const indyVdrErrorObject = JSON.parse(nativeError.deref() as string) as IndyVdrErrorObject - - throw new IndyVdrError(indyVdrErrorObject) -} diff --git a/wrappers/javascript/indy-vdr-nodejs/src/library/register.ts b/wrappers/javascript/indy-vdr-nodejs/src/library/register.ts index c425c3b6..8036eb4a 100644 --- a/wrappers/javascript/indy-vdr-nodejs/src/library/register.ts +++ b/wrappers/javascript/indy-vdr-nodejs/src/library/register.ts @@ -70,6 +70,8 @@ const getLibrary = () => { return Library(validLibraryPath, nativeBindings) } -// eslint-disable-next-line @typescript-eslint/ban-ts-comment -// @ts-ignore -export const nativeIndyVdr = getLibrary() as NativeMethods +let nativeIndyVdr: NativeMethods | undefined = undefined +export const getNativeIndyVdr = () => { + if (!nativeIndyVdr) nativeIndyVdr = getLibrary() as unknown as NativeMethods + return nativeIndyVdr +}