@1sat/react 0.0.2 → 0.0.4
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/context.d.ts +11 -5
- package/dist/context.d.ts.map +1 -1
- package/dist/hooks.d.ts +104 -73
- package/dist/hooks.d.ts.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +133 -112
- package/package.json +3 -3
package/dist/context.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type ConnectResult, type
|
|
1
|
+
import { type ConnectResult, type OneSatProvider as OneSatProviderInterface } from '@1sat/connect';
|
|
2
2
|
import { type ReactNode } from 'react';
|
|
3
3
|
export interface OneSatContextValue {
|
|
4
4
|
/** The underlying provider instance */
|
|
@@ -21,8 +21,14 @@ export interface OneSatContextValue {
|
|
|
21
21
|
error: Error | null;
|
|
22
22
|
}
|
|
23
23
|
export interface OneSatProviderProps {
|
|
24
|
-
/**
|
|
25
|
-
|
|
24
|
+
/** Name of the dApp (shown in approval popup) */
|
|
25
|
+
appName?: string;
|
|
26
|
+
/** Base URL for the wallet popup (default: https://1sat.market) */
|
|
27
|
+
popupUrl?: string;
|
|
28
|
+
/** Request timeout in milliseconds (default: 300000 = 5 minutes) */
|
|
29
|
+
timeout?: number;
|
|
30
|
+
/** Network to use (default: main) */
|
|
31
|
+
network?: 'main' | 'test';
|
|
26
32
|
/** Children to render */
|
|
27
33
|
children: ReactNode;
|
|
28
34
|
}
|
|
@@ -35,14 +41,14 @@ export interface OneSatProviderProps {
|
|
|
35
41
|
*
|
|
36
42
|
* function App() {
|
|
37
43
|
* return (
|
|
38
|
-
* <OneSatProvider
|
|
44
|
+
* <OneSatProvider appName="My dApp">
|
|
39
45
|
* <MyApp />
|
|
40
46
|
* </OneSatProvider>
|
|
41
47
|
* )
|
|
42
48
|
* }
|
|
43
49
|
* ```
|
|
44
50
|
*/
|
|
45
|
-
export declare function OneSatProvider({
|
|
51
|
+
export declare function OneSatProvider({ appName, popupUrl, timeout, network, children, }: OneSatProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
46
52
|
/**
|
|
47
53
|
* Hook to access the 1Sat context
|
|
48
54
|
* @throws Error if used outside of OneSatProvider
|
package/dist/context.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.tsx"],"names":[],"mappings":"AAEA,OAAO,EACN,KAAK,aAAa,EAClB,KAAK,
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.tsx"],"names":[],"mappings":"AAEA,OAAO,EACN,KAAK,aAAa,EAClB,KAAK,cAAc,IAAI,uBAAuB,EAE9C,MAAM,eAAe,CAAA;AACtB,OAAO,EACN,KAAK,SAAS,EAOd,MAAM,OAAO,CAAA;AAEd,MAAM,WAAW,kBAAkB;IAClC,uCAAuC;IACvC,QAAQ,EAAE,uBAAuB,GAAG,IAAI,CAAA;IACxC,sCAAsC;IACtC,WAAW,EAAE,OAAO,CAAA;IACpB,0CAA0C;IAC1C,YAAY,EAAE,OAAO,CAAA;IACrB,yCAAyC;IACzC,cAAc,EAAE,MAAM,GAAG,IAAI,CAAA;IAC7B,yCAAyC;IACzC,cAAc,EAAE,MAAM,GAAG,IAAI,CAAA;IAC7B,6CAA6C;IAC7C,cAAc,EAAE,MAAM,GAAG,IAAI,CAAA;IAC7B,4BAA4B;IAC5B,OAAO,EAAE,MAAM,OAAO,CAAC,aAAa,CAAC,CAAA;IACrC,iCAAiC;IACjC,UAAU,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;IAC/B,2BAA2B;IAC3B,KAAK,EAAE,KAAK,GAAG,IAAI,CAAA;CACnB;AAID,MAAM,WAAW,mBAAmB;IACnC,iDAAiD;IACjD,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,mEAAmE;IACnE,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,oEAAoE;IACpE,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,qCAAqC;IACrC,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;IACzB,yBAAyB;IACzB,QAAQ,EAAE,SAAS,CAAA;CACnB;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,cAAc,CAAC,EAC9B,OAAO,EACP,QAAQ,EACR,OAAO,EACP,OAAO,EACP,QAAQ,GACR,EAAE,mBAAmB,2CAqGrB;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,IAAI,kBAAkB,CAMrD"}
|
package/dist/hooks.d.ts
CHANGED
|
@@ -1,31 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
/**
|
|
3
|
-
* Main hook for 1Sat wallet interaction
|
|
4
|
-
*
|
|
5
|
-
* @example
|
|
6
|
-
* ```tsx
|
|
7
|
-
* function MyComponent() {
|
|
8
|
-
* const { isConnected, connect, paymentAddress } = useOneSat()
|
|
9
|
-
*
|
|
10
|
-
* if (!isConnected) {
|
|
11
|
-
* return <button onClick={connect}>Connect Wallet</button>
|
|
12
|
-
* }
|
|
13
|
-
*
|
|
14
|
-
* return <p>Connected: {paymentAddress}</p>
|
|
15
|
-
* }
|
|
16
|
-
* ```
|
|
17
|
-
*/
|
|
18
|
-
export declare function useOneSat(): {
|
|
19
|
-
provider: import("@1sat/connect").OneSatProvider | null;
|
|
20
|
-
isConnected: boolean;
|
|
21
|
-
isConnecting: boolean;
|
|
22
|
-
paymentAddress: string | null;
|
|
23
|
-
ordinalAddress: string | null;
|
|
24
|
-
identityPubKey: string | null;
|
|
25
|
-
connect: () => Promise<import("@1sat/connect").ConnectResult>;
|
|
26
|
-
disconnect: () => Promise<void>;
|
|
27
|
-
error: Error | null;
|
|
28
|
-
};
|
|
1
|
+
import type { CancelListingRequest, CreateListingRequest, InscribeRequest, OrdinalOutput, PurchaseListingRequest, SendOrdinalsRequest, SignTransactionRequest, TokenOutput, TransferTokenRequest, Utxo } from '@1sat/connect';
|
|
29
2
|
/**
|
|
30
3
|
* Hook to get the wallet balance
|
|
31
4
|
*
|
|
@@ -53,17 +26,11 @@ export declare function useBalance(): {
|
|
|
53
26
|
* function Gallery() {
|
|
54
27
|
* const { ordinals, isLoading } = useOrdinals()
|
|
55
28
|
* if (isLoading) return <span>Loading...</span>
|
|
56
|
-
* return (
|
|
57
|
-
* <div>
|
|
58
|
-
* {ordinals.map(ord => (
|
|
59
|
-
* <div key={ord.outpoint}>{ord.origin}</div>
|
|
60
|
-
* ))}
|
|
61
|
-
* </div>
|
|
62
|
-
* )
|
|
29
|
+
* return ordinals.map(ord => <div key={ord.outpoint}>{ord.origin}</div>)
|
|
63
30
|
* }
|
|
64
31
|
* ```
|
|
65
32
|
*/
|
|
66
|
-
export declare function useOrdinals(
|
|
33
|
+
export declare function useOrdinals(limit?: number, offset?: number): {
|
|
67
34
|
ordinals: OrdinalOutput[];
|
|
68
35
|
isLoading: boolean;
|
|
69
36
|
error: Error | null;
|
|
@@ -77,43 +44,44 @@ export declare function useOrdinals(options?: ListOptions): {
|
|
|
77
44
|
* function Tokens() {
|
|
78
45
|
* const { tokens, isLoading } = useTokens()
|
|
79
46
|
* if (isLoading) return <span>Loading...</span>
|
|
80
|
-
* return (
|
|
81
|
-
* <div>
|
|
82
|
-
* {tokens.map(token => (
|
|
83
|
-
* <div key={token.outpoint}>
|
|
84
|
-
* {token.symbol}: {token.amount}
|
|
85
|
-
* </div>
|
|
86
|
-
* ))}
|
|
87
|
-
* </div>
|
|
88
|
-
* )
|
|
47
|
+
* return tokens.map(t => <div key={t.outpoint}>{t.symbol}: {t.amount}</div>)
|
|
89
48
|
* }
|
|
90
49
|
* ```
|
|
91
50
|
*/
|
|
92
|
-
export declare function useTokens(
|
|
51
|
+
export declare function useTokens(limit?: number, offset?: number): {
|
|
93
52
|
tokens: TokenOutput[];
|
|
94
53
|
isLoading: boolean;
|
|
95
54
|
error: Error | null;
|
|
96
55
|
refetch: () => Promise<void>;
|
|
97
56
|
};
|
|
98
57
|
/**
|
|
99
|
-
* Hook
|
|
58
|
+
* Hook to get payment UTXOs from the wallet
|
|
100
59
|
*
|
|
101
60
|
* @example
|
|
102
61
|
* ```tsx
|
|
103
|
-
* function
|
|
104
|
-
* const {
|
|
105
|
-
*
|
|
106
|
-
* const handleSign = async () => {
|
|
107
|
-
* const result = await signTransaction(rawTx)
|
|
108
|
-
* console.log('Signed:', result.txid)
|
|
109
|
-
* }
|
|
110
|
-
*
|
|
111
|
-
* return <button onClick={handleSign} disabled={isLoading}>Sign</button>
|
|
62
|
+
* function Utxos() {
|
|
63
|
+
* const { utxos, refetch } = useUtxos()
|
|
64
|
+
* return <span>{utxos.length} UTXOs available</span>
|
|
112
65
|
* }
|
|
113
66
|
* ```
|
|
114
67
|
*/
|
|
68
|
+
export declare function useUtxos(): {
|
|
69
|
+
utxos: Utxo[];
|
|
70
|
+
isLoading: boolean;
|
|
71
|
+
error: Error | null;
|
|
72
|
+
refetch: () => Promise<void>;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* Hook for signing transactions
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```tsx
|
|
79
|
+
* const { signTransaction, isLoading } = useSignTransaction()
|
|
80
|
+
* const result = await signTransaction({ rawtx, description: 'Send payment' })
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
115
83
|
export declare function useSignTransaction(): {
|
|
116
|
-
signTransaction: (
|
|
84
|
+
signTransaction: (request: SignTransactionRequest) => Promise<import("@1sat/connect").SignTransactionResult>;
|
|
117
85
|
isLoading: boolean;
|
|
118
86
|
error: Error | null;
|
|
119
87
|
};
|
|
@@ -122,16 +90,8 @@ export declare function useSignTransaction(): {
|
|
|
122
90
|
*
|
|
123
91
|
* @example
|
|
124
92
|
* ```tsx
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
* const handleSign = async () => {
|
|
129
|
-
* const result = await signMessage('Hello, World!')
|
|
130
|
-
* console.log('Signature:', result.signature)
|
|
131
|
-
* }
|
|
132
|
-
*
|
|
133
|
-
* return <button onClick={handleSign} disabled={isLoading}>Sign Message</button>
|
|
134
|
-
* }
|
|
93
|
+
* const { signMessage, isLoading } = useSignMessage()
|
|
94
|
+
* const result = await signMessage('Hello, World!')
|
|
135
95
|
* ```
|
|
136
96
|
*/
|
|
137
97
|
export declare function useSignMessage(): {
|
|
@@ -141,14 +101,85 @@ export declare function useSignMessage(): {
|
|
|
141
101
|
};
|
|
142
102
|
/**
|
|
143
103
|
* Hook for inscribing ordinals
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```tsx
|
|
107
|
+
* const { inscribe, isLoading } = useInscribe()
|
|
108
|
+
* const result = await inscribe({ dataB64, contentType: 'text/plain' })
|
|
109
|
+
* ```
|
|
144
110
|
*/
|
|
145
111
|
export declare function useInscribe(): {
|
|
146
|
-
inscribe: (
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
112
|
+
inscribe: (request: InscribeRequest) => Promise<import("@1sat/connect").InscribeResult>;
|
|
113
|
+
isLoading: boolean;
|
|
114
|
+
error: Error | null;
|
|
115
|
+
};
|
|
116
|
+
/**
|
|
117
|
+
* Hook for sending ordinals
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```tsx
|
|
121
|
+
* const { sendOrdinals, isLoading } = useSendOrdinals()
|
|
122
|
+
* await sendOrdinals({ outpoints: ['txid_0'], destinationAddress: 'addr' })
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
export declare function useSendOrdinals(): {
|
|
126
|
+
sendOrdinals: (request: SendOrdinalsRequest) => Promise<import("@1sat/connect").SendResult>;
|
|
127
|
+
isLoading: boolean;
|
|
128
|
+
error: Error | null;
|
|
129
|
+
};
|
|
130
|
+
/**
|
|
131
|
+
* Hook for transferring tokens (BSV20/21)
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```tsx
|
|
135
|
+
* const { transferToken, isLoading } = useTransferToken()
|
|
136
|
+
* await transferToken({ tokenId: 'origin_0', amount: '100', destinationAddress: 'addr' })
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
export declare function useTransferToken(): {
|
|
140
|
+
transferToken: (request: TransferTokenRequest) => Promise<import("@1sat/connect").SendResult>;
|
|
141
|
+
isLoading: boolean;
|
|
142
|
+
error: Error | null;
|
|
143
|
+
};
|
|
144
|
+
/**
|
|
145
|
+
* Hook for creating marketplace listings
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```tsx
|
|
149
|
+
* const { createListing, isLoading } = useCreateListing()
|
|
150
|
+
* await createListing({ outpoints: ['txid_0'], priceSatoshis: 100000 })
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
export declare function useCreateListing(): {
|
|
154
|
+
createListing: (request: CreateListingRequest) => Promise<import("@1sat/connect").ListingResult>;
|
|
155
|
+
isLoading: boolean;
|
|
156
|
+
error: Error | null;
|
|
157
|
+
};
|
|
158
|
+
/**
|
|
159
|
+
* Hook for purchasing a listed ordinal
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```tsx
|
|
163
|
+
* const { purchaseListing, isLoading } = usePurchaseListing()
|
|
164
|
+
* await purchaseListing({ listingOutpoint: 'txid_0' })
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
export declare function usePurchaseListing(): {
|
|
168
|
+
purchaseListing: (request: PurchaseListingRequest) => Promise<import("@1sat/connect").SendResult>;
|
|
169
|
+
isLoading: boolean;
|
|
170
|
+
error: Error | null;
|
|
171
|
+
};
|
|
172
|
+
/**
|
|
173
|
+
* Hook for cancelling marketplace listings
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* ```tsx
|
|
177
|
+
* const { cancelListing, isLoading } = useCancelListing()
|
|
178
|
+
* await cancelListing({ listingOutpoints: ['txid_0'] })
|
|
179
|
+
* ```
|
|
180
|
+
*/
|
|
181
|
+
export declare function useCancelListing(): {
|
|
182
|
+
cancelListing: (request: CancelListingRequest) => Promise<import("@1sat/connect").SendResult>;
|
|
152
183
|
isLoading: boolean;
|
|
153
184
|
error: Error | null;
|
|
154
185
|
};
|
package/dist/hooks.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAEX,
|
|
1
|
+
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAEX,oBAAoB,EACpB,oBAAoB,EACpB,eAAe,EAEf,aAAa,EACb,sBAAsB,EACtB,mBAAmB,EACnB,sBAAsB,EACtB,WAAW,EACX,oBAAoB,EACpB,IAAI,EACJ,MAAM,eAAe,CAAA;AA6CtB;;;;;;;;;;;GAWG;AACH,wBAAgB,UAAU;;;;;;EAkCzB;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,WAAW,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM;;;;;EAgC1D;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,SAAS,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM;;;;;EAgCxD;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,QAAQ;;;;;EA4BvB;AAID;;;;;;;;GAQG;AACH,wBAAgB,kBAAkB;;;;EASjC;AAED;;;;;;;;GAQG;AACH,wBAAgB,cAAc;;;;EAO7B;AAED;;;;;;;;GAQG;AACH,wBAAgB,WAAW;;;;EAO1B;AAED;;;;;;;;GAQG;AACH,wBAAgB,eAAe;;;;EAS9B;AAED;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB;;;;EAS/B;AAED;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB;;;;EAS/B;AAED;;;;;;;;GAQG;AACH,wBAAgB,kBAAkB;;;;EASjC;AAED;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB;;;;EAS/B"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export * from '@1sat/connect';
|
|
2
2
|
export { OneSatProvider, useOneSatContext, type OneSatContextValue, type OneSatProviderProps, } from './context';
|
|
3
|
-
export {
|
|
3
|
+
export { useBalance, useOrdinals, useTokens, useUtxos, useSignTransaction, useSignMessage, useInscribe, useSendOrdinals, useTransferToken, useCreateListing, usePurchaseListing, useCancelListing, } from './hooks';
|
|
4
4
|
export { ConnectButton, type ConnectButtonProps } from './ConnectButton';
|
|
5
5
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,eAAe,CAAA;AAG7B,OAAO,EACN,cAAc,EACd,gBAAgB,EAChB,KAAK,kBAAkB,EACvB,KAAK,mBAAmB,GACxB,MAAM,WAAW,CAAA;AAGlB,OAAO,EACN,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,eAAe,CAAA;AAG7B,OAAO,EACN,cAAc,EACd,gBAAgB,EAChB,KAAK,kBAAkB,EACvB,KAAK,mBAAmB,GACxB,MAAM,WAAW,CAAA;AAGlB,OAAO,EACN,UAAU,EACV,WAAW,EACX,SAAS,EACT,QAAQ,EACR,kBAAkB,EAClB,cAAc,EACd,WAAW,EACX,eAAe,EACf,gBAAgB,EAChB,gBAAgB,EAChB,kBAAkB,EAClB,gBAAgB,GAChB,MAAM,SAAS,CAAA;AAGhB,OAAO,EAAE,aAAa,EAAE,KAAK,kBAAkB,EAAE,MAAM,iBAAiB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
1
3
|
// src/index.ts
|
|
2
4
|
export * from "@1sat/connect";
|
|
3
5
|
|
|
@@ -13,10 +15,16 @@ import {
|
|
|
13
15
|
useMemo,
|
|
14
16
|
useState
|
|
15
17
|
} from "react";
|
|
16
|
-
import {
|
|
18
|
+
import { jsx } from "react/jsx-runtime";
|
|
17
19
|
"use client";
|
|
18
20
|
var OneSatContext = createContext(null);
|
|
19
|
-
function OneSatProvider({
|
|
21
|
+
function OneSatProvider({
|
|
22
|
+
appName,
|
|
23
|
+
popupUrl,
|
|
24
|
+
timeout,
|
|
25
|
+
network,
|
|
26
|
+
children
|
|
27
|
+
}) {
|
|
20
28
|
const [provider, setProvider] = useState(null);
|
|
21
29
|
const [isConnected, setIsConnected] = useState(false);
|
|
22
30
|
const [isConnecting, setIsConnecting] = useState(false);
|
|
@@ -27,7 +35,7 @@ function OneSatProvider({ config, children }) {
|
|
|
27
35
|
useEffect(() => {
|
|
28
36
|
if (typeof window === "undefined")
|
|
29
37
|
return;
|
|
30
|
-
const p = createOneSat(
|
|
38
|
+
const p = createOneSat({ appName, popupUrl, timeout, network });
|
|
31
39
|
setProvider(p);
|
|
32
40
|
if (p.isConnected()) {
|
|
33
41
|
const addresses = p.getAddresses();
|
|
@@ -56,7 +64,7 @@ function OneSatProvider({ config, children }) {
|
|
|
56
64
|
p.off("connect", handleConnect);
|
|
57
65
|
p.off("disconnect", handleDisconnect);
|
|
58
66
|
};
|
|
59
|
-
}, [
|
|
67
|
+
}, [appName, popupUrl, timeout, network]);
|
|
60
68
|
const connect = useCallback(async () => {
|
|
61
69
|
if (!provider) {
|
|
62
70
|
throw new Error("Provider not initialized");
|
|
@@ -100,10 +108,10 @@ function OneSatProvider({ config, children }) {
|
|
|
100
108
|
disconnect,
|
|
101
109
|
error
|
|
102
110
|
]);
|
|
103
|
-
return /* @__PURE__ */
|
|
111
|
+
return /* @__PURE__ */ jsx(OneSatContext.Provider, {
|
|
104
112
|
value,
|
|
105
113
|
children
|
|
106
|
-
}
|
|
114
|
+
});
|
|
107
115
|
}
|
|
108
116
|
function useOneSatContext() {
|
|
109
117
|
const context = useContext(OneSatContext);
|
|
@@ -113,21 +121,30 @@ function useOneSatContext() {
|
|
|
113
121
|
return context;
|
|
114
122
|
}
|
|
115
123
|
// src/hooks.ts
|
|
116
|
-
import { useCallback as useCallback2, useEffect as useEffect2, useState as useState2 } from "react";
|
|
124
|
+
import { useCallback as useCallback2, useEffect as useEffect2, useRef, useState as useState2 } from "react";
|
|
117
125
|
"use client";
|
|
118
|
-
function
|
|
119
|
-
const
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
126
|
+
function useProviderAction(actionFn) {
|
|
127
|
+
const { provider, isConnected } = useOneSatContext();
|
|
128
|
+
const [isLoading, setIsLoading] = useState2(false);
|
|
129
|
+
const [error, setError] = useState2(null);
|
|
130
|
+
const actionRef = useRef(actionFn);
|
|
131
|
+
actionRef.current = actionFn;
|
|
132
|
+
const mutate = useCallback2(async (...args) => {
|
|
133
|
+
if (!provider || !isConnected)
|
|
134
|
+
throw new Error("Wallet not connected");
|
|
135
|
+
setIsLoading(true);
|
|
136
|
+
setError(null);
|
|
137
|
+
try {
|
|
138
|
+
return await actionRef.current(provider, ...args);
|
|
139
|
+
} catch (e) {
|
|
140
|
+
const err = e instanceof Error ? e : new Error(String(e));
|
|
141
|
+
setError(err);
|
|
142
|
+
throw err;
|
|
143
|
+
} finally {
|
|
144
|
+
setIsLoading(false);
|
|
145
|
+
}
|
|
146
|
+
}, [provider, isConnected]);
|
|
147
|
+
return { mutate, isLoading, error };
|
|
131
148
|
}
|
|
132
149
|
function useBalance() {
|
|
133
150
|
const { provider, isConnected } = useOneSatContext();
|
|
@@ -140,8 +157,7 @@ function useBalance() {
|
|
|
140
157
|
setIsLoading(true);
|
|
141
158
|
setError(null);
|
|
142
159
|
try {
|
|
143
|
-
|
|
144
|
-
setBalance(result);
|
|
160
|
+
setBalance(await provider.getBalance());
|
|
145
161
|
} catch (e) {
|
|
146
162
|
setError(e instanceof Error ? e : new Error(String(e)));
|
|
147
163
|
} finally {
|
|
@@ -163,7 +179,7 @@ function useBalance() {
|
|
|
163
179
|
refetch
|
|
164
180
|
};
|
|
165
181
|
}
|
|
166
|
-
function useOrdinals(
|
|
182
|
+
function useOrdinals(limit, offset) {
|
|
167
183
|
const { provider, isConnected } = useOneSatContext();
|
|
168
184
|
const [ordinals, setOrdinals] = useState2([]);
|
|
169
185
|
const [isLoading, setIsLoading] = useState2(false);
|
|
@@ -174,14 +190,14 @@ function useOrdinals(options) {
|
|
|
174
190
|
setIsLoading(true);
|
|
175
191
|
setError(null);
|
|
176
192
|
try {
|
|
177
|
-
const
|
|
178
|
-
setOrdinals(
|
|
193
|
+
const opts = limit !== undefined || offset !== undefined ? { limit, offset } : undefined;
|
|
194
|
+
setOrdinals(await provider.getOrdinals(opts));
|
|
179
195
|
} catch (e) {
|
|
180
196
|
setError(e instanceof Error ? e : new Error(String(e)));
|
|
181
197
|
} finally {
|
|
182
198
|
setIsLoading(false);
|
|
183
199
|
}
|
|
184
|
-
}, [provider, isConnected,
|
|
200
|
+
}, [provider, isConnected, limit, offset]);
|
|
185
201
|
useEffect2(() => {
|
|
186
202
|
if (isConnected) {
|
|
187
203
|
refetch();
|
|
@@ -189,14 +205,9 @@ function useOrdinals(options) {
|
|
|
189
205
|
setOrdinals([]);
|
|
190
206
|
}
|
|
191
207
|
}, [isConnected, refetch]);
|
|
192
|
-
return {
|
|
193
|
-
ordinals,
|
|
194
|
-
isLoading,
|
|
195
|
-
error,
|
|
196
|
-
refetch
|
|
197
|
-
};
|
|
208
|
+
return { ordinals, isLoading, error, refetch };
|
|
198
209
|
}
|
|
199
|
-
function useTokens(
|
|
210
|
+
function useTokens(limit, offset) {
|
|
200
211
|
const { provider, isConnected } = useOneSatContext();
|
|
201
212
|
const [tokens, setTokens] = useState2([]);
|
|
202
213
|
const [isLoading, setIsLoading] = useState2(false);
|
|
@@ -207,14 +218,14 @@ function useTokens(options) {
|
|
|
207
218
|
setIsLoading(true);
|
|
208
219
|
setError(null);
|
|
209
220
|
try {
|
|
210
|
-
const
|
|
211
|
-
setTokens(
|
|
221
|
+
const opts = limit !== undefined || offset !== undefined ? { limit, offset } : undefined;
|
|
222
|
+
setTokens(await provider.getTokens(opts));
|
|
212
223
|
} catch (e) {
|
|
213
224
|
setError(e instanceof Error ? e : new Error(String(e)));
|
|
214
225
|
} finally {
|
|
215
226
|
setIsLoading(false);
|
|
216
227
|
}
|
|
217
|
-
}, [provider, isConnected,
|
|
228
|
+
}, [provider, isConnected, limit, offset]);
|
|
218
229
|
useEffect2(() => {
|
|
219
230
|
if (isConnected) {
|
|
220
231
|
refetch();
|
|
@@ -222,96 +233,101 @@ function useTokens(options) {
|
|
|
222
233
|
setTokens([]);
|
|
223
234
|
}
|
|
224
235
|
}, [isConnected, refetch]);
|
|
225
|
-
return {
|
|
226
|
-
tokens,
|
|
227
|
-
isLoading,
|
|
228
|
-
error,
|
|
229
|
-
refetch
|
|
230
|
-
};
|
|
236
|
+
return { tokens, isLoading, error, refetch };
|
|
231
237
|
}
|
|
232
|
-
function
|
|
238
|
+
function useUtxos() {
|
|
233
239
|
const { provider, isConnected } = useOneSatContext();
|
|
240
|
+
const [utxos, setUtxos] = useState2([]);
|
|
234
241
|
const [isLoading, setIsLoading] = useState2(false);
|
|
235
242
|
const [error, setError] = useState2(null);
|
|
236
|
-
const
|
|
237
|
-
if (!provider || !isConnected)
|
|
238
|
-
|
|
239
|
-
}
|
|
243
|
+
const refetch = useCallback2(async () => {
|
|
244
|
+
if (!provider || !isConnected)
|
|
245
|
+
return;
|
|
240
246
|
setIsLoading(true);
|
|
241
247
|
setError(null);
|
|
242
248
|
try {
|
|
243
|
-
|
|
244
|
-
return result;
|
|
249
|
+
setUtxos(await provider.getUtxos());
|
|
245
250
|
} catch (e) {
|
|
246
|
-
|
|
247
|
-
setError(err);
|
|
248
|
-
throw err;
|
|
251
|
+
setError(e instanceof Error ? e : new Error(String(e)));
|
|
249
252
|
} finally {
|
|
250
253
|
setIsLoading(false);
|
|
251
254
|
}
|
|
252
255
|
}, [provider, isConnected]);
|
|
253
|
-
|
|
254
|
-
|
|
256
|
+
useEffect2(() => {
|
|
257
|
+
if (isConnected) {
|
|
258
|
+
refetch();
|
|
259
|
+
} else {
|
|
260
|
+
setUtxos([]);
|
|
261
|
+
}
|
|
262
|
+
}, [isConnected, refetch]);
|
|
263
|
+
return { utxos, isLoading, error, refetch };
|
|
264
|
+
}
|
|
265
|
+
function useSignTransaction() {
|
|
266
|
+
const {
|
|
267
|
+
mutate: signTransaction,
|
|
255
268
|
isLoading,
|
|
256
269
|
error
|
|
257
|
-
};
|
|
270
|
+
} = useProviderAction((p, request) => p.signTransaction(request));
|
|
271
|
+
return { signTransaction, isLoading, error };
|
|
258
272
|
}
|
|
259
273
|
function useSignMessage() {
|
|
260
|
-
const {
|
|
261
|
-
|
|
262
|
-
const [error, setError] = useState2(null);
|
|
263
|
-
const signMessage = useCallback2(async (message) => {
|
|
264
|
-
if (!provider || !isConnected) {
|
|
265
|
-
throw new Error("Wallet not connected");
|
|
266
|
-
}
|
|
267
|
-
setIsLoading(true);
|
|
268
|
-
setError(null);
|
|
269
|
-
try {
|
|
270
|
-
const result = await provider.signMessage(message);
|
|
271
|
-
return result;
|
|
272
|
-
} catch (e) {
|
|
273
|
-
const err = e instanceof Error ? e : new Error(String(e));
|
|
274
|
-
setError(err);
|
|
275
|
-
throw err;
|
|
276
|
-
} finally {
|
|
277
|
-
setIsLoading(false);
|
|
278
|
-
}
|
|
279
|
-
}, [provider, isConnected]);
|
|
280
|
-
return {
|
|
281
|
-
signMessage,
|
|
274
|
+
const {
|
|
275
|
+
mutate: signMessage,
|
|
282
276
|
isLoading,
|
|
283
277
|
error
|
|
284
|
-
};
|
|
278
|
+
} = useProviderAction((p, message) => p.signMessage(message));
|
|
279
|
+
return { signMessage, isLoading, error };
|
|
285
280
|
}
|
|
286
281
|
function useInscribe() {
|
|
287
|
-
const {
|
|
288
|
-
|
|
289
|
-
const [error, setError] = useState2(null);
|
|
290
|
-
const inscribe = useCallback2(async (params) => {
|
|
291
|
-
if (!provider || !isConnected) {
|
|
292
|
-
throw new Error("Wallet not connected");
|
|
293
|
-
}
|
|
294
|
-
setIsLoading(true);
|
|
295
|
-
setError(null);
|
|
296
|
-
try {
|
|
297
|
-
const result = await provider.inscribe(params);
|
|
298
|
-
return result;
|
|
299
|
-
} catch (e) {
|
|
300
|
-
const err = e instanceof Error ? e : new Error(String(e));
|
|
301
|
-
setError(err);
|
|
302
|
-
throw err;
|
|
303
|
-
} finally {
|
|
304
|
-
setIsLoading(false);
|
|
305
|
-
}
|
|
306
|
-
}, [provider, isConnected]);
|
|
307
|
-
return {
|
|
308
|
-
inscribe,
|
|
282
|
+
const {
|
|
283
|
+
mutate: inscribe,
|
|
309
284
|
isLoading,
|
|
310
285
|
error
|
|
311
|
-
};
|
|
286
|
+
} = useProviderAction((p, request) => p.inscribe(request));
|
|
287
|
+
return { inscribe, isLoading, error };
|
|
288
|
+
}
|
|
289
|
+
function useSendOrdinals() {
|
|
290
|
+
const {
|
|
291
|
+
mutate: sendOrdinals,
|
|
292
|
+
isLoading,
|
|
293
|
+
error
|
|
294
|
+
} = useProviderAction((p, request) => p.sendOrdinals(request));
|
|
295
|
+
return { sendOrdinals, isLoading, error };
|
|
296
|
+
}
|
|
297
|
+
function useTransferToken() {
|
|
298
|
+
const {
|
|
299
|
+
mutate: transferToken,
|
|
300
|
+
isLoading,
|
|
301
|
+
error
|
|
302
|
+
} = useProviderAction((p, request) => p.transferToken(request));
|
|
303
|
+
return { transferToken, isLoading, error };
|
|
304
|
+
}
|
|
305
|
+
function useCreateListing() {
|
|
306
|
+
const {
|
|
307
|
+
mutate: createListing,
|
|
308
|
+
isLoading,
|
|
309
|
+
error
|
|
310
|
+
} = useProviderAction((p, request) => p.createListing(request));
|
|
311
|
+
return { createListing, isLoading, error };
|
|
312
|
+
}
|
|
313
|
+
function usePurchaseListing() {
|
|
314
|
+
const {
|
|
315
|
+
mutate: purchaseListing,
|
|
316
|
+
isLoading,
|
|
317
|
+
error
|
|
318
|
+
} = useProviderAction((p, request) => p.purchaseListing(request));
|
|
319
|
+
return { purchaseListing, isLoading, error };
|
|
320
|
+
}
|
|
321
|
+
function useCancelListing() {
|
|
322
|
+
const {
|
|
323
|
+
mutate: cancelListing,
|
|
324
|
+
isLoading,
|
|
325
|
+
error
|
|
326
|
+
} = useProviderAction((p, request) => p.cancelListing(request));
|
|
327
|
+
return { cancelListing, isLoading, error };
|
|
312
328
|
}
|
|
313
329
|
// src/ConnectButton.tsx
|
|
314
|
-
import {
|
|
330
|
+
import { jsx as jsx2, Fragment } from "react/jsx-runtime";
|
|
315
331
|
"use client";
|
|
316
332
|
function truncateAddress(address) {
|
|
317
333
|
if (address.length <= 12)
|
|
@@ -336,7 +352,7 @@ function ConnectButton({
|
|
|
336
352
|
paymentAddress,
|
|
337
353
|
connect: providerConnect,
|
|
338
354
|
disconnect: providerDisconnect
|
|
339
|
-
} =
|
|
355
|
+
} = useOneSatContext();
|
|
340
356
|
const connect = async () => {
|
|
341
357
|
try {
|
|
342
358
|
await providerConnect();
|
|
@@ -354,7 +370,7 @@ function ConnectButton({
|
|
|
354
370
|
}
|
|
355
371
|
};
|
|
356
372
|
if (children) {
|
|
357
|
-
return /* @__PURE__ */
|
|
373
|
+
return /* @__PURE__ */ jsx2(Fragment, {
|
|
358
374
|
children: children({
|
|
359
375
|
isConnected,
|
|
360
376
|
isConnecting,
|
|
@@ -362,43 +378,48 @@ function ConnectButton({
|
|
|
362
378
|
connect,
|
|
363
379
|
disconnect
|
|
364
380
|
})
|
|
365
|
-
}
|
|
381
|
+
});
|
|
366
382
|
}
|
|
367
383
|
if (isConnecting) {
|
|
368
|
-
return /* @__PURE__ */
|
|
384
|
+
return /* @__PURE__ */ jsx2("button", {
|
|
369
385
|
className,
|
|
370
386
|
style,
|
|
371
387
|
disabled: true,
|
|
372
388
|
type: "button",
|
|
373
389
|
children: connectingLabel
|
|
374
|
-
}
|
|
390
|
+
});
|
|
375
391
|
}
|
|
376
392
|
if (isConnected) {
|
|
377
393
|
const label = typeof connectedLabel === "function" ? connectedLabel(paymentAddress ?? "") : connectedLabel ?? truncateAddress(paymentAddress ?? "");
|
|
378
|
-
return /* @__PURE__ */
|
|
394
|
+
return /* @__PURE__ */ jsx2("button", {
|
|
379
395
|
className,
|
|
380
396
|
style,
|
|
381
397
|
onClick: disconnectOnClick ? disconnect : undefined,
|
|
382
398
|
type: "button",
|
|
383
399
|
children: label
|
|
384
|
-
}
|
|
400
|
+
});
|
|
385
401
|
}
|
|
386
|
-
return /* @__PURE__ */
|
|
402
|
+
return /* @__PURE__ */ jsx2("button", {
|
|
387
403
|
className,
|
|
388
404
|
style,
|
|
389
405
|
onClick: connect,
|
|
390
406
|
type: "button",
|
|
391
407
|
children: connectLabel
|
|
392
|
-
}
|
|
408
|
+
});
|
|
393
409
|
}
|
|
394
410
|
export {
|
|
411
|
+
useUtxos,
|
|
412
|
+
useTransferToken,
|
|
395
413
|
useTokens,
|
|
396
414
|
useSignTransaction,
|
|
397
415
|
useSignMessage,
|
|
416
|
+
useSendOrdinals,
|
|
417
|
+
usePurchaseListing,
|
|
398
418
|
useOrdinals,
|
|
399
419
|
useOneSatContext,
|
|
400
|
-
useOneSat,
|
|
401
420
|
useInscribe,
|
|
421
|
+
useCreateListing,
|
|
422
|
+
useCancelListing,
|
|
402
423
|
useBalance,
|
|
403
424
|
OneSatProvider,
|
|
404
425
|
ConnectButton
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@1sat/react",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "React hooks and components for 1Sat wallet integration",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -13,13 +13,13 @@
|
|
|
13
13
|
},
|
|
14
14
|
"files": ["dist"],
|
|
15
15
|
"scripts": {
|
|
16
|
-
"build": "bun build
|
|
16
|
+
"build": "bun run build.ts && tsc --emitDeclarationOnly",
|
|
17
17
|
"dev": "tsc --watch"
|
|
18
18
|
},
|
|
19
19
|
"keywords": ["1sat", "bsv", "ordinals", "wallet", "sdk", "react", "hooks"],
|
|
20
20
|
"license": "MIT",
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@1sat/connect": "^0.0.
|
|
22
|
+
"@1sat/connect": "^0.0.4"
|
|
23
23
|
},
|
|
24
24
|
"peerDependencies": {
|
|
25
25
|
"react": "^18.0.0 || ^19.0.0"
|