@pear-protocol/symmio-client 0.2.39 → 0.2.41
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/index.d.mts +3 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +31 -17
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +31 -17
- package/dist/index.mjs.map +1 -1
- package/dist/react/index.d.mts +14 -13
- package/dist/react/index.d.ts +14 -13
- package/dist/react/index.js +61 -126
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mjs +61 -126
- package/dist/react/index.mjs.map +1 -1
- package/package.json +2 -1
package/dist/react/index.js
CHANGED
|
@@ -5,6 +5,7 @@ var react = require('react');
|
|
|
5
5
|
var symmCore = require('@pear-protocol/symm-core');
|
|
6
6
|
var zustand = require('zustand');
|
|
7
7
|
var jsxRuntime = require('react/jsx-runtime');
|
|
8
|
+
var siwe = require('siwe');
|
|
8
9
|
var reactQuery = require('@tanstack/react-query');
|
|
9
10
|
var viem = require('viem');
|
|
10
11
|
|
|
@@ -556,19 +557,17 @@ function createSiweMessage(params) {
|
|
|
556
557
|
const expirationTime = new Date(
|
|
557
558
|
Date.now() + 30 * 24 * 60 * 60 * 1e3
|
|
558
559
|
).toISOString();
|
|
559
|
-
const message =
|
|
560
|
-
|
|
561
|
-
params.address,
|
|
562
|
-
|
|
563
|
-
params.
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
`Expiration Time: ${expirationTime}`
|
|
571
|
-
].join("\n");
|
|
560
|
+
const message = new siwe.SiweMessage({
|
|
561
|
+
domain: params.domain,
|
|
562
|
+
address: params.address,
|
|
563
|
+
statement: params.statement,
|
|
564
|
+
chainId: params.chainId,
|
|
565
|
+
nonce: params.nonce,
|
|
566
|
+
version,
|
|
567
|
+
uri: params.uri,
|
|
568
|
+
issuedAt,
|
|
569
|
+
expirationTime
|
|
570
|
+
}).prepareMessage();
|
|
572
571
|
return { message, issuedAt, expirationTime };
|
|
573
572
|
}
|
|
574
573
|
async function getNonce(chainId, subAccount) {
|
|
@@ -597,10 +596,24 @@ async function login(chainId, params) {
|
|
|
597
596
|
body: JSON.stringify(body)
|
|
598
597
|
});
|
|
599
598
|
if (!response.ok) {
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
599
|
+
let errorMessage = "";
|
|
600
|
+
if (typeof response.text === "function") {
|
|
601
|
+
const rawBody = await response.text().catch(() => "");
|
|
602
|
+
if (rawBody) {
|
|
603
|
+
try {
|
|
604
|
+
const parsed = JSON.parse(rawBody);
|
|
605
|
+
errorMessage = parsed.error_message ?? parsed.message ?? parsed.detail ?? rawBody;
|
|
606
|
+
} catch {
|
|
607
|
+
errorMessage = rawBody;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
if (!errorMessage && typeof response.json === "function") {
|
|
612
|
+
const errorData = await response.json().catch(() => null);
|
|
613
|
+
errorMessage = errorData?.error_message ?? errorData?.message ?? errorData?.detail ?? "";
|
|
614
|
+
}
|
|
615
|
+
const reason = errorMessage || response.statusText;
|
|
616
|
+
throw new Error(reason ? `Login failed: ${reason}` : "Login failed");
|
|
604
617
|
}
|
|
605
618
|
const data = await response.json();
|
|
606
619
|
if (!data.access_token) {
|
|
@@ -664,15 +677,15 @@ function clearCachedToken(address, chainId) {
|
|
|
664
677
|
}
|
|
665
678
|
async function fetchAccessToken(walletClient, signerAddress, accountAddress, chainId, domain) {
|
|
666
679
|
const resolvedDomain = domain ?? (typeof window !== "undefined" ? window.location.host : "localhost");
|
|
667
|
-
const
|
|
680
|
+
const hedgerLoginUrl = new URL("login", getHedgerBaseUrl(chainId)).href;
|
|
668
681
|
const nonce = await getNonce(chainId, accountAddress);
|
|
669
682
|
const { message, issuedAt, expirationTime } = createSiweMessage({
|
|
670
683
|
address: signerAddress,
|
|
671
|
-
statement:
|
|
684
|
+
statement: `msg: ${accountAddress}`,
|
|
672
685
|
chainId,
|
|
673
686
|
nonce,
|
|
674
687
|
domain: resolvedDomain,
|
|
675
|
-
uri
|
|
688
|
+
uri: hedgerLoginUrl
|
|
676
689
|
});
|
|
677
690
|
const signature = await walletClient.signMessage({
|
|
678
691
|
account: signerAddress,
|
|
@@ -732,40 +745,30 @@ function logSymmAuth(event, details) {
|
|
|
732
745
|
}
|
|
733
746
|
console.log("[symm-auth]", event, details ?? {});
|
|
734
747
|
}
|
|
735
|
-
function extractSubaccountAddresses(summary) {
|
|
736
|
-
const accounts = summary?.data?.accounts;
|
|
737
|
-
if (!Array.isArray(accounts)) {
|
|
738
|
-
return [];
|
|
739
|
-
}
|
|
740
|
-
return accounts.flatMap((account) => {
|
|
741
|
-
const accountAddress = account.accountAddress ?? account.address;
|
|
742
|
-
return accountAddress ? [accountAddress] : [];
|
|
743
|
-
});
|
|
744
|
-
}
|
|
745
748
|
function useSymmAuth(params) {
|
|
746
749
|
const context = useSymmContext();
|
|
747
750
|
const ctx = context;
|
|
748
|
-
const { symmCoreClient } = context;
|
|
749
751
|
const address = params?.address ?? ctx.address;
|
|
750
752
|
const chainId = params?.chainId ?? ctx.chainId ?? 42161;
|
|
751
753
|
const walletClient = params?.walletClient ?? ctx.walletClient;
|
|
752
754
|
const siweDomain = params?.siweDomain;
|
|
755
|
+
const activeAccountAddress = params?.activeAccountAddress ?? address;
|
|
753
756
|
const token = useSymmAuthStore((state) => {
|
|
754
|
-
if (
|
|
755
|
-
return state.tokensByKey[symmAuthTokenKey(
|
|
757
|
+
if (activeAccountAddress) {
|
|
758
|
+
return state.tokensByKey[symmAuthTokenKey(activeAccountAddress, chainId)] ?? null;
|
|
756
759
|
}
|
|
757
760
|
return ctx.accessToken ?? ctx.authToken ?? null;
|
|
758
761
|
});
|
|
759
762
|
const setToken = useSymmAuthStore((state) => state.setToken);
|
|
760
763
|
const getToken = useSymmAuthStore((state) => state.getToken);
|
|
761
764
|
const clearToken = useSymmAuthStore((state) => state.clearToken);
|
|
762
|
-
const previousAddressRef = react.useRef(
|
|
765
|
+
const previousAddressRef = react.useRef(activeAccountAddress);
|
|
763
766
|
const previousChainIdRef = react.useRef(chainId);
|
|
764
767
|
const refreshAuthRef = react.useRef(async () => null);
|
|
765
768
|
const refreshAuth = react.useCallback(
|
|
766
769
|
async (accountAddress, options) => {
|
|
767
770
|
if (!walletClient || !address) return null;
|
|
768
|
-
const resolvedAccountAddress = accountAddress ?? address;
|
|
771
|
+
const resolvedAccountAddress = accountAddress ?? activeAccountAddress ?? address;
|
|
769
772
|
if (!options?.force) {
|
|
770
773
|
const inStore = getToken(resolvedAccountAddress, chainId);
|
|
771
774
|
if (inStore) {
|
|
@@ -817,139 +820,70 @@ function useSymmAuth(params) {
|
|
|
817
820
|
return null;
|
|
818
821
|
}
|
|
819
822
|
},
|
|
820
|
-
[walletClient, address, chainId, siweDomain, getToken, setToken, clearToken]
|
|
823
|
+
[walletClient, address, activeAccountAddress, chainId, siweDomain, getToken, setToken, clearToken]
|
|
821
824
|
);
|
|
822
825
|
refreshAuthRef.current = refreshAuth;
|
|
823
826
|
const clearAuth = react.useCallback(() => {
|
|
824
|
-
if (
|
|
825
|
-
clearCachedToken(
|
|
826
|
-
clearToken(
|
|
827
|
+
if (activeAccountAddress) {
|
|
828
|
+
clearCachedToken(activeAccountAddress, chainId);
|
|
829
|
+
clearToken(activeAccountAddress, chainId);
|
|
827
830
|
}
|
|
828
|
-
}, [
|
|
829
|
-
console.log("symm auth", {
|
|
830
|
-
address,
|
|
831
|
-
chainId,
|
|
832
|
-
walletClient,
|
|
833
|
-
symmCoreClient
|
|
834
|
-
});
|
|
831
|
+
}, [activeAccountAddress, chainId, clearToken]);
|
|
835
832
|
react.useEffect(() => {
|
|
836
833
|
const previousAddress = previousAddressRef.current;
|
|
837
834
|
const previousChainId = previousChainIdRef.current;
|
|
838
|
-
const addressChanged = previousAddress !==
|
|
835
|
+
const addressChanged = previousAddress !== activeAccountAddress;
|
|
839
836
|
const chainChanged = previousChainId !== chainId;
|
|
840
837
|
let cancelled = false;
|
|
841
|
-
previousAddressRef.current =
|
|
838
|
+
previousAddressRef.current = activeAccountAddress;
|
|
842
839
|
previousChainIdRef.current = chainId;
|
|
843
840
|
logSymmAuth("bootstrap:dependencies", {
|
|
844
|
-
address,
|
|
841
|
+
signerAddress: address,
|
|
842
|
+
activeAccountAddress,
|
|
845
843
|
chainId,
|
|
846
844
|
hasWalletClient: !!walletClient,
|
|
847
|
-
hasSymmCoreClient: !!symmCoreClient,
|
|
848
845
|
hasRefreshAuth: !!refreshAuthRef.current,
|
|
849
846
|
addressChanged,
|
|
850
847
|
chainChanged,
|
|
851
848
|
previousAddress,
|
|
852
849
|
previousChainId
|
|
853
850
|
});
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
hasWalletClient: !!walletClient,
|
|
858
|
-
hasSymmCoreClient: !!symmCoreClient,
|
|
859
|
-
hasRefreshAuth: !!refreshAuthRef.current,
|
|
860
|
-
addressChanged,
|
|
861
|
-
chainChanged,
|
|
862
|
-
previousAddress,
|
|
863
|
-
previousChainId
|
|
864
|
-
});
|
|
865
|
-
if (!address) {
|
|
866
|
-
logSymmAuth("bootstrap:skip-no-address", {
|
|
851
|
+
if (!activeAccountAddress) {
|
|
852
|
+
logSymmAuth("bootstrap:skip-no-active-account", {
|
|
853
|
+
signerAddress: address,
|
|
867
854
|
chainId,
|
|
868
|
-
hasWalletClient: !!walletClient
|
|
869
|
-
hasSymmCoreClient: !!symmCoreClient
|
|
870
|
-
});
|
|
871
|
-
console.log("bootstrap:skip-no-address", {
|
|
872
|
-
chainId,
|
|
873
|
-
hasWalletClient: !!walletClient,
|
|
874
|
-
hasSymmCoreClient: !!symmCoreClient
|
|
855
|
+
hasWalletClient: !!walletClient
|
|
875
856
|
});
|
|
876
857
|
return;
|
|
877
858
|
}
|
|
878
|
-
const userAddress = address;
|
|
879
859
|
if (previousAddress && (addressChanged || chainChanged)) {
|
|
880
860
|
clearCachedToken(previousAddress, previousChainId);
|
|
881
861
|
clearToken(previousAddress, previousChainId);
|
|
882
862
|
}
|
|
883
863
|
async function bootstrapAuth() {
|
|
884
864
|
logSymmAuth("bootstrap:run", {
|
|
885
|
-
|
|
865
|
+
signerAddress: address,
|
|
866
|
+
activeAccountAddress,
|
|
886
867
|
chainId,
|
|
887
868
|
hasWalletClient: !!walletClient,
|
|
888
|
-
hasSymmCoreClient: !!symmCoreClient,
|
|
889
869
|
hasRefreshAuth: !!refreshAuthRef.current
|
|
890
870
|
});
|
|
891
|
-
console.log("bootstrap:run", {
|
|
892
|
-
userAddress,
|
|
893
|
-
chainId,
|
|
894
|
-
hasWalletClient: !!walletClient,
|
|
895
|
-
hasSymmCoreClient: !!symmCoreClient,
|
|
896
|
-
hasRefreshAuth: !!refreshAuthRef.current
|
|
897
|
-
});
|
|
898
|
-
let targets = [userAddress];
|
|
899
|
-
if (symmCoreClient) {
|
|
900
|
-
try {
|
|
901
|
-
const summary = await symmCoreClient.accounts.getSummary({
|
|
902
|
-
userAddress,
|
|
903
|
-
chainId
|
|
904
|
-
});
|
|
905
|
-
const subaccounts = extractSubaccountAddresses(summary);
|
|
906
|
-
if (subaccounts.length > 0) {
|
|
907
|
-
targets = subaccounts;
|
|
908
|
-
}
|
|
909
|
-
logSymmAuth("bootstrap:subaccounts-resolved", {
|
|
910
|
-
userAddress,
|
|
911
|
-
chainId,
|
|
912
|
-
subaccounts
|
|
913
|
-
});
|
|
914
|
-
console.log("bootstrap:subaccounts-resolved", {
|
|
915
|
-
userAddress,
|
|
916
|
-
chainId,
|
|
917
|
-
subaccounts
|
|
918
|
-
});
|
|
919
|
-
} catch (error) {
|
|
920
|
-
logSymmAuth("bootstrap:subaccounts-failed", {
|
|
921
|
-
userAddress,
|
|
922
|
-
chainId,
|
|
923
|
-
error: error instanceof Error ? error.message : String(error)
|
|
924
|
-
});
|
|
925
|
-
console.log("bootstrap:subaccounts-failed", {
|
|
926
|
-
userAddress,
|
|
927
|
-
chainId,
|
|
928
|
-
error: error instanceof Error ? error.message : String(error)
|
|
929
|
-
});
|
|
930
|
-
}
|
|
931
|
-
}
|
|
932
|
-
const uniqueTargets = [...new Set(targets)].filter(
|
|
933
|
-
(accountAddress) => !!accountAddress
|
|
934
|
-
);
|
|
935
871
|
logSymmAuth("bootstrap:start", {
|
|
936
|
-
|
|
872
|
+
signerAddress: address,
|
|
937
873
|
chainId,
|
|
938
|
-
|
|
874
|
+
activeAccountAddress,
|
|
939
875
|
walletReady: !!walletClient
|
|
940
876
|
});
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
return;
|
|
944
|
-
}
|
|
945
|
-
await refreshAuthRef.current(accountAddress);
|
|
877
|
+
if (cancelled) {
|
|
878
|
+
return;
|
|
946
879
|
}
|
|
880
|
+
await refreshAuthRef.current(activeAccountAddress);
|
|
947
881
|
}
|
|
948
|
-
bootstrapAuth();
|
|
882
|
+
void bootstrapAuth();
|
|
949
883
|
return () => {
|
|
950
884
|
cancelled = true;
|
|
951
885
|
};
|
|
952
|
-
}, [address, walletClient, chainId,
|
|
886
|
+
}, [address, activeAccountAddress, walletClient, chainId, clearToken]);
|
|
953
887
|
return {
|
|
954
888
|
accessToken: token,
|
|
955
889
|
authToken: token,
|
|
@@ -25201,6 +25135,7 @@ function useResolveTradeAuthToken(params = {}) {
|
|
|
25201
25135
|
const chainId = params.chainId ?? context.chainId;
|
|
25202
25136
|
const { refreshAuth } = useSymmAuth({
|
|
25203
25137
|
address,
|
|
25138
|
+
activeAccountAddress: address,
|
|
25204
25139
|
chainId,
|
|
25205
25140
|
walletClient: params.walletClient,
|
|
25206
25141
|
siweDomain: params.siweDomain
|