otx-btc-wallet-react 0.1.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 +726 -0
- package/dist/index.d.mts +436 -0
- package/dist/index.d.ts +436 -0
- package/dist/index.js +586 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +573 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +45 -0
- package/src/context.tsx +24 -0
- package/src/hooks/useAccount.ts +74 -0
- package/src/hooks/useConnect.ts +136 -0
- package/src/hooks/useDisconnect.ts +95 -0
- package/src/hooks/useMultiAddress.ts +135 -0
- package/src/hooks/useNetwork.ts +33 -0
- package/src/hooks/useSendTransaction.ts +137 -0
- package/src/hooks/useSignMessage.ts +136 -0
- package/src/hooks/useSignPsbt.ts +144 -0
- package/src/hooks/useSignPsbts.ts +164 -0
- package/src/index.ts +31 -0
- package/src/provider.tsx +70 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
import { ResolvedConfig, AddressType, WalletAccount, BitcoinConnector, ConnectionStatus, BitcoinNetwork, SignPsbtOptions } from 'otx-btc-wallet-core';
|
|
4
|
+
|
|
5
|
+
interface BtcWalletProviderProps {
|
|
6
|
+
/** Configuration created with createConfig() */
|
|
7
|
+
config: ResolvedConfig;
|
|
8
|
+
/** Child components */
|
|
9
|
+
children: ReactNode;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Provider component for otx-btc-wallet
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```tsx
|
|
16
|
+
* import { BtcWalletProvider } from 'otx-btc-wallet-react';
|
|
17
|
+
* import { config } from './config';
|
|
18
|
+
*
|
|
19
|
+
* function App() {
|
|
20
|
+
* return (
|
|
21
|
+
* <BtcWalletProvider config={config}>
|
|
22
|
+
* <YourApp />
|
|
23
|
+
* </BtcWalletProvider>
|
|
24
|
+
* );
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
declare function BtcWalletProvider({ config, children, }: BtcWalletProviderProps): ReactNode;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Context for otx-btc-wallet configuration
|
|
32
|
+
*/
|
|
33
|
+
declare const BtcWalletContext: react.Context<ResolvedConfig | null>;
|
|
34
|
+
/**
|
|
35
|
+
* Hook to access the otx-btc-wallet config
|
|
36
|
+
* @throws Error if used outside of BtcWalletProvider
|
|
37
|
+
*/
|
|
38
|
+
declare function useConfig(): ResolvedConfig;
|
|
39
|
+
|
|
40
|
+
interface UseAccountReturn {
|
|
41
|
+
/** Connected address */
|
|
42
|
+
address: string | undefined;
|
|
43
|
+
/** Connected public key */
|
|
44
|
+
publicKey: string | undefined;
|
|
45
|
+
/** Address type */
|
|
46
|
+
addressType: AddressType | undefined;
|
|
47
|
+
/** Full account object */
|
|
48
|
+
account: WalletAccount | undefined;
|
|
49
|
+
/** Active connector */
|
|
50
|
+
connector: BitcoinConnector | undefined;
|
|
51
|
+
/** Connection status */
|
|
52
|
+
status: ConnectionStatus;
|
|
53
|
+
/** Is connected */
|
|
54
|
+
isConnected: boolean;
|
|
55
|
+
/** Is connecting */
|
|
56
|
+
isConnecting: boolean;
|
|
57
|
+
/** Is disconnected */
|
|
58
|
+
isDisconnected: boolean;
|
|
59
|
+
/** Is reconnecting */
|
|
60
|
+
isReconnecting: boolean;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Hook to get current account state
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```tsx
|
|
67
|
+
* function Profile() {
|
|
68
|
+
* const { address, isConnected, connector } = useAccount();
|
|
69
|
+
*
|
|
70
|
+
* if (!isConnected) return <div>Not connected</div>;
|
|
71
|
+
*
|
|
72
|
+
* return (
|
|
73
|
+
* <div>
|
|
74
|
+
* <p>Address: {address}</p>
|
|
75
|
+
* <p>Connected via {connector?.name}</p>
|
|
76
|
+
* </div>
|
|
77
|
+
* );
|
|
78
|
+
* }
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
declare function useAccount(): UseAccountReturn;
|
|
82
|
+
|
|
83
|
+
type ConnectArgs = {
|
|
84
|
+
connector: BitcoinConnector;
|
|
85
|
+
network?: BitcoinNetwork;
|
|
86
|
+
};
|
|
87
|
+
interface UseConnectReturn {
|
|
88
|
+
/** Connect to a wallet (fire-and-forget) */
|
|
89
|
+
connect: (args: ConnectArgs) => void;
|
|
90
|
+
/** Connect to a wallet (returns promise) */
|
|
91
|
+
connectAsync: (args: ConnectArgs) => Promise<WalletAccount>;
|
|
92
|
+
/** Available connectors */
|
|
93
|
+
connectors: BitcoinConnector[];
|
|
94
|
+
/** Last error */
|
|
95
|
+
error: Error | null;
|
|
96
|
+
/** Is currently connecting */
|
|
97
|
+
isLoading: boolean;
|
|
98
|
+
/** Did connection fail */
|
|
99
|
+
isError: boolean;
|
|
100
|
+
/** Did connection succeed */
|
|
101
|
+
isSuccess: boolean;
|
|
102
|
+
/** Reset state */
|
|
103
|
+
reset: () => void;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Hook to connect to a wallet
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```tsx
|
|
110
|
+
* function ConnectButtons() {
|
|
111
|
+
* const { connect, connectors, isLoading, error } = useConnect();
|
|
112
|
+
*
|
|
113
|
+
* return (
|
|
114
|
+
* <div>
|
|
115
|
+
* {connectors.map((connector) => (
|
|
116
|
+
* <button
|
|
117
|
+
* key={connector.id}
|
|
118
|
+
* onClick={() => connect({ connector })}
|
|
119
|
+
* disabled={isLoading}
|
|
120
|
+
* >
|
|
121
|
+
* Connect {connector.name}
|
|
122
|
+
* </button>
|
|
123
|
+
* ))}
|
|
124
|
+
* {error && <p>Error: {error.message}</p>}
|
|
125
|
+
* </div>
|
|
126
|
+
* );
|
|
127
|
+
* }
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
declare function useConnect(): UseConnectReturn;
|
|
131
|
+
|
|
132
|
+
interface UseDisconnectReturn {
|
|
133
|
+
/** Disconnect from wallet (fire-and-forget) */
|
|
134
|
+
disconnect: () => void;
|
|
135
|
+
/** Disconnect from wallet (returns promise) */
|
|
136
|
+
disconnectAsync: () => Promise<void>;
|
|
137
|
+
/** Is currently disconnecting */
|
|
138
|
+
isLoading: boolean;
|
|
139
|
+
/** Did disconnect fail */
|
|
140
|
+
isError: boolean;
|
|
141
|
+
/** Did disconnect succeed */
|
|
142
|
+
isSuccess: boolean;
|
|
143
|
+
/** Last error */
|
|
144
|
+
error: Error | null;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Hook to disconnect from wallet
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```tsx
|
|
151
|
+
* function DisconnectButton() {
|
|
152
|
+
* const { disconnect, isLoading } = useDisconnect();
|
|
153
|
+
*
|
|
154
|
+
* return (
|
|
155
|
+
* <button onClick={disconnect} disabled={isLoading}>
|
|
156
|
+
* Disconnect
|
|
157
|
+
* </button>
|
|
158
|
+
* );
|
|
159
|
+
* }
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
declare function useDisconnect(): UseDisconnectReturn;
|
|
163
|
+
|
|
164
|
+
interface UseNetworkReturn {
|
|
165
|
+
/** Current network */
|
|
166
|
+
network: BitcoinNetwork;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Hook to get current network
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* ```tsx
|
|
173
|
+
* function NetworkInfo() {
|
|
174
|
+
* const { network } = useNetwork();
|
|
175
|
+
*
|
|
176
|
+
* return <p>Network: {network}</p>;
|
|
177
|
+
* }
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
declare function useNetwork(): UseNetworkReturn;
|
|
181
|
+
|
|
182
|
+
interface UseSendTransactionReturn {
|
|
183
|
+
/** Send transaction (fire-and-forget) */
|
|
184
|
+
sendTransaction: (args: {
|
|
185
|
+
to: string;
|
|
186
|
+
amount: number;
|
|
187
|
+
}) => void;
|
|
188
|
+
/** Send transaction (returns promise with txid) */
|
|
189
|
+
sendTransactionAsync: (args: {
|
|
190
|
+
to: string;
|
|
191
|
+
amount: number;
|
|
192
|
+
}) => Promise<string>;
|
|
193
|
+
/** Transaction ID */
|
|
194
|
+
data: string | undefined;
|
|
195
|
+
/** Last error */
|
|
196
|
+
error: Error | null;
|
|
197
|
+
/** Is sending */
|
|
198
|
+
isLoading: boolean;
|
|
199
|
+
/** Did send fail */
|
|
200
|
+
isError: boolean;
|
|
201
|
+
/** Did send succeed */
|
|
202
|
+
isSuccess: boolean;
|
|
203
|
+
/** Reset state */
|
|
204
|
+
reset: () => void;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Hook to send a Bitcoin transaction
|
|
208
|
+
*
|
|
209
|
+
* @example
|
|
210
|
+
* ```tsx
|
|
211
|
+
* function SendForm() {
|
|
212
|
+
* const { sendTransaction, isLoading, data, error } = useSendTransaction();
|
|
213
|
+
*
|
|
214
|
+
* const handleSend = () => {
|
|
215
|
+
* sendTransaction({ to: 'bc1q...', amount: 10000 }); // 10000 sats
|
|
216
|
+
* };
|
|
217
|
+
*
|
|
218
|
+
* return (
|
|
219
|
+
* <div>
|
|
220
|
+
* <button onClick={handleSend} disabled={isLoading}>
|
|
221
|
+
* Send 10,000 sats
|
|
222
|
+
* </button>
|
|
223
|
+
* {data && <p>TX: {data}</p>}
|
|
224
|
+
* {error && <p>Error: {error.message}</p>}
|
|
225
|
+
* </div>
|
|
226
|
+
* );
|
|
227
|
+
* }
|
|
228
|
+
* ```
|
|
229
|
+
*/
|
|
230
|
+
declare function useSendTransaction(): UseSendTransactionReturn;
|
|
231
|
+
|
|
232
|
+
interface UseSignMessageReturn {
|
|
233
|
+
/** Sign message (fire-and-forget) */
|
|
234
|
+
signMessage: (args: {
|
|
235
|
+
message: string;
|
|
236
|
+
}) => void;
|
|
237
|
+
/** Sign message (returns promise with signature) */
|
|
238
|
+
signMessageAsync: (args: {
|
|
239
|
+
message: string;
|
|
240
|
+
}) => Promise<string>;
|
|
241
|
+
/** Signature */
|
|
242
|
+
data: string | undefined;
|
|
243
|
+
/** Last error */
|
|
244
|
+
error: Error | null;
|
|
245
|
+
/** Is signing */
|
|
246
|
+
isLoading: boolean;
|
|
247
|
+
/** Did signing fail */
|
|
248
|
+
isError: boolean;
|
|
249
|
+
/** Did signing succeed */
|
|
250
|
+
isSuccess: boolean;
|
|
251
|
+
/** Reset state */
|
|
252
|
+
reset: () => void;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Hook to sign a message
|
|
256
|
+
*
|
|
257
|
+
* @example
|
|
258
|
+
* ```tsx
|
|
259
|
+
* function SignMessage() {
|
|
260
|
+
* const { signMessage, isLoading, data } = useSignMessage();
|
|
261
|
+
*
|
|
262
|
+
* const handleSign = () => {
|
|
263
|
+
* signMessage({ message: 'Hello, Bitcoin!' });
|
|
264
|
+
* };
|
|
265
|
+
*
|
|
266
|
+
* return (
|
|
267
|
+
* <div>
|
|
268
|
+
* <button onClick={handleSign} disabled={isLoading}>
|
|
269
|
+
* Sign Message
|
|
270
|
+
* </button>
|
|
271
|
+
* {data && <p>Signature: {data}</p>}
|
|
272
|
+
* </div>
|
|
273
|
+
* );
|
|
274
|
+
* }
|
|
275
|
+
* ```
|
|
276
|
+
*/
|
|
277
|
+
declare function useSignMessage(): UseSignMessageReturn;
|
|
278
|
+
|
|
279
|
+
interface UseSignPsbtReturn {
|
|
280
|
+
/** Sign PSBT (fire-and-forget) */
|
|
281
|
+
signPsbt: (args: {
|
|
282
|
+
psbt: string;
|
|
283
|
+
options?: SignPsbtOptions;
|
|
284
|
+
}) => void;
|
|
285
|
+
/** Sign PSBT (returns promise with signed PSBT hex) */
|
|
286
|
+
signPsbtAsync: (args: {
|
|
287
|
+
psbt: string;
|
|
288
|
+
options?: SignPsbtOptions;
|
|
289
|
+
}) => Promise<string>;
|
|
290
|
+
/** Signed PSBT hex */
|
|
291
|
+
data: string | undefined;
|
|
292
|
+
/** Last error */
|
|
293
|
+
error: Error | null;
|
|
294
|
+
/** Is signing */
|
|
295
|
+
isLoading: boolean;
|
|
296
|
+
/** Did signing fail */
|
|
297
|
+
isError: boolean;
|
|
298
|
+
/** Did signing succeed */
|
|
299
|
+
isSuccess: boolean;
|
|
300
|
+
/** Reset state */
|
|
301
|
+
reset: () => void;
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Hook to sign a PSBT
|
|
305
|
+
*
|
|
306
|
+
* @example
|
|
307
|
+
* ```tsx
|
|
308
|
+
* function SignPsbt() {
|
|
309
|
+
* const { signPsbt, isLoading, data } = useSignPsbt();
|
|
310
|
+
*
|
|
311
|
+
* const handleSign = () => {
|
|
312
|
+
* signPsbt({
|
|
313
|
+
* psbt: '70736274ff01...',
|
|
314
|
+
* options: { autoFinalize: true }
|
|
315
|
+
* });
|
|
316
|
+
* };
|
|
317
|
+
*
|
|
318
|
+
* return (
|
|
319
|
+
* <div>
|
|
320
|
+
* <button onClick={handleSign} disabled={isLoading}>
|
|
321
|
+
* Sign PSBT
|
|
322
|
+
* </button>
|
|
323
|
+
* {data && <p>Signed: {data.slice(0, 20)}...</p>}
|
|
324
|
+
* </div>
|
|
325
|
+
* );
|
|
326
|
+
* }
|
|
327
|
+
* ```
|
|
328
|
+
*/
|
|
329
|
+
declare function useSignPsbt(): UseSignPsbtReturn;
|
|
330
|
+
|
|
331
|
+
interface UseSignPsbtsReturn {
|
|
332
|
+
/** Sign multiple PSBTs (fire-and-forget) */
|
|
333
|
+
signPsbts: (args: {
|
|
334
|
+
psbts: string[];
|
|
335
|
+
options?: SignPsbtOptions;
|
|
336
|
+
}) => void;
|
|
337
|
+
/** Sign multiple PSBTs (returns promise with signed PSBT hexes) */
|
|
338
|
+
signPsbtsAsync: (args: {
|
|
339
|
+
psbts: string[];
|
|
340
|
+
options?: SignPsbtOptions;
|
|
341
|
+
}) => Promise<string[]>;
|
|
342
|
+
/** Signed PSBT hexes */
|
|
343
|
+
data: string[] | undefined;
|
|
344
|
+
/** Last error */
|
|
345
|
+
error: Error | null;
|
|
346
|
+
/** Is signing */
|
|
347
|
+
isLoading: boolean;
|
|
348
|
+
/** Did signing fail */
|
|
349
|
+
isError: boolean;
|
|
350
|
+
/** Did signing succeed */
|
|
351
|
+
isSuccess: boolean;
|
|
352
|
+
/** Is batch signing supported by current connector */
|
|
353
|
+
isSupported: boolean;
|
|
354
|
+
/** Reset state */
|
|
355
|
+
reset: () => void;
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Hook to sign multiple PSBTs (batch signing)
|
|
359
|
+
*
|
|
360
|
+
* @example
|
|
361
|
+
* ```tsx
|
|
362
|
+
* function BatchSign() {
|
|
363
|
+
* const { signPsbts, isLoading, isSupported, data } = useSignPsbts();
|
|
364
|
+
*
|
|
365
|
+
* if (!isSupported) {
|
|
366
|
+
* return <p>Batch signing not supported by this wallet</p>;
|
|
367
|
+
* }
|
|
368
|
+
*
|
|
369
|
+
* const handleSign = () => {
|
|
370
|
+
* signPsbts({
|
|
371
|
+
* psbts: ['70736274ff01...', '70736274ff02...'],
|
|
372
|
+
* options: { autoFinalize: true }
|
|
373
|
+
* });
|
|
374
|
+
* };
|
|
375
|
+
*
|
|
376
|
+
* return (
|
|
377
|
+
* <button onClick={handleSign} disabled={isLoading}>
|
|
378
|
+
* Sign {2} PSBTs
|
|
379
|
+
* </button>
|
|
380
|
+
* );
|
|
381
|
+
* }
|
|
382
|
+
* ```
|
|
383
|
+
*/
|
|
384
|
+
declare function useSignPsbts(): UseSignPsbtsReturn;
|
|
385
|
+
|
|
386
|
+
interface UseMultiAddressReturn {
|
|
387
|
+
/** All available addresses from the wallet */
|
|
388
|
+
addresses: WalletAccount[];
|
|
389
|
+
/** Payment address (for sending/receiving BTC) */
|
|
390
|
+
paymentAddress: WalletAccount | null;
|
|
391
|
+
/** Ordinals address (for NFTs/inscriptions) */
|
|
392
|
+
ordinalsAddress: WalletAccount | null;
|
|
393
|
+
/** Currently selected primary address */
|
|
394
|
+
primaryAddress: WalletAccount | null;
|
|
395
|
+
/** Set the primary address */
|
|
396
|
+
setPrimaryAddress: (address: WalletAccount) => void;
|
|
397
|
+
/** Is loading addresses */
|
|
398
|
+
isLoading: boolean;
|
|
399
|
+
/** Refresh addresses from wallet */
|
|
400
|
+
refresh: () => Promise<void>;
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Hook to manage multiple addresses from a connected wallet
|
|
404
|
+
*
|
|
405
|
+
* Xverse and Leather wallets provide both payment and ordinals addresses.
|
|
406
|
+
* This hook helps manage and switch between them.
|
|
407
|
+
*
|
|
408
|
+
* @example
|
|
409
|
+
* ```tsx
|
|
410
|
+
* function MultiAddressDisplay() {
|
|
411
|
+
* const {
|
|
412
|
+
* paymentAddress,
|
|
413
|
+
* ordinalsAddress,
|
|
414
|
+
* primaryAddress,
|
|
415
|
+
* setPrimaryAddress
|
|
416
|
+
* } = useMultiAddress();
|
|
417
|
+
*
|
|
418
|
+
* return (
|
|
419
|
+
* <div>
|
|
420
|
+
* <h3>Payment: {paymentAddress?.address}</h3>
|
|
421
|
+
* <h3>Ordinals: {ordinalsAddress?.address}</h3>
|
|
422
|
+
* <select onChange={(e) => {
|
|
423
|
+
* const addr = e.target.value === 'payment' ? paymentAddress : ordinalsAddress;
|
|
424
|
+
* if (addr) setPrimaryAddress(addr);
|
|
425
|
+
* }}>
|
|
426
|
+
* <option value="payment">Payment</option>
|
|
427
|
+
* <option value="ordinals">Ordinals</option>
|
|
428
|
+
* </select>
|
|
429
|
+
* </div>
|
|
430
|
+
* );
|
|
431
|
+
* }
|
|
432
|
+
* ```
|
|
433
|
+
*/
|
|
434
|
+
declare function useMultiAddress(): UseMultiAddressReturn;
|
|
435
|
+
|
|
436
|
+
export { BtcWalletContext, BtcWalletProvider, type BtcWalletProviderProps, type ConnectArgs, type UseAccountReturn, type UseConnectReturn, type UseDisconnectReturn, type UseMultiAddressReturn, type UseNetworkReturn, type UseSendTransactionReturn, type UseSignMessageReturn, type UseSignPsbtReturn, type UseSignPsbtsReturn, useAccount, useConfig, useConnect, useDisconnect, useMultiAddress, useNetwork, useSendTransaction, useSignMessage, useSignPsbt, useSignPsbts };
|