@unicitylabs/sphere-sdk 0.6.4 → 0.6.6
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 +57 -0
- package/dist/connect/index.cjs.map +1 -1
- package/dist/connect/index.d.cts +30 -2
- package/dist/connect/index.d.ts +30 -2
- package/dist/connect/index.js +57 -0
- package/dist/connect/index.js.map +1 -1
- package/dist/core/index.cjs +84 -52
- package/dist/core/index.cjs.map +1 -1
- package/dist/core/index.d.cts +3 -0
- package/dist/core/index.d.ts +3 -0
- package/dist/core/index.js +84 -52
- package/dist/core/index.js.map +1 -1
- package/dist/impl/browser/connect/index.cjs +502 -1
- package/dist/impl/browser/connect/index.cjs.map +1 -1
- package/dist/impl/browser/connect/index.d.cts +181 -1
- package/dist/impl/browser/connect/index.d.ts +181 -1
- package/dist/impl/browser/connect/index.js +502 -1
- package/dist/impl/browser/connect/index.js.map +1 -1
- package/dist/impl/nodejs/connect/index.cjs.map +1 -1
- package/dist/impl/nodejs/connect/index.js.map +1 -1
- package/dist/index.cjs +84 -52
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +84 -52
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -73,6 +73,27 @@ interface PublicIdentity {
|
|
|
73
73
|
readonly nametag?: string;
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
+
/**
|
|
77
|
+
* Sphere Connect Permission System
|
|
78
|
+
* Defines scopes, maps methods/intents to required permissions.
|
|
79
|
+
*/
|
|
80
|
+
declare const PERMISSION_SCOPES: {
|
|
81
|
+
readonly IDENTITY_READ: "identity:read";
|
|
82
|
+
readonly BALANCE_READ: "balance:read";
|
|
83
|
+
readonly TOKENS_READ: "tokens:read";
|
|
84
|
+
readonly HISTORY_READ: "history:read";
|
|
85
|
+
readonly L1_READ: "l1:read";
|
|
86
|
+
readonly EVENTS_SUBSCRIBE: "events:subscribe";
|
|
87
|
+
readonly RESOLVE_PEER: "resolve:peer";
|
|
88
|
+
readonly TRANSFER_REQUEST: "transfer:request";
|
|
89
|
+
readonly L1_TRANSFER: "l1:transfer";
|
|
90
|
+
readonly DM_REQUEST: "dm:request";
|
|
91
|
+
readonly DM_READ: "dm:read";
|
|
92
|
+
readonly PAYMENT_REQUEST: "payment:request";
|
|
93
|
+
readonly SIGN_REQUEST: "sign:request";
|
|
94
|
+
};
|
|
95
|
+
type PermissionScope = (typeof PERMISSION_SCOPES)[keyof typeof PERMISSION_SCOPES];
|
|
96
|
+
|
|
76
97
|
/**
|
|
77
98
|
* Sphere Connect Types
|
|
78
99
|
* Session, configuration, and callback types.
|
|
@@ -86,6 +107,81 @@ interface ConnectTransport {
|
|
|
86
107
|
/** Clean up transport resources */
|
|
87
108
|
destroy(): void;
|
|
88
109
|
}
|
|
110
|
+
interface ConnectClientConfig {
|
|
111
|
+
/** Transport layer for communication */
|
|
112
|
+
transport: ConnectTransport;
|
|
113
|
+
/** dApp metadata sent during handshake */
|
|
114
|
+
dapp: DAppMetadata;
|
|
115
|
+
/** Permissions to request. Defaults to all. */
|
|
116
|
+
permissions?: PermissionScope[];
|
|
117
|
+
/** Timeout for query requests in ms. Default: 30000. */
|
|
118
|
+
timeout?: number;
|
|
119
|
+
/** Timeout for intent requests in ms (user interaction). Default: 120000. */
|
|
120
|
+
intentTimeout?: number;
|
|
121
|
+
/** Existing session ID to resume. If the host still has an active session
|
|
122
|
+
* with this ID, the connection is restored without re-showing the approval UI. */
|
|
123
|
+
resumeSessionId?: string;
|
|
124
|
+
/** If true, the connection will silently fail if the origin is not already approved by the wallet.
|
|
125
|
+
* No approval UI will be shown. Used for auto-connect on page load. */
|
|
126
|
+
silent?: boolean;
|
|
127
|
+
}
|
|
128
|
+
interface ConnectResult {
|
|
129
|
+
readonly sessionId: string;
|
|
130
|
+
readonly permissions: PermissionScope[];
|
|
131
|
+
readonly identity: PublicIdentity;
|
|
132
|
+
}
|
|
133
|
+
type ConnectEventHandler = (data: unknown) => void;
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* ConnectClient — dApp side of Sphere Connect.
|
|
137
|
+
*
|
|
138
|
+
* Lightweight client that communicates with a wallet's ConnectHost
|
|
139
|
+
* through a ConnectTransport. Provides query and intent methods
|
|
140
|
+
* that mirror the Sphere SDK API.
|
|
141
|
+
*
|
|
142
|
+
* Zero dependencies on the Sphere SDK core.
|
|
143
|
+
*/
|
|
144
|
+
|
|
145
|
+
declare class ConnectClient {
|
|
146
|
+
private readonly transport;
|
|
147
|
+
private readonly dapp;
|
|
148
|
+
private readonly requestedPermissions;
|
|
149
|
+
private readonly timeout;
|
|
150
|
+
private readonly intentTimeout;
|
|
151
|
+
private readonly resumeSessionId;
|
|
152
|
+
private readonly silent;
|
|
153
|
+
private sessionId;
|
|
154
|
+
private grantedPermissions;
|
|
155
|
+
private identity;
|
|
156
|
+
private connected;
|
|
157
|
+
private pendingRequests;
|
|
158
|
+
private eventHandlers;
|
|
159
|
+
private unsubscribeTransport;
|
|
160
|
+
private handshakeResolver;
|
|
161
|
+
constructor(config: ConnectClientConfig);
|
|
162
|
+
/** Connect to the wallet. Returns session info and public identity. */
|
|
163
|
+
connect(): Promise<ConnectResult>;
|
|
164
|
+
/** Disconnect from the wallet */
|
|
165
|
+
disconnect(): Promise<void>;
|
|
166
|
+
/** Whether currently connected */
|
|
167
|
+
get isConnected(): boolean;
|
|
168
|
+
/** Granted permission scopes */
|
|
169
|
+
get permissions(): readonly PermissionScope[];
|
|
170
|
+
/** Current session ID */
|
|
171
|
+
get session(): string | null;
|
|
172
|
+
/** Public identity received during handshake */
|
|
173
|
+
get walletIdentity(): PublicIdentity | null;
|
|
174
|
+
/** Send a query request and return the result */
|
|
175
|
+
query<T = unknown>(method: string, params?: Record<string, unknown>): Promise<T>;
|
|
176
|
+
/** Send an intent request. The wallet will open its UI for user confirmation. */
|
|
177
|
+
intent<T = unknown>(action: string, params: Record<string, unknown>): Promise<T>;
|
|
178
|
+
/** Subscribe to a wallet event. Returns unsubscribe function. */
|
|
179
|
+
on(event: string, handler: ConnectEventHandler): () => void;
|
|
180
|
+
private handleMessage;
|
|
181
|
+
private handleHandshakeResponse;
|
|
182
|
+
private handlePendingResponse;
|
|
183
|
+
private cleanup;
|
|
184
|
+
}
|
|
89
185
|
|
|
90
186
|
/**
|
|
91
187
|
* PostMessageTransport — Browser transport for Sphere Connect.
|
|
@@ -186,4 +282,88 @@ declare const ExtensionTransport: {
|
|
|
186
282
|
forHost(chromeApi: ChromeMessagingApi): ConnectTransport;
|
|
187
283
|
};
|
|
188
284
|
|
|
189
|
-
|
|
285
|
+
/**
|
|
286
|
+
* autoConnect — Universal dApp connection to Sphere wallet.
|
|
287
|
+
*
|
|
288
|
+
* Auto-detects the best available transport and connects:
|
|
289
|
+
* P1: iframe → PostMessageTransport to parent window
|
|
290
|
+
* P2: extension → ExtensionTransport via chrome extension
|
|
291
|
+
* P3: standalone → PostMessageTransport to popup window
|
|
292
|
+
*
|
|
293
|
+
* Usage:
|
|
294
|
+
* import { autoConnect } from '@unicitylabs/sphere-sdk/connect/browser';
|
|
295
|
+
*
|
|
296
|
+
* const client = await autoConnect({
|
|
297
|
+
* dapp: { name: 'My App', url: location.origin },
|
|
298
|
+
* walletUrl: 'https://sphere.unicity.network',
|
|
299
|
+
* });
|
|
300
|
+
*
|
|
301
|
+
* // Use the client — same API regardless of transport:
|
|
302
|
+
* const balance = await client.query('sphere_getBalance');
|
|
303
|
+
* await client.intent('send', { recipient: '@bob', amount: '1000', coinId: 'UCT' });
|
|
304
|
+
* client.on('transfer:incoming', (data) => console.log(data));
|
|
305
|
+
*/
|
|
306
|
+
|
|
307
|
+
/** Returns true when the page is running inside an iframe. */
|
|
308
|
+
declare function isInIframe(): boolean;
|
|
309
|
+
/** Returns true when the Sphere browser extension is installed and active. */
|
|
310
|
+
declare function hasExtension(): boolean;
|
|
311
|
+
/** Detected transport type. */
|
|
312
|
+
type DetectedTransport = 'iframe' | 'extension' | 'popup';
|
|
313
|
+
/** Detect which transport to use based on the current environment. */
|
|
314
|
+
declare function detectTransport(): DetectedTransport;
|
|
315
|
+
interface AutoConnectConfig {
|
|
316
|
+
/** dApp metadata sent during handshake. */
|
|
317
|
+
dapp: DAppMetadata;
|
|
318
|
+
/**
|
|
319
|
+
* Wallet URL for popup fallback (P3).
|
|
320
|
+
* The URL will be opened with `/connect?origin=<current origin>` appended.
|
|
321
|
+
* Required if the extension is not installed and the page is not in an iframe.
|
|
322
|
+
*/
|
|
323
|
+
walletUrl?: string;
|
|
324
|
+
/** Permissions to request. Defaults to all. */
|
|
325
|
+
permissions?: PermissionScope[];
|
|
326
|
+
/**
|
|
327
|
+
* If true, silently fail if the wallet has not previously approved this origin.
|
|
328
|
+
* No UI will be shown. Useful for auto-connect on page load.
|
|
329
|
+
* Default: false.
|
|
330
|
+
*/
|
|
331
|
+
silent?: boolean;
|
|
332
|
+
/** Existing session ID to resume (for popup mode). */
|
|
333
|
+
resumeSessionId?: string;
|
|
334
|
+
/** Timeout for query requests in ms. Default: 30000. */
|
|
335
|
+
timeout?: number;
|
|
336
|
+
/** Timeout for intent requests in ms. Default: 120000. */
|
|
337
|
+
intentTimeout?: number;
|
|
338
|
+
/**
|
|
339
|
+
* Popup window features (width, height, etc.).
|
|
340
|
+
* Default: 'width=420,height=720,scrollbars=yes,resizable=yes'
|
|
341
|
+
*/
|
|
342
|
+
popupFeatures?: string;
|
|
343
|
+
/**
|
|
344
|
+
* Force a specific transport instead of auto-detecting.
|
|
345
|
+
* Useful for testing or explicit control.
|
|
346
|
+
*/
|
|
347
|
+
forceTransport?: DetectedTransport;
|
|
348
|
+
}
|
|
349
|
+
interface AutoConnectResult {
|
|
350
|
+
/** Connected client — use for queries, intents, and events. */
|
|
351
|
+
client: ConnectClient;
|
|
352
|
+
/** Connection result with session info and identity. */
|
|
353
|
+
connection: ConnectResult;
|
|
354
|
+
/** Which transport was selected. */
|
|
355
|
+
transport: DetectedTransport;
|
|
356
|
+
/**
|
|
357
|
+
* Disconnect and clean up all resources.
|
|
358
|
+
* For popup mode, also closes the popup window.
|
|
359
|
+
*/
|
|
360
|
+
disconnect: () => Promise<void>;
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Auto-detect the best transport and connect to the Sphere wallet.
|
|
364
|
+
*
|
|
365
|
+
* @throws Error if connection fails or is rejected by the wallet.
|
|
366
|
+
*/
|
|
367
|
+
declare function autoConnect(config: AutoConnectConfig): Promise<AutoConnectResult>;
|
|
368
|
+
|
|
369
|
+
export { type AutoConnectConfig, type AutoConnectResult, type ChromeMessagingApi, type DetectedTransport, EXT_MSG_TO_CLIENT, EXT_MSG_TO_HOST, type ExtensionConnectEnvelope, ExtensionTransport, type PostMessageClientOptions, type PostMessageHostOptions, PostMessageTransport, autoConnect, detectTransport, hasExtension, isExtensionConnectEnvelope, isInIframe };
|