@buildersgarden/siwa 0.0.19 → 0.0.20
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/dist/addresses.js +5 -0
- package/dist/client-resolver.d.ts +36 -0
- package/dist/client-resolver.js +92 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +5 -1
package/dist/addresses.js
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
// Identity Registries
|
|
9
9
|
// ---------------------------------------------------------------------------
|
|
10
10
|
export const REGISTRY_ADDRESSES = {
|
|
11
|
+
1: '0x8004A169FB4a3325136EB29fA0ceB6D2e539a432', // Ethereum
|
|
11
12
|
8453: '0x8004A169FB4a3325136EB29fA0ceB6D2e539a432', // Base
|
|
12
13
|
84532: '0x8004A818BFB912233c491871b3d84c89A494BD9e', // Base Sepolia
|
|
13
14
|
11155111: '0x8004a6090Cd10A7288092483047B097295Fb8847', // ETH Sepolia
|
|
@@ -18,6 +19,7 @@ export const REGISTRY_ADDRESSES = {
|
|
|
18
19
|
// Reputation Registries
|
|
19
20
|
// ---------------------------------------------------------------------------
|
|
20
21
|
export const REPUTATION_ADDRESSES = {
|
|
22
|
+
1: '0x8004BAa17C55a88189AE136b182e5fdA19dE9b63', // Ethereum
|
|
21
23
|
8453: '0x8004BAa17C55a88189AE136b182e5fdA19dE9b63', // Base
|
|
22
24
|
84532: '0x8004B663056A597Dffe9eCcC1965A193B7388713', // Base Sepolia
|
|
23
25
|
};
|
|
@@ -25,6 +27,7 @@ export const REPUTATION_ADDRESSES = {
|
|
|
25
27
|
// RPC Endpoints (public, rate-limited)
|
|
26
28
|
// ---------------------------------------------------------------------------
|
|
27
29
|
export const RPC_ENDPOINTS = {
|
|
30
|
+
1: 'https://cloudflare-eth.com',
|
|
28
31
|
8453: 'https://mainnet.base.org',
|
|
29
32
|
84532: 'https://sepolia.base.org',
|
|
30
33
|
11155111: 'https://rpc.sepolia.org',
|
|
@@ -35,6 +38,7 @@ export const RPC_ENDPOINTS = {
|
|
|
35
38
|
// Chain Names
|
|
36
39
|
// ---------------------------------------------------------------------------
|
|
37
40
|
export const CHAIN_NAMES = {
|
|
41
|
+
1: 'Ethereum',
|
|
38
42
|
8453: 'Base',
|
|
39
43
|
84532: 'Base Sepolia',
|
|
40
44
|
11155111: 'Ethereum Sepolia',
|
|
@@ -54,6 +58,7 @@ export const FAUCETS = {
|
|
|
54
58
|
// Block Explorers
|
|
55
59
|
// ---------------------------------------------------------------------------
|
|
56
60
|
export const BLOCK_EXPLORERS = {
|
|
61
|
+
1: 'https://etherscan.io',
|
|
57
62
|
8453: 'https://basescan.org',
|
|
58
63
|
84532: 'https://sepolia.basescan.org',
|
|
59
64
|
11155111: 'https://sepolia.etherscan.io',
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* client-resolver.ts
|
|
3
|
+
*
|
|
4
|
+
* Dynamic PublicClient resolution for multi-chain SIWA servers.
|
|
5
|
+
* Lazily creates and caches viem PublicClient instances per chain ID.
|
|
6
|
+
*/
|
|
7
|
+
import { type PublicClient } from 'viem';
|
|
8
|
+
export interface ClientResolverOptions {
|
|
9
|
+
/** Explicit RPC URL overrides per chain ID. */
|
|
10
|
+
rpcOverrides?: Record<number, string>;
|
|
11
|
+
/** Restrict which chain IDs are accepted. When set, only these chains can be resolved. */
|
|
12
|
+
allowedChainIds?: number[];
|
|
13
|
+
}
|
|
14
|
+
export interface ClientResolver {
|
|
15
|
+
/** Get (or lazily create) a PublicClient for the given chain ID. Throws if unsupported. */
|
|
16
|
+
getClient(chainId: number): PublicClient;
|
|
17
|
+
/** Check whether a chain ID can be resolved. */
|
|
18
|
+
isSupported(chainId: number): boolean;
|
|
19
|
+
/** List all chain IDs that can be resolved. */
|
|
20
|
+
supportedChainIds(): number[];
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Extract the chain ID from an `eip155:{chainId}:{address}` agent registry string.
|
|
24
|
+
* Returns `null` if the format is invalid.
|
|
25
|
+
*/
|
|
26
|
+
export declare function parseChainId(agentRegistry: string): number | null;
|
|
27
|
+
/**
|
|
28
|
+
* Create a ClientResolver that lazily creates and caches PublicClient instances.
|
|
29
|
+
*
|
|
30
|
+
* RPC resolution order:
|
|
31
|
+
* 1. Explicit `rpcOverrides` map
|
|
32
|
+
* 2. Environment variable `RPC_URL_{chainId}` (e.g. `RPC_URL_42161`)
|
|
33
|
+
* 3. Built-in `RPC_ENDPOINTS` from addresses.ts
|
|
34
|
+
* 4. Throw with a helpful error listing supported chains
|
|
35
|
+
*/
|
|
36
|
+
export declare function createClientResolver(options?: ClientResolverOptions): ClientResolver;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* client-resolver.ts
|
|
3
|
+
*
|
|
4
|
+
* Dynamic PublicClient resolution for multi-chain SIWA servers.
|
|
5
|
+
* Lazily creates and caches viem PublicClient instances per chain ID.
|
|
6
|
+
*/
|
|
7
|
+
import { createPublicClient, http } from 'viem';
|
|
8
|
+
import { RPC_ENDPOINTS, CHAIN_NAMES } from './addresses.js';
|
|
9
|
+
// ─── Helpers ─────────────────────────────────────────────────────────
|
|
10
|
+
/**
|
|
11
|
+
* Extract the chain ID from an `eip155:{chainId}:{address}` agent registry string.
|
|
12
|
+
* Returns `null` if the format is invalid.
|
|
13
|
+
*/
|
|
14
|
+
export function parseChainId(agentRegistry) {
|
|
15
|
+
const parts = agentRegistry.split(':');
|
|
16
|
+
if (parts.length !== 3 || parts[0] !== 'eip155')
|
|
17
|
+
return null;
|
|
18
|
+
const id = parseInt(parts[1], 10);
|
|
19
|
+
return Number.isFinite(id) ? id : null;
|
|
20
|
+
}
|
|
21
|
+
// ─── Factory ─────────────────────────────────────────────────────────
|
|
22
|
+
/**
|
|
23
|
+
* Create a ClientResolver that lazily creates and caches PublicClient instances.
|
|
24
|
+
*
|
|
25
|
+
* RPC resolution order:
|
|
26
|
+
* 1. Explicit `rpcOverrides` map
|
|
27
|
+
* 2. Environment variable `RPC_URL_{chainId}` (e.g. `RPC_URL_42161`)
|
|
28
|
+
* 3. Built-in `RPC_ENDPOINTS` from addresses.ts
|
|
29
|
+
* 4. Throw with a helpful error listing supported chains
|
|
30
|
+
*/
|
|
31
|
+
export function createClientResolver(options) {
|
|
32
|
+
const cache = new Map();
|
|
33
|
+
const overrides = options?.rpcOverrides ?? {};
|
|
34
|
+
const allowed = options?.allowedChainIds
|
|
35
|
+
? new Set(options.allowedChainIds)
|
|
36
|
+
: null;
|
|
37
|
+
function resolveRpcUrl(chainId) {
|
|
38
|
+
// 1. Explicit override
|
|
39
|
+
if (overrides[chainId])
|
|
40
|
+
return overrides[chainId];
|
|
41
|
+
// 2. Environment variable
|
|
42
|
+
const envKey = `RPC_URL_${chainId}`;
|
|
43
|
+
const envVal = typeof process !== 'undefined' ? process.env?.[envKey] : undefined;
|
|
44
|
+
if (envVal)
|
|
45
|
+
return envVal;
|
|
46
|
+
// 3. Built-in defaults
|
|
47
|
+
if (RPC_ENDPOINTS[chainId])
|
|
48
|
+
return RPC_ENDPOINTS[chainId];
|
|
49
|
+
// 4. Not found
|
|
50
|
+
const supported = getSupportedChainIds();
|
|
51
|
+
const names = supported.map((id) => `${id} (${CHAIN_NAMES[id] || 'unknown'})`).join(', ');
|
|
52
|
+
throw new Error(`No RPC endpoint for chain ${chainId}. Supported chains: ${names}. ` +
|
|
53
|
+
`Set RPC_URL_${chainId} or pass rpcOverrides to createClientResolver().`);
|
|
54
|
+
}
|
|
55
|
+
function getSupportedChainIds() {
|
|
56
|
+
const ids = new Set([
|
|
57
|
+
...Object.keys(overrides).map(Number),
|
|
58
|
+
...Object.keys(RPC_ENDPOINTS).map(Number),
|
|
59
|
+
]);
|
|
60
|
+
if (allowed) {
|
|
61
|
+
return [...ids].filter((id) => allowed.has(id));
|
|
62
|
+
}
|
|
63
|
+
return [...ids];
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
getClient(chainId) {
|
|
67
|
+
if (allowed && !allowed.has(chainId)) {
|
|
68
|
+
const names = [...allowed].map((id) => `${id} (${CHAIN_NAMES[id] || 'unknown'})`).join(', ');
|
|
69
|
+
throw new Error(`Chain ${chainId} is not in the allowed list. Allowed: ${names}`);
|
|
70
|
+
}
|
|
71
|
+
let client = cache.get(chainId);
|
|
72
|
+
if (!client) {
|
|
73
|
+
const rpcUrl = resolveRpcUrl(chainId);
|
|
74
|
+
client = createPublicClient({ transport: http(rpcUrl) });
|
|
75
|
+
cache.set(chainId, client);
|
|
76
|
+
}
|
|
77
|
+
return client;
|
|
78
|
+
},
|
|
79
|
+
isSupported(chainId) {
|
|
80
|
+
if (allowed && !allowed.has(chainId))
|
|
81
|
+
return false;
|
|
82
|
+
try {
|
|
83
|
+
resolveRpcUrl(chainId);
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
supportedChainIds: getSupportedChainIds,
|
|
91
|
+
};
|
|
92
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@buildersgarden/siwa",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.20",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -70,6 +70,10 @@
|
|
|
70
70
|
"./x402": {
|
|
71
71
|
"types": "./dist/x402.d.ts",
|
|
72
72
|
"default": "./dist/x402.js"
|
|
73
|
+
},
|
|
74
|
+
"./client-resolver": {
|
|
75
|
+
"types": "./dist/client-resolver.d.ts",
|
|
76
|
+
"default": "./dist/client-resolver.js"
|
|
73
77
|
}
|
|
74
78
|
},
|
|
75
79
|
"main": "./dist/index.js",
|