Skip to content

Commit 9121e2a

Browse files
authored
fix: askar registration (#78)
Signed-off-by: Timo Glastra <[email protected]>
1 parent f626ebc commit 9121e2a

File tree

20 files changed

+145
-99
lines changed

20 files changed

+145
-99
lines changed

.changeset/wet-comics-love.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
"@openwallet-foundation/askar-react-native": patch
3+
"@openwallet-foundation/askar-nodejs": patch
4+
"@openwallet-foundation/askar-shared": patch
5+
---
6+
7+
fix: wrap the askar registration with a `NativeAskar` class that exposes two static members: a `.instance` getter and `register` method.
8+
9+
Previously when you did not import the native askar library on the first line (or above all logic that uses Askar) the reference to Askar would be undefined. (i.e. askar-shared is imported before askar-react-native or askar-nodejs)
10+
11+
The `registerAskar` method and `askar` property are kept for backwards compatibility, but these are still prone to the same error. `registerAskar` integrated with the new `NativeAskar` class, and so all usages within the shared library, as well as the nodejs and react-native wrappers will be fixed. We recommend to update to the new `NativeAskar` class for all other usages.

packages/askar-nodejs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
},
2929
"dependencies": {
3030
"@openwallet-foundation/askar-shared": "workspace:*",
31-
"koffi": "^2.9.0"
31+
"koffi": "^2.14.1"
3232
},
3333
"devDependencies": {
3434
"@types/node": "catalog:",

packages/askar-nodejs/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { registerAskar } from '@openwallet-foundation/askar-shared'
1+
import { NativeAskar } from '@openwallet-foundation/askar-shared'
22
import { NodeJSAskar } from './NodeJSAskar'
33

44
export const askarNodeJS = new NodeJSAskar()
5-
registerAskar({ askar: askarNodeJS })
5+
NativeAskar.register(askarNodeJS)
66

77
export * from '@openwallet-foundation/askar-shared'

packages/askar-nodejs/src/library/register.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const getLibrary = () => {
5454
path.join(p, `${extensions[platform].prefix ?? ''}${LIBNAME}${extensions[platform].extension}`)
5555
)
5656

57-
// Gaurd so we quit if there is no valid path for the library
57+
// Guard so we quit if there is no valid path for the library
5858
if (!libraries.some((libraryPath) => doesPathExist(libraryPath)))
5959
throw new Error(`Could not find ${LIBNAME} with these paths: ${libraries.join(' ')}`)
6060

packages/askar-react-native/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { registerAskar } from '@openwallet-foundation/askar-shared'
1+
import { NativeAskar } from '@openwallet-foundation/askar-shared'
22
import { NativeModules } from 'react-native'
33
import type { NativeBindings } from './NativeBindings'
44

@@ -17,4 +17,4 @@ if (!_askar) {
1717

1818
declare let _askar: NativeBindings
1919

20-
registerAskar({ askar: new ReactNativeAskar(_askar) })
20+
NativeAskar.register(new ReactNativeAskar(_askar))
Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,33 @@
1+
import { AskarError } from '../error'
12
import type { Askar } from './Askar'
23

4+
/**
5+
* @deprecated use `NativeAskar.instance` instead
6+
*/
37
export let askar: Askar
48

9+
export class NativeAskar {
10+
static #nativeAskar: Askar | undefined
11+
12+
public static get instance(): Askar {
13+
if (!NativeAskar.#nativeAskar)
14+
throw AskarError.customError({
15+
message:
16+
"Native askar has not been registered yet. Make sure to import '@openwallet-foundation/askar-nodejs' or '@openwallet-foundation/askar-react-native', or call 'NativeAskar.register' with a custom implementation.",
17+
})
18+
19+
return NativeAskar.#nativeAskar
20+
}
21+
22+
public static register(nativeAskar: Askar) {
23+
askar = nativeAskar
24+
NativeAskar.#nativeAskar = nativeAskar
25+
}
26+
}
27+
28+
/**
29+
* @deprecated use `NativeAskar.register` instead
30+
*/
531
export const registerAskar = (options: { askar: Askar }) => {
6-
askar = options.askar
32+
NativeAskar.register(options.askar)
733
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { askar } from '../askar'
1+
import { NativeAskar } from '../askar'
22

33
export enum Argon2Parameters {
44
Moderate = 0,
@@ -7,6 +7,6 @@ export enum Argon2Parameters {
77

88
export class Argon2 {
99
public static derivePassword(parameters: Argon2Parameters, password: Uint8Array, salt: Uint8Array): Uint8Array {
10-
return askar.argon2DerivePassword({ parameters, password, salt })
10+
return NativeAskar.instance.argon2DerivePassword({ parameters, password, salt })
1111
}
1212
}

packages/askar-shared/src/crypto/CryptoBox.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { askar } from '../askar'
1+
import { NativeAskar } from '../askar'
22
import type { Key } from './Key'
33

44
export class CryptoBox {
55
public static randomNonce() {
6-
return askar.keyCryptoBoxRandomNonce()
6+
return NativeAskar.instance.keyCryptoBoxRandomNonce()
77
}
88

99
public static cryptoBox({
@@ -17,7 +17,7 @@ export class CryptoBox {
1717
message: Uint8Array
1818
nonce: Uint8Array
1919
}) {
20-
return askar.keyCryptoBox({ nonce, message, senderKey, recipientKey })
20+
return NativeAskar.instance.keyCryptoBox({ nonce, message, senderKey, recipientKey })
2121
}
2222

2323
public static open({
@@ -31,14 +31,14 @@ export class CryptoBox {
3131
message: Uint8Array
3232
nonce: Uint8Array
3333
}) {
34-
return askar.keyCryptoBoxOpen({ nonce, message, senderKey, recipientKey })
34+
return NativeAskar.instance.keyCryptoBoxOpen({ nonce, message, senderKey, recipientKey })
3535
}
3636

3737
public static seal({ recipientKey, message }: { recipientKey: Key; message: Uint8Array }) {
38-
return askar.keyCryptoBoxSeal({ message, localKeyHandle: recipientKey.handle })
38+
return NativeAskar.instance.keyCryptoBoxSeal({ message, localKeyHandle: recipientKey.handle })
3939
}
4040

4141
public static sealOpen({ recipientKey, ciphertext }: { recipientKey: Key; ciphertext: Uint8Array }) {
42-
return askar.keyCryptoBoxSealOpen({ ciphertext, localKeyHandle: recipientKey.handle })
42+
return NativeAskar.instance.keyCryptoBoxSealOpen({ ciphertext, localKeyHandle: recipientKey.handle })
4343
}
4444
}

packages/askar-shared/src/crypto/Ecdh1PU.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { askar } from '../askar'
1+
import { NativeAskar } from '../askar'
22
import type { KeyAlgorithm } from '../enums'
33
import { Key } from './Key'
44

@@ -29,7 +29,7 @@ export class Ecdh1PU {
2929
ccTag?: Uint8Array
3030
}): Key {
3131
return new Key(
32-
askar.keyDeriveEcdh1pu({
32+
NativeAskar.instance.keyDeriveEcdh1pu({
3333
algId: this.algId,
3434
receive,
3535
apv: this.apv,

packages/askar-shared/src/crypto/EcdhEs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { askar } from '../askar'
1+
import { askar, NativeAskar } from '../askar'
22
import type { KeyAlgorithm } from '../enums'
33

44
import { Jwk } from './Jwk'
@@ -27,7 +27,7 @@ export class EcdhEs {
2727
receive: boolean
2828
}): Key {
2929
return new Key(
30-
askar.keyDeriveEcdhEs({
30+
NativeAskar.instance.keyDeriveEcdhEs({
3131
algId: this.algId,
3232
receive,
3333
apv: this.apv,

0 commit comments

Comments
 (0)