@satoshai/kit 0.6.0 → 0.8.0
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 +193 -16
- package/dist/index.cjs +227 -19
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +446 -7
- package/dist/index.d.ts +446 -7
- package/dist/index.js +223 -20
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -3,35 +3,163 @@ import { ClarityValue, TupleCV, PostCondition, PostConditionMode } from '@stacks
|
|
|
3
3
|
import { ClarityAbi, ExtractAbiFunctionNames, Pretty, ExtractAbiFunction, ClarityAbiArgToPrimitiveTypeValue } from 'clarity-abitype';
|
|
4
4
|
export { ClarityAbi } from 'clarity-abitype';
|
|
5
5
|
|
|
6
|
+
/** Discriminated type for narrowing caught errors to `BaseError`. */
|
|
7
|
+
type BaseErrorType = BaseError & {
|
|
8
|
+
name: 'StacksKitError';
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Base error class for all `@satoshai/kit` errors.
|
|
12
|
+
*
|
|
13
|
+
* All errors thrown by hooks extend this class, so you can catch them with
|
|
14
|
+
* `error instanceof BaseError` and use {@link BaseError.walk | walk()} to
|
|
15
|
+
* traverse the cause chain.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { BaseError } from '@satoshai/kit';
|
|
20
|
+
*
|
|
21
|
+
* try {
|
|
22
|
+
* await signMessageAsync({ message: 'hello' });
|
|
23
|
+
* } catch (err) {
|
|
24
|
+
* if (err instanceof BaseError) {
|
|
25
|
+
* console.log(err.shortMessage); // human-readable summary
|
|
26
|
+
* console.log(err.walk()); // root cause
|
|
27
|
+
* }
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
declare class BaseError extends Error {
|
|
32
|
+
name: string;
|
|
33
|
+
/** Short, human-readable error summary without details or cause chain. */
|
|
34
|
+
shortMessage: string;
|
|
35
|
+
constructor(shortMessage: string, options?: {
|
|
36
|
+
cause?: Error;
|
|
37
|
+
details?: string;
|
|
38
|
+
});
|
|
39
|
+
/**
|
|
40
|
+
* Walk the error cause chain. If `fn` is provided, returns the first error
|
|
41
|
+
* where `fn` returns `true`; otherwise returns the root cause.
|
|
42
|
+
*/
|
|
43
|
+
walk(fn?: (err: unknown) => boolean): unknown;
|
|
44
|
+
}
|
|
45
|
+
/** Discriminated type for narrowing to `WalletNotConnectedError`. */
|
|
46
|
+
type WalletNotConnectedErrorType = WalletNotConnectedError & {
|
|
47
|
+
name: 'WalletNotConnectedError';
|
|
48
|
+
};
|
|
49
|
+
/** Thrown when a mutation hook is called before a wallet is connected. */
|
|
50
|
+
declare class WalletNotConnectedError extends BaseError {
|
|
51
|
+
name: string;
|
|
52
|
+
constructor();
|
|
53
|
+
}
|
|
54
|
+
/** Discriminated type for narrowing to `WalletNotFoundError`. */
|
|
55
|
+
type WalletNotFoundErrorType = WalletNotFoundError & {
|
|
56
|
+
name: 'WalletNotFoundError';
|
|
57
|
+
};
|
|
58
|
+
/** Thrown when a wallet's browser extension is not installed (e.g. OKX). */
|
|
59
|
+
declare class WalletNotFoundError extends BaseError {
|
|
60
|
+
name: string;
|
|
61
|
+
/** The wallet ID that was not found. */
|
|
62
|
+
wallet: string;
|
|
63
|
+
constructor({ wallet }: {
|
|
64
|
+
wallet: string;
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
/** Discriminated type for narrowing to `UnsupportedMethodError`. */
|
|
68
|
+
type UnsupportedMethodErrorType = UnsupportedMethodError & {
|
|
69
|
+
name: 'UnsupportedMethodError';
|
|
70
|
+
};
|
|
71
|
+
/** Thrown when a wallet does not support the requested RPC method (e.g. OKX + `stx_signStructuredMessage`). */
|
|
72
|
+
declare class UnsupportedMethodError extends BaseError {
|
|
73
|
+
name: string;
|
|
74
|
+
/** The SIP-030 method name that is not supported. */
|
|
75
|
+
method: string;
|
|
76
|
+
/** The wallet that does not support the method. */
|
|
77
|
+
wallet: string;
|
|
78
|
+
constructor({ method, wallet }: {
|
|
79
|
+
method: string;
|
|
80
|
+
wallet: string;
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
/** Discriminated type for narrowing to `WalletRequestError`. */
|
|
84
|
+
type WalletRequestErrorType = WalletRequestError & {
|
|
85
|
+
name: 'WalletRequestError';
|
|
86
|
+
};
|
|
87
|
+
/** Thrown when a wallet RPC request fails (user rejection, timeout, etc.). The original error is attached as `cause`. */
|
|
88
|
+
declare class WalletRequestError extends BaseError {
|
|
89
|
+
name: string;
|
|
90
|
+
/** The SIP-030 method name that failed. */
|
|
91
|
+
method: string;
|
|
92
|
+
/** The wallet that returned the error. */
|
|
93
|
+
wallet: string;
|
|
94
|
+
constructor({ method, wallet, cause }: {
|
|
95
|
+
method: string;
|
|
96
|
+
wallet: string;
|
|
97
|
+
cause: Error;
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** All wallet IDs supported by `@satoshai/kit`. */
|
|
6
102
|
declare const SUPPORTED_STACKS_WALLETS: readonly ["xverse", "leather", "asigna", "fordefi", "wallet-connect", "okx"];
|
|
103
|
+
/** Union of supported wallet identifiers. */
|
|
7
104
|
type SupportedStacksWallet = (typeof SUPPORTED_STACKS_WALLETS)[number];
|
|
8
105
|
|
|
106
|
+
/** Stacks network identifier. */
|
|
9
107
|
type StacksChain = 'mainnet' | 'testnet';
|
|
108
|
+
/** Status of a mutation hook (`useConnect`, `useSignMessage`, etc.). */
|
|
10
109
|
type MutationStatus = 'idle' | 'pending' | 'error' | 'success';
|
|
110
|
+
/** Metadata passed to the WalletConnect relay for dApp identification. */
|
|
11
111
|
interface WalletConnectMetadata {
|
|
12
112
|
name: string;
|
|
13
113
|
description: string;
|
|
14
114
|
url: string;
|
|
15
115
|
icons: string[];
|
|
16
116
|
}
|
|
117
|
+
/** Optional callbacks for {@link useConnect}. */
|
|
17
118
|
interface ConnectOptions {
|
|
119
|
+
/** Called with the connected address and wallet ID on success. */
|
|
18
120
|
onSuccess?: (address: string, provider: SupportedStacksWallet) => void;
|
|
121
|
+
/** Called when the connection fails or is rejected. */
|
|
19
122
|
onError?: (error: Error) => void;
|
|
20
123
|
}
|
|
124
|
+
/** Props for the {@link StacksWalletProvider} component. */
|
|
21
125
|
interface StacksWalletProviderProps {
|
|
22
126
|
children: React.ReactNode;
|
|
127
|
+
/**
|
|
128
|
+
* Wallets to enable. Defaults to all supported wallets.
|
|
129
|
+
*
|
|
130
|
+
* **Tip:** Define this array outside your component or memoize it to keep
|
|
131
|
+
* the reference stable across renders.
|
|
132
|
+
*/
|
|
23
133
|
wallets?: SupportedStacksWallet[];
|
|
24
134
|
/** Show @stacks/connect's built-in wallet selection modal when `connect()` is called without a `providerId`. Defaults to `true`. Set to `false` to manage wallet selection yourself (headless). */
|
|
25
135
|
connectModal?: boolean;
|
|
136
|
+
/**
|
|
137
|
+
* WalletConnect configuration. Required when `wallets` includes `'wallet-connect'`.
|
|
138
|
+
*
|
|
139
|
+
* **Tip:** Define this object outside your component or memoize it to keep
|
|
140
|
+
* the reference stable across renders.
|
|
141
|
+
*/
|
|
26
142
|
walletConnect?: {
|
|
143
|
+
/** WalletConnect Cloud project ID from https://cloud.walletconnect.com. */
|
|
27
144
|
projectId: string;
|
|
145
|
+
/** Override default dApp metadata shown in the wallet. */
|
|
28
146
|
metadata?: Partial<WalletConnectMetadata>;
|
|
147
|
+
/** Stacks chains to request. Defaults to `['mainnet']`. */
|
|
29
148
|
chains?: StacksChain[];
|
|
30
149
|
};
|
|
150
|
+
/** Called after a wallet successfully connects. */
|
|
31
151
|
onConnect?: (provider: SupportedStacksWallet, address: string) => void;
|
|
152
|
+
/** Called when the connected account changes (e.g. Xverse account switch, WalletConnect account change). */
|
|
32
153
|
onAddressChange?: (newAddress: string) => void;
|
|
154
|
+
/** Called when the wallet disconnects (user-initiated or session expiry). */
|
|
33
155
|
onDisconnect?: () => void;
|
|
34
156
|
}
|
|
157
|
+
/**
|
|
158
|
+
* Discriminated union representing the wallet connection state.
|
|
159
|
+
*
|
|
160
|
+
* When `status` is `'connected'`, `address` and `provider` are guaranteed
|
|
161
|
+
* to be defined.
|
|
162
|
+
*/
|
|
35
163
|
type WalletState = {
|
|
36
164
|
status: 'disconnected';
|
|
37
165
|
address: undefined;
|
|
@@ -45,20 +173,54 @@ type WalletState = {
|
|
|
45
173
|
address: string;
|
|
46
174
|
provider: SupportedStacksWallet;
|
|
47
175
|
};
|
|
176
|
+
/** Metadata for a single wallet returned by {@link useWallets}. */
|
|
48
177
|
interface WalletInfo {
|
|
178
|
+
/** Wallet identifier used with `connect(id)`. */
|
|
49
179
|
id: SupportedStacksWallet;
|
|
180
|
+
/** Human-readable wallet name. */
|
|
50
181
|
name: string;
|
|
182
|
+
/** Wallet icon as a data URI. */
|
|
51
183
|
icon: string;
|
|
184
|
+
/** URL to download/install the wallet extension. */
|
|
52
185
|
webUrl: string;
|
|
186
|
+
/** `true` when the wallet extension is detected or WalletConnect is configured. */
|
|
53
187
|
available: boolean;
|
|
54
188
|
}
|
|
189
|
+
/** Full context value exposed by `StacksWalletProvider`. Extends {@link WalletState} with actions and wallet list. */
|
|
55
190
|
type WalletContextValue = WalletState & {
|
|
191
|
+
/** Connect to a wallet. Pass a wallet ID to bypass the modal, or call with no args to show the `@stacks/connect` modal. */
|
|
56
192
|
connect: (providerId?: SupportedStacksWallet, options?: ConnectOptions) => Promise<void>;
|
|
193
|
+
/** Disconnect the current wallet and clear persisted state. */
|
|
57
194
|
disconnect: (callback?: () => void) => void;
|
|
195
|
+
/** Reset a stuck connecting state back to idle. */
|
|
58
196
|
reset: () => void;
|
|
197
|
+
/** All configured wallets with availability status. */
|
|
59
198
|
wallets: WalletInfo[];
|
|
60
199
|
};
|
|
61
200
|
|
|
201
|
+
/**
|
|
202
|
+
* React context provider that manages wallet connection state for all `@satoshai/kit` hooks.
|
|
203
|
+
*
|
|
204
|
+
* Wrap your app (or the subtree that needs wallet access) with this provider.
|
|
205
|
+
* All hooks (`useConnect`, `useAddress`, `useWriteContract`, etc.) must be
|
|
206
|
+
* rendered inside this provider.
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* ```tsx
|
|
210
|
+
* import { StacksWalletProvider } from '@satoshai/kit';
|
|
211
|
+
*
|
|
212
|
+
* const wallets = ['xverse', 'leather', 'wallet-connect'] as const;
|
|
213
|
+
* const wc = { projectId: 'YOUR_PROJECT_ID' };
|
|
214
|
+
*
|
|
215
|
+
* function App() {
|
|
216
|
+
* return (
|
|
217
|
+
* <StacksWalletProvider wallets={[...wallets]} walletConnect={wc}>
|
|
218
|
+
* <YourApp />
|
|
219
|
+
* </StacksWalletProvider>
|
|
220
|
+
* );
|
|
221
|
+
* }
|
|
222
|
+
* ```
|
|
223
|
+
*/
|
|
62
224
|
declare const StacksWalletProvider: ({ children, wallets, connectModal, walletConnect, onConnect, onAddressChange, onDisconnect, }: StacksWalletProviderProps) => react_jsx_runtime.JSX.Element;
|
|
63
225
|
|
|
64
226
|
type UseAddressReturn = {
|
|
@@ -74,8 +236,48 @@ type UseAddressReturn = {
|
|
|
74
236
|
isDisconnected: false;
|
|
75
237
|
provider: SupportedStacksWallet;
|
|
76
238
|
};
|
|
239
|
+
/**
|
|
240
|
+
* Read the connected wallet's address and connection status.
|
|
241
|
+
*
|
|
242
|
+
* Returns a discriminated union — when `isConnected` is `true`, `address`
|
|
243
|
+
* and `provider` are guaranteed to be defined (no null checks needed).
|
|
244
|
+
*
|
|
245
|
+
* @example
|
|
246
|
+
* ```ts
|
|
247
|
+
* const { address, isConnected, provider } = useAddress();
|
|
248
|
+
*
|
|
249
|
+
* if (isConnected) {
|
|
250
|
+
* console.log(address); // 'SP...' — narrowed to string
|
|
251
|
+
* console.log(provider); // 'xverse' | 'leather' | ...
|
|
252
|
+
* }
|
|
253
|
+
* ```
|
|
254
|
+
*/
|
|
77
255
|
declare const useAddress: () => UseAddressReturn;
|
|
78
256
|
|
|
257
|
+
/**
|
|
258
|
+
* Connect to a Stacks wallet.
|
|
259
|
+
*
|
|
260
|
+
* Returns a mutation-style object with `connect`, `reset`, and status flags.
|
|
261
|
+
* Call `connect()` with no args to open the `@stacks/connect` wallet modal,
|
|
262
|
+
* or pass a specific wallet ID (e.g. `connect('xverse')`) to connect directly.
|
|
263
|
+
*
|
|
264
|
+
* @example
|
|
265
|
+
* ```ts
|
|
266
|
+
* const { connect, reset, isPending, isSuccess, error } = useConnect();
|
|
267
|
+
*
|
|
268
|
+
* // Modal mode (default)
|
|
269
|
+
* await connect();
|
|
270
|
+
*
|
|
271
|
+
* // Direct mode
|
|
272
|
+
* await connect('leather', {
|
|
273
|
+
* onSuccess: (address, provider) => console.log(address),
|
|
274
|
+
* onError: (err) => console.error(err),
|
|
275
|
+
* });
|
|
276
|
+
*
|
|
277
|
+
* // Cancel a stuck connection (e.g. OKX popup dismissed)
|
|
278
|
+
* reset();
|
|
279
|
+
* ```
|
|
280
|
+
*/
|
|
79
281
|
declare const useConnect: () => {
|
|
80
282
|
connect: (providerId?: SupportedStacksWallet, options?: ConnectOptions) => Promise<void>;
|
|
81
283
|
reset: () => void;
|
|
@@ -87,6 +289,20 @@ declare const useConnect: () => {
|
|
|
87
289
|
status: MutationStatus;
|
|
88
290
|
};
|
|
89
291
|
|
|
292
|
+
/**
|
|
293
|
+
* Disconnect the currently connected wallet.
|
|
294
|
+
*
|
|
295
|
+
* Clears wallet state, removes the persisted session from localStorage,
|
|
296
|
+
* and resets the provider context back to `disconnected`.
|
|
297
|
+
*
|
|
298
|
+
* @example
|
|
299
|
+
* ```ts
|
|
300
|
+
* const { disconnect, isSuccess } = useDisconnect();
|
|
301
|
+
*
|
|
302
|
+
* disconnect();
|
|
303
|
+
* disconnect(() => navigate('/'));
|
|
304
|
+
* ```
|
|
305
|
+
*/
|
|
90
306
|
declare const useDisconnect: () => {
|
|
91
307
|
disconnect: (callback?: () => void) => void;
|
|
92
308
|
reset: () => void;
|
|
@@ -98,25 +314,51 @@ declare const useDisconnect: () => {
|
|
|
98
314
|
status: MutationStatus;
|
|
99
315
|
};
|
|
100
316
|
|
|
317
|
+
/** Variables for {@link useSignMessage}. */
|
|
101
318
|
interface SignMessageVariables {
|
|
319
|
+
/** The plaintext message to sign. */
|
|
102
320
|
message: string;
|
|
321
|
+
/** Optional public key hint for wallets that manage multiple keys. */
|
|
103
322
|
publicKey?: string;
|
|
104
323
|
}
|
|
324
|
+
/** Successful result from {@link useSignMessage}. */
|
|
105
325
|
interface SignMessageData {
|
|
326
|
+
/** The public key that produced the signature. */
|
|
106
327
|
publicKey: string;
|
|
328
|
+
/** The hex-encoded signature. */
|
|
107
329
|
signature: string;
|
|
108
330
|
}
|
|
331
|
+
/** Callback options for the fire-and-forget `signMessage()` variant. */
|
|
109
332
|
interface SignMessageOptions {
|
|
110
333
|
onSuccess?: (data: SignMessageData) => void;
|
|
111
334
|
onError?: (error: Error) => void;
|
|
112
335
|
onSettled?: (data: SignMessageData | undefined, error: Error | null) => void;
|
|
113
336
|
}
|
|
337
|
+
/**
|
|
338
|
+
* Sign an arbitrary plaintext message with the connected wallet.
|
|
339
|
+
*
|
|
340
|
+
* Provides both a callback-style `signMessage()` and a promise-based
|
|
341
|
+
* `signMessageAsync()`, plus mutation status flags.
|
|
342
|
+
*
|
|
343
|
+
* @example
|
|
344
|
+
* ```ts
|
|
345
|
+
* const { signMessageAsync, isPending } = useSignMessage();
|
|
346
|
+
*
|
|
347
|
+
* const { publicKey, signature } = await signMessageAsync({
|
|
348
|
+
* message: 'Hello Stacks',
|
|
349
|
+
* });
|
|
350
|
+
* ```
|
|
351
|
+
*
|
|
352
|
+
* @throws {WalletNotConnectedError} If no wallet is connected.
|
|
353
|
+
* @throws {WalletNotFoundError} If OKX extension is not installed.
|
|
354
|
+
* @throws {WalletRequestError} If the wallet rejects or fails the request.
|
|
355
|
+
*/
|
|
114
356
|
declare const useSignMessage: () => {
|
|
115
357
|
signMessage: (variables: SignMessageVariables, options?: SignMessageOptions) => void;
|
|
116
358
|
signMessageAsync: (variables: SignMessageVariables) => Promise<SignMessageData>;
|
|
117
359
|
reset: () => void;
|
|
118
360
|
data: SignMessageData | undefined;
|
|
119
|
-
error:
|
|
361
|
+
error: BaseError | null;
|
|
120
362
|
isError: boolean;
|
|
121
363
|
isIdle: boolean;
|
|
122
364
|
isPending: boolean;
|
|
@@ -124,25 +366,52 @@ declare const useSignMessage: () => {
|
|
|
124
366
|
status: MutationStatus;
|
|
125
367
|
};
|
|
126
368
|
|
|
369
|
+
/** Variables for {@link useSignStructuredMessage}. */
|
|
127
370
|
interface SignStructuredMessageVariables {
|
|
371
|
+
/** The structured Clarity value to sign. */
|
|
128
372
|
message: ClarityValue;
|
|
373
|
+
/** SIP-018 domain tuple (name, version, chain-id). */
|
|
129
374
|
domain: TupleCV;
|
|
130
375
|
}
|
|
376
|
+
/** Successful result from {@link useSignStructuredMessage}. */
|
|
131
377
|
interface SignStructuredMessageData {
|
|
132
378
|
publicKey: string;
|
|
133
379
|
signature: string;
|
|
134
380
|
}
|
|
381
|
+
/** Callback options for the fire-and-forget `signStructuredMessage()` variant. */
|
|
135
382
|
interface SignStructuredMessageOptions {
|
|
136
383
|
onSuccess?: (data: SignStructuredMessageData) => void;
|
|
137
384
|
onError?: (error: Error) => void;
|
|
138
385
|
onSettled?: (data: SignStructuredMessageData | undefined, error: Error | null) => void;
|
|
139
386
|
}
|
|
387
|
+
/**
|
|
388
|
+
* Sign SIP-018 structured data with the connected wallet.
|
|
389
|
+
*
|
|
390
|
+
* Structured messages include a typed domain separator and a Clarity value
|
|
391
|
+
* body, enabling verifiable off-chain signatures that are replay-safe.
|
|
392
|
+
*
|
|
393
|
+
* @example
|
|
394
|
+
* ```ts
|
|
395
|
+
* import { tupleCV, stringAsciiCV, uintCV } from '@stacks/transactions';
|
|
396
|
+
*
|
|
397
|
+
* const { signStructuredMessageAsync } = useSignStructuredMessage();
|
|
398
|
+
*
|
|
399
|
+
* const { signature } = await signStructuredMessageAsync({
|
|
400
|
+
* domain: tupleCV({ name: stringAsciiCV('MyApp'), version: stringAsciiCV('1.0'), 'chain-id': uintCV(1) }),
|
|
401
|
+
* message: tupleCV({ action: stringAsciiCV('authorize') }),
|
|
402
|
+
* });
|
|
403
|
+
* ```
|
|
404
|
+
*
|
|
405
|
+
* @throws {WalletNotConnectedError} If no wallet is connected.
|
|
406
|
+
* @throws {UnsupportedMethodError} If the wallet does not support structured signing (OKX).
|
|
407
|
+
* @throws {WalletRequestError} If the wallet rejects or fails the request.
|
|
408
|
+
*/
|
|
140
409
|
declare const useSignStructuredMessage: () => {
|
|
141
410
|
signStructuredMessage: (variables: SignStructuredMessageVariables, options?: SignStructuredMessageOptions) => void;
|
|
142
411
|
signStructuredMessageAsync: (variables: SignStructuredMessageVariables) => Promise<SignStructuredMessageData>;
|
|
143
412
|
reset: () => void;
|
|
144
413
|
data: SignStructuredMessageData | undefined;
|
|
145
|
-
error:
|
|
414
|
+
error: BaseError | null;
|
|
146
415
|
isError: boolean;
|
|
147
416
|
isIdle: boolean;
|
|
148
417
|
isPending: boolean;
|
|
@@ -150,25 +419,52 @@ declare const useSignStructuredMessage: () => {
|
|
|
150
419
|
status: MutationStatus;
|
|
151
420
|
};
|
|
152
421
|
|
|
422
|
+
/** Variables for {@link useSignTransaction}. */
|
|
153
423
|
interface SignTransactionVariables {
|
|
424
|
+
/** Hex-encoded serialized transaction to sign. */
|
|
154
425
|
transaction: string;
|
|
426
|
+
/** Whether to broadcast the signed transaction. Defaults to the wallet's behavior. */
|
|
155
427
|
broadcast?: boolean;
|
|
156
428
|
}
|
|
429
|
+
/** Successful result from {@link useSignTransaction}. */
|
|
157
430
|
interface SignTransactionData {
|
|
431
|
+
/** The signed, hex-encoded transaction. */
|
|
158
432
|
transaction: string;
|
|
433
|
+
/** Transaction ID, present when the wallet broadcasts it. */
|
|
159
434
|
txid?: string;
|
|
160
435
|
}
|
|
436
|
+
/** Callback options for the fire-and-forget `signTransaction()` variant. */
|
|
161
437
|
interface SignTransactionOptions {
|
|
162
438
|
onSuccess?: (data: SignTransactionData) => void;
|
|
163
439
|
onError?: (error: Error) => void;
|
|
164
440
|
onSettled?: (data: SignTransactionData | undefined, error: Error | null) => void;
|
|
165
441
|
}
|
|
442
|
+
/**
|
|
443
|
+
* Sign a serialized Stacks transaction without automatic broadcast.
|
|
444
|
+
*
|
|
445
|
+
* Useful for sponsored transaction flows where a separate service pays the
|
|
446
|
+
* fee and broadcasts the transaction.
|
|
447
|
+
*
|
|
448
|
+
* @example
|
|
449
|
+
* ```ts
|
|
450
|
+
* const { signTransactionAsync } = useSignTransaction();
|
|
451
|
+
*
|
|
452
|
+
* const { transaction } = await signTransactionAsync({
|
|
453
|
+
* transaction: '0x0100...',
|
|
454
|
+
* broadcast: false,
|
|
455
|
+
* });
|
|
456
|
+
* ```
|
|
457
|
+
*
|
|
458
|
+
* @throws {WalletNotConnectedError} If no wallet is connected.
|
|
459
|
+
* @throws {UnsupportedMethodError} If the wallet does not support raw signing (OKX).
|
|
460
|
+
* @throws {WalletRequestError} If the wallet rejects or fails the request.
|
|
461
|
+
*/
|
|
166
462
|
declare const useSignTransaction: () => {
|
|
167
463
|
signTransaction: (variables: SignTransactionVariables, options?: SignTransactionOptions) => void;
|
|
168
464
|
signTransactionAsync: (variables: SignTransactionVariables) => Promise<SignTransactionData>;
|
|
169
465
|
reset: () => void;
|
|
170
466
|
data: SignTransactionData | undefined;
|
|
171
|
-
error:
|
|
467
|
+
error: BaseError | null;
|
|
172
468
|
isError: boolean;
|
|
173
469
|
isIdle: boolean;
|
|
174
470
|
isPending: boolean;
|
|
@@ -176,24 +472,52 @@ declare const useSignTransaction: () => {
|
|
|
176
472
|
status: MutationStatus;
|
|
177
473
|
};
|
|
178
474
|
|
|
475
|
+
/** Variables for {@link useTransferSTX}. */
|
|
179
476
|
interface TransferSTXVariables {
|
|
477
|
+
/** Recipient Stacks address (`SP...` or `ST...`). */
|
|
180
478
|
recipient: string;
|
|
479
|
+
/** Amount in microSTX. Accepts `bigint`, `number`, or numeric `string`. */
|
|
181
480
|
amount: bigint | number | string;
|
|
481
|
+
/** Optional memo string attached to the transfer. */
|
|
182
482
|
memo?: string;
|
|
483
|
+
/** Custom fee in microSTX. Omit to let the wallet estimate. */
|
|
183
484
|
fee?: bigint | number | string;
|
|
485
|
+
/** Custom nonce. Omit to let the wallet manage. */
|
|
184
486
|
nonce?: bigint | number | string;
|
|
185
487
|
}
|
|
488
|
+
/** Callback options for the fire-and-forget `transferSTX()` variant. */
|
|
186
489
|
interface TransferSTXOptions {
|
|
187
490
|
onSuccess?: (txid: string) => void;
|
|
188
491
|
onError?: (error: Error) => void;
|
|
189
492
|
onSettled?: (txid: string | undefined, error: Error | null) => void;
|
|
190
493
|
}
|
|
494
|
+
/**
|
|
495
|
+
* Transfer native STX tokens to a recipient address.
|
|
496
|
+
*
|
|
497
|
+
* Returns the broadcast transaction ID on success. Supports all 6 wallets
|
|
498
|
+
* (OKX uses its proprietary API internally).
|
|
499
|
+
*
|
|
500
|
+
* @example
|
|
501
|
+
* ```ts
|
|
502
|
+
* const { transferSTXAsync, isPending } = useTransferSTX();
|
|
503
|
+
*
|
|
504
|
+
* const txid = await transferSTXAsync({
|
|
505
|
+
* recipient: 'SP2...',
|
|
506
|
+
* amount: 1_000_000n, // 1 STX
|
|
507
|
+
* memo: 'coffee',
|
|
508
|
+
* });
|
|
509
|
+
* ```
|
|
510
|
+
*
|
|
511
|
+
* @throws {WalletNotConnectedError} If no wallet is connected.
|
|
512
|
+
* @throws {WalletNotFoundError} If OKX extension is not installed.
|
|
513
|
+
* @throws {WalletRequestError} If the wallet rejects or fails the request.
|
|
514
|
+
*/
|
|
191
515
|
declare const useTransferSTX: () => {
|
|
192
516
|
transferSTX: (variables: TransferSTXVariables, options?: TransferSTXOptions) => void;
|
|
193
517
|
transferSTXAsync: (variables: TransferSTXVariables) => Promise<string>;
|
|
194
518
|
reset: () => void;
|
|
195
519
|
data: string | undefined;
|
|
196
|
-
error:
|
|
520
|
+
error: BaseError | null;
|
|
197
521
|
isError: boolean;
|
|
198
522
|
isIdle: boolean;
|
|
199
523
|
isPending: boolean;
|
|
@@ -210,8 +534,11 @@ type PublicFunctionArgs<TAbi extends ClarityAbi, TFn extends ExtractAbiFunctionN
|
|
|
210
534
|
[K in ExtractAbiFunction<TAbi, TFn, 'public'>['args'][number] as K['name']]: ClarityAbiArgToPrimitiveTypeValue<K>;
|
|
211
535
|
}>;
|
|
212
536
|
|
|
537
|
+
/** Post-condition configuration for contract calls and STX transfers. */
|
|
213
538
|
interface PostConditionConfig {
|
|
539
|
+
/** Array of post-conditions that must be satisfied for the transaction to succeed. */
|
|
214
540
|
postConditions: PostCondition[];
|
|
541
|
+
/** Whether to allow or deny any asset transfers not covered by `postConditions`. */
|
|
215
542
|
mode: PostConditionMode;
|
|
216
543
|
}
|
|
217
544
|
/** Typed mode: ABI present, args is a named object with autocomplete. */
|
|
@@ -233,6 +560,7 @@ interface UntypedWriteContractVariables {
|
|
|
233
560
|
}
|
|
234
561
|
/** Backward-compatible alias for the untyped variant. */
|
|
235
562
|
type WriteContractVariables = UntypedWriteContractVariables;
|
|
563
|
+
/** Callback options for the fire-and-forget `writeContract()` variant. */
|
|
236
564
|
interface WriteContractOptions {
|
|
237
565
|
onSuccess?: (txHash: string) => void;
|
|
238
566
|
onError?: (error: Error) => void;
|
|
@@ -249,12 +577,52 @@ interface WriteContractFn {
|
|
|
249
577
|
(variables: UntypedWriteContractVariables, options?: WriteContractOptions): void;
|
|
250
578
|
}
|
|
251
579
|
|
|
580
|
+
/**
|
|
581
|
+
* Call a public function on a Clarity smart contract.
|
|
582
|
+
*
|
|
583
|
+
* Supports two modes:
|
|
584
|
+
* - **Typed** (with ABI) — pass an ABI object for autocomplete on `functionName` and `args`.
|
|
585
|
+
* - **Untyped** — pass `ClarityValue[]` directly as `args`.
|
|
586
|
+
*
|
|
587
|
+
* @example
|
|
588
|
+
* ```ts
|
|
589
|
+
* import { Pc, PostConditionMode } from '@stacks/transactions';
|
|
590
|
+
*
|
|
591
|
+
* const { writeContractAsync } = useWriteContract();
|
|
592
|
+
*
|
|
593
|
+
* // Untyped mode
|
|
594
|
+
* const txid = await writeContractAsync({
|
|
595
|
+
* address: 'SP...',
|
|
596
|
+
* contract: 'my-contract',
|
|
597
|
+
* functionName: 'transfer',
|
|
598
|
+
* args: [uintCV(100)],
|
|
599
|
+
* pc: {
|
|
600
|
+
* postConditions: [Pc.principal('SP...').willSendLte(100n).ustx()],
|
|
601
|
+
* mode: PostConditionMode.Deny,
|
|
602
|
+
* },
|
|
603
|
+
* });
|
|
604
|
+
*
|
|
605
|
+
* // Typed mode (with ABI — enables autocomplete)
|
|
606
|
+
* const txid = await writeContractAsync({
|
|
607
|
+
* abi: myContractAbi,
|
|
608
|
+
* address: 'SP...',
|
|
609
|
+
* contract: 'my-contract',
|
|
610
|
+
* functionName: 'transfer', // autocompleted from ABI
|
|
611
|
+
* args: { amount: 100n }, // named args, type-checked
|
|
612
|
+
* pc: { postConditions: [], mode: PostConditionMode.Deny },
|
|
613
|
+
* });
|
|
614
|
+
* ```
|
|
615
|
+
*
|
|
616
|
+
* @throws {WalletNotConnectedError} If no wallet is connected.
|
|
617
|
+
* @throws {WalletNotFoundError} If OKX extension is not installed.
|
|
618
|
+
* @throws {WalletRequestError} If the wallet rejects or fails the request.
|
|
619
|
+
*/
|
|
252
620
|
declare const useWriteContract: () => {
|
|
253
621
|
writeContract: WriteContractFn;
|
|
254
622
|
writeContractAsync: WriteContractAsyncFn;
|
|
255
623
|
reset: () => void;
|
|
256
624
|
data: string | undefined;
|
|
257
|
-
error:
|
|
625
|
+
error: BaseError | null;
|
|
258
626
|
isError: boolean;
|
|
259
627
|
isIdle: boolean;
|
|
260
628
|
isPending: boolean;
|
|
@@ -262,29 +630,100 @@ declare const useWriteContract: () => {
|
|
|
262
630
|
status: MutationStatus;
|
|
263
631
|
};
|
|
264
632
|
|
|
633
|
+
/**
|
|
634
|
+
* Resolve a BNS v2 primary name for a Stacks address.
|
|
635
|
+
*
|
|
636
|
+
* Returns `null` when no name is registered or the address is undefined.
|
|
637
|
+
* Automatically detects mainnet/testnet from the address prefix.
|
|
638
|
+
*
|
|
639
|
+
* @param address - Stacks address to resolve (`SP...` or `ST...`).
|
|
640
|
+
*
|
|
641
|
+
* @example
|
|
642
|
+
* ```ts
|
|
643
|
+
* const { bnsName, isLoading } = useBnsName('SP2...');
|
|
644
|
+
* // bnsName = 'satoshi.btc' | null
|
|
645
|
+
* ```
|
|
646
|
+
*/
|
|
265
647
|
declare const useBnsName: (address?: string) => {
|
|
266
648
|
bnsName: string | null;
|
|
267
649
|
isLoading: boolean;
|
|
268
650
|
};
|
|
269
651
|
|
|
652
|
+
/**
|
|
653
|
+
* List all configured wallets with availability status.
|
|
654
|
+
*
|
|
655
|
+
* Each wallet includes its `id`, display `name`, `icon` (data URI), `webUrl`
|
|
656
|
+
* (install link), and whether it's `available` (extension detected or
|
|
657
|
+
* WalletConnect configured).
|
|
658
|
+
*
|
|
659
|
+
* @example
|
|
660
|
+
* ```ts
|
|
661
|
+
* const { wallets } = useWallets();
|
|
662
|
+
*
|
|
663
|
+
* wallets.map(({ id, name, icon, available }) => (
|
|
664
|
+
* <button key={id} onClick={() => connect(id)} disabled={!available}>
|
|
665
|
+
* <img src={icon} alt={name} width={20} /> {name}
|
|
666
|
+
* </button>
|
|
667
|
+
* ));
|
|
668
|
+
* ```
|
|
669
|
+
*/
|
|
270
670
|
declare const useWallets: () => {
|
|
271
671
|
wallets: WalletInfo[];
|
|
272
672
|
};
|
|
273
673
|
|
|
674
|
+
/**
|
|
675
|
+
* Infer the Stacks network from an address prefix.
|
|
676
|
+
*
|
|
677
|
+
* @param address - A Stacks address starting with `SP`/`SM` (mainnet) or `ST`/`SN` (testnet).
|
|
678
|
+
* @returns `'mainnet'` or `'testnet'`.
|
|
679
|
+
* @throws If the address doesn't start with a known prefix.
|
|
680
|
+
*/
|
|
274
681
|
declare const getNetworkFromAddress: (address: string) => "mainnet" | "testnet";
|
|
275
682
|
|
|
683
|
+
/**
|
|
684
|
+
* Read the persisted wallet session from localStorage.
|
|
685
|
+
*
|
|
686
|
+
* Returns `null` on the server, when no session is stored, or when the
|
|
687
|
+
* stored provider is not in the supported wallets list.
|
|
688
|
+
*/
|
|
276
689
|
declare const getLocalStorageWallet: () => {
|
|
277
690
|
address: string;
|
|
278
691
|
provider: SupportedStacksWallet;
|
|
279
692
|
} | null;
|
|
280
693
|
|
|
694
|
+
/** Result of {@link getStacksWallets}. */
|
|
281
695
|
interface StacksWallets {
|
|
696
|
+
/** All wallets supported by `@satoshai/kit`. */
|
|
282
697
|
supported: SupportedStacksWallet[];
|
|
698
|
+
/** Subset of `supported` whose browser extension is currently detected. */
|
|
283
699
|
installed: SupportedStacksWallet[];
|
|
284
700
|
}
|
|
701
|
+
/**
|
|
702
|
+
* Detect which Stacks wallets are supported and installed.
|
|
703
|
+
*
|
|
704
|
+
* Safe to call on the server — `installed` will contain only `'wallet-connect'`
|
|
705
|
+
* when `window` is undefined.
|
|
706
|
+
*/
|
|
285
707
|
declare const getStacksWallets: () => StacksWallets;
|
|
286
708
|
|
|
287
|
-
/**
|
|
709
|
+
/**
|
|
710
|
+
* Pre-bind ABI, address, and contract name for reuse with `useWriteContract`.
|
|
711
|
+
*
|
|
712
|
+
* Returns the config object as-is but preserves the `const` ABI type, enabling
|
|
713
|
+
* autocomplete on `functionName` and type-checked `args` when spread into
|
|
714
|
+
* `writeContract()`.
|
|
715
|
+
*
|
|
716
|
+
* @example
|
|
717
|
+
* ```ts
|
|
718
|
+
* const pool = createContractConfig({
|
|
719
|
+
* abi: poolAbi,
|
|
720
|
+
* address: 'SP...',
|
|
721
|
+
* contract: 'pool-v1',
|
|
722
|
+
* });
|
|
723
|
+
*
|
|
724
|
+
* writeContract({ ...pool, functionName: 'deposit', args: { amount: 100n }, pc });
|
|
725
|
+
* ```
|
|
726
|
+
*/
|
|
288
727
|
declare function createContractConfig<const TAbi extends ClarityAbi>(config: {
|
|
289
728
|
abi: TAbi;
|
|
290
729
|
address: string;
|
|
@@ -295,4 +734,4 @@ declare function createContractConfig<const TAbi extends ClarityAbi>(config: {
|
|
|
295
734
|
contract: string;
|
|
296
735
|
};
|
|
297
736
|
|
|
298
|
-
export { type ConnectOptions, type MutationStatus, type PostConditionConfig, type PublicFunctionArgs, type PublicFunctionName, SUPPORTED_STACKS_WALLETS, type SignMessageData, type SignMessageOptions, type SignMessageVariables, type SignStructuredMessageData, type SignStructuredMessageOptions, type SignStructuredMessageVariables, type SignTransactionData, type SignTransactionOptions, type SignTransactionVariables, type StacksChain, StacksWalletProvider, type StacksWallets, type SupportedStacksWallet, type TraitReference, type TransferSTXOptions, type TransferSTXVariables, type TypedWriteContractVariables, type UntypedWriteContractVariables, type WalletConnectMetadata, type WalletContextValue, type WalletInfo, type WalletState, type WriteContractOptions, type WriteContractVariables, createContractConfig, getLocalStorageWallet, getNetworkFromAddress, getStacksWallets, useAddress, useBnsName, useConnect, useDisconnect, useSignMessage, useSignStructuredMessage, useSignTransaction, useTransferSTX, useWallets, useWriteContract };
|
|
737
|
+
export { BaseError, type BaseErrorType, type ConnectOptions, type MutationStatus, type PostConditionConfig, type PublicFunctionArgs, type PublicFunctionName, SUPPORTED_STACKS_WALLETS, type SignMessageData, type SignMessageOptions, type SignMessageVariables, type SignStructuredMessageData, type SignStructuredMessageOptions, type SignStructuredMessageVariables, type SignTransactionData, type SignTransactionOptions, type SignTransactionVariables, type StacksChain, StacksWalletProvider, type StacksWallets, type SupportedStacksWallet, type TraitReference, type TransferSTXOptions, type TransferSTXVariables, type TypedWriteContractVariables, UnsupportedMethodError, type UnsupportedMethodErrorType, type UntypedWriteContractVariables, type WalletConnectMetadata, type WalletContextValue, type WalletInfo, WalletNotConnectedError, type WalletNotConnectedErrorType, WalletNotFoundError, type WalletNotFoundErrorType, WalletRequestError, type WalletRequestErrorType, type WalletState, type WriteContractOptions, type WriteContractVariables, createContractConfig, getLocalStorageWallet, getNetworkFromAddress, getStacksWallets, useAddress, useBnsName, useConnect, useDisconnect, useSignMessage, useSignStructuredMessage, useSignTransaction, useTransferSTX, useWallets, useWriteContract };
|