Skip to content

Commit db870b7

Browse files
authored
Refactor Node.js API to use Uint8Array for data handling (#45)
* Refactor Node.js API to use Uint8Array for data handling - Updated method signatures in the `Keys` and `Transport` classes to replace `Buffer` with `Uint8Array`, enhancing consistency in data types across the API. - Modified encryption and decryption methods to utilize `Uint8Array`, improving performance and compatibility with modern JavaScript practices. - Adjusted TypeScript definitions to reflect the changes in method signatures, ensuring alignment with the updated Rust implementation. - Enhanced overall code clarity and maintainability by streamlining data handling processes in the API. * Refactor tests to utilize Uint8Array for data validation - Updated comprehensive API, lifecycle, cross-platform, and basic tests to replace Buffer checks with Uint8Array validations, enhancing consistency in data type handling. - Introduced a new utility function, `uint8ArrayEquals`, for comparing Uint8Array instances, improving code clarity and maintainability. - Adjusted assertions across multiple test files to ensure proper validation of cryptographic keys and encrypted data, aligning with recent API changes. - Enhanced overall test coverage and robustness by ensuring all relevant tests reflect the updated data handling practices.
1 parent a637c89 commit db870b7

File tree

10 files changed

+267
-240
lines changed

10 files changed

+267
-240
lines changed

runar-nodejs-api/index.d.ts

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/* auto-generated by NAPI-RS */
22
/* eslint-disable */
33
export declare class Discovery {
4-
constructor(keys: Keys, optionsCbor: Buffer)
5-
init(optionsCbor: Buffer): Promise<void>
4+
constructor(keys: Keys, optionsCbor: Uint8Array)
5+
init(optionsCbor: Uint8Array): Promise<void>
66
bindEventsToTransport(transport: Transport): Promise<void>
77
startAnnouncing(): Promise<void>
88
stopAnnouncing(): Promise<void>
99
shutdown(): Promise<void>
10-
updateLocalPeerInfo(peerInfoCbor: Buffer): Promise<void>
10+
updateLocalPeerInfo(peerInfoCbor: Uint8Array): Promise<void>
1111
}
1212

1313
export declare class Keys {
@@ -30,65 +30,65 @@ export declare class Keys {
3030
* This function encrypts data for a specific network and profile public keys
3131
* using the mobile key manager's envelope encryption.
3232
*/
33-
mobileEncryptWithEnvelope(data: Buffer, networkPublicKey: Buffer | undefined | null, profilePublicKeys: Array<Buffer>): Buffer
33+
mobileEncryptWithEnvelope(data: Uint8Array, networkPublicKey: Uint8Array | undefined | null, profilePublicKeys: Array<Uint8Array>): Uint8Array
3434
/**
3535
* Encrypt data using envelope encryption with node manager
3636
*
3737
* This function encrypts data for a specific network and profile public keys
3838
* using the node key manager's envelope encryption.
3939
*/
40-
nodeEncryptWithEnvelope(data: Buffer, networkPublicKey: Buffer | undefined | null, profilePublicKeys: Array<Buffer>): Buffer
40+
nodeEncryptWithEnvelope(data: Uint8Array, networkPublicKey: Uint8Array | undefined | null, profilePublicKeys: Array<Uint8Array>): Uint8Array
4141
nodeGetNodeId(): string
42-
nodeGetPublicKey(): Buffer
42+
nodeGetPublicKey(): Uint8Array
4343
enableAutoPersist(enabled: boolean): void
4444
wipePersistence(): Promise<void>
4545
flushState(): Promise<void>
4646
nodeGetKeystoreState(): number
4747
mobileGetKeystoreState(): number
4848
getKeystoreCaps(): DeviceKeystoreCaps
49-
encryptLocalData(data: Buffer): Buffer
50-
decryptLocalData(data: Buffer): Buffer
51-
mobileDecryptEnvelope(eedCbor: Buffer): Buffer
52-
nodeDecryptEnvelope(eedCbor: Buffer): Buffer
53-
nodeGenerateCsr(): Buffer
54-
mobileProcessSetupToken(stCbor: Buffer): Buffer
55-
nodeInstallCertificate(ncmCbor: Buffer): void
49+
encryptLocalData(data: Uint8Array): Uint8Array
50+
decryptLocalData(data: Uint8Array): Uint8Array
51+
mobileDecryptEnvelope(eedCbor: Uint8Array): Uint8Array
52+
nodeDecryptEnvelope(eedCbor: Uint8Array): Uint8Array
53+
nodeGenerateCsr(): Uint8Array
54+
mobileProcessSetupToken(stCbor: Uint8Array): Uint8Array
55+
nodeInstallCertificate(ncmCbor: Uint8Array): void
5656
mobileGenerateNetworkDataKey(): string
57-
mobileInstallNetworkPublicKey(networkPk: Buffer): void
58-
nodeInstallNetworkKey(nkmCbor: Buffer): void
59-
setLocalNodeInfo(nodeInfoCbor: Buffer): void
60-
encryptForPublicKey(data: Buffer, recipientPk: Buffer): Buffer
61-
encryptForNetwork(data: Buffer, networkId: string): Buffer
62-
decryptNetworkData(eedCbor: Buffer): Buffer
63-
encryptMessageForMobile(message: Buffer, mobilePk: Buffer): Buffer
64-
decryptMessageFromMobile(encrypted: Buffer): Buffer
65-
mobileDeriveUserProfileKey(label: string): Buffer
66-
mobileGetNetworkPublicKey(networkId: string): Buffer
67-
mobileCreateNetworkKeyMessage(networkId: string, nodeAgreementPk: Buffer): Buffer
68-
ensureSymmetricKey(keyName: string): Buffer
57+
mobileInstallNetworkPublicKey(networkPk: Uint8Array): void
58+
nodeInstallNetworkKey(nkmCbor: Uint8Array): void
59+
setLocalNodeInfo(nodeInfoCbor: Uint8Array): void
60+
encryptForPublicKey(data: Uint8Array, recipientPk: Uint8Array): Uint8Array
61+
encryptForNetwork(data: Uint8Array, networkId: string): Uint8Array
62+
decryptNetworkData(eedCbor: Uint8Array): Uint8Array
63+
encryptMessageForMobile(message: Uint8Array, mobilePk: Uint8Array): Uint8Array
64+
decryptMessageFromMobile(encrypted: Uint8Array): Uint8Array
65+
mobileDeriveUserProfileKey(label: string): Uint8Array
66+
mobileGetNetworkPublicKey(networkId: string): Uint8Array
67+
mobileCreateNetworkKeyMessage(networkId: string, nodeAgreementPk: Uint8Array): Uint8Array
68+
ensureSymmetricKey(keyName: string): Uint8Array
6969
/**
7070
* Get the user public key after mobile initialization
7171
* This is essential for encrypting setup tokens to the mobile
7272
*/
73-
mobileGetUserPublicKey(): Buffer
73+
mobileGetUserPublicKey(): Uint8Array
7474
/**
7575
* Get the node agreement public key
7676
* This is used for verifying agreement keys in CSR flow
7777
*/
78-
nodeGetAgreementPublicKey(): Buffer
78+
nodeGetAgreementPublicKey(): Uint8Array
7979
}
8080

8181
export declare class Transport {
82-
constructor(keys: Keys, optionsCbor: Buffer)
83-
completeRequest(requestId: string, responsePayload: Buffer, profilePublicKeys: Array<Buffer>): Promise<void>
82+
constructor(keys: Keys, optionsCbor: Uint8Array)
83+
completeRequest(requestId: string, responsePayload: Uint8Array, profilePublicKeys: Array<Uint8Array>): Promise<void>
8484
start(): Promise<void>
8585
stop(): Promise<void>
86-
connectPeer(peerInfoCbor: Buffer): Promise<void>
86+
connectPeer(peerInfoCbor: Uint8Array): Promise<void>
8787
isConnected(peerId: string): Promise<boolean>
88-
isConnectedToPublicKey(peerPublicKey: Buffer): Promise<boolean>
89-
request(path: string, correlationId: string, payload: Buffer, destPeerId: string, networkPublicKey?: Buffer | undefined | null, profilePublicKeys?: Array<Buffer> | undefined | null): Promise<Buffer>
90-
publish(path: string, correlationId: string, payload: Buffer, destPeerId: string, networkPublicKey?: Buffer | undefined | null): Promise<void>
91-
updatePeers(nodeInfoCbor: Buffer): Promise<void>
88+
isConnectedToPublicKey(peerPublicKey: Uint8Array): Promise<boolean>
89+
request(path: string, correlationId: string, payload: Uint8Array, destPeerId: string, networkPublicKey?: Uint8Array | undefined | null, profilePublicKeys?: Array<Uint8Array> | undefined | null): Promise<Uint8Array>
90+
publish(path: string, correlationId: string, payload: Uint8Array, destPeerId: string, networkPublicKey?: Uint8Array | undefined | null): Promise<void>
91+
updatePeers(nodeInfoCbor: Uint8Array): Promise<void>
9292
}
9393

9494
export interface DeviceKeystoreCaps {

0 commit comments

Comments
 (0)