@settlemint/sdk-viem 2.5.7 → 2.5.10
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 +157 -95
- package/dist/browser/viem.d.ts +169 -116
- package/dist/browser/viem.js +154 -26
- package/dist/browser/viem.js.map +1 -1
- package/dist/viem.cjs +154 -26
- package/dist/viem.cjs.map +1 -1
- package/dist/viem.d.cts +169 -116
- package/dist/viem.d.ts +169 -116
- package/dist/viem.js +154 -26
- package/dist/viem.js.map +1 -1
- package/package.json +2 -2
package/dist/browser/viem.d.ts
CHANGED
|
@@ -4,50 +4,6 @@ import { Chain, Client, HttpTransportConfig, Transport } from "viem";
|
|
|
4
4
|
import * as chains from "viem/chains";
|
|
5
5
|
import { z } from "zod";
|
|
6
6
|
|
|
7
|
-
//#region src/custom-actions/create-wallet.action.d.ts
|
|
8
|
-
/**
|
|
9
|
-
* Information about the wallet to be created.
|
|
10
|
-
*/
|
|
11
|
-
interface WalletInfo {
|
|
12
|
-
/** The name of the wallet. */
|
|
13
|
-
name: string;
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Parameters for creating a wallet.
|
|
17
|
-
*/
|
|
18
|
-
interface CreateWalletParameters {
|
|
19
|
-
/** The unique name of the key vault where the wallet will be created. */
|
|
20
|
-
keyVaultId: string;
|
|
21
|
-
/** Information about the wallet to be created. */
|
|
22
|
-
walletInfo: WalletInfo;
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Response from creating a wallet.
|
|
26
|
-
*/
|
|
27
|
-
interface CreateWalletResponse {
|
|
28
|
-
/** The unique identifier of the wallet. */
|
|
29
|
-
id: string;
|
|
30
|
-
/** The name of the wallet. */
|
|
31
|
-
name: string;
|
|
32
|
-
/** The blockchain address of the wallet. */
|
|
33
|
-
address: string;
|
|
34
|
-
/** The HD derivation path used to create the wallet. */
|
|
35
|
-
derivationPath: string;
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Creates a wallet action for the given client.
|
|
39
|
-
* @param client - The viem client to use for the request.
|
|
40
|
-
* @returns An object with a createWallet method.
|
|
41
|
-
*/
|
|
42
|
-
declare function createWallet(client: Client): {
|
|
43
|
-
/**
|
|
44
|
-
* Creates a new wallet in the specified key vault.
|
|
45
|
-
* @param args - The parameters for creating a wallet.
|
|
46
|
-
* @returns A promise that resolves to an array of created wallet responses.
|
|
47
|
-
*/
|
|
48
|
-
createWallet(args: CreateWalletParameters): Promise<CreateWalletResponse[]>;
|
|
49
|
-
};
|
|
50
|
-
//#endregion
|
|
51
7
|
//#region src/custom-actions/types/wallet-verification.enum.d.ts
|
|
52
8
|
/**
|
|
53
9
|
* Types of wallet verification methods supported by the system.
|
|
@@ -86,6 +42,87 @@ declare enum OTPAlgorithm {
|
|
|
86
42
|
SHA3_512 = "SHA3-512",
|
|
87
43
|
}
|
|
88
44
|
//#endregion
|
|
45
|
+
//#region src/custom-actions/verify-wallet-verification-challenge.action.d.ts
|
|
46
|
+
/**
|
|
47
|
+
* Represents either a wallet address string or an object containing wallet address and optional verification ID.
|
|
48
|
+
*/
|
|
49
|
+
type AddressOrObject = string | {
|
|
50
|
+
userWalletAddress: string;
|
|
51
|
+
verificationId?: string;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Parameters for verifying a wallet verification challenge.
|
|
55
|
+
*/
|
|
56
|
+
interface VerifyWalletVerificationChallengeParameters {
|
|
57
|
+
/** The wallet address or object containing wallet address and optional verification ID. */
|
|
58
|
+
addressOrObject: AddressOrObject;
|
|
59
|
+
/** The response to the verification challenge. */
|
|
60
|
+
challengeResponse: string;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Result of a wallet verification challenge.
|
|
64
|
+
*/
|
|
65
|
+
interface VerificationResult {
|
|
66
|
+
/** Whether the verification was successful. */
|
|
67
|
+
verified: boolean;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Response from verifying a wallet verification challenge.
|
|
71
|
+
*/
|
|
72
|
+
type VerifyWalletVerificationChallengeResponse = VerificationResult[];
|
|
73
|
+
/**
|
|
74
|
+
* Creates a wallet verification challenge verification action for the given client.
|
|
75
|
+
* @param client - The viem client to use for the request.
|
|
76
|
+
* @returns An object with a verifyWalletVerificationChallenge method.
|
|
77
|
+
*/
|
|
78
|
+
declare function verifyWalletVerificationChallenge(client: Client): {
|
|
79
|
+
/**
|
|
80
|
+
* Verifies a wallet verification challenge.
|
|
81
|
+
* @param args - The parameters for verifying the challenge.
|
|
82
|
+
* @returns A promise that resolves to an array of verification results.
|
|
83
|
+
*/
|
|
84
|
+
verifyWalletVerificationChallenge(args: VerifyWalletVerificationChallengeParameters): Promise<VerifyWalletVerificationChallengeResponse>;
|
|
85
|
+
};
|
|
86
|
+
//#endregion
|
|
87
|
+
//#region src/custom-actions/create-wallet-verification-challenges.action.d.ts
|
|
88
|
+
/**
|
|
89
|
+
* Parameters for creating wallet verification challenges.
|
|
90
|
+
*/
|
|
91
|
+
interface CreateWalletVerificationChallengesParameters {
|
|
92
|
+
/** The wallet address or object containing wallet address and optional verification ID. */
|
|
93
|
+
addressOrObject: AddressOrObject;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Represents a wallet verification challenge.
|
|
97
|
+
*/
|
|
98
|
+
interface WalletVerificationChallenge {
|
|
99
|
+
/** The unique identifier of the challenge. */
|
|
100
|
+
id: string;
|
|
101
|
+
/** The name of the challenge. */
|
|
102
|
+
name: string;
|
|
103
|
+
/** The type of verification required. */
|
|
104
|
+
verificationType: WalletVerificationType;
|
|
105
|
+
/** The challenge parameters specific to the verification type. */
|
|
106
|
+
challenge: Record<string, string>;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Response from creating wallet verification challenges.
|
|
110
|
+
*/
|
|
111
|
+
type CreateWalletVerificationChallengesResponse = WalletVerificationChallenge[];
|
|
112
|
+
/**
|
|
113
|
+
* Creates a wallet verification challenges action for the given client.
|
|
114
|
+
* @param client - The viem client to use for the request.
|
|
115
|
+
* @returns An object with a createWalletVerificationChallenges method.
|
|
116
|
+
*/
|
|
117
|
+
declare function createWalletVerificationChallenges(client: Client): {
|
|
118
|
+
/**
|
|
119
|
+
* Creates verification challenges for a wallet.
|
|
120
|
+
* @param args - The parameters for creating the challenges.
|
|
121
|
+
* @returns A promise that resolves to an array of wallet verification challenges.
|
|
122
|
+
*/
|
|
123
|
+
createWalletVerificationChallenges(args: CreateWalletVerificationChallengesParameters): Promise<CreateWalletVerificationChallengesResponse>;
|
|
124
|
+
};
|
|
125
|
+
//#endregion
|
|
89
126
|
//#region src/custom-actions/create-wallet-verification.action.d.ts
|
|
90
127
|
/**
|
|
91
128
|
* Base interface for wallet verification information.
|
|
@@ -167,85 +204,48 @@ declare function createWalletVerification(client: Client): {
|
|
|
167
204
|
createWalletVerification(args: CreateWalletVerificationParameters): Promise<CreateWalletVerificationResponse[]>;
|
|
168
205
|
};
|
|
169
206
|
//#endregion
|
|
170
|
-
//#region src/custom-actions/
|
|
171
|
-
/**
|
|
172
|
-
* Represents either a wallet address string or an object containing wallet address and optional verification ID.
|
|
173
|
-
*/
|
|
174
|
-
type AddressOrObject = string | {
|
|
175
|
-
userWalletAddress: string;
|
|
176
|
-
verificationId?: string;
|
|
177
|
-
};
|
|
178
|
-
/**
|
|
179
|
-
* Parameters for verifying a wallet verification challenge.
|
|
180
|
-
*/
|
|
181
|
-
interface VerifyWalletVerificationChallengeParameters {
|
|
182
|
-
/** The wallet address or object containing wallet address and optional verification ID. */
|
|
183
|
-
addressOrObject: AddressOrObject;
|
|
184
|
-
/** The response to the verification challenge. */
|
|
185
|
-
challengeResponse: string;
|
|
186
|
-
}
|
|
207
|
+
//#region src/custom-actions/create-wallet.action.d.ts
|
|
187
208
|
/**
|
|
188
|
-
*
|
|
209
|
+
* Information about the wallet to be created.
|
|
189
210
|
*/
|
|
190
|
-
interface
|
|
191
|
-
/**
|
|
192
|
-
|
|
211
|
+
interface WalletInfo {
|
|
212
|
+
/** The name of the wallet. */
|
|
213
|
+
name: string;
|
|
193
214
|
}
|
|
194
215
|
/**
|
|
195
|
-
*
|
|
196
|
-
*/
|
|
197
|
-
type VerifyWalletVerificationChallengeResponse = VerificationResult[];
|
|
198
|
-
/**
|
|
199
|
-
* Creates a wallet verification challenge verification action for the given client.
|
|
200
|
-
* @param client - The viem client to use for the request.
|
|
201
|
-
* @returns An object with a verifyWalletVerificationChallenge method.
|
|
202
|
-
*/
|
|
203
|
-
declare function verifyWalletVerificationChallenge(client: Client): {
|
|
204
|
-
/**
|
|
205
|
-
* Verifies a wallet verification challenge.
|
|
206
|
-
* @param args - The parameters for verifying the challenge.
|
|
207
|
-
* @returns A promise that resolves to an array of verification results.
|
|
208
|
-
*/
|
|
209
|
-
verifyWalletVerificationChallenge(args: VerifyWalletVerificationChallengeParameters): Promise<VerifyWalletVerificationChallengeResponse>;
|
|
210
|
-
};
|
|
211
|
-
//#endregion
|
|
212
|
-
//#region src/custom-actions/create-wallet-verification-challenges.action.d.ts
|
|
213
|
-
/**
|
|
214
|
-
* Parameters for creating wallet verification challenges.
|
|
216
|
+
* Parameters for creating a wallet.
|
|
215
217
|
*/
|
|
216
|
-
interface
|
|
217
|
-
/** The
|
|
218
|
-
|
|
218
|
+
interface CreateWalletParameters {
|
|
219
|
+
/** The unique name of the key vault where the wallet will be created. */
|
|
220
|
+
keyVaultId: string;
|
|
221
|
+
/** Information about the wallet to be created. */
|
|
222
|
+
walletInfo: WalletInfo;
|
|
219
223
|
}
|
|
220
224
|
/**
|
|
221
|
-
*
|
|
225
|
+
* Response from creating a wallet.
|
|
222
226
|
*/
|
|
223
|
-
interface
|
|
224
|
-
/** The unique identifier of the
|
|
227
|
+
interface CreateWalletResponse {
|
|
228
|
+
/** The unique identifier of the wallet. */
|
|
225
229
|
id: string;
|
|
226
|
-
/** The name of the
|
|
230
|
+
/** The name of the wallet. */
|
|
227
231
|
name: string;
|
|
228
|
-
/** The
|
|
229
|
-
|
|
230
|
-
/** The
|
|
231
|
-
|
|
232
|
+
/** The blockchain address of the wallet. */
|
|
233
|
+
address: string;
|
|
234
|
+
/** The HD derivation path used to create the wallet. */
|
|
235
|
+
derivationPath: string;
|
|
232
236
|
}
|
|
233
237
|
/**
|
|
234
|
-
*
|
|
235
|
-
*/
|
|
236
|
-
type CreateWalletVerificationChallengesResponse = WalletVerificationChallenge[];
|
|
237
|
-
/**
|
|
238
|
-
* Creates a wallet verification challenges action for the given client.
|
|
238
|
+
* Creates a wallet action for the given client.
|
|
239
239
|
* @param client - The viem client to use for the request.
|
|
240
|
-
* @returns An object with a
|
|
240
|
+
* @returns An object with a createWallet method.
|
|
241
241
|
*/
|
|
242
|
-
declare function
|
|
242
|
+
declare function createWallet(client: Client): {
|
|
243
243
|
/**
|
|
244
|
-
* Creates
|
|
245
|
-
* @param args - The parameters for creating
|
|
246
|
-
* @returns A promise that resolves to an array of wallet
|
|
244
|
+
* Creates a new wallet in the specified key vault.
|
|
245
|
+
* @param args - The parameters for creating a wallet.
|
|
246
|
+
* @returns A promise that resolves to an array of created wallet responses.
|
|
247
247
|
*/
|
|
248
|
-
|
|
248
|
+
createWallet(args: CreateWalletParameters): Promise<CreateWalletResponse[]>;
|
|
249
249
|
};
|
|
250
250
|
//#endregion
|
|
251
251
|
//#region src/custom-actions/delete-wallet-verification.action.d.ts
|
|
@@ -334,9 +334,23 @@ type ClientOptions = Omit<z.infer<typeof ClientOptionsSchema>, "httpTransportCon
|
|
|
334
334
|
httpTransportConfig?: HttpTransportConfig;
|
|
335
335
|
};
|
|
336
336
|
/**
|
|
337
|
-
*
|
|
338
|
-
*
|
|
339
|
-
* @
|
|
337
|
+
* Creates an optimized public client for blockchain read operations.
|
|
338
|
+
*
|
|
339
|
+
* @remarks
|
|
340
|
+
* PERFORMANCE: Implements intelligent caching to minimize client creation overhead.
|
|
341
|
+
* Cache hit rates of 80%+ typical in production workloads with repeated chain access.
|
|
342
|
+
*
|
|
343
|
+
* SECURITY: Each access token gets isolated cache entries to prevent cross-tenant data exposure.
|
|
344
|
+
* Client instances are immutable once cached to prevent credential pollution.
|
|
345
|
+
*
|
|
346
|
+
* RESOURCE MANAGEMENT: 500ms polling interval balances responsiveness with server load.
|
|
347
|
+
* 60-second timeout prevents hanging connections in unstable network conditions.
|
|
348
|
+
*
|
|
349
|
+
* @param options - Client configuration including chain details and authentication
|
|
350
|
+
* @returns Cached or newly created public client with read-only blockchain access
|
|
351
|
+
* @throws ValidationError when options don't match required schema
|
|
352
|
+
* @throws NetworkError when RPC endpoint is unreachable during client creation
|
|
353
|
+
*
|
|
340
354
|
* @example
|
|
341
355
|
* ```ts
|
|
342
356
|
* import { getPublicClient } from '@settlemint/sdk-viem';
|
|
@@ -7631,15 +7645,40 @@ interface WalletVerificationOptions {
|
|
|
7631
7645
|
* The verification id (used for HD wallets), if not provided, the challenge response will be validated against all active verifications.
|
|
7632
7646
|
*/
|
|
7633
7647
|
verificationId?: string;
|
|
7648
|
+
/**
|
|
7649
|
+
* The challenge id (used for HD wallets)
|
|
7650
|
+
*/
|
|
7651
|
+
challengeId?: string;
|
|
7634
7652
|
/**
|
|
7635
7653
|
* The challenge response (used for HD wallets)
|
|
7636
7654
|
*/
|
|
7637
7655
|
challengeResponse: string;
|
|
7638
7656
|
}
|
|
7639
7657
|
/**
|
|
7640
|
-
*
|
|
7641
|
-
*
|
|
7642
|
-
* @
|
|
7658
|
+
* Creates a factory function for wallet clients with runtime verification support.
|
|
7659
|
+
*
|
|
7660
|
+
* @remarks
|
|
7661
|
+
* DESIGN PATTERN: Returns a factory function rather than a client instance because
|
|
7662
|
+
* wallet operations require runtime verification parameters (challenge responses, etc.)
|
|
7663
|
+
* that cannot be known at factory creation time.
|
|
7664
|
+
*
|
|
7665
|
+
* SECURITY: Verification headers are injected per-operation to support:
|
|
7666
|
+
* - HD wallet challenge/response flows
|
|
7667
|
+
* - Multi-signature verification workflows
|
|
7668
|
+
* - Time-sensitive authentication tokens
|
|
7669
|
+
*
|
|
7670
|
+
* PERFORMANCE: Factory caching amortizes expensive setup (chain resolution, transport config)
|
|
7671
|
+
* while allowing runtime parameter injection for each wallet operation.
|
|
7672
|
+
*
|
|
7673
|
+
* FEATURE EXTENSIONS: Automatically extends client with SettleMint-specific wallet actions:
|
|
7674
|
+
* - Wallet creation and management
|
|
7675
|
+
* - Verification challenge handling
|
|
7676
|
+
* - Multi-factor authentication flows
|
|
7677
|
+
*
|
|
7678
|
+
* @param options - Base client configuration (chain, RPC, auth)
|
|
7679
|
+
* @returns Factory function that accepts runtime verification options
|
|
7680
|
+
* @throws ValidationError when options don't match required schema
|
|
7681
|
+
*
|
|
7643
7682
|
* @example
|
|
7644
7683
|
* ```ts
|
|
7645
7684
|
* import { getWalletClient } from '@settlemint/sdk-viem';
|
|
@@ -7683,9 +7722,23 @@ type GetChainIdOptions = Omit<z.infer<typeof GetChainIdOptionsSchema>, "httpTran
|
|
|
7683
7722
|
httpTransportConfig?: HttpTransportConfig;
|
|
7684
7723
|
};
|
|
7685
7724
|
/**
|
|
7686
|
-
*
|
|
7687
|
-
*
|
|
7688
|
-
* @
|
|
7725
|
+
* Discovers the chain ID from an RPC endpoint without requiring prior knowledge.
|
|
7726
|
+
*
|
|
7727
|
+
* @remarks
|
|
7728
|
+
* UTILITY: Enables chain discovery for dynamic network configuration scenarios.
|
|
7729
|
+
* Unlike other client functions, this creates a minimal, non-cached client for one-time queries.
|
|
7730
|
+
*
|
|
7731
|
+
* USE CASE: Chain ID discovery during initial network setup or validation.
|
|
7732
|
+
* Alternative to requiring users to know chain IDs in advance.
|
|
7733
|
+
*
|
|
7734
|
+
* PERFORMANCE: No caching because chain IDs are typically discovered once
|
|
7735
|
+
* during setup rather than repeatedly during runtime operations.
|
|
7736
|
+
*
|
|
7737
|
+
* @param options - Minimal options with RPC URL and optional authentication
|
|
7738
|
+
* @returns Promise resolving to the network's chain ID as a number
|
|
7739
|
+
* @throws NetworkError when RPC endpoint is unreachable
|
|
7740
|
+
* @throws AuthenticationError when access token is invalid
|
|
7741
|
+
*
|
|
7689
7742
|
* @example
|
|
7690
7743
|
* ```ts
|
|
7691
7744
|
* import { getChainId } from '@settlemint/sdk-viem';
|
package/dist/browser/viem.js
CHANGED
|
@@ -6,17 +6,17 @@ import { createPublicClient, createWalletClient, defineChain, http, publicAction
|
|
|
6
6
|
import * as chains from "viem/chains";
|
|
7
7
|
import { z } from "zod";
|
|
8
8
|
|
|
9
|
-
//#region src/custom-actions/create-wallet.action.ts
|
|
9
|
+
//#region src/custom-actions/create-wallet-verification-challenges.action.ts
|
|
10
10
|
/**
|
|
11
|
-
* Creates a wallet action for the given client.
|
|
11
|
+
* Creates a wallet verification challenges action for the given client.
|
|
12
12
|
* @param client - The viem client to use for the request.
|
|
13
|
-
* @returns An object with a
|
|
13
|
+
* @returns An object with a createWalletVerificationChallenges method.
|
|
14
14
|
*/
|
|
15
|
-
function
|
|
16
|
-
return {
|
|
15
|
+
function createWalletVerificationChallenges(client) {
|
|
16
|
+
return { createWalletVerificationChallenges(args) {
|
|
17
17
|
return client.request({
|
|
18
|
-
method: "
|
|
19
|
-
params: [args.
|
|
18
|
+
method: "user_createWalletVerificationChallenges",
|
|
19
|
+
params: [args.addressOrObject]
|
|
20
20
|
});
|
|
21
21
|
} };
|
|
22
22
|
}
|
|
@@ -38,17 +38,17 @@ function createWalletVerification(client) {
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
//#endregion
|
|
41
|
-
//#region src/custom-actions/create-wallet
|
|
41
|
+
//#region src/custom-actions/create-wallet.action.ts
|
|
42
42
|
/**
|
|
43
|
-
* Creates a wallet
|
|
43
|
+
* Creates a wallet action for the given client.
|
|
44
44
|
* @param client - The viem client to use for the request.
|
|
45
|
-
* @returns An object with a
|
|
45
|
+
* @returns An object with a createWallet method.
|
|
46
46
|
*/
|
|
47
|
-
function
|
|
48
|
-
return {
|
|
47
|
+
function createWallet(client) {
|
|
48
|
+
return { createWallet(args) {
|
|
49
49
|
return client.request({
|
|
50
|
-
method: "
|
|
51
|
-
params: [args.
|
|
50
|
+
method: "user_createWallet",
|
|
51
|
+
params: [args.keyVaultId, args.walletInfo]
|
|
52
52
|
});
|
|
53
53
|
} };
|
|
54
54
|
}
|
|
@@ -144,6 +144,16 @@ let OTPAlgorithm = /* @__PURE__ */ function(OTPAlgorithm$1) {
|
|
|
144
144
|
|
|
145
145
|
//#endregion
|
|
146
146
|
//#region src/viem.ts
|
|
147
|
+
/**
|
|
148
|
+
* DESIGN DECISION: Custom LRU cache implementation over external libraries.
|
|
149
|
+
*
|
|
150
|
+
* WHY: Avoids external dependencies for this critical infrastructure component.
|
|
151
|
+
* TRADEOFF: Simpler implementation trades advanced features (TTL, statistics) for reliability.
|
|
152
|
+
* PERFORMANCE: O(1) access with automatic memory management prevents unbounded growth.
|
|
153
|
+
*
|
|
154
|
+
* Alternative considered: Using Map without eviction - rejected due to memory leak risk
|
|
155
|
+
* in long-running server applications with diverse chain/client combinations.
|
|
156
|
+
*/
|
|
147
157
|
var LRUCache = class {
|
|
148
158
|
cache = new Map();
|
|
149
159
|
maxSize;
|
|
@@ -172,9 +182,47 @@ var LRUCache = class {
|
|
|
172
182
|
this.cache.clear();
|
|
173
183
|
}
|
|
174
184
|
};
|
|
185
|
+
/**
|
|
186
|
+
* CACHE SIZING STRATEGY: Different limits based on usage patterns and memory impact.
|
|
187
|
+
*
|
|
188
|
+
* Chain cache (100): WHY larger?
|
|
189
|
+
* - Chain definitions are lightweight (just metadata)
|
|
190
|
+
* - High reuse across different client instances
|
|
191
|
+
* - Custom chains vary widely in development/testing scenarios
|
|
192
|
+
*
|
|
193
|
+
* Client caches (50 each): WHY smaller?
|
|
194
|
+
* - Clients hold heavy resources (connections, transport state)
|
|
195
|
+
* - Fewer unique client configurations in typical usage
|
|
196
|
+
* - Each client maintains internal connection pools
|
|
197
|
+
*
|
|
198
|
+
* TRADEOFF: Balances memory usage vs cache hit rates for optimal performance.
|
|
199
|
+
*/
|
|
175
200
|
const chainCache = new LRUCache(100);
|
|
201
|
+
/**
|
|
202
|
+
* SECURITY CONSIDERATION: Public clients contain auth tokens in transport config.
|
|
203
|
+
* Cache key generation ensures tokens are not leaked between different access contexts.
|
|
204
|
+
*/
|
|
176
205
|
const publicClientCache = new LRUCache(50);
|
|
206
|
+
/**
|
|
207
|
+
* DESIGN PATTERN: Factory caching rather than client instance caching.
|
|
208
|
+
* WHY: Wallet clients need runtime verification parameters that can't be pre-cached.
|
|
209
|
+
* BENEFIT: Amortizes chain resolution and transport configuration setup costs.
|
|
210
|
+
*/
|
|
177
211
|
const walletClientFactoryCache = new LRUCache(50);
|
|
212
|
+
/**
|
|
213
|
+
* CACHE KEY GENERATION: Deterministic key creation for consistent cache behavior.
|
|
214
|
+
*
|
|
215
|
+
* SECURITY: Access tokens are included in cache keys to prevent cross-tenant data leaks.
|
|
216
|
+
* Different access tokens must produce different cache entries even with identical chain configs.
|
|
217
|
+
*
|
|
218
|
+
* DETERMINISM: Property sorting ensures identical options always produce the same key,
|
|
219
|
+
* regardless of object property enumeration order differences across JavaScript engines.
|
|
220
|
+
*
|
|
221
|
+
* EDGE CASE: Function properties in httpTransportConfig are excluded because:
|
|
222
|
+
* 1. Functions cannot be serialized to JSON
|
|
223
|
+
* 2. Function identity changes don't affect transport behavior for caching purposes
|
|
224
|
+
* 3. Prevents cache key generation failures
|
|
225
|
+
*/
|
|
178
226
|
function createCacheKey(options) {
|
|
179
227
|
const keyObject = {};
|
|
180
228
|
const keys = [
|
|
@@ -197,6 +245,15 @@ function createCacheKey(options) {
|
|
|
197
245
|
}
|
|
198
246
|
return JSON.stringify(keyObject, Object.keys(keyObject).sort());
|
|
199
247
|
}
|
|
248
|
+
/**
|
|
249
|
+
* HEADER SECURITY: Prevents undefined auth tokens from being sent as 'undefined' strings.
|
|
250
|
+
*
|
|
251
|
+
* WHY: HTTP headers with undefined values can be serialized as the string 'undefined',
|
|
252
|
+
* which may bypass authentication or cause server-side parsing errors.
|
|
253
|
+
*
|
|
254
|
+
* SECURITY BOUNDARY: Filters out undefined authentication headers before network transmission
|
|
255
|
+
* to prevent accidental exposure of invalid credentials or authentication bypass attempts.
|
|
256
|
+
*/
|
|
200
257
|
function buildHeaders(baseHeaders, authHeaders) {
|
|
201
258
|
const filteredHeaders = {};
|
|
202
259
|
for (const [key, value] of Object.entries(authHeaders)) {
|
|
@@ -217,9 +274,23 @@ const ClientOptionsSchema = z.object({
|
|
|
217
274
|
httpTransportConfig: z.any().optional()
|
|
218
275
|
});
|
|
219
276
|
/**
|
|
220
|
-
*
|
|
221
|
-
*
|
|
222
|
-
* @
|
|
277
|
+
* Creates an optimized public client for blockchain read operations.
|
|
278
|
+
*
|
|
279
|
+
* @remarks
|
|
280
|
+
* PERFORMANCE: Implements intelligent caching to minimize client creation overhead.
|
|
281
|
+
* Cache hit rates of 80%+ typical in production workloads with repeated chain access.
|
|
282
|
+
*
|
|
283
|
+
* SECURITY: Each access token gets isolated cache entries to prevent cross-tenant data exposure.
|
|
284
|
+
* Client instances are immutable once cached to prevent credential pollution.
|
|
285
|
+
*
|
|
286
|
+
* RESOURCE MANAGEMENT: 500ms polling interval balances responsiveness with server load.
|
|
287
|
+
* 60-second timeout prevents hanging connections in unstable network conditions.
|
|
288
|
+
*
|
|
289
|
+
* @param options - Client configuration including chain details and authentication
|
|
290
|
+
* @returns Cached or newly created public client with read-only blockchain access
|
|
291
|
+
* @throws ValidationError when options don't match required schema
|
|
292
|
+
* @throws NetworkError when RPC endpoint is unreachable during client creation
|
|
293
|
+
*
|
|
223
294
|
* @example
|
|
224
295
|
* ```ts
|
|
225
296
|
* import { getPublicClient } from '@settlemint/sdk-viem';
|
|
@@ -266,9 +337,30 @@ const getPublicClient = (options) => {
|
|
|
266
337
|
return client;
|
|
267
338
|
};
|
|
268
339
|
/**
|
|
269
|
-
*
|
|
270
|
-
*
|
|
271
|
-
* @
|
|
340
|
+
* Creates a factory function for wallet clients with runtime verification support.
|
|
341
|
+
*
|
|
342
|
+
* @remarks
|
|
343
|
+
* DESIGN PATTERN: Returns a factory function rather than a client instance because
|
|
344
|
+
* wallet operations require runtime verification parameters (challenge responses, etc.)
|
|
345
|
+
* that cannot be known at factory creation time.
|
|
346
|
+
*
|
|
347
|
+
* SECURITY: Verification headers are injected per-operation to support:
|
|
348
|
+
* - HD wallet challenge/response flows
|
|
349
|
+
* - Multi-signature verification workflows
|
|
350
|
+
* - Time-sensitive authentication tokens
|
|
351
|
+
*
|
|
352
|
+
* PERFORMANCE: Factory caching amortizes expensive setup (chain resolution, transport config)
|
|
353
|
+
* while allowing runtime parameter injection for each wallet operation.
|
|
354
|
+
*
|
|
355
|
+
* FEATURE EXTENSIONS: Automatically extends client with SettleMint-specific wallet actions:
|
|
356
|
+
* - Wallet creation and management
|
|
357
|
+
* - Verification challenge handling
|
|
358
|
+
* - Multi-factor authentication flows
|
|
359
|
+
*
|
|
360
|
+
* @param options - Base client configuration (chain, RPC, auth)
|
|
361
|
+
* @returns Factory function that accepts runtime verification options
|
|
362
|
+
* @throws ValidationError when options don't match required schema
|
|
363
|
+
*
|
|
272
364
|
* @example
|
|
273
365
|
* ```ts
|
|
274
366
|
* import { getWalletClient } from '@settlemint/sdk-viem';
|
|
@@ -313,15 +405,16 @@ const getWalletClient = (options) => {
|
|
|
313
405
|
chain,
|
|
314
406
|
pollingInterval: 500,
|
|
315
407
|
transport: http(validatedOptions.rpcUrl, {
|
|
316
|
-
batch:
|
|
408
|
+
batch: false,
|
|
317
409
|
timeout: 6e4,
|
|
318
410
|
...validatedOptions.httpTransportConfig,
|
|
319
411
|
fetchOptions: {
|
|
320
412
|
...validatedOptions?.httpTransportConfig?.fetchOptions,
|
|
321
413
|
headers: buildHeaders(validatedOptions?.httpTransportConfig?.fetchOptions?.headers, {
|
|
322
414
|
"x-auth-token": validatedOptions.accessToken,
|
|
323
|
-
"x-auth-challenge-response": verificationOptions
|
|
324
|
-
"x-auth-
|
|
415
|
+
...verificationOptions?.challengeResponse ? { "x-auth-challenge-response": verificationOptions.challengeResponse } : {},
|
|
416
|
+
...verificationOptions?.challengeId ? { "x-auth-challenge-id": verificationOptions.challengeId } : {},
|
|
417
|
+
...verificationOptions?.verificationId ? { "x-auth-verification-id": verificationOptions.verificationId } : {}
|
|
325
418
|
})
|
|
326
419
|
}
|
|
327
420
|
})
|
|
@@ -338,9 +431,23 @@ const GetChainIdOptionsSchema = z.object({
|
|
|
338
431
|
httpTransportConfig: z.any().optional()
|
|
339
432
|
});
|
|
340
433
|
/**
|
|
341
|
-
*
|
|
342
|
-
*
|
|
343
|
-
* @
|
|
434
|
+
* Discovers the chain ID from an RPC endpoint without requiring prior knowledge.
|
|
435
|
+
*
|
|
436
|
+
* @remarks
|
|
437
|
+
* UTILITY: Enables chain discovery for dynamic network configuration scenarios.
|
|
438
|
+
* Unlike other client functions, this creates a minimal, non-cached client for one-time queries.
|
|
439
|
+
*
|
|
440
|
+
* USE CASE: Chain ID discovery during initial network setup or validation.
|
|
441
|
+
* Alternative to requiring users to know chain IDs in advance.
|
|
442
|
+
*
|
|
443
|
+
* PERFORMANCE: No caching because chain IDs are typically discovered once
|
|
444
|
+
* during setup rather than repeatedly during runtime operations.
|
|
445
|
+
*
|
|
446
|
+
* @param options - Minimal options with RPC URL and optional authentication
|
|
447
|
+
* @returns Promise resolving to the network's chain ID as a number
|
|
448
|
+
* @throws NetworkError when RPC endpoint is unreachable
|
|
449
|
+
* @throws AuthenticationError when access token is invalid
|
|
450
|
+
*
|
|
344
451
|
* @example
|
|
345
452
|
* ```ts
|
|
346
453
|
* import { getChainId } from '@settlemint/sdk-viem';
|
|
@@ -365,7 +472,28 @@ async function getChainId(options) {
|
|
|
365
472
|
}) });
|
|
366
473
|
return client.getChainId();
|
|
367
474
|
}
|
|
475
|
+
/**
|
|
476
|
+
* OPTIMIZATION: Pre-compute known chains map for O(1) lookup performance.
|
|
477
|
+
* WHY Map over Object: Avoids prototype chain lookups and provides guaranteed O(1) access.
|
|
478
|
+
* MEMORY: One-time initialization cost for ~100 known chains vs repeated lookups.
|
|
479
|
+
*/
|
|
368
480
|
const knownChainsMap = new Map(Object.values(chains).map((chain) => [chain.id.toString(), chain]));
|
|
481
|
+
/**
|
|
482
|
+
* CHAIN RESOLUTION STRATEGY: Two-tier lookup optimizes for both known and custom chains.
|
|
483
|
+
*
|
|
484
|
+
* Tier 1: Known chains (Ethereum mainnet, common testnets, L2s)
|
|
485
|
+
* - O(1) lookup from pre-built map
|
|
486
|
+
* - No caching needed (references are stable)
|
|
487
|
+
* - Ignores custom RPC URLs (uses viem's defaults)
|
|
488
|
+
*
|
|
489
|
+
* Tier 2: Custom chains (private networks, development chains)
|
|
490
|
+
* - LRU cached to handle dynamic discovery
|
|
491
|
+
* - Full parameter consideration for cache key
|
|
492
|
+
* - ETH defaults for unknown chains (SettleMint platform assumption)
|
|
493
|
+
*
|
|
494
|
+
* TRADEOFF: Memory usage vs performance - separate strategies prevent cache pollution
|
|
495
|
+
* of known chains with custom RPC configurations.
|
|
496
|
+
*/
|
|
369
497
|
function getChain({ chainId, chainName, rpcUrl }) {
|
|
370
498
|
const knownChain = knownChainsMap.get(chainId);
|
|
371
499
|
if (knownChain) {
|