@learncard/sss-key-manager 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Learning Economy Foundation <sdk@learningeconomy.io>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,172 @@
1
+ # @learncard/sss-key-manager
2
+
3
+ Shamir Secret Sharing (SSS) key manager for LearnCard - replaces Web3Auth SFA.
4
+
5
+ ## Overview
6
+
7
+ This package provides a secure, self-hosted alternative to Web3Auth Single Factor Authentication (SFA) for managing cryptographic private keys. It uses Shamir Secret Sharing to split keys into multiple shares that can be distributed across device storage, server storage, and recovery methods.
8
+
9
+ ## Features
10
+
11
+ - **Key Splitting**: Split ed25519 private keys into 3 shares with 2-of-3 threshold
12
+ - **Device Storage**: Encrypted local storage using AES-GCM with IndexedDB
13
+ - **Server Storage**: Encrypted auth share stored on server with envelope encryption
14
+ - **Recovery Methods**:
15
+ - Password-based (Argon2id KDF)
16
+ - Passkey/WebAuthn PRF (coming soon)
17
+ - Backup file export/import
18
+ - **Migration**: Seamless migration from Web3Auth SFA
19
+
20
+ ## Installation
21
+
22
+ ```bash
23
+ pnpm add @learncard/sss-key-manager
24
+ ```
25
+
26
+ ## Usage
27
+
28
+ ### Basic Setup
29
+
30
+ ```typescript
31
+ import { createSSSKeyManager } from '@learncard/sss-key-manager';
32
+
33
+ const keyManager = createSSSKeyManager({
34
+ serverUrl: 'https://your-lca-api.com',
35
+ authProvider: {
36
+ getIdToken: async () => firebaseUser.getIdToken(),
37
+ getCurrentUser: async () => ({
38
+ id: firebaseUser.uid,
39
+ email: firebaseUser.email,
40
+ providerType: 'firebase',
41
+ }),
42
+ getProviderType: () => 'firebase',
43
+ signOut: async () => firebaseAuth.signOut(),
44
+ },
45
+ });
46
+ ```
47
+
48
+ ### New User Setup
49
+
50
+ ```typescript
51
+ // Generate new key and split into shares
52
+ const privateKey = await keyManager.setupNewKey();
53
+ console.log('DID:', deriveDid(privateKey));
54
+ ```
55
+
56
+ ### Returning User
57
+
58
+ ```typescript
59
+ // Reconstruct key from device + server shares
60
+ const privateKey = await keyManager.connect();
61
+ ```
62
+
63
+ ### Migration from Web3Auth
64
+
65
+ ```typescript
66
+ // Extract key from Web3Auth and migrate to SSS
67
+ const web3AuthPrivateKey = await web3Auth.provider.request({ method: 'eth_private_key' });
68
+ await keyManager.migrate(web3AuthPrivateKey);
69
+ ```
70
+
71
+ ### Recovery
72
+
73
+ ```typescript
74
+ // Add password recovery
75
+ await keyManager.addRecoveryMethod({
76
+ type: 'password',
77
+ password: userPassword,
78
+ });
79
+
80
+ // Recover with password
81
+ const privateKey = await keyManager.recover({
82
+ type: 'password',
83
+ password: userPassword,
84
+ });
85
+
86
+ // Export backup file
87
+ const backup = await keyManager.exportBackup(backupPassword);
88
+ downloadFile('wallet.lcbackup', JSON.stringify(backup));
89
+
90
+ // Import backup file
91
+ const privateKey = await keyManager.recover({
92
+ type: 'backup',
93
+ fileContents: backupFileJson,
94
+ password: backupPassword,
95
+ });
96
+ ```
97
+
98
+ ## Architecture
99
+
100
+ ```
101
+ ┌─────────────────────────────────────────────────────────┐
102
+ │ Private Key │
103
+ └─────────────────────────────────────────────────────────┘
104
+ │
105
+ SSS Split (3,2)
106
+ │
107
+ ┌─────────────────┼─────────────────┐
108
+ ▼ ▼ ▼
109
+ ┌───────────────┐ ┌───────────────┐ ┌───────────────┐
110
+ │ Device Share │ │ Auth Share │ │Recovery Share │
111
+ │ (IndexedDB) │ │ (Server) │ │ (Optional) │
112
+ │ AES-GCM local │ │ Envelope enc │ │ Password/Key │
113
+ └───────────────┘ └───────────────┘ └───────────────┘
114
+ ```
115
+
116
+ ## Security Model
117
+
118
+ - **Device Share**: Encrypted with non-extractable AES-GCM key stored in IndexedDB
119
+ - **Auth Share**: Server-side envelope encryption (DEK + KMS-encrypted DEK)
120
+ - **Recovery Share**: Password-based uses Argon2id KDF with secure parameters
121
+ - **Threshold**: Any 2 of 3 shares can reconstruct the key
122
+
123
+ ## Auth Provider Support
124
+
125
+ The package is designed to work with any authentication provider:
126
+
127
+ - Firebase Authentication (default for production)
128
+ - SuperTokens (recommended for self-hosting/local dev)
129
+ - Keycloak (enterprise SSO)
130
+ - Any OIDC-compliant provider
131
+
132
+ ## API Reference
133
+
134
+ ### `createSSSKeyManager(config)`
135
+
136
+ Creates a new SSS Key Manager instance.
137
+
138
+ ### `keyManager.connect()`
139
+
140
+ Reconstructs the private key from device and auth shares.
141
+
142
+ ### `keyManager.setupNewKey()`
143
+
144
+ Generates a new private key and splits it into shares.
145
+
146
+ ### `keyManager.setupWithKey(privateKey, primaryDid?)`
147
+
148
+ Sets up SSS with an existing private key (used for migration).
149
+
150
+ ### `keyManager.migrate(privateKey)`
151
+
152
+ Migrates from Web3Auth to SSS, preserving the existing key.
153
+
154
+ ### `keyManager.addRecoveryMethod(method)`
155
+
156
+ Adds a recovery method (password, passkey, or backup).
157
+
158
+ ### `keyManager.recover(method)`
159
+
160
+ Recovers the private key using a recovery method.
161
+
162
+ ### `keyManager.exportBackup(password)`
163
+
164
+ Exports an encrypted backup file.
165
+
166
+ ### `keyManager.getSecurityLevel()`
167
+
168
+ Returns the current security level based on configured recovery methods.
169
+
170
+ ## License
171
+
172
+ MIT
package/dist/index.js ADDED
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ if (process.env.NODE_ENV === 'production') {
4
+ module.exports = require('./sss-key-manager.cjs.production.min.js');
5
+ } else {
6
+ module.exports = require('./sss-key-manager.cjs.development.js');
7
+ }