@authon/react-native-wallet 0.0.0-stage → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -0
- package/README.md +251 -2
- package/android/build.gradle +40 -0
- package/android/consumer-rules.pro +10 -0
- package/android/src/main/AndroidManifest.xml +8 -0
- package/android/src/main/java/dev/authon/reactnative/wallet/AuthonWalletModule.kt +235 -0
- package/android/src/vendor/GENERATED.md +4 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/AndroidCapabilityProbe.kt +86 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/AuthonWallet.kt +218 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Bitcoin.kt +296 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Bytes.kt +74 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Chains.kt +157 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/CredentialManagerPasskeys.kt +192 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Der.kt +17 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/DeviceStore.kt +170 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/DialogWalletApprover.kt +141 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Eip712.kt +176 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Encodings.kt +133 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/EvmAccount.kt +45 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/EvmTransaction.kt +180 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/GfTables.kt +44 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/HdWallet.kt +130 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Http.kt +86 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/JsValues.kt +83 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/KeystoreDeviceStore.kt +236 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Prf.kt +111 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Proof.kt +134 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Shamir.kt +106 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/SignSummary.kt +335 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Solana.kt +195 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Tron.kt +184 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/WalletApi.kt +361 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/WalletCapabilities.kt +91 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/WalletError.kt +74 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/WalletOps.kt +551 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/WalletTypes.kt +78 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Xrp.kt +193 -0
- package/build/authonContext.d.ts +19 -0
- package/build/authonContext.d.ts.map +1 -0
- package/build/authonContext.js +20 -0
- package/build/authonContext.js.map +1 -0
- package/build/client.d.ts +92 -0
- package/build/client.d.ts.map +1 -0
- package/build/client.js +431 -0
- package/build/client.js.map +1 -0
- package/build/errors.d.ts +25 -0
- package/build/errors.d.ts.map +1 -0
- package/build/errors.js +15 -0
- package/build/errors.js.map +1 -0
- package/build/index.d.ts +7 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +4 -0
- package/build/index.js.map +1 -0
- package/build/native.d.ts +43 -0
- package/build/native.d.ts.map +1 -0
- package/build/native.js +12 -0
- package/build/native.js.map +1 -0
- package/build/types.d.ts +167 -0
- package/build/types.d.ts.map +1 -0
- package/build/types.js +3 -0
- package/build/types.js.map +1 -0
- package/build/useAuthonWallet.d.ts +25 -0
- package/build/useAuthonWallet.d.ts.map +1 -0
- package/build/useAuthonWallet.js +77 -0
- package/build/useAuthonWallet.js.map +1 -0
- package/expo-module.config.json +9 -0
- package/ios/AuthonReactNativeWallet.podspec +41 -0
- package/ios/AuthonWalletModule.swift +233 -0
- package/ios/vendor/AuthonWallet/AuthonWallet.swift +232 -0
- package/ios/vendor/AuthonWallet/Capabilities.swift +149 -0
- package/ios/vendor/AuthonWallet/Chains/Bitcoin.swift +296 -0
- package/ios/vendor/AuthonWallet/Chains/Solana.swift +207 -0
- package/ios/vendor/AuthonWallet/Chains/Tron.swift +181 -0
- package/ios/vendor/AuthonWallet/Chains/XRP.swift +191 -0
- package/ios/vendor/AuthonWallet/Crypto/BIP39Wordlist.swift +262 -0
- package/ios/vendor/AuthonWallet/Crypto/Chains.swift +278 -0
- package/ios/vendor/AuthonWallet/Crypto/HDKey.swift +108 -0
- package/ios/vendor/AuthonWallet/Crypto/Secp256k1.swift +128 -0
- package/ios/vendor/AuthonWallet/Crypto/Shamir.swift +123 -0
- package/ios/vendor/AuthonWallet/EVM/Address.swift +57 -0
- package/ios/vendor/AuthonWallet/EVM/EvmAccount.swift +48 -0
- package/ios/vendor/AuthonWallet/EVM/Transaction.swift +165 -0
- package/ios/vendor/AuthonWallet/EVM/TypedData.swift +193 -0
- package/ios/vendor/AuthonWallet/Support/BigInteger.swift +220 -0
- package/ios/vendor/AuthonWallet/Support/Bytes.swift +89 -0
- package/ios/vendor/AuthonWallet/Support/JSONValue.swift +341 -0
- package/ios/vendor/AuthonWallet/UI/WalletApprover.swift +250 -0
- package/ios/vendor/AuthonWallet/Wallet/Backup.swift +69 -0
- package/ios/vendor/AuthonWallet/Wallet/DeviceStore.swift +351 -0
- package/ios/vendor/AuthonWallet/Wallet/Passkeys.swift +190 -0
- package/ios/vendor/AuthonWallet/Wallet/Proof.swift +143 -0
- package/ios/vendor/AuthonWallet/Wallet/Shares.swift +116 -0
- package/ios/vendor/AuthonWallet/Wallet/Summary.swift +342 -0
- package/ios/vendor/AuthonWallet/Wallet/WalletAPI.swift +361 -0
- package/ios/vendor/AuthonWallet/Wallet/WalletOps.swift +654 -0
- package/ios/vendor/AuthonWallet/WalletError.swift +84 -0
- package/ios/vendor/CKeccak/LICENSE +22 -0
- package/ios/vendor/CKeccak/README.md +30 -0
- package/ios/vendor/CKeccak/byte_order.h +59 -0
- package/ios/vendor/CKeccak/include/ckeccak.h +13 -0
- package/ios/vendor/CKeccak/memzero.c +75 -0
- package/ios/vendor/CKeccak/memzero.h +8 -0
- package/ios/vendor/CKeccak/options.h +99 -0
- package/ios/vendor/CKeccak/ripemd160.c +343 -0
- package/ios/vendor/CKeccak/ripemd160.h +22 -0
- package/ios/vendor/CKeccak/sha3.c +413 -0
- package/ios/vendor/CKeccak/sha3.h +89 -0
- package/ios/vendor/CMonocypher/LICENCE.md +173 -0
- package/ios/vendor/CMonocypher/README.md +27 -0
- package/ios/vendor/CMonocypher/include/monocypher-ed25519.h +140 -0
- package/ios/vendor/CMonocypher/include/monocypher.h +321 -0
- package/ios/vendor/CMonocypher/monocypher-ed25519.c +500 -0
- package/ios/vendor/CMonocypher/monocypher.c +2956 -0
- package/ios/vendor/CSecp256k1/COPYING +19 -0
- package/ios/vendor/CSecp256k1/README.md +35 -0
- package/ios/vendor/CSecp256k1/build/precomputed_ecmult.c +3 -0
- package/ios/vendor/CSecp256k1/build/precomputed_ecmult_gen.c +3 -0
- package/ios/vendor/CSecp256k1/build/secp256k1.c +3 -0
- package/ios/vendor/CSecp256k1/include/secp256k1.h +899 -0
- package/ios/vendor/CSecp256k1/include/secp256k1_preallocated.h +134 -0
- package/ios/vendor/CSecp256k1/include/secp256k1_recovery.h +113 -0
- package/ios/vendor/CSecp256k1/src/assumptions.h +87 -0
- package/ios/vendor/CSecp256k1/src/checkmem.h +102 -0
- package/ios/vendor/CSecp256k1/src/ecdsa.h +21 -0
- package/ios/vendor/CSecp256k1/src/ecdsa_impl.h +304 -0
- package/ios/vendor/CSecp256k1/src/eckey.h +25 -0
- package/ios/vendor/CSecp256k1/src/eckey_impl.h +92 -0
- package/ios/vendor/CSecp256k1/src/ecmult.h +61 -0
- package/ios/vendor/CSecp256k1/src/ecmult_const.h +38 -0
- package/ios/vendor/CSecp256k1/src/ecmult_const_impl.h +399 -0
- package/ios/vendor/CSecp256k1/src/ecmult_gen.h +143 -0
- package/ios/vendor/CSecp256k1/src/ecmult_gen_impl.h +341 -0
- package/ios/vendor/CSecp256k1/src/ecmult_impl.h +853 -0
- package/ios/vendor/CSecp256k1/src/field.h +350 -0
- package/ios/vendor/CSecp256k1/src/field_10x26.h +57 -0
- package/ios/vendor/CSecp256k1/src/field_10x26_impl.h +1232 -0
- package/ios/vendor/CSecp256k1/src/field_5x52.h +62 -0
- package/ios/vendor/CSecp256k1/src/field_5x52_impl.h +522 -0
- package/ios/vendor/CSecp256k1/src/field_5x52_int128_impl.h +274 -0
- package/ios/vendor/CSecp256k1/src/field_impl.h +457 -0
- package/ios/vendor/CSecp256k1/src/group.h +212 -0
- package/ios/vendor/CSecp256k1/src/group_impl.h +974 -0
- package/ios/vendor/CSecp256k1/src/hash.h +44 -0
- package/ios/vendor/CSecp256k1/src/hash_impl.h +299 -0
- package/ios/vendor/CSecp256k1/src/hsort.h +33 -0
- package/ios/vendor/CSecp256k1/src/hsort_impl.h +125 -0
- package/ios/vendor/CSecp256k1/src/int128.h +90 -0
- package/ios/vendor/CSecp256k1/src/int128_impl.h +18 -0
- package/ios/vendor/CSecp256k1/src/int128_native.h +19 -0
- package/ios/vendor/CSecp256k1/src/int128_native_impl.h +94 -0
- package/ios/vendor/CSecp256k1/src/int128_struct.h +14 -0
- package/ios/vendor/CSecp256k1/src/int128_struct_impl.h +205 -0
- package/ios/vendor/CSecp256k1/src/modinv32.h +43 -0
- package/ios/vendor/CSecp256k1/src/modinv32_impl.h +725 -0
- package/ios/vendor/CSecp256k1/src/modinv64.h +47 -0
- package/ios/vendor/CSecp256k1/src/modinv64_impl.h +780 -0
- package/ios/vendor/CSecp256k1/src/modules/recovery/main_impl.h +159 -0
- package/ios/vendor/CSecp256k1/src/precomputed_ecmult.c +16456 -0
- package/ios/vendor/CSecp256k1/src/precomputed_ecmult.h +38 -0
- package/ios/vendor/CSecp256k1/src/precomputed_ecmult_gen.c +1779 -0
- package/ios/vendor/CSecp256k1/src/precomputed_ecmult_gen.h +26 -0
- package/ios/vendor/CSecp256k1/src/scalar.h +105 -0
- package/ios/vendor/CSecp256k1/src/scalar_4x64.h +19 -0
- package/ios/vendor/CSecp256k1/src/scalar_4x64_impl.h +1000 -0
- package/ios/vendor/CSecp256k1/src/scalar_8x32.h +19 -0
- package/ios/vendor/CSecp256k1/src/scalar_8x32_impl.h +816 -0
- package/ios/vendor/CSecp256k1/src/scalar_impl.h +321 -0
- package/ios/vendor/CSecp256k1/src/scalar_low.h +24 -0
- package/ios/vendor/CSecp256k1/src/scalar_low_impl.h +206 -0
- package/ios/vendor/CSecp256k1/src/scratch.h +44 -0
- package/ios/vendor/CSecp256k1/src/scratch_impl.h +99 -0
- package/ios/vendor/CSecp256k1/src/secp256k1.c +831 -0
- package/ios/vendor/CSecp256k1/src/selftest.h +32 -0
- package/ios/vendor/CSecp256k1/src/util.h +451 -0
- package/ios/vendor/GENERATED.md +4 -0
- package/package.json +66 -3
- package/src/authonContext.ts +30 -0
- package/src/client.ts +459 -0
- package/src/errors.ts +58 -0
- package/src/index.ts +26 -0
- package/src/native.ts +52 -0
- package/src/types.ts +171 -0
- package/src/useAuthonWallet.ts +103 -0
package/src/types.ts
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/** Names and shapes follow `@authon/js` (WalletClient) so web and app code read the same. */
|
|
2
|
+
|
|
3
|
+
export type Hex = `0x${string}`;
|
|
4
|
+
|
|
5
|
+
export interface EmbeddedWalletDevice {
|
|
6
|
+
id: string;
|
|
7
|
+
label: string | null;
|
|
8
|
+
createdAt: string | null;
|
|
9
|
+
lastUsedAt: string | null;
|
|
10
|
+
/** This app install. */
|
|
11
|
+
current: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface EmbeddedWallet {
|
|
15
|
+
/** Account 0 (m/44'/60'/0'/0/0), the same address the web wallet shows for this user. */
|
|
16
|
+
address: string;
|
|
17
|
+
/**
|
|
18
|
+
* True once the wallet has a passkey backup (`protect()`). An unprotected wallet exists only
|
|
19
|
+
* as this install's device share plus the server share: deleting the app or losing the device
|
|
20
|
+
* makes it unrecoverable. Do not let users receive funds until `protected` is true.
|
|
21
|
+
*/
|
|
22
|
+
protected: boolean;
|
|
23
|
+
/** True when the server has a wallet but this install holds no device share (call `restore()`). */
|
|
24
|
+
needsRestore: boolean;
|
|
25
|
+
/** A legacy backup without a passkey public key; call `upgradeBackup()` on a device that holds the wallet. */
|
|
26
|
+
backupUpgradeRecommended: boolean;
|
|
27
|
+
devices: EmbeddedWalletDevice[];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Chains whose account addresses the wallet derives from the same seed (NATIVE.md section 11). */
|
|
31
|
+
export type WalletChain = 'evm' | 'tron' | 'xrp' | 'bitcoin' | 'bitcoin-testnet' | 'solana';
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* One HD account. EVM: `m/44'/60'/0'/0/{index}` (account 0 is `EmbeddedWallet.address`); other chains
|
|
35
|
+
* use their standard paths (TRON `m/44'/195'/n'/0/0`, XRP `m/44'/144'/n'/0/0`, Bitcoin
|
|
36
|
+
* `m/84'/0'/n'/0/0`, Bitcoin testnet `m/84'/1'/n'/0/0`, Solana `m/44'/501'/n'/0'`).
|
|
37
|
+
*/
|
|
38
|
+
export interface WalletAccount {
|
|
39
|
+
index: number;
|
|
40
|
+
path: string;
|
|
41
|
+
address: string;
|
|
42
|
+
/** Set when `getAccounts` was asked for a chain. */
|
|
43
|
+
chain?: WalletChain;
|
|
44
|
+
/** Public key (hex) when the chain needs it to build a transaction (XRP SigningPubKey). */
|
|
45
|
+
publicKey?: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** `signChainTransaction` input (NATIVE.md section 12). The app builds the unsigned transaction. */
|
|
49
|
+
export interface WalletChainSignRequest {
|
|
50
|
+
chain: Exclude<WalletChain, 'evm'>;
|
|
51
|
+
/** Account index (default 0). */
|
|
52
|
+
account?: number;
|
|
53
|
+
/** The account's address on `chain`, from getAccounts. Signing fails (ADDRESS_MISMATCH) if it differs. */
|
|
54
|
+
address: string;
|
|
55
|
+
/**
|
|
56
|
+
* Chain-specific unsigned transaction: Solana `{ message }` (base64 Message, legacy or v0),
|
|
57
|
+
* TRON `{ rawDataHex }`, Bitcoin `{ psbt }` (base64), XRP `{ txBlobHex }` (encodeForSigning output).
|
|
58
|
+
*/
|
|
59
|
+
payload: Record<string, unknown>;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Signs with another account of the same wallet. Omitted or 0 = the wallet address. */
|
|
63
|
+
export interface WalletSignOptions {
|
|
64
|
+
account?: number;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** A personal_sign payload: UTF-8 text, or raw bytes as 0x hex. */
|
|
68
|
+
export type WalletMessage = string | { raw: string };
|
|
69
|
+
|
|
70
|
+
export interface TypedDataPayload {
|
|
71
|
+
domain: Record<string, unknown>;
|
|
72
|
+
types: Record<string, Array<{ name: string; type: string }>>;
|
|
73
|
+
primaryType: string;
|
|
74
|
+
message: Record<string, unknown>;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export interface WalletTransactionRequest {
|
|
78
|
+
to?: string;
|
|
79
|
+
from?: string;
|
|
80
|
+
value?: string | number | bigint;
|
|
81
|
+
data?: string;
|
|
82
|
+
nonce?: string | number;
|
|
83
|
+
gas?: string | number | bigint;
|
|
84
|
+
gasLimit?: string | number | bigint;
|
|
85
|
+
gasPrice?: string | number | bigint;
|
|
86
|
+
maxFeePerGas?: string | number | bigint;
|
|
87
|
+
maxPriorityFeePerGas?: string | number | bigint;
|
|
88
|
+
chainId?: string | number;
|
|
89
|
+
type?: string | number;
|
|
90
|
+
accessList?: Array<{ address: string; storageKeys: string[] }>;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export type WalletChangeListener = (wallet: EmbeddedWallet | null) => void;
|
|
94
|
+
|
|
95
|
+
/** `GET /v1/wallet/app-config` for this app (public, no session needed). */
|
|
96
|
+
export interface WalletAppConfig {
|
|
97
|
+
projectId: string | null;
|
|
98
|
+
/** The app is registered for the project (bundle ID / package name). */
|
|
99
|
+
allowed: boolean;
|
|
100
|
+
/** An Authon administrator approved the registration; passkey backup and restore need it. */
|
|
101
|
+
approved: boolean;
|
|
102
|
+
enabled: boolean;
|
|
103
|
+
chainIds: number[];
|
|
104
|
+
/** Project setting: show the native approval UI before every signature. */
|
|
105
|
+
confirmSigning: boolean;
|
|
106
|
+
/** Passkey relying party (`wallet.authon.dev`). */
|
|
107
|
+
rpId: string;
|
|
108
|
+
minFrameProtocol: number | null;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export type CapabilityStatus = 'available' | 'unavailable' | 'unknown';
|
|
112
|
+
|
|
113
|
+
export interface CapabilityCheck {
|
|
114
|
+
status: CapabilityStatus;
|
|
115
|
+
/** Reason code (see the README table); null when available. */
|
|
116
|
+
reason: string | null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** `capabilities()`: can this device and app use the wallet? Asked before sign-in, no session. */
|
|
120
|
+
export interface WalletCapabilities {
|
|
121
|
+
/** unavailable if any check is unavailable, else unknown if any is unknown, else available. */
|
|
122
|
+
ready: CapabilityStatus;
|
|
123
|
+
/** App registration and approval (APP_NOT_REGISTERED, APP_SIGNATURE_MISMATCH, APP_NOT_APPROVED, NETWORK_ERROR, API_ERROR). */
|
|
124
|
+
app: CapabilityCheck;
|
|
125
|
+
/** Embedded wallet enabled for the project (WALLET_DISABLED, NETWORK_ERROR). */
|
|
126
|
+
wallet: CapabilityCheck;
|
|
127
|
+
/** Hardware-protected key storage (HARDWARE_KEYSTORE_UNAVAILABLE, SIMULATOR, CHECK_FAILED). */
|
|
128
|
+
secureStorage: CapabilityCheck;
|
|
129
|
+
/** A device passcode / biometrics is set (DEVICE_LOCK_NOT_SET, CHECK_FAILED). */
|
|
130
|
+
deviceLock: CapabilityCheck;
|
|
131
|
+
/** Passkey PRF backup, static OS/API support only (OS_UNSUPPORTED, PASSKEY_API_UNAVAILABLE, PRF_PROVIDER_UNVERIFIED). */
|
|
132
|
+
passkeyBackup: CapabilityCheck;
|
|
133
|
+
/** X-Request-Id of the app-config response. */
|
|
134
|
+
diagnosticId: string | null;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export interface EIP1193RequestArguments {
|
|
138
|
+
method: string;
|
|
139
|
+
params?: unknown[] | Record<string, unknown>;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export interface EIP1193Provider {
|
|
143
|
+
request(args: EIP1193RequestArguments): Promise<unknown>;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export type TokenGetter = () => Promise<string | null | undefined> | string | null | undefined;
|
|
147
|
+
|
|
148
|
+
export interface AuthonWalletOptions {
|
|
149
|
+
publishableKey: string;
|
|
150
|
+
/** Defaults to https://api.authon.dev. */
|
|
151
|
+
apiUrl?: string;
|
|
152
|
+
/**
|
|
153
|
+
* Returns the current Authon session (access) token, refreshed when needed. Called before
|
|
154
|
+
* every wallet call; the token is handed to the native wallet for that call only.
|
|
155
|
+
*/
|
|
156
|
+
getToken: TokenGetter;
|
|
157
|
+
/**
|
|
158
|
+
* The app as registered in the Authon console: `TEAMID.bundleId` on iOS, the package name on
|
|
159
|
+
* Android. Detected when omitted (iOS: from the keychain access group; Android: package name).
|
|
160
|
+
*/
|
|
161
|
+
appId?: string | { ios?: string; android?: string };
|
|
162
|
+
/** Also require Face ID / Touch ID / device credential (iOS) or BiometricPrompt (Android) before each signature. */
|
|
163
|
+
requireDeviceAuthentication?: boolean;
|
|
164
|
+
/**
|
|
165
|
+
* TEST ONLY. Refused by the native module outside debug builds.
|
|
166
|
+
* - `allowSoftwareKeys`: software device keys when there is no Secure Enclave / StrongBox / TEE
|
|
167
|
+
* (simulator, emulator).
|
|
168
|
+
* - `autoApprove`: approve every signature without UI, for automated smoke tests.
|
|
169
|
+
*/
|
|
170
|
+
testing?: { allowSoftwareKeys?: boolean; autoApprove?: boolean };
|
|
171
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
+
import { getAuthonContext } from './authonContext';
|
|
3
|
+
import { AuthonWalletClient } from './client';
|
|
4
|
+
import { AuthonWalletError } from './errors';
|
|
5
|
+
import type { AuthonWalletOptions, EmbeddedWallet, TokenGetter } from './types';
|
|
6
|
+
|
|
7
|
+
export interface UseAuthonWalletOptions extends Omit<AuthonWalletOptions, 'getToken'> {
|
|
8
|
+
/** Defaults to the session of the surrounding `<AuthonProvider>` from @authon/react-native. */
|
|
9
|
+
getToken?: TokenGetter;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface UseAuthonWalletResult {
|
|
13
|
+
/** The client; call ensure(), signMessage(), protect() etc. on it. */
|
|
14
|
+
wallet: AuthonWalletClient;
|
|
15
|
+
/** The signed-in user's wallet, null when signed out or before it is created. */
|
|
16
|
+
current: EmbeddedWallet | null;
|
|
17
|
+
/** False until the first status check finished (or while signed out and the auth SDK is loading). */
|
|
18
|
+
isLoaded: boolean;
|
|
19
|
+
error: AuthonWalletError | null;
|
|
20
|
+
/** Reloads `current`. */
|
|
21
|
+
refresh: () => Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Wallet of the signed-in user. Inside `<AuthonProvider>` (@authon/react-native) the session token
|
|
26
|
+
* comes from the provider and the wallet follows sign-in and sign-out; elsewhere pass `getToken`.
|
|
27
|
+
* The client is recreated only when publishableKey, apiUrl or appId change.
|
|
28
|
+
*/
|
|
29
|
+
export function useAuthonWallet(options: UseAuthonWalletOptions): UseAuthonWalletResult {
|
|
30
|
+
const session = useContext(getAuthonContext());
|
|
31
|
+
const latest = useRef({ options, session });
|
|
32
|
+
latest.current = { options, session };
|
|
33
|
+
|
|
34
|
+
if (!options.getToken && !session) {
|
|
35
|
+
throw new AuthonWalletError(
|
|
36
|
+
'NOT_CONFIGURED',
|
|
37
|
+
'useAuthonWallet needs getToken, or must be used inside <AuthonProvider> from @authon/react-native',
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const appIdKey = typeof options.appId === 'string' ? options.appId : JSON.stringify(options.appId ?? null);
|
|
42
|
+
const testingKey = JSON.stringify(options.testing ?? null);
|
|
43
|
+
const wallet = useMemo(
|
|
44
|
+
() =>
|
|
45
|
+
new AuthonWalletClient({
|
|
46
|
+
...latest.current.options,
|
|
47
|
+
getToken: async () => {
|
|
48
|
+
const { options: o, session: s } = latest.current;
|
|
49
|
+
if (o.getToken) return o.getToken();
|
|
50
|
+
if (!s?.isSignedIn) return null;
|
|
51
|
+
const token = s.getToken?.();
|
|
52
|
+
if (token) return token;
|
|
53
|
+
return (await s.client?.refreshToken?.())?.accessToken ?? null;
|
|
54
|
+
},
|
|
55
|
+
}),
|
|
56
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
57
|
+
[options.publishableKey, options.apiUrl, appIdKey, options.requireDeviceAuthentication, testingKey],
|
|
58
|
+
);
|
|
59
|
+
// dispose() only resets the client, so this is safe under StrictMode's double effects.
|
|
60
|
+
useEffect(() => () => wallet.dispose(), [wallet]);
|
|
61
|
+
|
|
62
|
+
const [current, setCurrent] = useState<EmbeddedWallet | null>(null);
|
|
63
|
+
const [isLoaded, setLoaded] = useState(false);
|
|
64
|
+
const [error, setError] = useState<AuthonWalletError | null>(null);
|
|
65
|
+
|
|
66
|
+
useEffect(() => wallet.onChange(setCurrent), [wallet]);
|
|
67
|
+
|
|
68
|
+
const signedIn = options.getToken ? true : session?.isSignedIn === true;
|
|
69
|
+
const sessionUser = options.getToken ? null : session?.userId ?? null;
|
|
70
|
+
const authLoaded = options.getToken ? true : session?.isLoaded !== false;
|
|
71
|
+
|
|
72
|
+
const lastUser = useRef<string | null | undefined>(undefined);
|
|
73
|
+
|
|
74
|
+
const refresh = useCallback(async () => {
|
|
75
|
+
try {
|
|
76
|
+
setCurrent(await wallet.get());
|
|
77
|
+
setError(null);
|
|
78
|
+
} catch (e) {
|
|
79
|
+
setError(e instanceof AuthonWalletError ? e : new AuthonWalletError('UNKNOWN', String(e)));
|
|
80
|
+
} finally {
|
|
81
|
+
setLoaded(true);
|
|
82
|
+
}
|
|
83
|
+
}, [wallet]);
|
|
84
|
+
|
|
85
|
+
useEffect(() => {
|
|
86
|
+
if (!authLoaded) return;
|
|
87
|
+
if (!signedIn) {
|
|
88
|
+
wallet.dispose();
|
|
89
|
+
lastUser.current = undefined;
|
|
90
|
+
setCurrent(null);
|
|
91
|
+
setError(null);
|
|
92
|
+
setLoaded(true);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
// Another user signed in: drop the previous user's cached accounts and in-flight calls.
|
|
96
|
+
if (lastUser.current !== undefined && lastUser.current !== sessionUser) wallet.dispose();
|
|
97
|
+
lastUser.current = sessionUser;
|
|
98
|
+
setLoaded(false);
|
|
99
|
+
void refresh();
|
|
100
|
+
}, [authLoaded, signedIn, sessionUser, refresh, wallet]);
|
|
101
|
+
|
|
102
|
+
return { wallet, current, isLoaded, error, refresh };
|
|
103
|
+
}
|