@4mica/sdk 1.1.1 → 1.2.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/README.md +70 -57
- package/dist/config.d.ts +19 -1
- package/dist/config.js +30 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/networks.d.ts +40 -0
- package/dist/networks.js +49 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -27,19 +27,19 @@ Node.js 18+ is required.
|
|
|
27
27
|
|
|
28
28
|
## Networks
|
|
29
29
|
|
|
30
|
-
| Shorthand
|
|
31
|
-
|
|
32
|
-
| `ethereum-sepolia` | `eip155:11155111` | `https://ethereum.sepolia.4mica.xyz/` |
|
|
33
|
-
| `base-sepolia`
|
|
30
|
+
| Shorthand | CAIP-2 | Core API URL |
|
|
31
|
+
| ------------------ | ----------------- | ----------------------------------------- |
|
|
32
|
+
| `ethereum-sepolia` | `eip155:11155111` | `https://ethereum.sepolia.api.4mica.xyz/` |
|
|
33
|
+
| `base-sepolia` | `eip155:84532` | `https://base.sepolia.api.4mica.xyz/` |
|
|
34
34
|
|
|
35
35
|
The default network is Ethereum Sepolia. Use `.network()` or the `4MICA_NETWORK` environment
|
|
36
36
|
variable to switch networks.
|
|
37
37
|
|
|
38
38
|
```ts
|
|
39
|
-
import { NETWORKS } from
|
|
39
|
+
import { NETWORKS } from '@4mica/sdk';
|
|
40
40
|
|
|
41
|
-
console.log(NETWORKS[
|
|
42
|
-
console.log(NETWORKS[
|
|
41
|
+
console.log(NETWORKS['base-sepolia'].caip2); // "eip155:84532"
|
|
42
|
+
console.log(NETWORKS['base-sepolia'].rpcUrl); // "https://base.sepolia.api.4mica.xyz/"
|
|
43
43
|
```
|
|
44
44
|
|
|
45
45
|
## Initialization and Configuration
|
|
@@ -63,12 +63,12 @@ The SDK requires a signing key and can use sensible defaults for the rest:
|
|
|
63
63
|
### 1) Using ConfigBuilder
|
|
64
64
|
|
|
65
65
|
```ts
|
|
66
|
-
import { Client, ConfigBuilder } from
|
|
66
|
+
import { Client, ConfigBuilder } from '@4mica/sdk';
|
|
67
67
|
|
|
68
68
|
async function main() {
|
|
69
69
|
const cfg = new ConfigBuilder()
|
|
70
|
-
.network(
|
|
71
|
-
.walletPrivateKey(
|
|
70
|
+
.network('base-sepolia') // or "ethereum-sepolia" (default)
|
|
71
|
+
.walletPrivateKey('0x...')
|
|
72
72
|
.build();
|
|
73
73
|
|
|
74
74
|
const client = await Client.new(cfg);
|
|
@@ -88,12 +88,12 @@ Set environment variables (example `.env`):
|
|
|
88
88
|
4MICA_WALLET_PRIVATE_KEY="0x..."
|
|
89
89
|
4MICA_NETWORK="base-sepolia" # shorthand or CAIP-2 id
|
|
90
90
|
# or override URL directly:
|
|
91
|
-
# 4MICA_RPC_URL="https://base.sepolia.4mica.xyz/"
|
|
91
|
+
# 4MICA_RPC_URL="https://base.sepolia.api.4mica.xyz/"
|
|
92
92
|
4MICA_ETHEREUM_HTTP_RPC_URL="http://localhost:8545"
|
|
93
93
|
4MICA_CONTRACT_ADDRESS="0x..."
|
|
94
94
|
4MICA_ADMIN_API_KEY="ak_..."
|
|
95
95
|
4MICA_BEARER_TOKEN="Bearer <access_token>"
|
|
96
|
-
4MICA_AUTH_URL="https://ethereum.sepolia.4mica.xyz/"
|
|
96
|
+
4MICA_AUTH_URL="https://ethereum.sepolia.api.4mica.xyz/"
|
|
97
97
|
4MICA_AUTH_REFRESH_MARGIN_SECS="60"
|
|
98
98
|
```
|
|
99
99
|
|
|
@@ -107,7 +107,7 @@ env 4MICA_WALLET_PRIVATE_KEY="0x..." 4MICA_NETWORK="base-sepolia" node app.js
|
|
|
107
107
|
Then in code:
|
|
108
108
|
|
|
109
109
|
```ts
|
|
110
|
-
import { Client, ConfigBuilder } from
|
|
110
|
+
import { Client, ConfigBuilder } from '@4mica/sdk';
|
|
111
111
|
|
|
112
112
|
const cfg = new ConfigBuilder().fromEnv().build();
|
|
113
113
|
const client = await Client.new(cfg);
|
|
@@ -120,8 +120,8 @@ If you want to integrate with a custom signer (hardware wallet, remote signer, e
|
|
|
120
120
|
SIWE auth.
|
|
121
121
|
|
|
122
122
|
```ts
|
|
123
|
-
import { Client, ConfigBuilder } from
|
|
124
|
-
import { privateKeyToAccount } from
|
|
123
|
+
import { Client, ConfigBuilder } from '@4mica/sdk';
|
|
124
|
+
import { privateKeyToAccount } from 'viem/accounts';
|
|
125
125
|
|
|
126
126
|
const signer = privateKeyToAccount(process.env.PAYER_KEY as `0x${string}`);
|
|
127
127
|
const cfg = new ConfigBuilder().signer(signer).build();
|
|
@@ -133,11 +133,11 @@ const client = await Client.new(cfg);
|
|
|
133
133
|
Enable automatic SIWE auth refresh, or pass a static bearer token:
|
|
134
134
|
|
|
135
135
|
```ts
|
|
136
|
-
import { Client, ConfigBuilder } from
|
|
136
|
+
import { Client, ConfigBuilder } from '@4mica/sdk';
|
|
137
137
|
|
|
138
138
|
const cfg = new ConfigBuilder()
|
|
139
|
-
.walletPrivateKey(
|
|
140
|
-
.rpcUrl(
|
|
139
|
+
.walletPrivateKey('0x...')
|
|
140
|
+
.rpcUrl('https://api.4mica.xyz/')
|
|
141
141
|
.enableAuth()
|
|
142
142
|
.build();
|
|
143
143
|
|
|
@@ -149,8 +149,8 @@ Or use a static token:
|
|
|
149
149
|
|
|
150
150
|
```ts
|
|
151
151
|
const cfg = new ConfigBuilder()
|
|
152
|
-
.walletPrivateKey(
|
|
153
|
-
.bearerToken(
|
|
152
|
+
.walletPrivateKey('0x...')
|
|
153
|
+
.bearerToken('Bearer <access_token>')
|
|
154
154
|
.build();
|
|
155
155
|
```
|
|
156
156
|
|
|
@@ -167,6 +167,7 @@ The SDK exposes three main entry points:
|
|
|
167
167
|
### End-to-end Example (Base Sepolia + x402 v2)
|
|
168
168
|
|
|
169
169
|
See `examples/base-sepolia-x402-facilitator-e2e.ts` for a full flow in the `examples` folder:
|
|
170
|
+
|
|
170
171
|
- deposit collateral
|
|
171
172
|
- create/get a tab via facilitator
|
|
172
173
|
- issue and verify V1 + V2 guarantees
|
|
@@ -189,6 +190,7 @@ X-PAYMENT header (and optional `/settle` call) that the facilitator will accept.
|
|
|
189
190
|
#### What the SDK expects from `paymentRequirements`
|
|
190
191
|
|
|
191
192
|
At minimum you need:
|
|
193
|
+
|
|
192
194
|
- `scheme` and `network` (scheme must include `4mica`, e.g. `4mica-credit`)
|
|
193
195
|
- `payTo` (recipient address), `asset`, and `maxAmountRequired` (v1) or `amount` (v2)
|
|
194
196
|
- `extra.tabEndpoint` for tab resolution
|
|
@@ -200,8 +202,8 @@ At minimum you need:
|
|
|
200
202
|
Version 1 returns payment requirements in the JSON response body:
|
|
201
203
|
|
|
202
204
|
```ts
|
|
203
|
-
import { Client, ConfigBuilder, X402Flow } from
|
|
204
|
-
import type { PaymentRequirementsV1 } from
|
|
205
|
+
import { Client, ConfigBuilder, X402Flow } from '@4mica/sdk';
|
|
206
|
+
import type { PaymentRequirementsV1 } from '@4mica/sdk';
|
|
205
207
|
|
|
206
208
|
type ResourceResponse = {
|
|
207
209
|
x402Version: number;
|
|
@@ -209,23 +211,23 @@ type ResourceResponse = {
|
|
|
209
211
|
error?: string;
|
|
210
212
|
};
|
|
211
213
|
|
|
212
|
-
const cfg = new ConfigBuilder().walletPrivateKey(
|
|
214
|
+
const cfg = new ConfigBuilder().walletPrivateKey('0x...').build();
|
|
213
215
|
const client = await Client.new(cfg);
|
|
214
216
|
const flow = X402Flow.fromClient(client);
|
|
215
217
|
|
|
216
218
|
// 1) GET the protected endpoint and parse JSON body
|
|
217
|
-
const res = await fetch(
|
|
219
|
+
const res = await fetch('https://resource-url/resource');
|
|
218
220
|
const body = (await res.json()) as ResourceResponse;
|
|
219
221
|
|
|
220
222
|
// 2) Select a payment option
|
|
221
223
|
const requirements = body.accepts[0];
|
|
222
224
|
|
|
223
225
|
// 3) Build the X-PAYMENT header with the SDK
|
|
224
|
-
const payment = await flow.signPayment(requirements,
|
|
226
|
+
const payment = await flow.signPayment(requirements, '0xUser');
|
|
225
227
|
|
|
226
228
|
// 4) Call the protected resource with the header
|
|
227
|
-
await fetch(
|
|
228
|
-
headers: {
|
|
229
|
+
await fetch('https://resource-url/resource', {
|
|
230
|
+
headers: { 'X-PAYMENT': payment.header },
|
|
229
231
|
});
|
|
230
232
|
|
|
231
233
|
await client.aclose();
|
|
@@ -236,31 +238,31 @@ await client.aclose();
|
|
|
236
238
|
Version 2 uses the `payment-required` header (base64-encoded) instead of a JSON response body:
|
|
237
239
|
|
|
238
240
|
```ts
|
|
239
|
-
import { Client, ConfigBuilder, X402Flow } from
|
|
240
|
-
import type { X402PaymentRequired, PaymentRequirementsV2 } from
|
|
241
|
+
import { Client, ConfigBuilder, X402Flow } from '@4mica/sdk';
|
|
242
|
+
import type { X402PaymentRequired, PaymentRequirementsV2 } from '@4mica/sdk';
|
|
241
243
|
|
|
242
|
-
const cfg = new ConfigBuilder().walletPrivateKey(
|
|
244
|
+
const cfg = new ConfigBuilder().walletPrivateKey('0x...').build();
|
|
243
245
|
const client = await Client.new(cfg);
|
|
244
246
|
const flow = X402Flow.fromClient(client);
|
|
245
247
|
|
|
246
248
|
// 1) GET the protected endpoint and extract payment-required header
|
|
247
|
-
const res = await fetch(
|
|
248
|
-
const header = res.headers.get(
|
|
249
|
-
if (!header) throw new Error(
|
|
249
|
+
const res = await fetch('https://resource-url/resource');
|
|
250
|
+
const header = res.headers.get('payment-required');
|
|
251
|
+
if (!header) throw new Error('Missing payment-required header');
|
|
250
252
|
|
|
251
253
|
// 2) Decode the header
|
|
252
|
-
const decoded = Buffer.from(header,
|
|
254
|
+
const decoded = Buffer.from(header, 'base64').toString('utf8');
|
|
253
255
|
const paymentRequired = JSON.parse(decoded) as X402PaymentRequired;
|
|
254
256
|
|
|
255
257
|
// 3) Select a payment option
|
|
256
258
|
const accepted = paymentRequired.accepts[0] as PaymentRequirementsV2;
|
|
257
259
|
|
|
258
260
|
// 4) Build the PAYMENT-SIGNATURE header with the SDK
|
|
259
|
-
const signed = await flow.signPaymentV2(paymentRequired, accepted,
|
|
261
|
+
const signed = await flow.signPaymentV2(paymentRequired, accepted, '0xUser');
|
|
260
262
|
|
|
261
263
|
// 5) Call the protected resource with the header
|
|
262
|
-
await fetch(
|
|
263
|
-
headers: {
|
|
264
|
+
await fetch('https://resource-url/resource', {
|
|
265
|
+
headers: { 'PAYMENT-SIGNATURE': signed.header },
|
|
264
266
|
});
|
|
265
267
|
|
|
266
268
|
await client.aclose();
|
|
@@ -272,8 +274,8 @@ If your resource server proxies to the facilitator, you can reuse the SDK to set
|
|
|
272
274
|
verifying:
|
|
273
275
|
|
|
274
276
|
```ts
|
|
275
|
-
import { Client, ConfigBuilder, X402Flow } from
|
|
276
|
-
import type { PaymentRequirementsV1, X402SignedPayment } from
|
|
277
|
+
import { Client, ConfigBuilder, X402Flow } from '@4mica/sdk';
|
|
278
|
+
import type { PaymentRequirementsV1, X402SignedPayment } from '@4mica/sdk';
|
|
277
279
|
|
|
278
280
|
async function settle(
|
|
279
281
|
facilitatorUrl: string,
|
|
@@ -286,13 +288,14 @@ async function settle(
|
|
|
286
288
|
const flow = X402Flow.fromClient(core);
|
|
287
289
|
|
|
288
290
|
const settled = await flow.settlePayment(payment, paymentRequirements, facilitatorUrl);
|
|
289
|
-
console.log(
|
|
291
|
+
console.log('settlement result:', settled.settlement);
|
|
290
292
|
|
|
291
293
|
await core.aclose();
|
|
292
294
|
}
|
|
293
295
|
```
|
|
294
296
|
|
|
295
297
|
Notes:
|
|
298
|
+
|
|
296
299
|
- `signPayment` and `signPaymentV2` always use EIP-712 signing and will error if the scheme is not 4mica.
|
|
297
300
|
- `UserClient.signPayment` supports `SigningScheme.EIP712` (default) and `SigningScheme.EIP191`.
|
|
298
301
|
- `settlePayment` only hits `/settle`; resource servers should still call `/verify` first when enforcing access.
|
|
@@ -351,11 +354,17 @@ import {
|
|
|
351
354
|
computeValidationSubjectHash,
|
|
352
355
|
computeValidationRequestHash,
|
|
353
356
|
SigningScheme,
|
|
354
|
-
} from
|
|
357
|
+
} from '@4mica/sdk';
|
|
355
358
|
|
|
356
359
|
// 1) Build base V1 claims first
|
|
357
360
|
const baseClaims = PaymentGuaranteeRequestClaims.new(
|
|
358
|
-
userAddress,
|
|
361
|
+
userAddress,
|
|
362
|
+
recipientAddress,
|
|
363
|
+
tabId,
|
|
364
|
+
amount,
|
|
365
|
+
timestamp,
|
|
366
|
+
erc20Token,
|
|
367
|
+
reqId
|
|
359
368
|
);
|
|
360
369
|
|
|
361
370
|
// 2) Compute canonical hashes
|
|
@@ -363,14 +372,14 @@ const validationSubjectHash = computeValidationSubjectHash(baseClaims);
|
|
|
363
372
|
|
|
364
373
|
const partialV2 = new PaymentGuaranteeRequestClaimsV2({
|
|
365
374
|
...baseClaims,
|
|
366
|
-
validationRegistryAddress:
|
|
367
|
-
validationRequestHash:
|
|
375
|
+
validationRegistryAddress: '0x...',
|
|
376
|
+
validationRequestHash: '0x' + '00'.repeat(32), // placeholder
|
|
368
377
|
validationChainId: 1,
|
|
369
|
-
validatorAddress:
|
|
378
|
+
validatorAddress: '0x...',
|
|
370
379
|
validatorAgentId: 1n,
|
|
371
|
-
minValidationScore: 80,
|
|
380
|
+
minValidationScore: 80, // 1–100
|
|
372
381
|
validationSubjectHash,
|
|
373
|
-
requiredValidationTag:
|
|
382
|
+
requiredValidationTag: 'my-tag',
|
|
374
383
|
});
|
|
375
384
|
|
|
376
385
|
const validationRequestHash = computeValidationRequestHash(partialV2);
|
|
@@ -387,21 +396,25 @@ All SDK errors extend `FourMicaError`. Import individual error classes to distin
|
|
|
387
396
|
|
|
388
397
|
```ts
|
|
389
398
|
import {
|
|
390
|
-
ConfigError,
|
|
391
|
-
RpcError,
|
|
392
|
-
SigningError,
|
|
393
|
-
ContractError,
|
|
394
|
-
VerificationError,
|
|
395
|
-
X402Error,
|
|
396
|
-
AuthError,
|
|
399
|
+
ConfigError, // invalid ConfigBuilder input
|
|
400
|
+
RpcError, // 4Mica core service error (has .status and .body)
|
|
401
|
+
SigningError, // signing scheme unsupported or address mismatch
|
|
402
|
+
ContractError, // on-chain call failed or unexpected result
|
|
403
|
+
VerificationError, // BLS certificate decode/domain mismatch
|
|
404
|
+
X402Error, // x402 flow error (bad scheme, tab resolution, settlement)
|
|
405
|
+
AuthError, // base class for all auth errors
|
|
397
406
|
AuthMissingConfigError, // auth not configured when login() is called
|
|
398
|
-
} from
|
|
407
|
+
} from '@4mica/sdk';
|
|
399
408
|
|
|
400
409
|
try {
|
|
401
410
|
await client.recipient.remunerate(cert);
|
|
402
411
|
} catch (err) {
|
|
403
|
-
if (err instanceof VerificationError) {
|
|
404
|
-
|
|
412
|
+
if (err instanceof VerificationError) {
|
|
413
|
+
/* bad cert */
|
|
414
|
+
}
|
|
415
|
+
if (err instanceof ContractError) {
|
|
416
|
+
/* on-chain failure */
|
|
417
|
+
}
|
|
405
418
|
}
|
|
406
419
|
```
|
|
407
420
|
|
package/dist/config.d.ts
CHANGED
|
@@ -41,8 +41,25 @@ export declare class ConfigBuilder {
|
|
|
41
41
|
private _authEnabled;
|
|
42
42
|
private _authUrl?;
|
|
43
43
|
private _authRefreshMarginSecs?;
|
|
44
|
-
/**
|
|
44
|
+
/** Set the 4Mica core RPC URL directly. Use {@link network} to select a hosted network by name instead. Defaults to `https://ethereum.sepolia.api.4mica.xyz/`. */
|
|
45
45
|
rpcUrl(value: string): ConfigBuilder;
|
|
46
|
+
/**
|
|
47
|
+
* Select a hosted 4Mica network by shorthand or CAIP-2 identifier.
|
|
48
|
+
* Resolves to the corresponding core API URL.
|
|
49
|
+
* Mutually exclusive with {@link rpcUrl} — last call wins.
|
|
50
|
+
*
|
|
51
|
+
* Supported values: `"base-sepolia"` / `"eip155:84532"`,
|
|
52
|
+
* `"ethereum-sepolia"` / `"eip155:11155111"`.
|
|
53
|
+
*
|
|
54
|
+
* @throws {@link ConfigError} if the network is not recognised.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```ts
|
|
58
|
+
* new ConfigBuilder().network("base-sepolia").walletPrivateKey("0x...").build();
|
|
59
|
+
* new ConfigBuilder().network("eip155:84532").walletPrivateKey("0x...").build();
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
network(value: string): ConfigBuilder;
|
|
46
63
|
/** Set the wallet private key (hex string). Mutually exclusive with {@link signer}. */
|
|
47
64
|
walletPrivateKey(value: string): ConfigBuilder;
|
|
48
65
|
/** Set a pre-built viem `Account` directly. Mutually exclusive with {@link walletPrivateKey}. */
|
|
@@ -65,6 +82,7 @@ export declare class ConfigBuilder {
|
|
|
65
82
|
* Load configuration from environment variables.
|
|
66
83
|
*
|
|
67
84
|
* Recognised variables:
|
|
85
|
+
* - `4MICA_NETWORK` — shorthand or CAIP-2 id (e.g. `base-sepolia`); takes precedence over `4MICA_RPC_URL`
|
|
68
86
|
* - `4MICA_RPC_URL`
|
|
69
87
|
* - `4MICA_WALLET_PRIVATE_KEY`
|
|
70
88
|
* - `4MICA_ETHEREUM_HTTP_RPC_URL`
|
package/dist/config.js
CHANGED
|
@@ -4,6 +4,7 @@ exports.ConfigBuilder = void 0;
|
|
|
4
4
|
const accounts_1 = require("viem/accounts");
|
|
5
5
|
const errors_1 = require("./errors");
|
|
6
6
|
const utils_1 = require("./utils");
|
|
7
|
+
const networks_1 = require("./networks");
|
|
7
8
|
/**
|
|
8
9
|
* Fluent builder for {@link Config}.
|
|
9
10
|
*
|
|
@@ -17,7 +18,7 @@ const utils_1 = require("./utils");
|
|
|
17
18
|
* All fields can also be supplied from environment variables via {@link fromEnv}.
|
|
18
19
|
*/
|
|
19
20
|
class ConfigBuilder {
|
|
20
|
-
_rpcUrl = 'https://api.4mica.xyz/';
|
|
21
|
+
_rpcUrl = 'https://ethereum.sepolia.api.4mica.xyz/';
|
|
21
22
|
_walletPrivateKey;
|
|
22
23
|
_signer;
|
|
23
24
|
_ethereumHttpRpcUrl;
|
|
@@ -27,11 +28,35 @@ class ConfigBuilder {
|
|
|
27
28
|
_authEnabled = true;
|
|
28
29
|
_authUrl;
|
|
29
30
|
_authRefreshMarginSecs;
|
|
30
|
-
/**
|
|
31
|
+
/** Set the 4Mica core RPC URL directly. Use {@link network} to select a hosted network by name instead. Defaults to `https://ethereum.sepolia.api.4mica.xyz/`. */
|
|
31
32
|
rpcUrl(value) {
|
|
32
33
|
this._rpcUrl = value;
|
|
33
34
|
return this;
|
|
34
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Select a hosted 4Mica network by shorthand or CAIP-2 identifier.
|
|
38
|
+
* Resolves to the corresponding core API URL.
|
|
39
|
+
* Mutually exclusive with {@link rpcUrl} — last call wins.
|
|
40
|
+
*
|
|
41
|
+
* Supported values: `"base-sepolia"` / `"eip155:84532"`,
|
|
42
|
+
* `"ethereum-sepolia"` / `"eip155:11155111"`.
|
|
43
|
+
*
|
|
44
|
+
* @throws {@link ConfigError} if the network is not recognised.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* new ConfigBuilder().network("base-sepolia").walletPrivateKey("0x...").build();
|
|
49
|
+
* new ConfigBuilder().network("eip155:84532").walletPrivateKey("0x...").build();
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
network(value) {
|
|
53
|
+
const url = (0, networks_1.resolveNetworkRpcUrl)(value);
|
|
54
|
+
if (!url) {
|
|
55
|
+
throw new errors_1.ConfigError(`unknown network "${value}". Use a known shorthand (e.g. "base-sepolia") or CAIP-2 id, or call rpcUrl() directly.`);
|
|
56
|
+
}
|
|
57
|
+
this._rpcUrl = url;
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
35
60
|
/** Set the wallet private key (hex string). Mutually exclusive with {@link signer}. */
|
|
36
61
|
walletPrivateKey(value) {
|
|
37
62
|
this._walletPrivateKey = value;
|
|
@@ -83,6 +108,7 @@ class ConfigBuilder {
|
|
|
83
108
|
* Load configuration from environment variables.
|
|
84
109
|
*
|
|
85
110
|
* Recognised variables:
|
|
111
|
+
* - `4MICA_NETWORK` — shorthand or CAIP-2 id (e.g. `base-sepolia`); takes precedence over `4MICA_RPC_URL`
|
|
86
112
|
* - `4MICA_RPC_URL`
|
|
87
113
|
* - `4MICA_WALLET_PRIVATE_KEY`
|
|
88
114
|
* - `4MICA_ETHEREUM_HTTP_RPC_URL`
|
|
@@ -94,6 +120,8 @@ class ConfigBuilder {
|
|
|
94
120
|
*/
|
|
95
121
|
fromEnv() {
|
|
96
122
|
const env = process.env;
|
|
123
|
+
if (env['4MICA_NETWORK'])
|
|
124
|
+
this.network(env['4MICA_NETWORK']);
|
|
97
125
|
if (env['4MICA_RPC_URL'])
|
|
98
126
|
this._rpcUrl = env['4MICA_RPC_URL'];
|
|
99
127
|
if (env['4MICA_WALLET_PRIVATE_KEY'])
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -16,6 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./errors"), exports);
|
|
18
18
|
__exportStar(require("./constants"), exports);
|
|
19
|
+
__exportStar(require("./networks"), exports);
|
|
19
20
|
__exportStar(require("./config"), exports);
|
|
20
21
|
__exportStar(require("./utils"), exports);
|
|
21
22
|
__exportStar(require("./models"), exports);
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/** Metadata for a hosted 4Mica network deployment. */
|
|
2
|
+
export interface NetworkInfo {
|
|
3
|
+
/** CAIP-2 network identifier (e.g. `eip155:84532`). */
|
|
4
|
+
caip2: string;
|
|
5
|
+
/** Hosted 4Mica core API URL for this network. */
|
|
6
|
+
rpcUrl: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Hosted 4Mica network deployments, keyed by human-readable shorthand.
|
|
10
|
+
*
|
|
11
|
+
* Pass the shorthand (or the CAIP-2 string) to {@link ConfigBuilder.network}
|
|
12
|
+
* to select a network without writing a URL.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { Client, ConfigBuilder, NETWORKS } from "@4mica/sdk";
|
|
17
|
+
*
|
|
18
|
+
* // By shorthand
|
|
19
|
+
* const cfg = new ConfigBuilder()
|
|
20
|
+
* .network("base-sepolia")
|
|
21
|
+
* .walletPrivateKey("0x...")
|
|
22
|
+
* .build();
|
|
23
|
+
*
|
|
24
|
+
* // Inspect available networks
|
|
25
|
+
* console.log(NETWORKS["base-sepolia"].caip2); // "eip155:84532"
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare const NETWORKS: Record<string, NetworkInfo>;
|
|
29
|
+
/**
|
|
30
|
+
* Resolve a network shorthand or CAIP-2 identifier to a core API URL.
|
|
31
|
+
* Returns `undefined` if the identifier is not a known hosted network.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* resolveNetworkRpcUrl("base-sepolia"); // "https://base.sepolia.api.4mica.xyz/"
|
|
36
|
+
* resolveNetworkRpcUrl("eip155:84532"); // "https://base.sepolia.api.4mica.xyz/"
|
|
37
|
+
* resolveNetworkRpcUrl("eip155:1"); // undefined
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export declare function resolveNetworkRpcUrl(network: string): string | undefined;
|
package/dist/networks.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NETWORKS = void 0;
|
|
4
|
+
exports.resolveNetworkRpcUrl = resolveNetworkRpcUrl;
|
|
5
|
+
/**
|
|
6
|
+
* Hosted 4Mica network deployments, keyed by human-readable shorthand.
|
|
7
|
+
*
|
|
8
|
+
* Pass the shorthand (or the CAIP-2 string) to {@link ConfigBuilder.network}
|
|
9
|
+
* to select a network without writing a URL.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { Client, ConfigBuilder, NETWORKS } from "@4mica/sdk";
|
|
14
|
+
*
|
|
15
|
+
* // By shorthand
|
|
16
|
+
* const cfg = new ConfigBuilder()
|
|
17
|
+
* .network("base-sepolia")
|
|
18
|
+
* .walletPrivateKey("0x...")
|
|
19
|
+
* .build();
|
|
20
|
+
*
|
|
21
|
+
* // Inspect available networks
|
|
22
|
+
* console.log(NETWORKS["base-sepolia"].caip2); // "eip155:84532"
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
exports.NETWORKS = {
|
|
26
|
+
'base-sepolia': {
|
|
27
|
+
caip2: 'eip155:84532',
|
|
28
|
+
rpcUrl: 'https://base.sepolia.api.4mica.xyz/',
|
|
29
|
+
},
|
|
30
|
+
'ethereum-sepolia': {
|
|
31
|
+
caip2: 'eip155:11155111',
|
|
32
|
+
rpcUrl: 'https://ethereum.sepolia.api.4mica.xyz/',
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
const NETWORKS_BY_CAIP2 = Object.fromEntries(Object.values(exports.NETWORKS).map((n) => [n.caip2, n]));
|
|
36
|
+
/**
|
|
37
|
+
* Resolve a network shorthand or CAIP-2 identifier to a core API URL.
|
|
38
|
+
* Returns `undefined` if the identifier is not a known hosted network.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* resolveNetworkRpcUrl("base-sepolia"); // "https://base.sepolia.api.4mica.xyz/"
|
|
43
|
+
* resolveNetworkRpcUrl("eip155:84532"); // "https://base.sepolia.api.4mica.xyz/"
|
|
44
|
+
* resolveNetworkRpcUrl("eip155:1"); // undefined
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
function resolveNetworkRpcUrl(network) {
|
|
48
|
+
return exports.NETWORKS[network]?.rpcUrl ?? NETWORKS_BY_CAIP2[network]?.rpcUrl;
|
|
49
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@4mica/sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "TypeScript SDK for interacting with the 4Mica payment network",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -70,4 +70,4 @@
|
|
|
70
70
|
"typescript-eslint": "^8.54.0",
|
|
71
71
|
"vitest": "^4.0.17"
|
|
72
72
|
}
|
|
73
|
-
}
|
|
73
|
+
}
|