@satoshai/kit 0.5.0 → 0.6.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 +62 -0
- package/dist/index.cjs +149 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +54 -2
- package/dist/index.d.ts +54 -2
- package/dist/index.js +148 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
# @satoshai/kit
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/@satoshai/kit)
|
|
4
|
+
[](https://github.com/satoshai-dev/kit/actions/workflows/ci.yml)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
|
|
3
7
|
Typesafe Stacks wallet & contract interaction library for React. Wagmi-inspired hook API for connecting wallets, signing messages, and calling contracts on the Stacks blockchain.
|
|
4
8
|
|
|
5
9
|
## Features
|
|
@@ -9,6 +13,8 @@ Typesafe Stacks wallet & contract interaction library for React. Wagmi-inspired
|
|
|
9
13
|
- **`useWallets`** — Configured wallets with availability status
|
|
10
14
|
- **`useAddress`** — Access connected wallet address and status
|
|
11
15
|
- **`useSignMessage`** — Sign arbitrary messages
|
|
16
|
+
- **`useSignStructuredMessage`** — Sign SIP-018 structured data
|
|
17
|
+
- **`useSignTransaction`** — Sign serialized transactions (sponsored tx flows)
|
|
12
18
|
- **`useWriteContract`** — Call smart contracts with post-conditions
|
|
13
19
|
- **`useTransferSTX`** — Native STX transfers
|
|
14
20
|
- **`useBnsName`** — Resolve BNS v2 names
|
|
@@ -180,6 +186,40 @@ signMessage({ message: 'Hello Stacks' }, {
|
|
|
180
186
|
const { publicKey, signature } = await signMessageAsync({ message: 'Hello Stacks' });
|
|
181
187
|
```
|
|
182
188
|
|
|
189
|
+
### `useSignStructuredMessage()`
|
|
190
|
+
|
|
191
|
+
Sign SIP-018 structured data for typed, verifiable off-chain messages.
|
|
192
|
+
|
|
193
|
+
> **Note:** OKX wallet does not support structured message signing and will throw an error.
|
|
194
|
+
|
|
195
|
+
```ts
|
|
196
|
+
import { tupleCV, stringAsciiCV, uintCV } from '@stacks/transactions';
|
|
197
|
+
|
|
198
|
+
const { signStructuredMessage, signStructuredMessageAsync, data, error, isPending } = useSignStructuredMessage();
|
|
199
|
+
|
|
200
|
+
// Callback style
|
|
201
|
+
signStructuredMessage({
|
|
202
|
+
domain: tupleCV({
|
|
203
|
+
name: stringAsciiCV('MyApp'),
|
|
204
|
+
version: stringAsciiCV('1.0'),
|
|
205
|
+
'chain-id': uintCV(1),
|
|
206
|
+
}),
|
|
207
|
+
message: tupleCV({
|
|
208
|
+
action: stringAsciiCV('authorize'),
|
|
209
|
+
amount: uintCV(1000),
|
|
210
|
+
}),
|
|
211
|
+
}, {
|
|
212
|
+
onSuccess: ({ publicKey, signature }) => {},
|
|
213
|
+
onError: (error) => {},
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
// Async style
|
|
217
|
+
const { publicKey, signature } = await signStructuredMessageAsync({
|
|
218
|
+
domain: tupleCV({ ... }),
|
|
219
|
+
message: tupleCV({ ... }),
|
|
220
|
+
});
|
|
221
|
+
```
|
|
222
|
+
|
|
183
223
|
### `useTransferSTX()`
|
|
184
224
|
|
|
185
225
|
```ts
|
|
@@ -224,6 +264,28 @@ writeContract({
|
|
|
224
264
|
});
|
|
225
265
|
```
|
|
226
266
|
|
|
267
|
+
### `useSignTransaction()`
|
|
268
|
+
|
|
269
|
+
Sign a serialized transaction without automatically broadcasting it. Useful for sponsored transaction flows where a separate service pays the fee.
|
|
270
|
+
|
|
271
|
+
> **Note:** OKX wallet does not support raw transaction signing and will throw an error.
|
|
272
|
+
|
|
273
|
+
```ts
|
|
274
|
+
const { signTransaction, signTransactionAsync, data, error, isPending } = useSignTransaction();
|
|
275
|
+
|
|
276
|
+
// Callback style
|
|
277
|
+
signTransaction({ transaction: '0x0100...', broadcast: false }, {
|
|
278
|
+
onSuccess: ({ transaction, txid }) => {},
|
|
279
|
+
onError: (error) => {},
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
// Async style
|
|
283
|
+
const { transaction, txid } = await signTransactionAsync({
|
|
284
|
+
transaction: '0x0100...',
|
|
285
|
+
broadcast: false,
|
|
286
|
+
});
|
|
287
|
+
```
|
|
288
|
+
|
|
227
289
|
### `useBnsName()`
|
|
228
290
|
|
|
229
291
|
```ts
|
package/dist/index.cjs
CHANGED
|
@@ -752,6 +752,153 @@ var useSignMessage = () => {
|
|
|
752
752
|
[signMessage, signMessageAsync, reset, data, error, status]
|
|
753
753
|
);
|
|
754
754
|
};
|
|
755
|
+
var useSignStructuredMessage = () => {
|
|
756
|
+
const { isConnected, provider } = useAddress();
|
|
757
|
+
const [data, setData] = react.useState(
|
|
758
|
+
void 0
|
|
759
|
+
);
|
|
760
|
+
const [error, setError] = react.useState(null);
|
|
761
|
+
const [status, setStatus] = react.useState("idle");
|
|
762
|
+
const signStructuredMessageAsync = react.useCallback(
|
|
763
|
+
async (variables) => {
|
|
764
|
+
if (!isConnected) {
|
|
765
|
+
throw new Error("Wallet is not connected");
|
|
766
|
+
}
|
|
767
|
+
if (provider === "okx") {
|
|
768
|
+
throw new Error(
|
|
769
|
+
"Structured message signing is not supported by OKX wallet"
|
|
770
|
+
);
|
|
771
|
+
}
|
|
772
|
+
setStatus("pending");
|
|
773
|
+
setError(null);
|
|
774
|
+
setData(void 0);
|
|
775
|
+
try {
|
|
776
|
+
const result = await connect.request("stx_signStructuredMessage", {
|
|
777
|
+
message: variables.message,
|
|
778
|
+
domain: variables.domain
|
|
779
|
+
});
|
|
780
|
+
setData(result);
|
|
781
|
+
setStatus("success");
|
|
782
|
+
return result;
|
|
783
|
+
} catch (err) {
|
|
784
|
+
const error2 = err instanceof Error ? err : new Error(String(err));
|
|
785
|
+
setError(error2);
|
|
786
|
+
setStatus("error");
|
|
787
|
+
throw error2;
|
|
788
|
+
}
|
|
789
|
+
},
|
|
790
|
+
[isConnected, provider]
|
|
791
|
+
);
|
|
792
|
+
const signStructuredMessage = react.useCallback(
|
|
793
|
+
(variables, options) => {
|
|
794
|
+
signStructuredMessageAsync(variables).then((data2) => {
|
|
795
|
+
options?.onSuccess?.(data2);
|
|
796
|
+
options?.onSettled?.(data2, null);
|
|
797
|
+
}).catch((error2) => {
|
|
798
|
+
options?.onError?.(error2);
|
|
799
|
+
options?.onSettled?.(void 0, error2);
|
|
800
|
+
});
|
|
801
|
+
},
|
|
802
|
+
[signStructuredMessageAsync]
|
|
803
|
+
);
|
|
804
|
+
const reset = react.useCallback(() => {
|
|
805
|
+
setData(void 0);
|
|
806
|
+
setError(null);
|
|
807
|
+
setStatus("idle");
|
|
808
|
+
}, []);
|
|
809
|
+
return react.useMemo(
|
|
810
|
+
() => ({
|
|
811
|
+
signStructuredMessage,
|
|
812
|
+
signStructuredMessageAsync,
|
|
813
|
+
reset,
|
|
814
|
+
data,
|
|
815
|
+
error,
|
|
816
|
+
isError: status === "error",
|
|
817
|
+
isIdle: status === "idle",
|
|
818
|
+
isPending: status === "pending",
|
|
819
|
+
isSuccess: status === "success",
|
|
820
|
+
status
|
|
821
|
+
}),
|
|
822
|
+
[
|
|
823
|
+
signStructuredMessage,
|
|
824
|
+
signStructuredMessageAsync,
|
|
825
|
+
reset,
|
|
826
|
+
data,
|
|
827
|
+
error,
|
|
828
|
+
status
|
|
829
|
+
]
|
|
830
|
+
);
|
|
831
|
+
};
|
|
832
|
+
var useSignTransaction = () => {
|
|
833
|
+
const { isConnected, provider } = useAddress();
|
|
834
|
+
const [data, setData] = react.useState(void 0);
|
|
835
|
+
const [error, setError] = react.useState(null);
|
|
836
|
+
const [status, setStatus] = react.useState("idle");
|
|
837
|
+
const signTransactionAsync = react.useCallback(
|
|
838
|
+
async (variables) => {
|
|
839
|
+
if (!isConnected) {
|
|
840
|
+
throw new Error("Wallet is not connected");
|
|
841
|
+
}
|
|
842
|
+
if (provider === "okx") {
|
|
843
|
+
throw new Error(
|
|
844
|
+
"Transaction signing is not supported by OKX wallet"
|
|
845
|
+
);
|
|
846
|
+
}
|
|
847
|
+
setStatus("pending");
|
|
848
|
+
setError(null);
|
|
849
|
+
setData(void 0);
|
|
850
|
+
try {
|
|
851
|
+
const result = await connect.request("stx_signTransaction", {
|
|
852
|
+
transaction: variables.transaction,
|
|
853
|
+
...variables.broadcast !== void 0 && {
|
|
854
|
+
broadcast: variables.broadcast
|
|
855
|
+
}
|
|
856
|
+
});
|
|
857
|
+
setData(result);
|
|
858
|
+
setStatus("success");
|
|
859
|
+
return result;
|
|
860
|
+
} catch (err) {
|
|
861
|
+
const error2 = err instanceof Error ? err : new Error(String(err));
|
|
862
|
+
setError(error2);
|
|
863
|
+
setStatus("error");
|
|
864
|
+
throw error2;
|
|
865
|
+
}
|
|
866
|
+
},
|
|
867
|
+
[isConnected, provider]
|
|
868
|
+
);
|
|
869
|
+
const signTransaction = react.useCallback(
|
|
870
|
+
(variables, options) => {
|
|
871
|
+
signTransactionAsync(variables).then((data2) => {
|
|
872
|
+
options?.onSuccess?.(data2);
|
|
873
|
+
options?.onSettled?.(data2, null);
|
|
874
|
+
}).catch((error2) => {
|
|
875
|
+
options?.onError?.(error2);
|
|
876
|
+
options?.onSettled?.(void 0, error2);
|
|
877
|
+
});
|
|
878
|
+
},
|
|
879
|
+
[signTransactionAsync]
|
|
880
|
+
);
|
|
881
|
+
const reset = react.useCallback(() => {
|
|
882
|
+
setData(void 0);
|
|
883
|
+
setError(null);
|
|
884
|
+
setStatus("idle");
|
|
885
|
+
}, []);
|
|
886
|
+
return react.useMemo(
|
|
887
|
+
() => ({
|
|
888
|
+
signTransaction,
|
|
889
|
+
signTransactionAsync,
|
|
890
|
+
reset,
|
|
891
|
+
data,
|
|
892
|
+
error,
|
|
893
|
+
isError: status === "error",
|
|
894
|
+
isIdle: status === "idle",
|
|
895
|
+
isPending: status === "pending",
|
|
896
|
+
isSuccess: status === "success",
|
|
897
|
+
status
|
|
898
|
+
}),
|
|
899
|
+
[signTransaction, signTransactionAsync, reset, data, error, status]
|
|
900
|
+
);
|
|
901
|
+
};
|
|
755
902
|
|
|
756
903
|
// src/utils/get-network-from-address.ts
|
|
757
904
|
var getNetworkFromAddress = (address) => {
|
|
@@ -1144,6 +1291,8 @@ exports.useBnsName = useBnsName;
|
|
|
1144
1291
|
exports.useConnect = useConnect;
|
|
1145
1292
|
exports.useDisconnect = useDisconnect;
|
|
1146
1293
|
exports.useSignMessage = useSignMessage;
|
|
1294
|
+
exports.useSignStructuredMessage = useSignStructuredMessage;
|
|
1295
|
+
exports.useSignTransaction = useSignTransaction;
|
|
1147
1296
|
exports.useTransferSTX = useTransferSTX;
|
|
1148
1297
|
exports.useWallets = useWallets;
|
|
1149
1298
|
exports.useWriteContract = useWriteContract;
|