@unicitylabs/sphere-sdk 0.3.8 → 0.3.9
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/connect/index.cjs +770 -0
- package/dist/connect/index.cjs.map +1 -0
- package/dist/connect/index.d.cts +312 -0
- package/dist/connect/index.d.ts +312 -0
- package/dist/connect/index.js +747 -0
- package/dist/connect/index.js.map +1 -0
- package/dist/core/index.cjs +87 -7
- package/dist/core/index.cjs.map +1 -1
- package/dist/core/index.d.cts +57 -1
- package/dist/core/index.d.ts +57 -1
- package/dist/core/index.js +87 -7
- package/dist/core/index.js.map +1 -1
- package/dist/impl/browser/connect/index.cjs +271 -0
- package/dist/impl/browser/connect/index.cjs.map +1 -0
- package/dist/impl/browser/connect/index.d.cts +137 -0
- package/dist/impl/browser/connect/index.d.ts +137 -0
- package/dist/impl/browser/connect/index.js +248 -0
- package/dist/impl/browser/connect/index.js.map +1 -0
- package/dist/impl/browser/index.cjs +167 -32
- package/dist/impl/browser/index.cjs.map +1 -1
- package/dist/impl/browser/index.js +170 -33
- package/dist/impl/browser/index.js.map +1 -1
- package/dist/impl/browser/ipfs.cjs.map +1 -1
- package/dist/impl/browser/ipfs.js.map +1 -1
- package/dist/impl/nodejs/connect/index.cjs +372 -0
- package/dist/impl/nodejs/connect/index.cjs.map +1 -0
- package/dist/impl/nodejs/connect/index.d.cts +178 -0
- package/dist/impl/nodejs/connect/index.d.ts +178 -0
- package/dist/impl/nodejs/connect/index.js +333 -0
- package/dist/impl/nodejs/connect/index.js.map +1 -0
- package/dist/impl/nodejs/index.cjs +114 -4
- package/dist/impl/nodejs/index.cjs.map +1 -1
- package/dist/impl/nodejs/index.d.cts +49 -0
- package/dist/impl/nodejs/index.d.ts +49 -0
- package/dist/impl/nodejs/index.js +117 -5
- package/dist/impl/nodejs/index.js.map +1 -1
- package/dist/index.cjs +87 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +57 -1
- package/dist/index.d.ts +57 -1
- package/dist/index.js +87 -7
- package/dist/index.js.map +1 -1
- package/package.json +31 -1
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
/** Signal sent by wallet popup to dApp when ConnectHost is ready */
|
|
2
|
+
declare const HOST_READY_TYPE: "sphere-connect:host-ready";
|
|
3
|
+
/** Default timeout (ms) for waiting for the host-ready signal */
|
|
4
|
+
declare const HOST_READY_TIMEOUT = 30000;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Sphere Connect Protocol
|
|
8
|
+
* JSON-RPC-like message types for wallet ↔ dApp communication.
|
|
9
|
+
*/
|
|
10
|
+
declare const SPHERE_CONNECT_NAMESPACE = "sphere-connect";
|
|
11
|
+
declare const SPHERE_CONNECT_VERSION = "1.0";
|
|
12
|
+
|
|
13
|
+
declare const RPC_METHODS: {
|
|
14
|
+
readonly GET_IDENTITY: "sphere_getIdentity";
|
|
15
|
+
readonly GET_BALANCE: "sphere_getBalance";
|
|
16
|
+
readonly GET_ASSETS: "sphere_getAssets";
|
|
17
|
+
readonly GET_FIAT_BALANCE: "sphere_getFiatBalance";
|
|
18
|
+
readonly GET_TOKENS: "sphere_getTokens";
|
|
19
|
+
readonly GET_HISTORY: "sphere_getHistory";
|
|
20
|
+
readonly L1_GET_BALANCE: "sphere_l1GetBalance";
|
|
21
|
+
readonly L1_GET_HISTORY: "sphere_l1GetHistory";
|
|
22
|
+
readonly RESOLVE: "sphere_resolve";
|
|
23
|
+
readonly SUBSCRIBE: "sphere_subscribe";
|
|
24
|
+
readonly UNSUBSCRIBE: "sphere_unsubscribe";
|
|
25
|
+
readonly DISCONNECT: "sphere_disconnect";
|
|
26
|
+
};
|
|
27
|
+
type RpcMethod = (typeof RPC_METHODS)[keyof typeof RPC_METHODS];
|
|
28
|
+
declare const INTENT_ACTIONS: {
|
|
29
|
+
readonly SEND: "send";
|
|
30
|
+
readonly L1_SEND: "l1_send";
|
|
31
|
+
readonly DM: "dm";
|
|
32
|
+
readonly PAYMENT_REQUEST: "payment_request";
|
|
33
|
+
readonly RECEIVE: "receive";
|
|
34
|
+
readonly SIGN_MESSAGE: "sign_message";
|
|
35
|
+
};
|
|
36
|
+
type IntentAction = (typeof INTENT_ACTIONS)[keyof typeof INTENT_ACTIONS];
|
|
37
|
+
declare const ERROR_CODES: {
|
|
38
|
+
readonly PARSE_ERROR: -32700;
|
|
39
|
+
readonly INVALID_REQUEST: -32600;
|
|
40
|
+
readonly METHOD_NOT_FOUND: -32601;
|
|
41
|
+
readonly INVALID_PARAMS: -32602;
|
|
42
|
+
readonly INTERNAL_ERROR: -32603;
|
|
43
|
+
readonly NOT_CONNECTED: 4001;
|
|
44
|
+
readonly PERMISSION_DENIED: 4002;
|
|
45
|
+
readonly USER_REJECTED: 4003;
|
|
46
|
+
readonly SESSION_EXPIRED: 4004;
|
|
47
|
+
readonly ORIGIN_BLOCKED: 4005;
|
|
48
|
+
readonly RATE_LIMITED: 4006;
|
|
49
|
+
readonly INSUFFICIENT_BALANCE: 4100;
|
|
50
|
+
readonly INVALID_RECIPIENT: 4101;
|
|
51
|
+
readonly TRANSFER_FAILED: 4102;
|
|
52
|
+
readonly INTENT_CANCELLED: 4200;
|
|
53
|
+
};
|
|
54
|
+
type ErrorCode = (typeof ERROR_CODES)[keyof typeof ERROR_CODES];
|
|
55
|
+
interface SphereMessageBase {
|
|
56
|
+
readonly ns: typeof SPHERE_CONNECT_NAMESPACE;
|
|
57
|
+
readonly v: typeof SPHERE_CONNECT_VERSION;
|
|
58
|
+
}
|
|
59
|
+
/** Query request: dApp → Wallet */
|
|
60
|
+
interface SphereRpcRequest extends SphereMessageBase {
|
|
61
|
+
readonly type: 'request';
|
|
62
|
+
readonly id: string;
|
|
63
|
+
readonly method: string;
|
|
64
|
+
readonly params?: Record<string, unknown>;
|
|
65
|
+
}
|
|
66
|
+
/** Query response: Wallet → dApp */
|
|
67
|
+
interface SphereRpcResponse extends SphereMessageBase {
|
|
68
|
+
readonly type: 'response';
|
|
69
|
+
readonly id: string;
|
|
70
|
+
readonly result?: unknown;
|
|
71
|
+
readonly error?: SphereRpcError;
|
|
72
|
+
}
|
|
73
|
+
/** Intent request: dApp → Wallet (opens wallet UI) */
|
|
74
|
+
interface SphereIntentRequest extends SphereMessageBase {
|
|
75
|
+
readonly type: 'intent';
|
|
76
|
+
readonly id: string;
|
|
77
|
+
readonly action: string;
|
|
78
|
+
readonly params: Record<string, unknown>;
|
|
79
|
+
}
|
|
80
|
+
/** Intent result: Wallet → dApp (after user action) */
|
|
81
|
+
interface SphereIntentResult extends SphereMessageBase {
|
|
82
|
+
readonly type: 'intent_result';
|
|
83
|
+
readonly id: string;
|
|
84
|
+
readonly result?: unknown;
|
|
85
|
+
readonly error?: SphereRpcError;
|
|
86
|
+
}
|
|
87
|
+
/** Event push: Wallet → dApp (unsolicited) */
|
|
88
|
+
interface SphereEventMessage extends SphereMessageBase {
|
|
89
|
+
readonly type: 'event';
|
|
90
|
+
readonly event: string;
|
|
91
|
+
readonly data: unknown;
|
|
92
|
+
}
|
|
93
|
+
/** Handshake: bidirectional */
|
|
94
|
+
interface SphereHandshake extends SphereMessageBase {
|
|
95
|
+
readonly type: 'handshake';
|
|
96
|
+
readonly direction: 'request' | 'response';
|
|
97
|
+
readonly permissions: string[];
|
|
98
|
+
readonly dapp?: DAppMetadata;
|
|
99
|
+
readonly sessionId?: string;
|
|
100
|
+
readonly identity?: PublicIdentity;
|
|
101
|
+
}
|
|
102
|
+
interface SphereRpcError {
|
|
103
|
+
readonly code: number;
|
|
104
|
+
readonly message: string;
|
|
105
|
+
readonly data?: unknown;
|
|
106
|
+
}
|
|
107
|
+
type SphereConnectMessage = SphereRpcRequest | SphereRpcResponse | SphereIntentRequest | SphereIntentResult | SphereEventMessage | SphereHandshake;
|
|
108
|
+
interface DAppMetadata {
|
|
109
|
+
readonly name: string;
|
|
110
|
+
readonly description?: string;
|
|
111
|
+
readonly icon?: string;
|
|
112
|
+
readonly url: string;
|
|
113
|
+
}
|
|
114
|
+
interface PublicIdentity {
|
|
115
|
+
readonly chainPubkey: string;
|
|
116
|
+
readonly l1Address: string;
|
|
117
|
+
readonly directAddress?: string;
|
|
118
|
+
readonly nametag?: string;
|
|
119
|
+
}
|
|
120
|
+
/** Check if a message belongs to the Sphere Connect protocol */
|
|
121
|
+
declare function isSphereConnectMessage(msg: unknown): msg is SphereConnectMessage;
|
|
122
|
+
/** Create a unique request ID */
|
|
123
|
+
declare function createRequestId(): string;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Sphere Connect Permission System
|
|
127
|
+
* Defines scopes, maps methods/intents to required permissions.
|
|
128
|
+
*/
|
|
129
|
+
declare const PERMISSION_SCOPES: {
|
|
130
|
+
readonly IDENTITY_READ: "identity:read";
|
|
131
|
+
readonly BALANCE_READ: "balance:read";
|
|
132
|
+
readonly TOKENS_READ: "tokens:read";
|
|
133
|
+
readonly HISTORY_READ: "history:read";
|
|
134
|
+
readonly L1_READ: "l1:read";
|
|
135
|
+
readonly EVENTS_SUBSCRIBE: "events:subscribe";
|
|
136
|
+
readonly RESOLVE_PEER: "resolve:peer";
|
|
137
|
+
readonly TRANSFER_REQUEST: "transfer:request";
|
|
138
|
+
readonly L1_TRANSFER: "l1:transfer";
|
|
139
|
+
readonly DM_REQUEST: "dm:request";
|
|
140
|
+
readonly PAYMENT_REQUEST: "payment:request";
|
|
141
|
+
readonly SIGN_REQUEST: "sign:request";
|
|
142
|
+
};
|
|
143
|
+
type PermissionScope = (typeof PERMISSION_SCOPES)[keyof typeof PERMISSION_SCOPES];
|
|
144
|
+
/** All available permission scopes */
|
|
145
|
+
declare const ALL_PERMISSIONS: readonly PermissionScope[];
|
|
146
|
+
/** Permissions always granted on connect */
|
|
147
|
+
declare const DEFAULT_PERMISSIONS: readonly PermissionScope[];
|
|
148
|
+
declare const METHOD_PERMISSIONS: Record<string, PermissionScope>;
|
|
149
|
+
declare const INTENT_PERMISSIONS: Record<string, PermissionScope>;
|
|
150
|
+
/** Check if granted permissions allow calling a method */
|
|
151
|
+
declare function hasMethodPermission(granted: ReadonlySet<string>, method: string): boolean;
|
|
152
|
+
/** Check if granted permissions allow an intent action */
|
|
153
|
+
declare function hasIntentPermission(granted: ReadonlySet<string>, action: string): boolean;
|
|
154
|
+
/** Validate that all requested permissions are known scopes */
|
|
155
|
+
declare function validatePermissions(permissions: string[]): permissions is PermissionScope[];
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Sphere Connect Types
|
|
159
|
+
* Session, configuration, and callback types.
|
|
160
|
+
*/
|
|
161
|
+
|
|
162
|
+
interface ConnectTransport {
|
|
163
|
+
/** Send a message to the other side */
|
|
164
|
+
send(message: SphereConnectMessage): void;
|
|
165
|
+
/** Subscribe to incoming messages. Returns unsubscribe function. */
|
|
166
|
+
onMessage(handler: (message: SphereConnectMessage) => void): () => void;
|
|
167
|
+
/** Clean up transport resources */
|
|
168
|
+
destroy(): void;
|
|
169
|
+
}
|
|
170
|
+
interface ConnectSession {
|
|
171
|
+
readonly id: string;
|
|
172
|
+
readonly dapp: DAppMetadata;
|
|
173
|
+
readonly permissions: PermissionScope[];
|
|
174
|
+
readonly createdAt: number;
|
|
175
|
+
readonly expiresAt: number;
|
|
176
|
+
active: boolean;
|
|
177
|
+
}
|
|
178
|
+
interface ConnectHostConfig {
|
|
179
|
+
/** Sphere SDK instance to bridge */
|
|
180
|
+
sphere: unknown;
|
|
181
|
+
/** Transport layer for communication */
|
|
182
|
+
transport: ConnectTransport;
|
|
183
|
+
/** Called when dApp requests connection. Wallet shows approval UI. */
|
|
184
|
+
onConnectionRequest: (dapp: DAppMetadata, requestedPermissions: PermissionScope[]) => Promise<{
|
|
185
|
+
approved: boolean;
|
|
186
|
+
grantedPermissions: PermissionScope[];
|
|
187
|
+
}>;
|
|
188
|
+
/** Called when dApp sends an intent. Wallet opens corresponding UI. */
|
|
189
|
+
onIntent: (action: string, params: Record<string, unknown>, session: ConnectSession) => Promise<{
|
|
190
|
+
result?: unknown;
|
|
191
|
+
error?: {
|
|
192
|
+
code: number;
|
|
193
|
+
message: string;
|
|
194
|
+
};
|
|
195
|
+
}>;
|
|
196
|
+
/** Session time-to-live in ms. Default: 86400000 (24h). 0 = no expiry. */
|
|
197
|
+
sessionTtlMs?: number;
|
|
198
|
+
/** Max requests per second per session. Default: 20. */
|
|
199
|
+
maxRequestsPerSecond?: number;
|
|
200
|
+
}
|
|
201
|
+
interface ConnectClientConfig {
|
|
202
|
+
/** Transport layer for communication */
|
|
203
|
+
transport: ConnectTransport;
|
|
204
|
+
/** dApp metadata sent during handshake */
|
|
205
|
+
dapp: DAppMetadata;
|
|
206
|
+
/** Permissions to request. Defaults to all. */
|
|
207
|
+
permissions?: PermissionScope[];
|
|
208
|
+
/** Timeout for query requests in ms. Default: 30000. */
|
|
209
|
+
timeout?: number;
|
|
210
|
+
/** Timeout for intent requests in ms (user interaction). Default: 120000. */
|
|
211
|
+
intentTimeout?: number;
|
|
212
|
+
}
|
|
213
|
+
interface ConnectResult {
|
|
214
|
+
readonly sessionId: string;
|
|
215
|
+
readonly permissions: PermissionScope[];
|
|
216
|
+
readonly identity: PublicIdentity;
|
|
217
|
+
}
|
|
218
|
+
type ConnectEventHandler = (data: unknown) => void;
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* ConnectHost — Wallet side of Sphere Connect.
|
|
222
|
+
*
|
|
223
|
+
* Wraps a Sphere instance and exposes its API through a ConnectTransport.
|
|
224
|
+
* Handles permission checking, rate limiting, session management,
|
|
225
|
+
* and delegates intents to the wallet app via callbacks.
|
|
226
|
+
*/
|
|
227
|
+
|
|
228
|
+
declare class ConnectHost {
|
|
229
|
+
private readonly sphere;
|
|
230
|
+
private readonly transport;
|
|
231
|
+
private readonly config;
|
|
232
|
+
private session;
|
|
233
|
+
private grantedPermissions;
|
|
234
|
+
private eventSubscriptions;
|
|
235
|
+
private rateLimitCounter;
|
|
236
|
+
private rateLimitResetAt;
|
|
237
|
+
private unsubscribeTransport;
|
|
238
|
+
constructor(config: ConnectHostConfig);
|
|
239
|
+
/** Get current active session */
|
|
240
|
+
getSession(): ConnectSession | null;
|
|
241
|
+
/** Revoke the current session */
|
|
242
|
+
revokeSession(): void;
|
|
243
|
+
/** Destroy the host, clean up all resources */
|
|
244
|
+
destroy(): void;
|
|
245
|
+
private handleMessage;
|
|
246
|
+
private handleHandshake;
|
|
247
|
+
private sendHandshakeResponse;
|
|
248
|
+
private handleRpcRequest;
|
|
249
|
+
private handleIntentRequest;
|
|
250
|
+
private executeMethod;
|
|
251
|
+
private handleSubscribe;
|
|
252
|
+
private handleUnsubscribe;
|
|
253
|
+
private cleanupEventSubscriptions;
|
|
254
|
+
private getPublicIdentity;
|
|
255
|
+
private stripTokenSdkData;
|
|
256
|
+
private sendResult;
|
|
257
|
+
private sendError;
|
|
258
|
+
private sendIntentResult;
|
|
259
|
+
private sendIntentError;
|
|
260
|
+
private checkRateLimit;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* ConnectClient — dApp side of Sphere Connect.
|
|
265
|
+
*
|
|
266
|
+
* Lightweight client that communicates with a wallet's ConnectHost
|
|
267
|
+
* through a ConnectTransport. Provides query and intent methods
|
|
268
|
+
* that mirror the Sphere SDK API.
|
|
269
|
+
*
|
|
270
|
+
* Zero dependencies on the Sphere SDK core.
|
|
271
|
+
*/
|
|
272
|
+
|
|
273
|
+
declare class ConnectClient {
|
|
274
|
+
private readonly transport;
|
|
275
|
+
private readonly dapp;
|
|
276
|
+
private readonly requestedPermissions;
|
|
277
|
+
private readonly timeout;
|
|
278
|
+
private readonly intentTimeout;
|
|
279
|
+
private sessionId;
|
|
280
|
+
private grantedPermissions;
|
|
281
|
+
private identity;
|
|
282
|
+
private connected;
|
|
283
|
+
private pendingRequests;
|
|
284
|
+
private eventHandlers;
|
|
285
|
+
private unsubscribeTransport;
|
|
286
|
+
private handshakeResolver;
|
|
287
|
+
constructor(config: ConnectClientConfig);
|
|
288
|
+
/** Connect to the wallet. Returns session info and public identity. */
|
|
289
|
+
connect(): Promise<ConnectResult>;
|
|
290
|
+
/** Disconnect from the wallet */
|
|
291
|
+
disconnect(): Promise<void>;
|
|
292
|
+
/** Whether currently connected */
|
|
293
|
+
get isConnected(): boolean;
|
|
294
|
+
/** Granted permission scopes */
|
|
295
|
+
get permissions(): readonly PermissionScope[];
|
|
296
|
+
/** Current session ID */
|
|
297
|
+
get session(): string | null;
|
|
298
|
+
/** Public identity received during handshake */
|
|
299
|
+
get walletIdentity(): PublicIdentity | null;
|
|
300
|
+
/** Send a query request and return the result */
|
|
301
|
+
query<T = unknown>(method: string, params?: Record<string, unknown>): Promise<T>;
|
|
302
|
+
/** Send an intent request. The wallet will open its UI for user confirmation. */
|
|
303
|
+
intent<T = unknown>(action: string, params: Record<string, unknown>): Promise<T>;
|
|
304
|
+
/** Subscribe to a wallet event. Returns unsubscribe function. */
|
|
305
|
+
on(event: string, handler: ConnectEventHandler): () => void;
|
|
306
|
+
private handleMessage;
|
|
307
|
+
private handleHandshakeResponse;
|
|
308
|
+
private handlePendingResponse;
|
|
309
|
+
private cleanup;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
export { ALL_PERMISSIONS, ConnectClient, type ConnectClientConfig, type ConnectEventHandler, ConnectHost, type ConnectHostConfig, type ConnectResult, type ConnectSession, type ConnectTransport, type DAppMetadata, DEFAULT_PERMISSIONS, ERROR_CODES, type ErrorCode, HOST_READY_TIMEOUT, HOST_READY_TYPE, INTENT_ACTIONS, INTENT_PERMISSIONS, type IntentAction, METHOD_PERMISSIONS, PERMISSION_SCOPES, type PermissionScope, type PublicIdentity, RPC_METHODS, type RpcMethod, SPHERE_CONNECT_NAMESPACE, SPHERE_CONNECT_VERSION, type SphereConnectMessage, type SphereEventMessage, type SphereHandshake, type SphereIntentRequest, type SphereIntentResult, type SphereRpcError, type SphereRpcRequest, type SphereRpcResponse, createRequestId, hasIntentPermission, hasMethodPermission, isSphereConnectMessage, validatePermissions };
|