@compilot/react-sdk 2.3.9-dev → 2.3.10-dev

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.
@@ -744,223 +744,6 @@ var useAsyncMutationState = function useAsyncMutationState(_ref) {
744
744
  return _objectSpread2(_objectSpread2({}, globalState), {}, _defineProperty(_defineProperty(_defineProperty({}, mutationName, mutateAsync), "setData", setData), "setPending", setPending));
745
745
  };
746
746
 
747
- /**
748
- * A hook that returns a function that opens the ComPilot ID widget.
749
- *
750
- * @param loginParams - The login parameters to use when opening the widget.
751
- * @returns The async mutation state of the widget opening.
752
- *
753
- * @category Hook
754
- */
755
- var useOpenWidget = function useOpenWidget(loginParams) {
756
- var config = useComPilotConfig();
757
- var mutationFn = react.useCallback(function () {
758
- return webSdk.openWidget(config, loginParams);
759
- }, [config]);
760
- var mutationState = useAsyncMutationState({
761
- mutationName: "openWidget",
762
- mutationKey: ["openWidget", config._internal.identifier],
763
- mutationFn: mutationFn,
764
- defaultValue: webSdk.isOpen(config)
765
- });
766
- react.useEffect(function () {
767
- var unsubscribe = webSdk.watchWidgetVisibleState(config, {
768
- onChange: function onChange(isVisible) {
769
- mutationState.setData(isVisible);
770
- },
771
- onIsLoadingChange: mutationState.setPending
772
- });
773
- return unsubscribe;
774
- }, [config, mutationState.setData, mutationState.setPending]);
775
- return mutationState;
776
- };
777
-
778
- /**
779
- * A callback that returns a transaction authorization data signature.
780
- *
781
- * @category Callback
782
- */
783
-
784
- /**
785
- * A hook that returns a function that returns a transaction authorization data signature.
786
- *
787
- * @returns A function that returns a transaction authorization data signature.
788
- *
789
- * @category Hook
790
- *
791
- * @example
792
- *
793
- * ##### EIP-155 Transaction Wagmi Example
794
- *
795
- * ```tsx
796
- * import { useGetTxAuthDataSignature } from "@compilot/react-sdk";
797
- * import { waitForTransactionReceipt } from "viem/actions";
798
- * import { encodeFunctionData } from "viem";
799
- * import { usePublicClient, useWalletClient } from "wagmi";
800
- *
801
- * const MyComponent = () => {
802
- *
803
- * const { getTxAuthDataSignature } = useGetTxAuthDataSignature();
804
- * const walletClient = useWalletClient();
805
- * const publicClient = usePublicClient();
806
- *
807
- * const gatedNftMint = async () => {
808
- *
809
- * // Get signature for the given parameters
810
- * const signatureResponse = await getTxAuthDataSignature({
811
- * namespace: "eip155",
812
- * contractAbi,
813
- * contractAddress,
814
- * functionName: "mintNFTGated",
815
- * args: [account.address],
816
- * userAddress: account.address,
817
- * chainId: EvmChainId.parse(chainId),
818
- * });
819
- *
820
- * if (!signatureResponse.isAuthorized) {
821
- * throw new Error("User is not authorized");
822
- * }
823
- *
824
- * // Mint Gated Nft with signature
825
- * const unsignedTx = encodeFunctionData({
826
- * abi: contractAbi,
827
- * functionName: "mintNFTGated",
828
- * args: [account.address],
829
- * });
830
- *
831
- * // Build the raw transaction data with the signature
832
- * const txData = (unsignedTx + signatureResponse.payload) as `0x${string}`;
833
- *
834
- * // try to mint nft
835
- * const tx = await walletClient.data.sendTransaction({
836
- * to: contractAddress,
837
- * data: txData,
838
- * });
839
- *
840
- * const receipt = await waitForTransactionReceipt(publicClient, {
841
- * hash: tx,
842
- * });
843
- *
844
- * return receipt;
845
- * };
846
- *
847
- * return <button onClick={gatedNftMint}>Mint Gated NFT</button>;
848
- * };
849
- * ```
850
- *
851
- * ### Tezos Transaction Example
852
- *
853
- * ```tsx
854
- * import { useGetTxAuthDataSignature } from "@compilot/react-sdk";
855
- * import { packDataBytes, Parser } from "@taquito/michel-codec";
856
- * import type { MichelsonData, MichelsonType } from "@taquito/michel-codec";
857
- * import { RpcClient } from "@taquito/rpc";
858
- *
859
- * const MyComponent = () => {
860
- * const { getTxAuthDataSignature } = useGetTxAuthDataSignature();
861
- * const wallet = useWallet();
862
- *
863
- * const signAndSend = async () => {
864
- *
865
- * // prepare the mint function call
866
- * const storage: any = await claimerContract.storage();
867
- * const lastAssetId = storage.siggated_extension.extension.lastMinted.toNumber() as number;
868
- * const functionCallArgs = {
869
- * owner: userAddress,
870
- * token_id: (lastAssetId + 1).toString(), //"1",
871
- * };
872
- * const functionCallArgsBytes = convertMint(
873
- * functionCallArgs.owner,
874
- * functionCallArgs.token_id,
875
- * );
876
- *
877
- * // Get signature for the given parameters
878
- * const signatureResponse = await getTxAuthDataSignature({
879
- * namespace: "tezos",
880
- * contractAddress: "KT1JN7a2es4Ne8SuePZU7YrHKG49hfgCCyBK",
881
- * functionName: "%mint_gated%",
882
- * args: functionCallArgsBytes,
883
- * chainID: TezosChainId.parse(currentChainId),
884
- * userAddress: userAddress as TezosImplicitAddress,
885
- * });
886
- *
887
- * // Check if the user is authorized
888
- * if (!signatureResponse.isAuthorized) {
889
- * return {
890
- * signatureResponse: {
891
- * isAuthorized: false,
892
- * signature: "None",
893
- * },
894
- * };
895
- * }
896
- *
897
- * // Mint Gated Nft with signature
898
- * const op = await claimerContract.methodsObject
899
- * .mint_gated({
900
- * userAddress,
901
- * expirationBlock: signatureResponse.blockExpiration,
902
- * functionName,
903
- * functionArgs: functionCallArgsBytes,
904
- * signerPublicKey: COMPILOT_SIGNER_PK,
905
- * signature: signatureResponse.signature,
906
- * })
907
- * .send();
908
- * await op.confirmation(2);
909
- * };
910
- * return <button onClick={signAndSend}>Sign and Send</button>;
911
- * };
912
- *
913
- *
914
- * // Helper function to convert mint function to bytes
915
- * function convertMint(owner_str: string, token_id: string) {
916
- * const data = `(Pair "${owner_str}" ${token_id})`;
917
- * const type = `(pair address nat)`;
918
- * const p = new Parser();
919
- * const dataJSON = p.parseMichelineExpression(data);
920
- * const typeJSON = p.parseMichelineExpression(type);
921
- * const packed = packDataBytes(
922
- * dataJSON as MichelsonData,
923
- * typeJSON as MichelsonType,
924
- * );
925
- * return packed.bytes;
926
- * }
927
- *
928
- * // Tezos signer public key
929
- * const COMPILOT_SIGNER_PK = "edpkurPsQ8eUApnLUJ9ZPDvu98E8VNj4KtJa1aZr16Cr5ow5VHKnz4";
930
- * const client = new RpcClient("https://rpc.ghostnet.teztnets.com/");
931
- * ```
932
- *
933
- */
934
- var useGetTxAuthDataSignature = function useGetTxAuthDataSignature() {
935
- var config = useComPilotConfig();
936
- var mutationFn = react.useCallback(/*#__PURE__*/function () {
937
- var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(params) {
938
- var res;
939
- return _regeneratorRuntime().wrap(function _callee$(_context) {
940
- while (1) switch (_context.prev = _context.next) {
941
- case 0:
942
- _context.next = 2;
943
- return webSdk.getTxAuthDataSignature(config, params);
944
- case 2:
945
- res = _context.sent;
946
- return _context.abrupt("return", res);
947
- case 4:
948
- case "end":
949
- return _context.stop();
950
- }
951
- }, _callee);
952
- }));
953
- return function (_x) {
954
- return _ref.apply(this, arguments);
955
- };
956
- }(), [config._internal.widgetStateStore, config._internal.identifier]);
957
- return useAsyncMutationState({
958
- mutationName: "getTxAuthDataSignature",
959
- mutationKey: ["getTxAuthDataSignature", config._internal.identifier],
960
- mutationFn: mutationFn
961
- });
962
- };
963
-
964
747
  /**
965
748
  * A hook that returns a function that authenticates the user without opening the ComPilot ID widget.
966
749
  * This will authenticate the user only if they are not already authenticated.
@@ -1232,12 +1015,296 @@ var useDisconnect = function useDisconnect() {
1232
1015
  });
1233
1016
  };
1234
1017
 
1018
+ /**
1019
+ * A callback that returns a transaction authorization data signature.
1020
+ *
1021
+ * @category Callback
1022
+ */
1023
+
1024
+ /**
1025
+ * A hook that returns a function that returns a transaction authorization data signature.
1026
+ *
1027
+ * @returns A function that returns a transaction authorization data signature.
1028
+ *
1029
+ * @category Hook
1030
+ *
1031
+ * @example
1032
+ *
1033
+ * ##### EIP-155 Transaction Wagmi Example
1034
+ *
1035
+ * ```tsx
1036
+ * import { useGetTxAuthDataSignature } from "@compilot/react-sdk";
1037
+ * import { waitForTransactionReceipt } from "viem/actions";
1038
+ * import { encodeFunctionData } from "viem";
1039
+ * import { usePublicClient, useWalletClient } from "wagmi";
1040
+ *
1041
+ * const MyComponent = () => {
1042
+ *
1043
+ * const { getTxAuthDataSignature } = useGetTxAuthDataSignature();
1044
+ * const walletClient = useWalletClient();
1045
+ * const publicClient = usePublicClient();
1046
+ *
1047
+ * const gatedNftMint = async () => {
1048
+ *
1049
+ * // Get signature for the given parameters
1050
+ * const signatureResponse = await getTxAuthDataSignature({
1051
+ * namespace: "eip155",
1052
+ * contractAbi,
1053
+ * contractAddress,
1054
+ * functionName: "mintNFTGated",
1055
+ * args: [account.address],
1056
+ * userAddress: account.address,
1057
+ * chainId: EvmChainId.parse(chainId),
1058
+ * });
1059
+ *
1060
+ * if (!signatureResponse.isAuthorized) {
1061
+ * throw new Error("User is not authorized");
1062
+ * }
1063
+ *
1064
+ * // Mint Gated Nft with signature
1065
+ * const unsignedTx = encodeFunctionData({
1066
+ * abi: contractAbi,
1067
+ * functionName: "mintNFTGated",
1068
+ * args: [account.address],
1069
+ * });
1070
+ *
1071
+ * // Build the raw transaction data with the signature
1072
+ * const txData = (unsignedTx + signatureResponse.payload) as `0x${string}`;
1073
+ *
1074
+ * // try to mint nft
1075
+ * const tx = await walletClient.data.sendTransaction({
1076
+ * to: contractAddress,
1077
+ * data: txData,
1078
+ * });
1079
+ *
1080
+ * const receipt = await waitForTransactionReceipt(publicClient, {
1081
+ * hash: tx,
1082
+ * });
1083
+ *
1084
+ * return receipt;
1085
+ * };
1086
+ *
1087
+ * return <button onClick={gatedNftMint}>Mint Gated NFT</button>;
1088
+ * };
1089
+ * ```
1090
+ *
1091
+ * ### Tezos Transaction Example
1092
+ *
1093
+ * ```tsx
1094
+ * import { useGetTxAuthDataSignature } from "@compilot/react-sdk";
1095
+ * import { packDataBytes, Parser } from "@taquito/michel-codec";
1096
+ * import type { MichelsonData, MichelsonType } from "@taquito/michel-codec";
1097
+ * import { RpcClient } from "@taquito/rpc";
1098
+ *
1099
+ * const MyComponent = () => {
1100
+ * const { getTxAuthDataSignature } = useGetTxAuthDataSignature();
1101
+ * const wallet = useWallet();
1102
+ *
1103
+ * const signAndSend = async () => {
1104
+ *
1105
+ * // prepare the mint function call
1106
+ * const storage: any = await claimerContract.storage();
1107
+ * const lastAssetId = storage.siggated_extension.extension.lastMinted.toNumber() as number;
1108
+ * const functionCallArgs = {
1109
+ * owner: userAddress,
1110
+ * token_id: (lastAssetId + 1).toString(), //"1",
1111
+ * };
1112
+ * const functionCallArgsBytes = convertMint(
1113
+ * functionCallArgs.owner,
1114
+ * functionCallArgs.token_id,
1115
+ * );
1116
+ *
1117
+ * // Get signature for the given parameters
1118
+ * const signatureResponse = await getTxAuthDataSignature({
1119
+ * namespace: "tezos",
1120
+ * contractAddress: "KT1JN7a2es4Ne8SuePZU7YrHKG49hfgCCyBK",
1121
+ * functionName: "%mint_gated%",
1122
+ * args: functionCallArgsBytes,
1123
+ * chainID: TezosChainId.parse(currentChainId),
1124
+ * userAddress: userAddress as TezosImplicitAddress,
1125
+ * });
1126
+ *
1127
+ * // Check if the user is authorized
1128
+ * if (!signatureResponse.isAuthorized) {
1129
+ * return {
1130
+ * signatureResponse: {
1131
+ * isAuthorized: false,
1132
+ * signature: "None",
1133
+ * },
1134
+ * };
1135
+ * }
1136
+ *
1137
+ * // Mint Gated Nft with signature
1138
+ * const op = await claimerContract.methodsObject
1139
+ * .mint_gated({
1140
+ * userAddress,
1141
+ * expirationBlock: signatureResponse.blockExpiration,
1142
+ * functionName,
1143
+ * functionArgs: functionCallArgsBytes,
1144
+ * signerPublicKey: COMPILOT_SIGNER_PK,
1145
+ * signature: signatureResponse.signature,
1146
+ * })
1147
+ * .send();
1148
+ * await op.confirmation(2);
1149
+ * };
1150
+ * return <button onClick={signAndSend}>Sign and Send</button>;
1151
+ * };
1152
+ *
1153
+ *
1154
+ * // Helper function to convert mint function to bytes
1155
+ * function convertMint(owner_str: string, token_id: string) {
1156
+ * const data = `(Pair "${owner_str}" ${token_id})`;
1157
+ * const type = `(pair address nat)`;
1158
+ * const p = new Parser();
1159
+ * const dataJSON = p.parseMichelineExpression(data);
1160
+ * const typeJSON = p.parseMichelineExpression(type);
1161
+ * const packed = packDataBytes(
1162
+ * dataJSON as MichelsonData,
1163
+ * typeJSON as MichelsonType,
1164
+ * );
1165
+ * return packed.bytes;
1166
+ * }
1167
+ *
1168
+ * // Tezos signer public key
1169
+ * const COMPILOT_SIGNER_PK = "edpkurPsQ8eUApnLUJ9ZPDvu98E8VNj4KtJa1aZr16Cr5ow5VHKnz4";
1170
+ * const client = new RpcClient("https://rpc.ghostnet.teztnets.com/");
1171
+ * ```
1172
+ *
1173
+ */
1174
+ var useGetTxAuthDataSignature = function useGetTxAuthDataSignature() {
1175
+ var config = useComPilotConfig();
1176
+ var mutationFn = react.useCallback(/*#__PURE__*/function () {
1177
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(params) {
1178
+ var res;
1179
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
1180
+ while (1) switch (_context.prev = _context.next) {
1181
+ case 0:
1182
+ _context.next = 2;
1183
+ return webSdk.getTxAuthDataSignature(config, params);
1184
+ case 2:
1185
+ res = _context.sent;
1186
+ return _context.abrupt("return", res);
1187
+ case 4:
1188
+ case "end":
1189
+ return _context.stop();
1190
+ }
1191
+ }, _callee);
1192
+ }));
1193
+ return function (_x) {
1194
+ return _ref.apply(this, arguments);
1195
+ };
1196
+ }(), [config._internal.widgetStateStore, config._internal.identifier]);
1197
+ return useAsyncMutationState({
1198
+ mutationName: "getTxAuthDataSignature",
1199
+ mutationKey: ["getTxAuthDataSignature", config._internal.identifier],
1200
+ mutationFn: mutationFn
1201
+ });
1202
+ };
1203
+
1204
+ /**
1205
+ * A hook that returns the current identity wallets.
1206
+ */
1207
+ var useIdentityWallets = function useIdentityWallets() {
1208
+ var config = useComPilotConfig();
1209
+ var auth = config._internal.widgetStateStore.select(function (state) {
1210
+ return state.auth;
1211
+ });
1212
+ var _useState = react.useState(0),
1213
+ _useState2 = _slicedToArray(_useState, 2),
1214
+ queryCacheBuster = _useState2[0],
1215
+ setQueryCacheBuster = _useState2[1];
1216
+ var _useAsyncQueryState = useAsyncQueryState({
1217
+ queryKey: ["useIdentityWallets", config._internal.identifier, auth === "loading" || auth === "not_authenticated" ? "" : auth.token],
1218
+ defaultValue: null
1219
+ }),
1220
+ state = _useAsyncQueryState.state,
1221
+ setError = _useAsyncQueryState.setError,
1222
+ setResult = _useAsyncQueryState.setResult,
1223
+ startLoading = _useAsyncQueryState.startLoading;
1224
+ react.useEffect(function () {
1225
+ if (!config) {
1226
+ return;
1227
+ }
1228
+ startLoading();
1229
+ webSdk.getIdentityWallets(config).then(function (wallets) {
1230
+ setResult(wallets);
1231
+ })["catch"](function (err) {
1232
+ config._internal.logger.error("Failed to fetch identity wallets", err);
1233
+ setError(err instanceof Error ? err : new Error("Unknown error"));
1234
+ });
1235
+ }, [config, queryCacheBuster]);
1236
+ var mutationFn = react.useCallback(/*#__PURE__*/function () {
1237
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(wallet) {
1238
+ var res;
1239
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
1240
+ while (1) switch (_context.prev = _context.next) {
1241
+ case 0:
1242
+ res = webSdk.attachWalletToIdentity(config, {
1243
+ wallet: wallet
1244
+ });
1245
+ setQueryCacheBuster(function (prev) {
1246
+ return prev + 1;
1247
+ });
1248
+ return _context.abrupt("return", res);
1249
+ case 3:
1250
+ case "end":
1251
+ return _context.stop();
1252
+ }
1253
+ }, _callee);
1254
+ }));
1255
+ return function (_x) {
1256
+ return _ref.apply(this, arguments);
1257
+ };
1258
+ }(), [config]);
1259
+ var addWalletMutation = useAsyncMutationState({
1260
+ mutationName: "attachWalletToIdentity",
1261
+ mutationKey: ["attachWalletToIdentity", config._internal.identifier, auth === "loading" || auth === "not_authenticated" ? "" : auth.token],
1262
+ mutationFn: mutationFn
1263
+ });
1264
+ return {
1265
+ identityWallets: state,
1266
+ addWalletMutation: addWalletMutation
1267
+ };
1268
+ };
1269
+
1270
+ /**
1271
+ * A hook that returns a function that opens the ComPilot ID widget.
1272
+ *
1273
+ * @param loginParams - The login parameters to use when opening the widget.
1274
+ * @returns The async mutation state of the widget opening.
1275
+ *
1276
+ * @category Hook
1277
+ */
1278
+ var useOpenWidget = function useOpenWidget(loginParams) {
1279
+ var config = useComPilotConfig();
1280
+ var mutationFn = react.useCallback(function () {
1281
+ return webSdk.openWidget(config, loginParams);
1282
+ }, [config]);
1283
+ var mutationState = useAsyncMutationState({
1284
+ mutationName: "openWidget",
1285
+ mutationKey: ["openWidget", config._internal.identifier],
1286
+ mutationFn: mutationFn,
1287
+ defaultValue: webSdk.isOpen(config)
1288
+ });
1289
+ react.useEffect(function () {
1290
+ var unsubscribe = webSdk.watchWidgetVisibleState(config, {
1291
+ onChange: function onChange(isVisible) {
1292
+ mutationState.setData(isVisible);
1293
+ },
1294
+ onIsLoadingChange: mutationState.setPending
1295
+ });
1296
+ return unsubscribe;
1297
+ }, [config, mutationState.setData, mutationState.setPending]);
1298
+ return mutationState;
1299
+ };
1300
+
1235
1301
  exports.ComPilotProvider = ComPilotProvider;
1236
1302
  exports.useAuthenticate = useAuthenticate;
1237
1303
  exports.useComPilotConfig = useComPilotConfig;
1238
1304
  exports.useCustomerStatus = useCustomerStatus;
1239
1305
  exports.useDisconnect = useDisconnect;
1240
1306
  exports.useGetTxAuthDataSignature = useGetTxAuthDataSignature;
1307
+ exports.useIdentityWallets = useIdentityWallets;
1241
1308
  exports.useOpenWidget = useOpenWidget;
1242
1309
  Object.keys(webSdk).forEach(function (k) {
1243
1310
  if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {