Skip to content

Commit b544c9c

Browse files
committed
chore: byte buffer with free
Signed-off-by: Timo Glastra <[email protected]>
1 parent ee612b0 commit b544c9c

File tree

2 files changed

+102
-107
lines changed

2 files changed

+102
-107
lines changed

packages/askar-nodejs/src/NodeJSAskar.ts

Lines changed: 94 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,95 @@
1+
import type {
2+
AeadParamsOptions,
3+
Askar,
4+
AskarErrorObject,
5+
EncryptedBuffer,
6+
EntryListCountOptions,
7+
EntryListFreeOptions,
8+
EntryListGetCategoryOptions,
9+
EntryListGetNameOptions,
10+
EntryListGetTagsOptions,
11+
EntryListGetValueOptions,
12+
KeyAeadDecryptOptions,
13+
KeyAeadEncryptOptions,
14+
KeyAeadGetPaddingOptions,
15+
KeyAeadGetParamsOptions,
16+
KeyAeadRandomNonceOptions,
17+
KeyConvertOptions,
18+
KeyCryptoBoxOpenOptions,
19+
KeyCryptoBoxOptions,
20+
KeyCryptoBoxSealOpenOptions,
21+
KeyCryptoBoxSealOptions,
22+
KeyDeriveEcdh1puOptions,
23+
KeyDeriveEcdhEsOptions,
24+
KeyEntryListCountOptions,
25+
KeyEntryListFreeOptions,
26+
KeyEntryListGetAlgorithmOptions,
27+
KeyEntryListGetMetadataOptions,
28+
KeyEntryListGetNameOptions,
29+
KeyEntryListGetTagsOptions,
30+
KeyEntryListLoadLocalOptions,
31+
KeyFreeOptions,
32+
KeyFromJwkOptions,
33+
KeyFromKeyExchangeOptions,
34+
KeyFromPublicBytesOptions,
35+
KeyFromSecretBytesOptions,
36+
KeyFromSeedOptions,
37+
KeyGenerateOptions,
38+
KeyGetAlgorithmOptions,
39+
KeyGetEphemeralOptions,
40+
KeyGetJwkPublicOptions,
41+
KeyGetJwkSecretOptions,
42+
KeyGetJwkThumbprintOptions,
43+
KeyGetPublicBytesOptions,
44+
KeyGetSecretBytesOptions,
45+
KeySignMessageOptions,
46+
KeyUnwrapKeyOptions,
47+
KeyVerifySignatureOptions,
48+
KeyWrapKeyOptions,
49+
MigrateIndySdkOptions,
50+
ScanFreeOptions,
51+
ScanNextOptions,
52+
ScanStartOptions,
53+
SessionCloseOptions,
54+
SessionCountOptions,
55+
SessionFetchAllKeysOptions,
56+
SessionFetchAllOptions,
57+
SessionFetchKeyOptions,
58+
SessionFetchOptions,
59+
SessionInsertKeyOptions,
60+
SessionRemoveAllOptions,
61+
SessionRemoveKeyOptions,
62+
SessionStartOptions,
63+
SessionUpdateKeyOptions,
64+
SessionUpdateOptions,
65+
SetCustomLoggerOptions,
66+
SetMaxLogLevelOptions,
67+
StoreCloseOptions,
68+
StoreCopyToOptions,
69+
StoreCreateProfileOptions,
70+
StoreGenerateRawKeyOptions,
71+
StoreGetDefaultProfileOptions,
72+
StoreGetProfileNameOptions,
73+
StoreListProfilesOptions,
74+
StoreOpenOptions,
75+
StoreProvisionOptions,
76+
StoreRekeyOptions,
77+
StoreRemoveOptions,
78+
StoreRemoveProfileOptions,
79+
StoreSetDefaultProfileOptions,
80+
} from '@openwallet-foundation/askar-shared'
181
import {
282
AeadParams,
3-
type AeadParamsOptions,
4-
type Askar,
583
AskarError,
6-
type AskarErrorObject,
7-
type EncryptedBuffer,
8-
type EntryListCountOptions,
9-
type EntryListFreeOptions,
10-
type EntryListGetCategoryOptions,
11-
type EntryListGetNameOptions,
12-
type EntryListGetTagsOptions,
13-
type EntryListGetValueOptions,
1484
EntryListHandle,
15-
type KeyAeadDecryptOptions,
16-
type KeyAeadEncryptOptions,
17-
type KeyAeadGetPaddingOptions,
18-
type KeyAeadGetParamsOptions,
19-
type KeyAeadRandomNonceOptions,
20-
type KeyConvertOptions,
21-
type KeyCryptoBoxOpenOptions,
22-
type KeyCryptoBoxOptions,
23-
type KeyCryptoBoxSealOpenOptions,
24-
type KeyCryptoBoxSealOptions,
25-
type KeyDeriveEcdh1puOptions,
26-
type KeyDeriveEcdhEsOptions,
27-
type KeyEntryListCountOptions,
28-
type KeyEntryListFreeOptions,
29-
type KeyEntryListGetAlgorithmOptions,
30-
type KeyEntryListGetMetadataOptions,
31-
type KeyEntryListGetNameOptions,
32-
type KeyEntryListGetTagsOptions,
3385
KeyEntryListHandle,
34-
type KeyEntryListLoadLocalOptions,
35-
type KeyFreeOptions,
36-
type KeyFromJwkOptions,
37-
type KeyFromKeyExchangeOptions,
38-
type KeyFromPublicBytesOptions,
39-
type KeyFromSecretBytesOptions,
40-
type KeyFromSeedOptions,
41-
type KeyGenerateOptions,
42-
type KeyGetAlgorithmOptions,
43-
type KeyGetEphemeralOptions,
44-
type KeyGetJwkPublicOptions,
45-
type KeyGetJwkSecretOptions,
46-
type KeyGetJwkThumbprintOptions,
47-
type KeyGetPublicBytesOptions,
48-
type KeyGetSecretBytesOptions,
49-
type KeySignMessageOptions,
50-
type KeyUnwrapKeyOptions,
51-
type KeyVerifySignatureOptions,
52-
type KeyWrapKeyOptions,
5386
LocalKeyHandle,
54-
type MigrateIndySdkOptions,
55-
type ScanFreeOptions,
5687
ScanHandle,
57-
type ScanNextOptions,
58-
type ScanStartOptions,
59-
type SessionCloseOptions,
60-
type SessionCountOptions,
61-
type SessionFetchAllKeysOptions,
62-
type SessionFetchAllOptions,
63-
type SessionFetchKeyOptions,
64-
type SessionFetchOptions,
6588
SessionHandle,
66-
type SessionInsertKeyOptions,
67-
type SessionRemoveAllOptions,
68-
type SessionRemoveKeyOptions,
69-
type SessionStartOptions,
70-
type SessionUpdateKeyOptions,
71-
type SessionUpdateOptions,
72-
type SetCustomLoggerOptions,
73-
type SetMaxLogLevelOptions,
74-
type StoreCloseOptions,
75-
type StoreCopyToOptions,
76-
type StoreCreateProfileOptions,
77-
type StoreGenerateRawKeyOptions,
78-
type StoreGetDefaultProfileOptions,
79-
type StoreGetProfileNameOptions,
8089
StoreHandle,
81-
type StoreListProfilesOptions,
82-
type StoreOpenOptions,
83-
type StoreProvisionOptions,
84-
type StoreRekeyOptions,
85-
type StoreRemoveOptions,
86-
type StoreRemoveProfileOptions,
87-
type StoreSetDefaultProfileOptions,
8890
handleInvalidNullResponse,
8991
} from '@openwallet-foundation/askar-shared'
9092
import {
91-
type ByteBufferType,
92-
type EncryptedBufferType,
9393
FFI_ENTRY_LIST_HANDLE,
9494
FFI_INT8,
9595
FFI_INT64,
@@ -99,8 +99,6 @@ import {
9999
FFI_STORE_HANDLE,
100100
FFI_STRING,
101101
FFI_STRING_LIST_HANDLE,
102-
type NativeCallback,
103-
type NativeCallbackWithResponse,
104102
allocateAeadParams,
105103
allocateEncryptedBuffer,
106104
allocateInt8Buffer,
@@ -111,12 +109,13 @@ import {
111109
allocateStringListHandle,
112110
deallocateCallbackBuffer,
113111
encryptedBufferStructToClass,
114-
secretBufferToBuffer,
112+
secretBufferToBufferWithFree,
115113
serializeArguments,
116114
toNativeCallback,
117115
toNativeCallbackWithResponse,
118116
toNativeLogCallback,
119117
} from './ffi'
118+
import type { ByteBufferType, EncryptedBufferType, NativeCallback, NativeCallbackWithResponse } from './ffi'
120119
import { getNativeAskar } from './library'
121120

122121
function handleNullableReturnPointer<Return>(returnValue: Buffer): Return | null {
@@ -313,8 +312,7 @@ export class NodeJSAskar implements Askar {
313312
const errorCode = this.nativeAskar.askar_entry_list_get_value(entryListHandle, index, ret)
314313
this.handleError(errorCode)
315314
const byteBuffer = handleReturnPointer<ByteBufferType>(ret)
316-
const bufferArray = new Uint8Array(Buffer.from(secretBufferToBuffer(byteBuffer)))
317-
this.nativeAskar.askar_buffer_free(byteBuffer as unknown as Buffer)
315+
const bufferArray = new Uint8Array(secretBufferToBufferWithFree(byteBuffer))
318316

319317
return bufferArray
320318
}
@@ -326,8 +324,7 @@ export class NodeJSAskar implements Askar {
326324
const errorCode = this.nativeAskar.askar_key_aead_decrypt(localKeyHandle, ciphertext, nonce, tag, aad, ret)
327325
this.handleError(errorCode)
328326
const byteBuffer = handleReturnPointer<ByteBufferType>(ret)
329-
const bufferArray = new Uint8Array(Buffer.from(secretBufferToBuffer(byteBuffer)))
330-
this.nativeAskar.askar_buffer_free(byteBuffer as unknown as Buffer)
327+
const bufferArray = new Uint8Array(secretBufferToBufferWithFree(byteBuffer))
331328

332329
return bufferArray
333330
}
@@ -372,8 +369,7 @@ export class NodeJSAskar implements Askar {
372369
const errorCode = this.nativeAskar.askar_key_aead_random_nonce(localKeyHandle, ret)
373370
this.handleError(errorCode)
374371
const byteBuffer = handleReturnPointer<ByteBufferType>(ret)
375-
const bufferArray = new Uint8Array(Buffer.from(secretBufferToBuffer(byteBuffer)))
376-
this.nativeAskar.askar_buffer_free(byteBuffer as unknown as Buffer)
372+
const bufferArray = new Uint8Array(secretBufferToBufferWithFree(byteBuffer))
377373

378374
return bufferArray
379375
}
@@ -396,8 +392,7 @@ export class NodeJSAskar implements Askar {
396392
const errorCode = this.nativeAskar.askar_key_crypto_box(recipientKey, senderKey, message, nonce, ret)
397393
this.handleError(errorCode)
398394
const byteBuffer = handleReturnPointer<ByteBufferType>(ret)
399-
const bufferArray = new Uint8Array(Buffer.from(secretBufferToBuffer(byteBuffer)))
400-
this.nativeAskar.askar_buffer_free(byteBuffer as unknown as Buffer)
395+
const bufferArray = new Uint8Array(secretBufferToBufferWithFree(byteBuffer))
401396

402397
return bufferArray
403398
}
@@ -409,8 +404,7 @@ export class NodeJSAskar implements Askar {
409404
const errorCode = this.nativeAskar.askar_key_crypto_box_open(recipientKey, senderKey, message, nonce, ret)
410405
this.handleError(errorCode)
411406
const byteBuffer = handleReturnPointer<ByteBufferType>(ret)
412-
const bufferArray = new Uint8Array(Buffer.from(secretBufferToBuffer(byteBuffer)))
413-
this.nativeAskar.askar_buffer_free(byteBuffer as unknown as Buffer)
407+
const bufferArray = new Uint8Array(secretBufferToBufferWithFree(byteBuffer))
414408

415409
return bufferArray
416410
}
@@ -421,8 +415,7 @@ export class NodeJSAskar implements Askar {
421415
const errorCode = this.nativeAskar.askar_key_crypto_box_random_nonce(ret)
422416
this.handleError(errorCode)
423417
const byteBuffer = handleReturnPointer<ByteBufferType>(ret)
424-
const bufferArray = new Uint8Array(Buffer.from(secretBufferToBuffer(byteBuffer)))
425-
this.nativeAskar.askar_buffer_free(byteBuffer as unknown as Buffer)
418+
const bufferArray = new Uint8Array(secretBufferToBufferWithFree(byteBuffer))
426419

427420
return bufferArray
428421
}
@@ -434,8 +427,7 @@ export class NodeJSAskar implements Askar {
434427
const errorCode = this.nativeAskar.askar_key_crypto_box_seal(localKeyHandle, message, ret)
435428
this.handleError(errorCode)
436429
const byteBuffer = handleReturnPointer<ByteBufferType>(ret)
437-
const bufferArray = new Uint8Array(Buffer.from(secretBufferToBuffer(byteBuffer)))
438-
this.nativeAskar.askar_buffer_free(byteBuffer as unknown as Buffer)
430+
const bufferArray = new Uint8Array(secretBufferToBufferWithFree(byteBuffer))
439431

440432
return bufferArray
441433
}
@@ -447,8 +439,7 @@ export class NodeJSAskar implements Askar {
447439
const errorCode = this.nativeAskar.askar_key_crypto_box_seal_open(localKeyHandle, ciphertext, ret)
448440
this.handleError(errorCode)
449441
const byteBuffer = handleReturnPointer<ByteBufferType>(ret)
450-
const bufferArray = new Uint8Array(Buffer.from(secretBufferToBuffer(byteBuffer)))
451-
this.nativeAskar.askar_buffer_free(byteBuffer as unknown as Buffer)
442+
const bufferArray = new Uint8Array(secretBufferToBufferWithFree(byteBuffer))
452443

453444
return bufferArray
454445
}
@@ -673,8 +664,7 @@ export class NodeJSAskar implements Askar {
673664
const errorCode = this.nativeAskar.askar_key_get_jwk_secret(localKeyHandle, ret)
674665
this.handleError(errorCode)
675666
const byteBuffer = handleReturnPointer<ByteBufferType>(ret)
676-
const bufferArray = new Uint8Array(Buffer.from(secretBufferToBuffer(byteBuffer)))
677-
this.nativeAskar.askar_buffer_free(byteBuffer as unknown as Buffer)
667+
const bufferArray = new Uint8Array(secretBufferToBufferWithFree(byteBuffer))
678668

679669
return bufferArray
680670
}
@@ -696,8 +686,7 @@ export class NodeJSAskar implements Askar {
696686
const errorCode = this.nativeAskar.askar_key_get_public_bytes(localKeyHandle, ret)
697687
this.handleError(errorCode)
698688
const byteBuffer = handleReturnPointer<ByteBufferType>(ret)
699-
const bufferArray = new Uint8Array(Buffer.from(secretBufferToBuffer(byteBuffer)))
700-
this.nativeAskar.askar_buffer_free(byteBuffer as unknown as Buffer)
689+
const bufferArray = new Uint8Array(secretBufferToBufferWithFree(byteBuffer))
701690

702691
return bufferArray
703692
}
@@ -709,8 +698,7 @@ export class NodeJSAskar implements Askar {
709698
const errorCode = this.nativeAskar.askar_key_get_secret_bytes(localKeyHandle, ret)
710699
this.handleError(errorCode)
711700
const byteBuffer = handleReturnPointer<ByteBufferType>(ret)
712-
const bufferArray = new Uint8Array(Buffer.from(secretBufferToBuffer(byteBuffer)))
713-
this.nativeAskar.askar_buffer_free(byteBuffer as unknown as Buffer)
701+
const bufferArray = new Uint8Array(secretBufferToBufferWithFree(byteBuffer))
714702

715703
return bufferArray
716704
}
@@ -722,8 +710,7 @@ export class NodeJSAskar implements Askar {
722710
const errorCode = this.nativeAskar.askar_key_sign_message(localKeyHandle, message, sigType, ret)
723711
this.handleError(errorCode)
724712
const byteBuffer = handleReturnPointer<ByteBufferType>(ret)
725-
const bufferArray = new Uint8Array(Buffer.from(secretBufferToBuffer(byteBuffer)))
726-
this.nativeAskar.askar_buffer_free(byteBuffer as unknown as Buffer)
713+
const bufferArray = new Uint8Array(secretBufferToBufferWithFree(byteBuffer))
727714

728715
return bufferArray
729716
}

packages/askar-nodejs/src/ffi/conversion.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Pointer } from '@2060.io/ref-napi'
22
import { reinterpret } from '@2060.io/ref-napi'
33
import { EncryptedBuffer } from '@openwallet-foundation/askar-shared'
44
import type { TypedArray } from 'ref-array-di'
5+
import { getNativeAskar } from '../library'
56
import type { ByteBufferType, EncryptedBufferType } from './structures'
67
import { ByteBufferStruct } from './structures'
78

@@ -19,8 +20,15 @@ export const uint8arrayToByteBufferStruct = (buf: Buffer) => {
1920
}
2021

2122
export const byteBufferToBuffer = ({ data, len }: ByteBufferType) => reinterpret(data, len)
23+
export const byteBufferToBufferWithFree = ({ data, len }: ByteBufferType) => {
24+
const buffer = reinterpret(data, len)
25+
const newBuffer = Buffer.from(buffer)
26+
getNativeAskar().askar_buffer_free(buffer)
27+
return newBuffer
28+
}
2229

2330
export const secretBufferToBuffer = byteBufferToBuffer
31+
export const secretBufferToBufferWithFree = byteBufferToBufferWithFree
2432

2533
export const encryptedBufferStructToClass = ({ secretBuffer, tagPos, noncePos }: EncryptedBufferType) => {
2634
const buffer = Uint8Array.from(secretBufferToBuffer(secretBuffer))

0 commit comments

Comments
 (0)