@tetherto/wdk-react-native-secure-storage 1.0.0-beta.1
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/LICENSE +180 -0
- package/README.md +472 -0
- package/package.json +72 -0
- package/src/__tests__/__mocks__/expo-crypto.ts +35 -0
- package/src/__tests__/__mocks__/expo-local-authentication.ts +40 -0
- package/src/__tests__/__mocks__/react-native-keychain.ts +60 -0
- package/src/__tests__/errors.test.ts +78 -0
- package/src/__tests__/logger.test.ts +131 -0
- package/src/__tests__/secureStorage.test.ts +1073 -0
- package/src/__tests__/utils.test.ts +182 -0
- package/src/__tests__/validation.test.ts +222 -0
- package/src/constants.ts +12 -0
- package/src/errors.ts +97 -0
- package/src/index.ts +41 -0
- package/src/keychainHelpers.ts +34 -0
- package/src/logger.ts +125 -0
- package/src/secureStorage.ts +707 -0
- package/src/utils.ts +176 -0
- package/src/validation.ts +160 -0
- package/tsconfig.json +24 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(which shall not include Communications that are clearly marked or
|
|
39
|
+
otherwise designated in writing by the copyright owner as "Not a Work").
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is clearly marked or otherwise designated
|
|
61
|
+
in writing by the copyright owner as "Not a Contribution".
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,472 @@
|
|
|
1
|
+
# @tetherto/wdk-react-native-secure-storage
|
|
2
|
+
|
|
3
|
+
Secure storage abstractions for React Native - provides secure storage for sensitive data (encrypted seeds, keys) using react-native-keychain.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🔒 Secure storage using native keychain/keystore
|
|
8
|
+
- 📱 iOS Keychain integration with selective iCloud sync
|
|
9
|
+
- 🤖 Android Keystore integration with selective Google Cloud backup
|
|
10
|
+
- 🔐 Biometric authentication support
|
|
11
|
+
- 💾 Encrypted data storage at rest
|
|
12
|
+
- ✅ Comprehensive input validation
|
|
13
|
+
- 📊 Structured logging support
|
|
14
|
+
- ⏱️ Configurable timeouts
|
|
15
|
+
- 🎯 TypeScript support with full type safety
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
### From npm (Recommended for Production)
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install @tetherto/wdk-react-native-secure-storage
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### From GitHub (Development/Unpublished Versions)
|
|
26
|
+
|
|
27
|
+
If the package is not yet published to npm, you can install directly from GitHub:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
npm install https://github.com/tetherto/wdk-react-native-secure-storage.git
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Or add to your `package.json`:
|
|
34
|
+
|
|
35
|
+
```json
|
|
36
|
+
{
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@tetherto/wdk-react-native-secure-storage": "github:tetherto/wdk-react-native-secure-storage"
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Then run `npm install`.
|
|
44
|
+
|
|
45
|
+
## Peer Dependencies
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
npm install react-native@">=0.70.0"
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Usage
|
|
52
|
+
|
|
53
|
+
### Basic Usage
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
import { createSecureStorage } from '@tetherto/wdk-react-native-secure-storage'
|
|
57
|
+
|
|
58
|
+
// Create storage instance
|
|
59
|
+
const storage = createSecureStorage()
|
|
60
|
+
|
|
61
|
+
// Store encryption key
|
|
62
|
+
await storage.setEncryptionKey('my-encryption-key', 'user@example.com')
|
|
63
|
+
|
|
64
|
+
// Retrieve encryption key
|
|
65
|
+
const key = await storage.getEncryptionKey('user@example.com')
|
|
66
|
+
if (key) {
|
|
67
|
+
console.log('Key retrieved:', key)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Store encrypted seed
|
|
71
|
+
await storage.setEncryptedSeed('encrypted-seed-data', 'user@example.com')
|
|
72
|
+
|
|
73
|
+
// Store encrypted entropy
|
|
74
|
+
await storage.setEncryptedEntropy('encrypted-entropy-data', 'user@example.com')
|
|
75
|
+
|
|
76
|
+
// Get all encrypted data
|
|
77
|
+
const allData = await storage.getAllEncrypted('user@example.com')
|
|
78
|
+
console.log('All data:', allData)
|
|
79
|
+
|
|
80
|
+
// Check if wallet exists
|
|
81
|
+
const exists = await storage.hasWallet('user@example.com')
|
|
82
|
+
|
|
83
|
+
// Delete wallet
|
|
84
|
+
await storage.deleteWallet('user@example.com')
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Advanced Usage with Options
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
import { createSecureStorage, defaultLogger, LogLevel } from '@tetherto/wdk-react-native-secure-storage'
|
|
91
|
+
|
|
92
|
+
// Configure logger
|
|
93
|
+
defaultLogger.setLevel(LogLevel.INFO)
|
|
94
|
+
|
|
95
|
+
// Create storage with custom options
|
|
96
|
+
const storage = createSecureStorage({
|
|
97
|
+
logger: customLogger, // Optional custom logger
|
|
98
|
+
authentication: {
|
|
99
|
+
promptMessage: 'Authenticate to access your wallet',
|
|
100
|
+
cancelLabel: 'Cancel',
|
|
101
|
+
disableDeviceFallback: false,
|
|
102
|
+
},
|
|
103
|
+
timeoutMs: 30000, // 30 seconds default
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
// Use storage
|
|
107
|
+
await storage.setEncryptionKey('key', 'user@example.com')
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Error Handling
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
import {
|
|
114
|
+
createSecureStorage,
|
|
115
|
+
ValidationError,
|
|
116
|
+
KeychainWriteError,
|
|
117
|
+
KeychainReadError,
|
|
118
|
+
AuthenticationError,
|
|
119
|
+
TimeoutError,
|
|
120
|
+
} from '@tetherto/wdk-react-native-secure-storage'
|
|
121
|
+
|
|
122
|
+
const storage = createSecureStorage()
|
|
123
|
+
|
|
124
|
+
try {
|
|
125
|
+
await storage.setEncryptionKey('my-key', 'user@example.com')
|
|
126
|
+
} catch (error) {
|
|
127
|
+
if (error instanceof ValidationError) {
|
|
128
|
+
console.error('Invalid input:', error.message)
|
|
129
|
+
} else if (error instanceof KeychainWriteError) {
|
|
130
|
+
console.error('Failed to write to keychain:', error.message)
|
|
131
|
+
} else if (error instanceof TimeoutError) {
|
|
132
|
+
console.error('Operation timed out:', error.message)
|
|
133
|
+
} else {
|
|
134
|
+
console.error('Unexpected error:', error)
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
try {
|
|
139
|
+
const key = await storage.getEncryptionKey('user@example.com')
|
|
140
|
+
if (!key) {
|
|
141
|
+
console.log('Key not found')
|
|
142
|
+
}
|
|
143
|
+
} catch (error) {
|
|
144
|
+
if (error instanceof AuthenticationError) {
|
|
145
|
+
console.error('Authentication failed:', error.message)
|
|
146
|
+
} else if (error instanceof KeychainReadError) {
|
|
147
|
+
console.error('Failed to read from keychain:', error.message)
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### Multiple Wallets
|
|
153
|
+
|
|
154
|
+
The identifier parameter allows you to support multiple wallets:
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
// Store data for different users
|
|
158
|
+
await storage.setEncryptionKey('key1', 'user1@example.com')
|
|
159
|
+
await storage.setEncryptionKey('key2', 'user2@example.com')
|
|
160
|
+
|
|
161
|
+
// Retrieve specific user's data
|
|
162
|
+
const key1 = await storage.getEncryptionKey('user1@example.com')
|
|
163
|
+
const key2 = await storage.getEncryptionKey('user2@example.com')
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
## API Reference
|
|
167
|
+
|
|
168
|
+
### `createSecureStorage(options?)`
|
|
169
|
+
|
|
170
|
+
Creates a new instance of secure storage. Each call returns a new instance with the specified options.
|
|
171
|
+
For most apps, you should create one instance and reuse it throughout your application.
|
|
172
|
+
|
|
173
|
+
**Options:**
|
|
174
|
+
- `logger?: Logger` - Custom logger instance
|
|
175
|
+
- `authentication?: AuthenticationOptions` - Authentication prompt configuration
|
|
176
|
+
- `timeoutMs?: number` - Timeout for keychain operations (default: 30000ms, min: 1000ms, max: 300000ms)
|
|
177
|
+
|
|
178
|
+
**Returns:** `SecureStorage` instance
|
|
179
|
+
|
|
180
|
+
### `SecureStorage` Interface
|
|
181
|
+
|
|
182
|
+
#### `setEncryptionKey(key: string, identifier?: string): Promise<void>`
|
|
183
|
+
|
|
184
|
+
Stores an encryption key securely.
|
|
185
|
+
|
|
186
|
+
**Parameters:**
|
|
187
|
+
- `key: string` - The encryption key (max 10KB, non-empty)
|
|
188
|
+
- `identifier?: string` - Optional identifier for multiple wallets (max 256 chars)
|
|
189
|
+
|
|
190
|
+
**Throws:**
|
|
191
|
+
- `ValidationError` - If input is invalid
|
|
192
|
+
- `KeychainWriteError` - If keychain operation fails
|
|
193
|
+
- `TimeoutError` - If operation times out
|
|
194
|
+
|
|
195
|
+
#### `getEncryptionKey(identifier?: string): Promise<string | null>`
|
|
196
|
+
|
|
197
|
+
Retrieves an encryption key.
|
|
198
|
+
|
|
199
|
+
**Parameters:**
|
|
200
|
+
- `identifier?: string` - Optional identifier
|
|
201
|
+
|
|
202
|
+
**Returns:** The encryption key or `null` if not found
|
|
203
|
+
|
|
204
|
+
**Throws:**
|
|
205
|
+
- `ValidationError` - If identifier is invalid
|
|
206
|
+
- `AuthenticationError` - If authentication fails
|
|
207
|
+
- `KeychainReadError` - If keychain operation fails
|
|
208
|
+
- `TimeoutError` - If operation times out
|
|
209
|
+
|
|
210
|
+
#### `setEncryptedSeed(encryptedSeed: string, identifier?: string): Promise<void>`
|
|
211
|
+
|
|
212
|
+
Stores encrypted seed data.
|
|
213
|
+
|
|
214
|
+
#### `getEncryptedSeed(identifier?: string): Promise<string | null>`
|
|
215
|
+
|
|
216
|
+
Retrieves encrypted seed data.
|
|
217
|
+
|
|
218
|
+
#### `setEncryptedEntropy(encryptedEntropy: string, identifier?: string): Promise<void>`
|
|
219
|
+
|
|
220
|
+
Stores encrypted entropy data.
|
|
221
|
+
|
|
222
|
+
#### `getEncryptedEntropy(identifier?: string): Promise<string | null>`
|
|
223
|
+
|
|
224
|
+
Retrieves encrypted entropy data.
|
|
225
|
+
|
|
226
|
+
#### `getAllEncrypted(identifier?: string): Promise<{encryptedSeed: string | null, encryptedEntropy: string | null, encryptionKey: string | null}>`
|
|
227
|
+
|
|
228
|
+
Retrieves all encrypted wallet data at once.
|
|
229
|
+
|
|
230
|
+
#### `hasWallet(identifier?: string): Promise<boolean>`
|
|
231
|
+
|
|
232
|
+
Checks if wallet credentials exist.
|
|
233
|
+
|
|
234
|
+
#### `deleteWallet(identifier?: string): Promise<void>`
|
|
235
|
+
|
|
236
|
+
Deletes all wallet credentials.
|
|
237
|
+
|
|
238
|
+
**Throws:**
|
|
239
|
+
- `ValidationError` - If identifier is invalid
|
|
240
|
+
- `SecureStorageError` - If deletion fails (with details of which items failed)
|
|
241
|
+
- `TimeoutError` - If operation times out
|
|
242
|
+
|
|
243
|
+
#### `isBiometricAvailable(): Promise<boolean>`
|
|
244
|
+
|
|
245
|
+
Checks if biometric authentication is available.
|
|
246
|
+
|
|
247
|
+
#### `authenticate(): Promise<boolean>`
|
|
248
|
+
|
|
249
|
+
Authenticates with biometrics. Returns `true` if successful, `false` otherwise.
|
|
250
|
+
|
|
251
|
+
**Throws:**
|
|
252
|
+
- `AuthenticationError` - If authentication fails
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
### Logger Interface
|
|
256
|
+
|
|
257
|
+
The module provides a `Logger` interface for structured logging. The default logger can be configured:
|
|
258
|
+
|
|
259
|
+
```typescript
|
|
260
|
+
import { defaultLogger, LogLevel } from '@tetherto/wdk-react-native-secure-storage'
|
|
261
|
+
|
|
262
|
+
// Set the minimum log level (logs below this level will be ignored)
|
|
263
|
+
defaultLogger.setLevel(LogLevel.INFO)
|
|
264
|
+
|
|
265
|
+
// Available log levels: DEBUG, INFO, WARN, ERROR
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
You can also provide a custom logger that implements the `Logger` interface:
|
|
269
|
+
|
|
270
|
+
```typescript
|
|
271
|
+
const customLogger: Logger = {
|
|
272
|
+
debug: (message, context) => { /* ... */ },
|
|
273
|
+
info: (message, context) => { /* ... */ },
|
|
274
|
+
warn: (message, context) => { /* ... */ },
|
|
275
|
+
error: (message, error, context) => { /* ... */ },
|
|
276
|
+
setLevel: (level) => { /* optional */ },
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const storage = createSecureStorage({ logger: customLogger })
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
## Module Lifecycle & Resource Management
|
|
283
|
+
|
|
284
|
+
The module has no shared state or cleanup requirements. Each storage instance is independent and can be used without any module-level lifecycle management.
|
|
285
|
+
|
|
286
|
+
## Security Features
|
|
287
|
+
|
|
288
|
+
### Input Validation
|
|
289
|
+
- All inputs are validated before processing
|
|
290
|
+
- Maximum length limits enforced (10KB for values, 256 chars for identifiers)
|
|
291
|
+
- Invalid characters rejected
|
|
292
|
+
- Type checking at runtime
|
|
293
|
+
- All validation happens before any side effects
|
|
294
|
+
|
|
295
|
+
### Error Handling
|
|
296
|
+
- Comprehensive error types for different failure scenarios
|
|
297
|
+
- Detailed error messages
|
|
298
|
+
- Proper error propagation
|
|
299
|
+
|
|
300
|
+
### Logging
|
|
301
|
+
- Structured logging for security events
|
|
302
|
+
- Configurable log levels
|
|
303
|
+
- No sensitive data in logs
|
|
304
|
+
|
|
305
|
+
## Error Types
|
|
306
|
+
|
|
307
|
+
- `SecureStorageError` - Base error class
|
|
308
|
+
- `KeychainError` - Keychain operation errors
|
|
309
|
+
- `KeychainWriteError` - Keychain write failures
|
|
310
|
+
- `KeychainReadError` - Keychain read failures
|
|
311
|
+
- `AuthenticationError` - Authentication failures
|
|
312
|
+
- `ValidationError` - Input validation failures
|
|
313
|
+
- `TimeoutError` - Operation timeout errors
|
|
314
|
+
|
|
315
|
+
## Development
|
|
316
|
+
|
|
317
|
+
### Testing
|
|
318
|
+
|
|
319
|
+
```bash
|
|
320
|
+
# Run tests
|
|
321
|
+
npm test
|
|
322
|
+
|
|
323
|
+
# Run tests in watch mode
|
|
324
|
+
npm run test:watch
|
|
325
|
+
|
|
326
|
+
# Run tests with coverage
|
|
327
|
+
npm run test:coverage
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
### Code Quality
|
|
331
|
+
|
|
332
|
+
```bash
|
|
333
|
+
# Type checking
|
|
334
|
+
npm run typecheck
|
|
335
|
+
|
|
336
|
+
# Linting
|
|
337
|
+
npm run lint
|
|
338
|
+
npm run lint:fix
|
|
339
|
+
|
|
340
|
+
# Formatting
|
|
341
|
+
npm run format
|
|
342
|
+
npm run format:check
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
**Note:** ESLint and Prettier are configured but need to be installed as dev dependencies:
|
|
346
|
+
```bash
|
|
347
|
+
npm install --save-dev eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin prettier
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
## Production Readiness
|
|
351
|
+
|
|
352
|
+
This module is production-ready and includes:
|
|
353
|
+
|
|
354
|
+
✅ **Built and tested** - All code is compiled to JavaScript with TypeScript definitions
|
|
355
|
+
✅ **Proper exports** - Only necessary files are included in the npm package
|
|
356
|
+
✅ **Security hardened** - Input validation and secure storage with device-level protections
|
|
357
|
+
✅ **Error handling** - Comprehensive error types for all failure scenarios
|
|
358
|
+
✅ **Logging** - Structured logging with configurable levels (defaults to ERROR in production)
|
|
359
|
+
✅ **Type safety** - Full TypeScript support with exported types
|
|
360
|
+
✅ **Documentation** - Complete API documentation and usage examples
|
|
361
|
+
|
|
362
|
+
### Production Best Practices
|
|
363
|
+
|
|
364
|
+
1. **Configure Logging**: Set appropriate log levels for your environment:
|
|
365
|
+
```typescript
|
|
366
|
+
import { defaultLogger, LogLevel } from '@tetherto/wdk-react-native-secure-storage'
|
|
367
|
+
|
|
368
|
+
// In production, only log errors and warnings
|
|
369
|
+
defaultLogger.setLevel(LogLevel.WARN)
|
|
370
|
+
|
|
371
|
+
// In development, you might want more verbose logging
|
|
372
|
+
if (__DEV__) {
|
|
373
|
+
defaultLogger.setLevel(LogLevel.DEBUG)
|
|
374
|
+
}
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
2. **Error Handling**: Always handle errors appropriately:
|
|
378
|
+
```typescript
|
|
379
|
+
try {
|
|
380
|
+
await storage.setEncryptionKey(key, identifier)
|
|
381
|
+
} catch (error) {
|
|
382
|
+
// Log error to your error tracking service (e.g., Sentry)
|
|
383
|
+
// Never log sensitive data like keys or seeds
|
|
384
|
+
if (error instanceof ValidationError) {
|
|
385
|
+
// Handle validation errors
|
|
386
|
+
} else if (error instanceof KeychainWriteError) {
|
|
387
|
+
// Handle keychain errors
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
3. **Single Instance**: Create one storage instance and reuse it:
|
|
393
|
+
```typescript
|
|
394
|
+
// Good: Create once and reuse
|
|
395
|
+
const storage = createSecureStorage({ logger: customLogger })
|
|
396
|
+
|
|
397
|
+
// Avoid: Creating multiple instances unnecessarily
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
## Security Considerations
|
|
401
|
+
|
|
402
|
+
- Data is encrypted at rest by iOS Keychain / Android Keystore
|
|
403
|
+
- Cloud sync behavior:
|
|
404
|
+
- **Encryption key**: Synced via iCloud Keychain (iOS) and Google Cloud backup (Android)
|
|
405
|
+
- **Encrypted seed and entropy**: Device-only storage (not synced across devices)
|
|
406
|
+
- Biometric authentication required when available
|
|
407
|
+
- Device-level keychain/keystore provides rate limiting and lockout mechanisms
|
|
408
|
+
- **No sensitive data is logged** - The logger only logs error messages and metadata
|
|
409
|
+
|
|
410
|
+
## Security Limitations
|
|
411
|
+
|
|
412
|
+
⚠️ **Important Security Notes:**
|
|
413
|
+
|
|
414
|
+
1. **Timeout Resource Usage**: The timeout implementation uses `Promise.race()` which does NOT cancel the underlying keychain operation. The operation continues executing even after timeout, though its result is ignored. This means:
|
|
415
|
+
- Under extreme load, timed-out keychain operations may continue executing in the background
|
|
416
|
+
- Memory and resources are not immediately freed on timeout
|
|
417
|
+
- This is generally acceptable because:
|
|
418
|
+
- Keychain operations are typically fast (< 1 second)
|
|
419
|
+
- They are bounded in duration by the OS
|
|
420
|
+
- Timeouts are a safety mechanism, not a normal occurrence
|
|
421
|
+
- React Native's single-threaded nature limits concurrent operations
|
|
422
|
+
- **Monitoring**: In production, monitor timeout frequency. If timeouts occur frequently, investigate keychain performance or increase timeout values.
|
|
423
|
+
|
|
424
|
+
2. **Device Authentication**: On devices without authentication (no PIN/password/biometrics), data is still encrypted at rest but accessible when the device is unlocked. This is a limitation of the underlying platform security model.
|
|
425
|
+
|
|
426
|
+
3. **Device-Level Rate Limiting**: The module relies on device-level keychain/keystore rate limiting and lockout mechanisms. These are more robust than app-level rate limiting and persist across app restarts.
|
|
427
|
+
|
|
428
|
+
## Contributing
|
|
429
|
+
|
|
430
|
+
Contributions are welcome! Please follow these steps:
|
|
431
|
+
|
|
432
|
+
1. Fork the repository
|
|
433
|
+
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
|
|
434
|
+
3. Make your changes
|
|
435
|
+
4. Run tests (`npm test`)
|
|
436
|
+
5. Run type checking (`npm run typecheck`)
|
|
437
|
+
6. Run linting (`npm run lint`)
|
|
438
|
+
7. Format code (`npm run format`)
|
|
439
|
+
8. Build the project (`npm run build`)
|
|
440
|
+
9. Commit your changes (`git commit -m 'Add amazing feature'`)
|
|
441
|
+
10. Push to the branch (`git push origin feature/amazing-feature`)
|
|
442
|
+
11. Open a Pull Request
|
|
443
|
+
|
|
444
|
+
### Development Setup
|
|
445
|
+
|
|
446
|
+
```bash
|
|
447
|
+
# Clone the repository
|
|
448
|
+
git clone https://github.com/tetherto/wdk-react-native-secure-storage.git
|
|
449
|
+
cd wdk-react-native-secure-storage
|
|
450
|
+
|
|
451
|
+
# Install dependencies
|
|
452
|
+
npm install
|
|
453
|
+
|
|
454
|
+
# Run tests
|
|
455
|
+
npm test
|
|
456
|
+
|
|
457
|
+
# Run type checking
|
|
458
|
+
npm run typecheck
|
|
459
|
+
|
|
460
|
+
# Run linting
|
|
461
|
+
npm run lint
|
|
462
|
+
|
|
463
|
+
# Format code
|
|
464
|
+
npm run format
|
|
465
|
+
|
|
466
|
+
# Build
|
|
467
|
+
npm run build
|
|
468
|
+
```
|
|
469
|
+
|
|
470
|
+
## License
|
|
471
|
+
|
|
472
|
+
Apache-2.0
|