@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.
@@ -0,0 +1,231 @@
1
+ import { ethers } from 'ethers';
2
+
3
+ /**
4
+ * Types for Noosphere crypto package
5
+ */
6
+ /**
7
+ * Keystore format for storing EOA and payment wallets
8
+ */
9
+ interface NoosphereKeystore {
10
+ version: string;
11
+ eoa: {
12
+ address: string;
13
+ keystore: string;
14
+ };
15
+ paymentWallets: {
16
+ [walletAddress: string]: {
17
+ address: string;
18
+ privateKey: string;
19
+ subscriptionId?: string;
20
+ createdAt: string;
21
+ metadata?: Record<string, any>;
22
+ };
23
+ };
24
+ createdAt: string;
25
+ updatedAt: string;
26
+ }
27
+ /**
28
+ * Payment wallet information
29
+ */
30
+ interface PaymentWalletInfo {
31
+ address: string;
32
+ subscriptionId?: string;
33
+ createdAt: string;
34
+ metadata?: Record<string, any>;
35
+ }
36
+ /**
37
+ * Keystore information (without decrypting)
38
+ */
39
+ interface KeystoreInfo {
40
+ version: string;
41
+ eoaAddress: string;
42
+ paymentWalletCount: number;
43
+ createdAt: string;
44
+ updatedAt: string;
45
+ }
46
+
47
+ /**
48
+ * KeystoreManager manages EOA and payment wallets in a single encrypted keystore file
49
+ * This follows the Hub's keystore structure
50
+ */
51
+ declare class KeystoreManager {
52
+ private keystorePath;
53
+ private password;
54
+ private keystore;
55
+ constructor(keystorePath: string, password: string);
56
+ /**
57
+ * Initialize a new keystore with an EOA
58
+ * @param privateKey - Private key of the EOA (agent's main wallet)
59
+ * @param provider - Ethereum provider
60
+ */
61
+ static initialize(keystorePath: string, password: string, privateKey: string, provider: ethers.Provider): Promise<KeystoreManager>;
62
+ /**
63
+ * Load existing keystore
64
+ */
65
+ load(): Promise<void>;
66
+ /**
67
+ * Get the EOA wallet
68
+ */
69
+ getEOA(provider: ethers.Provider): Promise<ethers.Wallet>;
70
+ /**
71
+ * Get EOA address without decrypting
72
+ */
73
+ getEOAAddress(): string;
74
+ /**
75
+ * Add a payment wallet to the keystore
76
+ * @param walletAddress - Address of the payment wallet
77
+ * @param privateKey - Private key of the payment wallet
78
+ * @param subscriptionId - Optional subscription ID this wallet is for
79
+ */
80
+ addPaymentWallet(walletAddress: string, privateKey: string, subscriptionId?: string, metadata?: Record<string, any>): Promise<void>;
81
+ /**
82
+ * Get a payment wallet
83
+ */
84
+ getPaymentWallet(walletAddress: string, provider: ethers.Provider): Promise<ethers.Wallet>;
85
+ /**
86
+ * List all payment wallet addresses
87
+ */
88
+ listPaymentWallets(): PaymentWalletInfo[];
89
+ /**
90
+ * Remove a payment wallet
91
+ */
92
+ removePaymentWallet(walletAddress: string): Promise<void>;
93
+ /**
94
+ * Get keystore info without decrypting
95
+ */
96
+ getInfo(): KeystoreInfo;
97
+ /**
98
+ * Check if a payment wallet exists
99
+ */
100
+ hasPaymentWallet(walletAddress: string): boolean;
101
+ /**
102
+ * Update EOA (re-encrypt with new password or new private key)
103
+ */
104
+ updateEOA(newPrivateKey: string, provider: ethers.Provider): Promise<void>;
105
+ /**
106
+ * Save keystore to disk
107
+ */
108
+ private save;
109
+ /**
110
+ * Encrypt data using password
111
+ * Uses a simple base64 encoding for now - in production, use proper encryption
112
+ */
113
+ private encryptData;
114
+ /**
115
+ * Decrypt data using password
116
+ */
117
+ private decryptData;
118
+ /**
119
+ * Export keystore for backup
120
+ */
121
+ exportKeystore(): Promise<string>;
122
+ /**
123
+ * Import keystore from backup
124
+ */
125
+ static importKeystore(keystorePath: string, password: string, keystoreJson: string): Promise<KeystoreManager>;
126
+ /**
127
+ * Change password (re-encrypt all wallets)
128
+ */
129
+ changePassword(oldPassword: string, newPassword: string, provider: ethers.Provider): Promise<void>;
130
+ }
131
+
132
+ declare class WalletManager {
133
+ private wallet;
134
+ private provider;
135
+ private keystoreManager?;
136
+ constructor(privateKey: string, provider: ethers.Provider, keystoreManager?: KeystoreManager);
137
+ /**
138
+ * Initialize WalletManager from KeystoreManager
139
+ * This is the recommended way to create a WalletManager for production use
140
+ */
141
+ static fromKeystoreManager(keystoreManager: KeystoreManager, provider: ethers.Provider): Promise<WalletManager>;
142
+ /**
143
+ * Get the agent's wallet address
144
+ */
145
+ getAddress(): string;
146
+ /**
147
+ * Get the wallet instance
148
+ */
149
+ getWallet(): ethers.Wallet;
150
+ /**
151
+ * Generate deterministic payment wallet using HDNode derivation
152
+ * @param subscriptionId - Subscription ID to derive wallet for
153
+ */
154
+ getDeterministicPaymentWallet(subscriptionId: bigint): Promise<string>;
155
+ /**
156
+ * Get or generate mnemonic for HD wallet derivation
157
+ */
158
+ private getMnemonic;
159
+ /**
160
+ * Sign EIP-712 typed data for delegated subscription creation
161
+ */
162
+ signTypedData(domain: ethers.TypedDataDomain, types: Record<string, ethers.TypedDataField[]>, value: Record<string, any>): Promise<string>;
163
+ /**
164
+ * Get current ETH balance
165
+ */
166
+ getBalance(): Promise<bigint>;
167
+ /**
168
+ * Get ERC20 token balance
169
+ */
170
+ getTokenBalance(tokenAddress: string): Promise<bigint>;
171
+ /**
172
+ * Load wallet from keystore file
173
+ */
174
+ static fromKeystore(keystorePath: string, password: string, provider: ethers.Provider): Promise<WalletManager>;
175
+ /**
176
+ * Save wallet to encrypted keystore file
177
+ */
178
+ toKeystore(password: string, outputPath: string): Promise<void>;
179
+ /**
180
+ * Create a new payment wallet using WalletFactory contract
181
+ * @param walletFactoryAddress - Address of the WalletFactory contract
182
+ * @param initialOwner - Address that will own the new wallet (typically subscription owner)
183
+ * @param subscriptionId - Optional subscription ID to associate with this wallet
184
+ * @returns Transaction receipt and new wallet address
185
+ */
186
+ createPaymentWallet(walletFactoryAddress: string, initialOwner: string, subscriptionId?: string): Promise<{
187
+ walletAddress: string;
188
+ txHash: string;
189
+ }>;
190
+ /**
191
+ * Create a new EOA payment wallet and save to keystore
192
+ * This creates a regular externally-owned account (EOA) wallet
193
+ * @param subscriptionId - Optional subscription ID to associate with this wallet
194
+ * @returns Wallet address and private key (for immediate use, then should be discarded)
195
+ */
196
+ createEOAPaymentWallet(subscriptionId?: string): Promise<{
197
+ walletAddress: string;
198
+ privateKey: string;
199
+ }>;
200
+ /**
201
+ * Get a payment wallet from keystore
202
+ */
203
+ getPaymentWallet(walletAddress: string): Promise<ethers.Wallet>;
204
+ /**
205
+ * List all payment wallets in keystore
206
+ */
207
+ listPaymentWallets(): Array<{
208
+ address: string;
209
+ subscriptionId?: string;
210
+ }>;
211
+ /**
212
+ * Check if a wallet address was created by WalletFactory
213
+ * @param walletFactoryAddress - Address of the WalletFactory contract
214
+ * @param walletAddress - Wallet address to validate
215
+ * @returns True if wallet was created by this factory
216
+ */
217
+ isValidWallet(walletFactoryAddress: string, walletAddress: string): Promise<boolean>;
218
+ /**
219
+ * Fund a payment wallet with ETH
220
+ * @param walletAddress - Address of the wallet to fund
221
+ * @param amount - Amount in ETH as string (e.g., "0.1")
222
+ */
223
+ fundWallet(walletAddress: string, amount: string): Promise<string>;
224
+ /**
225
+ * Get balance of a payment wallet
226
+ * @param walletAddress - Address of the wallet to check
227
+ */
228
+ getWalletBalance(walletAddress: string): Promise<bigint>;
229
+ }
230
+
231
+ export { type KeystoreInfo, KeystoreManager, type NoosphereKeystore, type PaymentWalletInfo, WalletManager };
@@ -0,0 +1,231 @@
1
+ import { ethers } from 'ethers';
2
+
3
+ /**
4
+ * Types for Noosphere crypto package
5
+ */
6
+ /**
7
+ * Keystore format for storing EOA and payment wallets
8
+ */
9
+ interface NoosphereKeystore {
10
+ version: string;
11
+ eoa: {
12
+ address: string;
13
+ keystore: string;
14
+ };
15
+ paymentWallets: {
16
+ [walletAddress: string]: {
17
+ address: string;
18
+ privateKey: string;
19
+ subscriptionId?: string;
20
+ createdAt: string;
21
+ metadata?: Record<string, any>;
22
+ };
23
+ };
24
+ createdAt: string;
25
+ updatedAt: string;
26
+ }
27
+ /**
28
+ * Payment wallet information
29
+ */
30
+ interface PaymentWalletInfo {
31
+ address: string;
32
+ subscriptionId?: string;
33
+ createdAt: string;
34
+ metadata?: Record<string, any>;
35
+ }
36
+ /**
37
+ * Keystore information (without decrypting)
38
+ */
39
+ interface KeystoreInfo {
40
+ version: string;
41
+ eoaAddress: string;
42
+ paymentWalletCount: number;
43
+ createdAt: string;
44
+ updatedAt: string;
45
+ }
46
+
47
+ /**
48
+ * KeystoreManager manages EOA and payment wallets in a single encrypted keystore file
49
+ * This follows the Hub's keystore structure
50
+ */
51
+ declare class KeystoreManager {
52
+ private keystorePath;
53
+ private password;
54
+ private keystore;
55
+ constructor(keystorePath: string, password: string);
56
+ /**
57
+ * Initialize a new keystore with an EOA
58
+ * @param privateKey - Private key of the EOA (agent's main wallet)
59
+ * @param provider - Ethereum provider
60
+ */
61
+ static initialize(keystorePath: string, password: string, privateKey: string, provider: ethers.Provider): Promise<KeystoreManager>;
62
+ /**
63
+ * Load existing keystore
64
+ */
65
+ load(): Promise<void>;
66
+ /**
67
+ * Get the EOA wallet
68
+ */
69
+ getEOA(provider: ethers.Provider): Promise<ethers.Wallet>;
70
+ /**
71
+ * Get EOA address without decrypting
72
+ */
73
+ getEOAAddress(): string;
74
+ /**
75
+ * Add a payment wallet to the keystore
76
+ * @param walletAddress - Address of the payment wallet
77
+ * @param privateKey - Private key of the payment wallet
78
+ * @param subscriptionId - Optional subscription ID this wallet is for
79
+ */
80
+ addPaymentWallet(walletAddress: string, privateKey: string, subscriptionId?: string, metadata?: Record<string, any>): Promise<void>;
81
+ /**
82
+ * Get a payment wallet
83
+ */
84
+ getPaymentWallet(walletAddress: string, provider: ethers.Provider): Promise<ethers.Wallet>;
85
+ /**
86
+ * List all payment wallet addresses
87
+ */
88
+ listPaymentWallets(): PaymentWalletInfo[];
89
+ /**
90
+ * Remove a payment wallet
91
+ */
92
+ removePaymentWallet(walletAddress: string): Promise<void>;
93
+ /**
94
+ * Get keystore info without decrypting
95
+ */
96
+ getInfo(): KeystoreInfo;
97
+ /**
98
+ * Check if a payment wallet exists
99
+ */
100
+ hasPaymentWallet(walletAddress: string): boolean;
101
+ /**
102
+ * Update EOA (re-encrypt with new password or new private key)
103
+ */
104
+ updateEOA(newPrivateKey: string, provider: ethers.Provider): Promise<void>;
105
+ /**
106
+ * Save keystore to disk
107
+ */
108
+ private save;
109
+ /**
110
+ * Encrypt data using password
111
+ * Uses a simple base64 encoding for now - in production, use proper encryption
112
+ */
113
+ private encryptData;
114
+ /**
115
+ * Decrypt data using password
116
+ */
117
+ private decryptData;
118
+ /**
119
+ * Export keystore for backup
120
+ */
121
+ exportKeystore(): Promise<string>;
122
+ /**
123
+ * Import keystore from backup
124
+ */
125
+ static importKeystore(keystorePath: string, password: string, keystoreJson: string): Promise<KeystoreManager>;
126
+ /**
127
+ * Change password (re-encrypt all wallets)
128
+ */
129
+ changePassword(oldPassword: string, newPassword: string, provider: ethers.Provider): Promise<void>;
130
+ }
131
+
132
+ declare class WalletManager {
133
+ private wallet;
134
+ private provider;
135
+ private keystoreManager?;
136
+ constructor(privateKey: string, provider: ethers.Provider, keystoreManager?: KeystoreManager);
137
+ /**
138
+ * Initialize WalletManager from KeystoreManager
139
+ * This is the recommended way to create a WalletManager for production use
140
+ */
141
+ static fromKeystoreManager(keystoreManager: KeystoreManager, provider: ethers.Provider): Promise<WalletManager>;
142
+ /**
143
+ * Get the agent's wallet address
144
+ */
145
+ getAddress(): string;
146
+ /**
147
+ * Get the wallet instance
148
+ */
149
+ getWallet(): ethers.Wallet;
150
+ /**
151
+ * Generate deterministic payment wallet using HDNode derivation
152
+ * @param subscriptionId - Subscription ID to derive wallet for
153
+ */
154
+ getDeterministicPaymentWallet(subscriptionId: bigint): Promise<string>;
155
+ /**
156
+ * Get or generate mnemonic for HD wallet derivation
157
+ */
158
+ private getMnemonic;
159
+ /**
160
+ * Sign EIP-712 typed data for delegated subscription creation
161
+ */
162
+ signTypedData(domain: ethers.TypedDataDomain, types: Record<string, ethers.TypedDataField[]>, value: Record<string, any>): Promise<string>;
163
+ /**
164
+ * Get current ETH balance
165
+ */
166
+ getBalance(): Promise<bigint>;
167
+ /**
168
+ * Get ERC20 token balance
169
+ */
170
+ getTokenBalance(tokenAddress: string): Promise<bigint>;
171
+ /**
172
+ * Load wallet from keystore file
173
+ */
174
+ static fromKeystore(keystorePath: string, password: string, provider: ethers.Provider): Promise<WalletManager>;
175
+ /**
176
+ * Save wallet to encrypted keystore file
177
+ */
178
+ toKeystore(password: string, outputPath: string): Promise<void>;
179
+ /**
180
+ * Create a new payment wallet using WalletFactory contract
181
+ * @param walletFactoryAddress - Address of the WalletFactory contract
182
+ * @param initialOwner - Address that will own the new wallet (typically subscription owner)
183
+ * @param subscriptionId - Optional subscription ID to associate with this wallet
184
+ * @returns Transaction receipt and new wallet address
185
+ */
186
+ createPaymentWallet(walletFactoryAddress: string, initialOwner: string, subscriptionId?: string): Promise<{
187
+ walletAddress: string;
188
+ txHash: string;
189
+ }>;
190
+ /**
191
+ * Create a new EOA payment wallet and save to keystore
192
+ * This creates a regular externally-owned account (EOA) wallet
193
+ * @param subscriptionId - Optional subscription ID to associate with this wallet
194
+ * @returns Wallet address and private key (for immediate use, then should be discarded)
195
+ */
196
+ createEOAPaymentWallet(subscriptionId?: string): Promise<{
197
+ walletAddress: string;
198
+ privateKey: string;
199
+ }>;
200
+ /**
201
+ * Get a payment wallet from keystore
202
+ */
203
+ getPaymentWallet(walletAddress: string): Promise<ethers.Wallet>;
204
+ /**
205
+ * List all payment wallets in keystore
206
+ */
207
+ listPaymentWallets(): Array<{
208
+ address: string;
209
+ subscriptionId?: string;
210
+ }>;
211
+ /**
212
+ * Check if a wallet address was created by WalletFactory
213
+ * @param walletFactoryAddress - Address of the WalletFactory contract
214
+ * @param walletAddress - Wallet address to validate
215
+ * @returns True if wallet was created by this factory
216
+ */
217
+ isValidWallet(walletFactoryAddress: string, walletAddress: string): Promise<boolean>;
218
+ /**
219
+ * Fund a payment wallet with ETH
220
+ * @param walletAddress - Address of the wallet to fund
221
+ * @param amount - Amount in ETH as string (e.g., "0.1")
222
+ */
223
+ fundWallet(walletAddress: string, amount: string): Promise<string>;
224
+ /**
225
+ * Get balance of a payment wallet
226
+ * @param walletAddress - Address of the wallet to check
227
+ */
228
+ getWalletBalance(walletAddress: string): Promise<bigint>;
229
+ }
230
+
231
+ export { type KeystoreInfo, KeystoreManager, type NoosphereKeystore, type PaymentWalletInfo, WalletManager };