@1sat/react 0.0.1
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/ConnectButton.d.ts +56 -0
- package/dist/ConnectButton.d.ts.map +1 -0
- package/dist/context.d.ts +51 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/hooks.d.ts +155 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +405 -0
- package/package.json +32 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
export interface ConnectButtonProps {
|
|
3
|
+
/** Custom class name for the button */
|
|
4
|
+
className?: string;
|
|
5
|
+
/** Custom styles for the button */
|
|
6
|
+
style?: React.CSSProperties;
|
|
7
|
+
/** Text to show when disconnected */
|
|
8
|
+
connectLabel?: ReactNode;
|
|
9
|
+
/** Text to show when connecting */
|
|
10
|
+
connectingLabel?: ReactNode;
|
|
11
|
+
/** Text to show when connected (receives address) */
|
|
12
|
+
connectedLabel?: ReactNode | ((address: string) => ReactNode);
|
|
13
|
+
/** Called after successful connection */
|
|
14
|
+
onConnect?: () => void;
|
|
15
|
+
/** Called after disconnection */
|
|
16
|
+
onDisconnect?: () => void;
|
|
17
|
+
/** Called on error */
|
|
18
|
+
onError?: (error: Error) => void;
|
|
19
|
+
/** Whether to disconnect on click when connected */
|
|
20
|
+
disconnectOnClick?: boolean;
|
|
21
|
+
/** Custom render function for full control */
|
|
22
|
+
children?: (props: {
|
|
23
|
+
isConnected: boolean;
|
|
24
|
+
isConnecting: boolean;
|
|
25
|
+
address: string | null;
|
|
26
|
+
connect: () => Promise<void>;
|
|
27
|
+
disconnect: () => Promise<void>;
|
|
28
|
+
}) => ReactNode;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Ready-to-use connect button component
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```tsx
|
|
35
|
+
* // Basic usage
|
|
36
|
+
* <ConnectButton />
|
|
37
|
+
*
|
|
38
|
+
* // With custom labels
|
|
39
|
+
* <ConnectButton
|
|
40
|
+
* connectLabel="Connect 1Sat"
|
|
41
|
+
* connectingLabel="Connecting..."
|
|
42
|
+
* connectedLabel={(addr) => `Connected: ${addr}`}
|
|
43
|
+
* />
|
|
44
|
+
*
|
|
45
|
+
* // With custom render
|
|
46
|
+
* <ConnectButton>
|
|
47
|
+
* {({ isConnected, connect, disconnect }) => (
|
|
48
|
+
* <button onClick={isConnected ? disconnect : connect}>
|
|
49
|
+
* {isConnected ? 'Disconnect' : 'Connect'}
|
|
50
|
+
* </button>
|
|
51
|
+
* )}
|
|
52
|
+
* </ConnectButton>
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export declare function ConnectButton({ className, style, connectLabel, connectingLabel, connectedLabel, onConnect, onDisconnect, onError, disconnectOnClick, children, }: ConnectButtonProps): import("react/jsx-runtime").JSX.Element;
|
|
56
|
+
//# sourceMappingURL=ConnectButton.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ConnectButton.d.ts","sourceRoot":"","sources":["../src/ConnectButton.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAGtC,MAAM,WAAW,kBAAkB;IAClC,uCAAuC;IACvC,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,mCAAmC;IACnC,KAAK,CAAC,EAAE,KAAK,CAAC,aAAa,CAAA;IAC3B,qCAAqC;IACrC,YAAY,CAAC,EAAE,SAAS,CAAA;IACxB,mCAAmC;IACnC,eAAe,CAAC,EAAE,SAAS,CAAA;IAC3B,qDAAqD;IACrD,cAAc,CAAC,EAAE,SAAS,GAAG,CAAC,CAAC,OAAO,EAAE,MAAM,KAAK,SAAS,CAAC,CAAA;IAC7D,yCAAyC;IACzC,SAAS,CAAC,EAAE,MAAM,IAAI,CAAA;IACtB,iCAAiC;IACjC,YAAY,CAAC,EAAE,MAAM,IAAI,CAAA;IACzB,sBAAsB;IACtB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAA;IAChC,oDAAoD;IACpD,iBAAiB,CAAC,EAAE,OAAO,CAAA;IAC3B,8CAA8C;IAC9C,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE;QAClB,WAAW,EAAE,OAAO,CAAA;QACpB,YAAY,EAAE,OAAO,CAAA;QACrB,OAAO,EAAE,MAAM,GAAG,IAAI,CAAA;QACtB,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;QAC5B,UAAU,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;KAC/B,KAAK,SAAS,CAAA;CACf;AAUD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,aAAa,CAAC,EAC7B,SAAS,EACT,KAAK,EACL,YAA+B,EAC/B,eAAiC,EACjC,cAAc,EACd,SAAS,EACT,YAAY,EACZ,OAAO,EACP,iBAAwB,EACxB,QAAQ,GACR,EAAE,kBAAkB,2CA0EpB"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { type ConnectResult, type OneSatConfig, type OneSatProvider as OneSatProviderInterface } from '@1sat/connect';
|
|
2
|
+
import { type ReactNode } from 'react';
|
|
3
|
+
export interface OneSatContextValue {
|
|
4
|
+
/** The underlying provider instance */
|
|
5
|
+
provider: OneSatProviderInterface | null;
|
|
6
|
+
/** Whether the wallet is connected */
|
|
7
|
+
isConnected: boolean;
|
|
8
|
+
/** Whether a connection is in progress */
|
|
9
|
+
isConnecting: boolean;
|
|
10
|
+
/** The payment address (if connected) */
|
|
11
|
+
paymentAddress: string | null;
|
|
12
|
+
/** The ordinal address (if connected) */
|
|
13
|
+
ordinalAddress: string | null;
|
|
14
|
+
/** The identity public key (if connected) */
|
|
15
|
+
identityPubKey: string | null;
|
|
16
|
+
/** Connect to the wallet */
|
|
17
|
+
connect: () => Promise<ConnectResult>;
|
|
18
|
+
/** Disconnect from the wallet */
|
|
19
|
+
disconnect: () => Promise<void>;
|
|
20
|
+
/** Any connection error */
|
|
21
|
+
error: Error | null;
|
|
22
|
+
}
|
|
23
|
+
export interface OneSatProviderProps {
|
|
24
|
+
/** Configuration for the provider */
|
|
25
|
+
config?: OneSatConfig;
|
|
26
|
+
/** Children to render */
|
|
27
|
+
children: ReactNode;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* React provider for 1Sat wallet functionality
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```tsx
|
|
34
|
+
* import { OneSatProvider } from '@1sat/react'
|
|
35
|
+
*
|
|
36
|
+
* function App() {
|
|
37
|
+
* return (
|
|
38
|
+
* <OneSatProvider config={{ appName: 'My dApp' }}>
|
|
39
|
+
* <MyApp />
|
|
40
|
+
* </OneSatProvider>
|
|
41
|
+
* )
|
|
42
|
+
* }
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export declare function OneSatProvider({ config, children }: OneSatProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
46
|
+
/**
|
|
47
|
+
* Hook to access the 1Sat context
|
|
48
|
+
* @throws Error if used outside of OneSatProvider
|
|
49
|
+
*/
|
|
50
|
+
export declare function useOneSatContext(): OneSatContextValue;
|
|
51
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.tsx"],"names":[],"mappings":"AAEA,OAAO,EACN,KAAK,aAAa,EAClB,KAAK,YAAY,EACjB,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,qCAAqC;IACrC,MAAM,CAAC,EAAE,YAAY,CAAA;IACrB,yBAAyB;IACzB,QAAQ,EAAE,SAAS,CAAA;CACnB;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,cAAc,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,EAAE,mBAAmB,2CAwGvE;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,IAAI,kBAAkB,CAMrD"}
|
package/dist/hooks.d.ts
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import type { ListOptions, OrdinalOutput, TokenOutput } from '@1sat/connect';
|
|
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
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Hook to get the wallet balance
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```tsx
|
|
34
|
+
* function Balance() {
|
|
35
|
+
* const { satoshis, isLoading } = useBalance()
|
|
36
|
+
* if (isLoading) return <span>Loading...</span>
|
|
37
|
+
* return <span>{satoshis} sats</span>
|
|
38
|
+
* }
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export declare function useBalance(): {
|
|
42
|
+
satoshis: number;
|
|
43
|
+
usd: number;
|
|
44
|
+
isLoading: boolean;
|
|
45
|
+
error: Error | null;
|
|
46
|
+
refetch: () => Promise<void>;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Hook to get ordinals from the wallet
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```tsx
|
|
53
|
+
* function Gallery() {
|
|
54
|
+
* const { ordinals, isLoading } = useOrdinals()
|
|
55
|
+
* 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
|
+
* )
|
|
63
|
+
* }
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
export declare function useOrdinals(options?: ListOptions): {
|
|
67
|
+
ordinals: OrdinalOutput[];
|
|
68
|
+
isLoading: boolean;
|
|
69
|
+
error: Error | null;
|
|
70
|
+
refetch: () => Promise<void>;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Hook to get tokens from the wallet
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```tsx
|
|
77
|
+
* function Tokens() {
|
|
78
|
+
* const { tokens, isLoading } = useTokens()
|
|
79
|
+
* 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
|
+
* )
|
|
89
|
+
* }
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
export declare function useTokens(options?: ListOptions): {
|
|
93
|
+
tokens: TokenOutput[];
|
|
94
|
+
isLoading: boolean;
|
|
95
|
+
error: Error | null;
|
|
96
|
+
refetch: () => Promise<void>;
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* Hook for signing transactions
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```tsx
|
|
103
|
+
* function SignTx() {
|
|
104
|
+
* const { signTransaction, isLoading } = useSignTransaction()
|
|
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>
|
|
112
|
+
* }
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
export declare function useSignTransaction(): {
|
|
116
|
+
signTransaction: (rawtx: string, description?: string) => Promise<import("@1sat/connect").SignTransactionResult>;
|
|
117
|
+
isLoading: boolean;
|
|
118
|
+
error: Error | null;
|
|
119
|
+
};
|
|
120
|
+
/**
|
|
121
|
+
* Hook for signing messages (BSM)
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```tsx
|
|
125
|
+
* function SignMessage() {
|
|
126
|
+
* const { signMessage, isLoading } = useSignMessage()
|
|
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
|
+
* }
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
export declare function useSignMessage(): {
|
|
138
|
+
signMessage: (message: string) => Promise<import("@1sat/connect").SignMessageResult>;
|
|
139
|
+
isLoading: boolean;
|
|
140
|
+
error: Error | null;
|
|
141
|
+
};
|
|
142
|
+
/**
|
|
143
|
+
* Hook for inscribing ordinals
|
|
144
|
+
*/
|
|
145
|
+
export declare function useInscribe(): {
|
|
146
|
+
inscribe: (params: {
|
|
147
|
+
dataB64: string;
|
|
148
|
+
contentType: string;
|
|
149
|
+
destinationAddress?: string;
|
|
150
|
+
metaData?: Record<string, string>;
|
|
151
|
+
}) => Promise<import("@1sat/connect").InscribeResult>;
|
|
152
|
+
isLoading: boolean;
|
|
153
|
+
error: Error | null;
|
|
154
|
+
};
|
|
155
|
+
//# sourceMappingURL=hooks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAEX,WAAW,EACX,aAAa,EACb,WAAW,EACX,MAAM,eAAe,CAAA;AAItB;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,SAAS;;;;;;;;;;EAcxB;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,UAAU;;;;;;EAqCzB;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,WAAW,CAAC,OAAO,CAAC,EAAE,WAAW;;;;;EAoChD;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,SAAS,CAAC,OAAO,CAAC,EAAE,WAAW;;;;;EAoC9C;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,kBAAkB;6BAMlB,MAAM,gBAAgB,MAAM;;;EA2B3C;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,cAAc;2BAMZ,MAAM;;;EA2BvB;AAED;;GAEG;AACH,wBAAgB,WAAW;uBAMV;QACd,OAAO,EAAE,MAAM,CAAA;QACf,WAAW,EAAE,MAAM,CAAA;QACnB,kBAAkB,CAAC,EAAE,MAAM,CAAA;QAC3B,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;KACjC;;;EA2BF"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export * from '@1sat/connect';
|
|
2
|
+
export { OneSatProvider, useOneSatContext, type OneSatContextValue, type OneSatProviderProps, } from './context';
|
|
3
|
+
export { useOneSat, useBalance, useOrdinals, useTokens, useSignTransaction, useSignMessage, useInscribe, } from './hooks';
|
|
4
|
+
export { ConnectButton, type ConnectButtonProps } from './ConnectButton';
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +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,SAAS,EACT,UAAU,EACV,WAAW,EACX,SAAS,EACT,kBAAkB,EAClB,cAAc,EACd,WAAW,GACX,MAAM,SAAS,CAAA;AAGhB,OAAO,EAAE,aAAa,EAAE,KAAK,kBAAkB,EAAE,MAAM,iBAAiB,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
export * from "@1sat/connect";
|
|
3
|
+
|
|
4
|
+
// src/context.tsx
|
|
5
|
+
import {
|
|
6
|
+
createOneSat
|
|
7
|
+
} from "@1sat/connect";
|
|
8
|
+
import {
|
|
9
|
+
createContext,
|
|
10
|
+
useCallback,
|
|
11
|
+
useContext,
|
|
12
|
+
useEffect,
|
|
13
|
+
useMemo,
|
|
14
|
+
useState
|
|
15
|
+
} from "react";
|
|
16
|
+
import { jsxDEV } from "react/jsx-dev-runtime";
|
|
17
|
+
"use client";
|
|
18
|
+
var OneSatContext = createContext(null);
|
|
19
|
+
function OneSatProvider({ config, children }) {
|
|
20
|
+
const [provider, setProvider] = useState(null);
|
|
21
|
+
const [isConnected, setIsConnected] = useState(false);
|
|
22
|
+
const [isConnecting, setIsConnecting] = useState(false);
|
|
23
|
+
const [paymentAddress, setPaymentAddress] = useState(null);
|
|
24
|
+
const [ordinalAddress, setOrdinalAddress] = useState(null);
|
|
25
|
+
const [identityPubKey, setIdentityPubKey] = useState(null);
|
|
26
|
+
const [error, setError] = useState(null);
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
if (typeof window === "undefined")
|
|
29
|
+
return;
|
|
30
|
+
const p = createOneSat(config);
|
|
31
|
+
setProvider(p);
|
|
32
|
+
if (p.isConnected()) {
|
|
33
|
+
const addresses = p.getAddresses();
|
|
34
|
+
if (addresses) {
|
|
35
|
+
setIsConnected(true);
|
|
36
|
+
setPaymentAddress(addresses.paymentAddress);
|
|
37
|
+
setOrdinalAddress(addresses.ordinalAddress);
|
|
38
|
+
setIdentityPubKey(p.getIdentityPubKey());
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const handleConnect = (result) => {
|
|
42
|
+
setIsConnected(true);
|
|
43
|
+
setPaymentAddress(result.paymentAddress);
|
|
44
|
+
setOrdinalAddress(result.ordinalAddress);
|
|
45
|
+
setIdentityPubKey(result.identityPubKey);
|
|
46
|
+
};
|
|
47
|
+
const handleDisconnect = () => {
|
|
48
|
+
setIsConnected(false);
|
|
49
|
+
setPaymentAddress(null);
|
|
50
|
+
setOrdinalAddress(null);
|
|
51
|
+
setIdentityPubKey(null);
|
|
52
|
+
};
|
|
53
|
+
p.on("connect", handleConnect);
|
|
54
|
+
p.on("disconnect", handleDisconnect);
|
|
55
|
+
return () => {
|
|
56
|
+
p.off("connect", handleConnect);
|
|
57
|
+
p.off("disconnect", handleDisconnect);
|
|
58
|
+
};
|
|
59
|
+
}, [config]);
|
|
60
|
+
const connect = useCallback(async () => {
|
|
61
|
+
if (!provider) {
|
|
62
|
+
throw new Error("Provider not initialized");
|
|
63
|
+
}
|
|
64
|
+
setIsConnecting(true);
|
|
65
|
+
setError(null);
|
|
66
|
+
try {
|
|
67
|
+
const result = await provider.connect();
|
|
68
|
+
return result;
|
|
69
|
+
} catch (e) {
|
|
70
|
+
const err = e instanceof Error ? e : new Error(String(e));
|
|
71
|
+
setError(err);
|
|
72
|
+
throw err;
|
|
73
|
+
} finally {
|
|
74
|
+
setIsConnecting(false);
|
|
75
|
+
}
|
|
76
|
+
}, [provider]);
|
|
77
|
+
const disconnect = useCallback(async () => {
|
|
78
|
+
if (!provider)
|
|
79
|
+
return;
|
|
80
|
+
await provider.disconnect();
|
|
81
|
+
}, [provider]);
|
|
82
|
+
const value = useMemo(() => ({
|
|
83
|
+
provider,
|
|
84
|
+
isConnected,
|
|
85
|
+
isConnecting,
|
|
86
|
+
paymentAddress,
|
|
87
|
+
ordinalAddress,
|
|
88
|
+
identityPubKey,
|
|
89
|
+
connect,
|
|
90
|
+
disconnect,
|
|
91
|
+
error
|
|
92
|
+
}), [
|
|
93
|
+
provider,
|
|
94
|
+
isConnected,
|
|
95
|
+
isConnecting,
|
|
96
|
+
paymentAddress,
|
|
97
|
+
ordinalAddress,
|
|
98
|
+
identityPubKey,
|
|
99
|
+
connect,
|
|
100
|
+
disconnect,
|
|
101
|
+
error
|
|
102
|
+
]);
|
|
103
|
+
return /* @__PURE__ */ jsxDEV(OneSatContext.Provider, {
|
|
104
|
+
value,
|
|
105
|
+
children
|
|
106
|
+
}, undefined, false, undefined, this);
|
|
107
|
+
}
|
|
108
|
+
function useOneSatContext() {
|
|
109
|
+
const context = useContext(OneSatContext);
|
|
110
|
+
if (!context) {
|
|
111
|
+
throw new Error("useOneSatContext must be used within an OneSatProvider");
|
|
112
|
+
}
|
|
113
|
+
return context;
|
|
114
|
+
}
|
|
115
|
+
// src/hooks.ts
|
|
116
|
+
import { useCallback as useCallback2, useEffect as useEffect2, useState as useState2 } from "react";
|
|
117
|
+
"use client";
|
|
118
|
+
function useOneSat() {
|
|
119
|
+
const context = useOneSatContext();
|
|
120
|
+
return {
|
|
121
|
+
provider: context.provider,
|
|
122
|
+
isConnected: context.isConnected,
|
|
123
|
+
isConnecting: context.isConnecting,
|
|
124
|
+
paymentAddress: context.paymentAddress,
|
|
125
|
+
ordinalAddress: context.ordinalAddress,
|
|
126
|
+
identityPubKey: context.identityPubKey,
|
|
127
|
+
connect: context.connect,
|
|
128
|
+
disconnect: context.disconnect,
|
|
129
|
+
error: context.error
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
function useBalance() {
|
|
133
|
+
const { provider, isConnected } = useOneSatContext();
|
|
134
|
+
const [balance, setBalance] = useState2(null);
|
|
135
|
+
const [isLoading, setIsLoading] = useState2(false);
|
|
136
|
+
const [error, setError] = useState2(null);
|
|
137
|
+
const refetch = useCallback2(async () => {
|
|
138
|
+
if (!provider || !isConnected)
|
|
139
|
+
return;
|
|
140
|
+
setIsLoading(true);
|
|
141
|
+
setError(null);
|
|
142
|
+
try {
|
|
143
|
+
const result = await provider.getBalance();
|
|
144
|
+
setBalance(result);
|
|
145
|
+
} catch (e) {
|
|
146
|
+
setError(e instanceof Error ? e : new Error(String(e)));
|
|
147
|
+
} finally {
|
|
148
|
+
setIsLoading(false);
|
|
149
|
+
}
|
|
150
|
+
}, [provider, isConnected]);
|
|
151
|
+
useEffect2(() => {
|
|
152
|
+
if (isConnected) {
|
|
153
|
+
refetch();
|
|
154
|
+
} else {
|
|
155
|
+
setBalance(null);
|
|
156
|
+
}
|
|
157
|
+
}, [isConnected, refetch]);
|
|
158
|
+
return {
|
|
159
|
+
satoshis: balance?.satoshis ?? 0,
|
|
160
|
+
usd: balance?.usd ?? 0,
|
|
161
|
+
isLoading,
|
|
162
|
+
error,
|
|
163
|
+
refetch
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
function useOrdinals(options) {
|
|
167
|
+
const { provider, isConnected } = useOneSatContext();
|
|
168
|
+
const [ordinals, setOrdinals] = useState2([]);
|
|
169
|
+
const [isLoading, setIsLoading] = useState2(false);
|
|
170
|
+
const [error, setError] = useState2(null);
|
|
171
|
+
const refetch = useCallback2(async () => {
|
|
172
|
+
if (!provider || !isConnected)
|
|
173
|
+
return;
|
|
174
|
+
setIsLoading(true);
|
|
175
|
+
setError(null);
|
|
176
|
+
try {
|
|
177
|
+
const result = await provider.getOrdinals(options);
|
|
178
|
+
setOrdinals(result);
|
|
179
|
+
} catch (e) {
|
|
180
|
+
setError(e instanceof Error ? e : new Error(String(e)));
|
|
181
|
+
} finally {
|
|
182
|
+
setIsLoading(false);
|
|
183
|
+
}
|
|
184
|
+
}, [provider, isConnected, options]);
|
|
185
|
+
useEffect2(() => {
|
|
186
|
+
if (isConnected) {
|
|
187
|
+
refetch();
|
|
188
|
+
} else {
|
|
189
|
+
setOrdinals([]);
|
|
190
|
+
}
|
|
191
|
+
}, [isConnected, refetch]);
|
|
192
|
+
return {
|
|
193
|
+
ordinals,
|
|
194
|
+
isLoading,
|
|
195
|
+
error,
|
|
196
|
+
refetch
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
function useTokens(options) {
|
|
200
|
+
const { provider, isConnected } = useOneSatContext();
|
|
201
|
+
const [tokens, setTokens] = useState2([]);
|
|
202
|
+
const [isLoading, setIsLoading] = useState2(false);
|
|
203
|
+
const [error, setError] = useState2(null);
|
|
204
|
+
const refetch = useCallback2(async () => {
|
|
205
|
+
if (!provider || !isConnected)
|
|
206
|
+
return;
|
|
207
|
+
setIsLoading(true);
|
|
208
|
+
setError(null);
|
|
209
|
+
try {
|
|
210
|
+
const result = await provider.getTokens(options);
|
|
211
|
+
setTokens(result);
|
|
212
|
+
} catch (e) {
|
|
213
|
+
setError(e instanceof Error ? e : new Error(String(e)));
|
|
214
|
+
} finally {
|
|
215
|
+
setIsLoading(false);
|
|
216
|
+
}
|
|
217
|
+
}, [provider, isConnected, options]);
|
|
218
|
+
useEffect2(() => {
|
|
219
|
+
if (isConnected) {
|
|
220
|
+
refetch();
|
|
221
|
+
} else {
|
|
222
|
+
setTokens([]);
|
|
223
|
+
}
|
|
224
|
+
}, [isConnected, refetch]);
|
|
225
|
+
return {
|
|
226
|
+
tokens,
|
|
227
|
+
isLoading,
|
|
228
|
+
error,
|
|
229
|
+
refetch
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
function useSignTransaction() {
|
|
233
|
+
const { provider, isConnected } = useOneSatContext();
|
|
234
|
+
const [isLoading, setIsLoading] = useState2(false);
|
|
235
|
+
const [error, setError] = useState2(null);
|
|
236
|
+
const signTransaction = useCallback2(async (rawtx, description) => {
|
|
237
|
+
if (!provider || !isConnected) {
|
|
238
|
+
throw new Error("Wallet not connected");
|
|
239
|
+
}
|
|
240
|
+
setIsLoading(true);
|
|
241
|
+
setError(null);
|
|
242
|
+
try {
|
|
243
|
+
const result = await provider.signTransaction({ rawtx, description });
|
|
244
|
+
return result;
|
|
245
|
+
} catch (e) {
|
|
246
|
+
const err = e instanceof Error ? e : new Error(String(e));
|
|
247
|
+
setError(err);
|
|
248
|
+
throw err;
|
|
249
|
+
} finally {
|
|
250
|
+
setIsLoading(false);
|
|
251
|
+
}
|
|
252
|
+
}, [provider, isConnected]);
|
|
253
|
+
return {
|
|
254
|
+
signTransaction,
|
|
255
|
+
isLoading,
|
|
256
|
+
error
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
function useSignMessage() {
|
|
260
|
+
const { provider, isConnected } = useOneSatContext();
|
|
261
|
+
const [isLoading, setIsLoading] = useState2(false);
|
|
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,
|
|
282
|
+
isLoading,
|
|
283
|
+
error
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
function useInscribe() {
|
|
287
|
+
const { provider, isConnected } = useOneSatContext();
|
|
288
|
+
const [isLoading, setIsLoading] = useState2(false);
|
|
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,
|
|
309
|
+
isLoading,
|
|
310
|
+
error
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
// src/ConnectButton.tsx
|
|
314
|
+
import { jsxDEV as jsxDEV2, Fragment } from "react/jsx-dev-runtime";
|
|
315
|
+
"use client";
|
|
316
|
+
function truncateAddress(address) {
|
|
317
|
+
if (address.length <= 12)
|
|
318
|
+
return address;
|
|
319
|
+
return `${address.slice(0, 6)}...${address.slice(-4)}`;
|
|
320
|
+
}
|
|
321
|
+
function ConnectButton({
|
|
322
|
+
className,
|
|
323
|
+
style,
|
|
324
|
+
connectLabel = "Connect Wallet",
|
|
325
|
+
connectingLabel = "Connecting...",
|
|
326
|
+
connectedLabel,
|
|
327
|
+
onConnect,
|
|
328
|
+
onDisconnect,
|
|
329
|
+
onError,
|
|
330
|
+
disconnectOnClick = true,
|
|
331
|
+
children
|
|
332
|
+
}) {
|
|
333
|
+
const {
|
|
334
|
+
isConnected,
|
|
335
|
+
isConnecting,
|
|
336
|
+
paymentAddress,
|
|
337
|
+
connect: providerConnect,
|
|
338
|
+
disconnect: providerDisconnect
|
|
339
|
+
} = useOneSat();
|
|
340
|
+
const connect = async () => {
|
|
341
|
+
try {
|
|
342
|
+
await providerConnect();
|
|
343
|
+
onConnect?.();
|
|
344
|
+
} catch (e) {
|
|
345
|
+
onError?.(e instanceof Error ? e : new Error(String(e)));
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
const disconnect = async () => {
|
|
349
|
+
try {
|
|
350
|
+
await providerDisconnect();
|
|
351
|
+
onDisconnect?.();
|
|
352
|
+
} catch (e) {
|
|
353
|
+
onError?.(e instanceof Error ? e : new Error(String(e)));
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
if (children) {
|
|
357
|
+
return /* @__PURE__ */ jsxDEV2(Fragment, {
|
|
358
|
+
children: children({
|
|
359
|
+
isConnected,
|
|
360
|
+
isConnecting,
|
|
361
|
+
address: paymentAddress,
|
|
362
|
+
connect,
|
|
363
|
+
disconnect
|
|
364
|
+
})
|
|
365
|
+
}, undefined, false, undefined, this);
|
|
366
|
+
}
|
|
367
|
+
if (isConnecting) {
|
|
368
|
+
return /* @__PURE__ */ jsxDEV2("button", {
|
|
369
|
+
className,
|
|
370
|
+
style,
|
|
371
|
+
disabled: true,
|
|
372
|
+
type: "button",
|
|
373
|
+
children: connectingLabel
|
|
374
|
+
}, undefined, false, undefined, this);
|
|
375
|
+
}
|
|
376
|
+
if (isConnected) {
|
|
377
|
+
const label = typeof connectedLabel === "function" ? connectedLabel(paymentAddress ?? "") : connectedLabel ?? truncateAddress(paymentAddress ?? "");
|
|
378
|
+
return /* @__PURE__ */ jsxDEV2("button", {
|
|
379
|
+
className,
|
|
380
|
+
style,
|
|
381
|
+
onClick: disconnectOnClick ? disconnect : undefined,
|
|
382
|
+
type: "button",
|
|
383
|
+
children: label
|
|
384
|
+
}, undefined, false, undefined, this);
|
|
385
|
+
}
|
|
386
|
+
return /* @__PURE__ */ jsxDEV2("button", {
|
|
387
|
+
className,
|
|
388
|
+
style,
|
|
389
|
+
onClick: connect,
|
|
390
|
+
type: "button",
|
|
391
|
+
children: connectLabel
|
|
392
|
+
}, undefined, false, undefined, this);
|
|
393
|
+
}
|
|
394
|
+
export {
|
|
395
|
+
useTokens,
|
|
396
|
+
useSignTransaction,
|
|
397
|
+
useSignMessage,
|
|
398
|
+
useOrdinals,
|
|
399
|
+
useOneSatContext,
|
|
400
|
+
useOneSat,
|
|
401
|
+
useInscribe,
|
|
402
|
+
useBalance,
|
|
403
|
+
OneSatProvider,
|
|
404
|
+
ConnectButton
|
|
405
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@1sat/react",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "React hooks and components for 1Sat wallet integration",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": ["dist"],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "bun build ./src/index.ts --outdir ./dist --target browser --external react --external @1sat/connect && tsc --emitDeclarationOnly",
|
|
17
|
+
"dev": "tsc --watch"
|
|
18
|
+
},
|
|
19
|
+
"keywords": ["1sat", "bsv", "ordinals", "wallet", "sdk", "react", "hooks"],
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@1sat/connect": "workspace:*"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"react": "^18.0.0 || ^19.0.0"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/react": "^19.0.0",
|
|
29
|
+
"react": "^19.0.0",
|
|
30
|
+
"typescript": "^5.9.3"
|
|
31
|
+
}
|
|
32
|
+
}
|