@noosphere/crypto 0.1.0-alpha.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/README.md ADDED
@@ -0,0 +1,202 @@
1
+ # @noosphere/crypto
2
+
3
+ Cryptographic utilities and wallet management for Noosphere SDK.
4
+
5
+ This package provides secure keystore and wallet management functionality shared across agents, users, and verifiers.
6
+
7
+ ## Features
8
+
9
+ - **KeystoreManager**: Secure storage for EOA and payment wallets
10
+ - **WalletManager**: Wallet operations and EIP-712 signing
11
+ - **Hub-Compatible**: Uses the same keystore structure as Noosphere Hub
12
+ - **Password-Protected**: AES-128-CTR encryption for EOA wallets
13
+ - **Payment Wallet Support**: Manage multiple payment wallets per subscription
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm install @noosphere/crypto
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ ### Initialize Keystore (First Time)
24
+
25
+ ```typescript
26
+ import { KeystoreManager } from '@noosphere/crypto';
27
+ import { ethers } from 'ethers';
28
+
29
+ const provider = new ethers.JsonRpcProvider('https://sepolia.infura.io/v3/YOUR_KEY');
30
+
31
+ // First-time setup
32
+ const keystoreManager = await KeystoreManager.initialize(
33
+ './.noosphere/keystore.json',
34
+ 'secure-password',
35
+ '0x...your-private-key',
36
+ provider
37
+ );
38
+ ```
39
+
40
+ ### Load Existing Keystore
41
+
42
+ ```typescript
43
+ import { KeystoreManager, WalletManager } from '@noosphere/crypto';
44
+
45
+ // Load keystore
46
+ const keystoreManager = new KeystoreManager(
47
+ './.noosphere/keystore.json',
48
+ 'secure-password'
49
+ );
50
+ await keystoreManager.load();
51
+
52
+ // Get EOA wallet
53
+ const provider = new ethers.JsonRpcProvider('...');
54
+ const wallet = await keystoreManager.getEOA(provider);
55
+
56
+ // Or use WalletManager
57
+ const walletManager = await WalletManager.fromKeystoreManager(
58
+ keystoreManager,
59
+ provider
60
+ );
61
+ ```
62
+
63
+ ### Payment Wallet Management
64
+
65
+ ```typescript
66
+ // Create EOA payment wallet (automatically saved to keystore)
67
+ const { walletAddress } = await walletManager.createEOAPaymentWallet(
68
+ 'subscription-123'
69
+ );
70
+
71
+ // List all payment wallets
72
+ const wallets = walletManager.listPaymentWallets();
73
+ wallets.forEach(wallet => {
74
+ console.log(`${wallet.address} - ${wallet.subscriptionId}`);
75
+ });
76
+
77
+ // Get specific payment wallet
78
+ const paymentWallet = await walletManager.getPaymentWallet(walletAddress);
79
+ ```
80
+
81
+ ### WalletFactory Integration
82
+
83
+ ```typescript
84
+ // Create smart contract wallet via WalletFactory
85
+ const { walletAddress, txHash } = await walletManager.createPaymentWallet(
86
+ walletFactoryAddress,
87
+ subscriptionOwner
88
+ );
89
+
90
+ // Validate wallet
91
+ const isValid = await walletManager.isValidWallet(
92
+ walletFactoryAddress,
93
+ walletAddress
94
+ );
95
+ ```
96
+
97
+ ## API Reference
98
+
99
+ ### KeystoreManager
100
+
101
+ #### Static Methods
102
+
103
+ - `initialize(keystorePath, password, privateKey, provider)` - Initialize new keystore
104
+ - `importKeystore(keystorePath, password, keystoreJson)` - Import keystore from backup
105
+
106
+ #### Instance Methods
107
+
108
+ - `load()` - Load existing keystore
109
+ - `getEOA(provider)` - Get decrypted EOA wallet
110
+ - `getEOAAddress()` - Get EOA address without decrypting
111
+ - `addPaymentWallet(address, privateKey, subscriptionId?, metadata?)` - Add payment wallet
112
+ - `getPaymentWallet(address, provider)` - Get payment wallet
113
+ - `listPaymentWallets()` - List all payment wallets
114
+ - `removePaymentWallet(address)` - Remove payment wallet
115
+ - `getInfo()` - Get keystore info without decrypting
116
+ - `hasPaymentWallet(address)` - Check if wallet exists
117
+ - `updateEOA(privateKey, provider)` - Update EOA
118
+ - `exportKeystore()` - Export for backup
119
+ - `changePassword(oldPassword, newPassword, provider)` - Change password
120
+
121
+ ### WalletManager
122
+
123
+ #### Static Methods
124
+
125
+ - `fromKeystoreManager(keystoreManager, provider)` - **RECOMMENDED** initialization
126
+ - `fromKeystore(keystorePath, password, provider)` - Load from keystore file
127
+
128
+ #### Instance Methods
129
+
130
+ - `getAddress()` - Get wallet address
131
+ - `getWallet()` - Get wallet instance
132
+ - `getDeterministicPaymentWallet(subscriptionId)` - Generate deterministic wallet
133
+ - `signTypedData(domain, types, value)` - Sign EIP-712 data
134
+ - `getBalance()` - Get ETH balance
135
+ - `getTokenBalance(tokenAddress)` - Get ERC20 balance
136
+ - `createPaymentWallet(walletFactoryAddress, owner, subscriptionId?)` - Create via WalletFactory
137
+ - `createEOAPaymentWallet(subscriptionId?)` - Create EOA wallet
138
+ - `getPaymentWallet(address)` - Get payment wallet from keystore
139
+ - `listPaymentWallets()` - List payment wallets
140
+ - `isValidWallet(walletFactoryAddress, address)` - Validate factory wallet
141
+ - `fundWallet(address, amount)` - Fund wallet with ETH
142
+ - `getWalletBalance(address)` - Get wallet balance
143
+ - `toKeystore(password, outputPath)` - Save to keystore
144
+
145
+ ## Types
146
+
147
+ ### NoosphereKeystore
148
+
149
+ ```typescript
150
+ interface NoosphereKeystore {
151
+ version: string;
152
+ eoa: {
153
+ address: string;
154
+ keystore: string; // Encrypted JSON
155
+ };
156
+ paymentWallets: {
157
+ [address: string]: {
158
+ address: string;
159
+ privateKey: string; // Encrypted
160
+ subscriptionId?: string;
161
+ createdAt: string;
162
+ metadata?: Record<string, any>;
163
+ };
164
+ };
165
+ createdAt: string;
166
+ updatedAt: string;
167
+ }
168
+ ```
169
+
170
+ ### PaymentWalletInfo
171
+
172
+ ```typescript
173
+ interface PaymentWalletInfo {
174
+ address: string;
175
+ subscriptionId?: string;
176
+ createdAt: string;
177
+ metadata?: Record<string, any>;
178
+ }
179
+ ```
180
+
181
+ ### KeystoreInfo
182
+
183
+ ```typescript
184
+ interface KeystoreInfo {
185
+ version: string;
186
+ eoaAddress: string;
187
+ paymentWalletCount: number;
188
+ createdAt: string;
189
+ updatedAt: string;
190
+ }
191
+ ```
192
+
193
+ ## Security
194
+
195
+ - **EOA Encryption**: Uses ethers.js native encryption (AES-128-CTR, PBKDF2)
196
+ - **Password Protection**: All wallets protected by password
197
+ - **No Plaintext Keys**: Private keys never stored in plaintext
198
+ - **Metadata Support**: Store additional info securely
199
+
200
+ ## License
201
+
202
+ MIT