@zyfai/sdk 1.8.0 → 1.9.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/README.md +6 -33
- package/dist/index.d.mts +19 -148
- package/dist/index.d.ts +19 -148
- package/dist/index.js +451 -527
- package/dist/index.mjs +453 -541
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -61,7 +61,6 @@ __export(index_exports, {
|
|
|
61
61
|
ZyfaiSDK: () => ZyfaiSDK,
|
|
62
62
|
buildV1ChainSessions: () => buildV1ChainSessions,
|
|
63
63
|
createBankrProvider: () => createBankrProvider,
|
|
64
|
-
createSafeOwnerProvider: () => createSafeOwnerProvider,
|
|
65
64
|
decodeUserOperationCalls: () => decodeUserOperationCalls,
|
|
66
65
|
getChainConfig: () => getChainConfig,
|
|
67
66
|
getDataApiBaseUrl: () => getDataApiBaseUrl,
|
|
@@ -1413,6 +1412,10 @@ var INTENT_EXECUTOR = "0x00000000005aD9ce1f5035FD62CA96CEf16AdAAF";
|
|
|
1413
1412
|
var PROXY_EXECUTOR = "0xF659d30D4EB88B06A909F20839D8959Bd77d8790";
|
|
1414
1413
|
var INTENT_SMART_SESSIONS = "0xad568b3f825a8d5ffc06dd3253526b64d810ae89";
|
|
1415
1414
|
var OWNABLE_VALIDATOR = "0x000000000013fdB5234E4E3162a810F54d9f7E98";
|
|
1415
|
+
var OWNABLE_VALIDATOR_DEPLOYMENTS = [
|
|
1416
|
+
"0x2483DA3A338895199E5e538530213157e931Bf06",
|
|
1417
|
+
OWNABLE_VALIDATOR
|
|
1418
|
+
];
|
|
1416
1419
|
var SAFE_7579_ADDRESS = "0x7579EE8307284F293B1927136486880611F20002";
|
|
1417
1420
|
var ERC7579_LAUNCHPAD_ADDRESS = "0x7579011aB74c46090561ea277Ba79D510c6C00ff";
|
|
1418
1421
|
var POOL_MODULE_ADDRESSES = [
|
|
@@ -1551,12 +1554,9 @@ async function signMultiChainSessionTypedData(walletClient, typedData) {
|
|
|
1551
1554
|
if (!walletClient.account) {
|
|
1552
1555
|
throw new Error("Wallet not connected. Please connect your wallet first.");
|
|
1553
1556
|
}
|
|
1554
|
-
return walletClient.
|
|
1557
|
+
return walletClient.signMessage({
|
|
1555
1558
|
account: walletClient.account,
|
|
1556
|
-
|
|
1557
|
-
types: typedData.types,
|
|
1558
|
-
primaryType: typedData.primaryType,
|
|
1559
|
-
message: typedData.message
|
|
1559
|
+
message: { raw: (0, import_viem5.hashTypedData)(typedData) }
|
|
1560
1560
|
});
|
|
1561
1561
|
}
|
|
1562
1562
|
|
|
@@ -1645,13 +1645,12 @@ var isOwnableValidatorOwner = async (safeAddress, userAddress, publicClient) =>
|
|
|
1645
1645
|
}
|
|
1646
1646
|
return false;
|
|
1647
1647
|
};
|
|
1648
|
+
var isEip7702Delegation = (code) => !!code && code.toLowerCase().startsWith("0xef0100");
|
|
1649
|
+
var hasPersistentContractCode = (code) => !!code && code !== "0x" && !isEip7702Delegation(code);
|
|
1648
1650
|
var getAccountType = async (address, publicClient) => {
|
|
1649
1651
|
try {
|
|
1650
1652
|
const code = await publicClient.getCode({ address });
|
|
1651
|
-
if (!code || code === "0x" || code.length === 2) {
|
|
1652
|
-
return "EOA";
|
|
1653
|
-
}
|
|
1654
|
-
if (code.startsWith("0xef01")) {
|
|
1653
|
+
if (!code || code === "0x" || code.length === 2 || isEip7702Delegation(code)) {
|
|
1655
1654
|
return "EOA";
|
|
1656
1655
|
}
|
|
1657
1656
|
try {
|
|
@@ -1679,23 +1678,85 @@ var getAccountType = async (address, publicClient) => {
|
|
|
1679
1678
|
return "Unknown";
|
|
1680
1679
|
}
|
|
1681
1680
|
};
|
|
1681
|
+
var signNestedSafeDeployTypedData = async (owner, parentSafe, typedData) => {
|
|
1682
|
+
if (!owner.account) {
|
|
1683
|
+
throw new Error("Wallet not connected. Please connect your wallet first.");
|
|
1684
|
+
}
|
|
1685
|
+
return owner.signTypedData({
|
|
1686
|
+
account: parentSafe,
|
|
1687
|
+
domain: typedData.domain,
|
|
1688
|
+
types: typedData.types,
|
|
1689
|
+
primaryType: typedData.primaryType,
|
|
1690
|
+
message: typedData.message
|
|
1691
|
+
});
|
|
1692
|
+
};
|
|
1693
|
+
var completeSafeDeploy = async (config, strategyBody, body) => {
|
|
1694
|
+
const deployResponse = await config.httpClient.post(
|
|
1695
|
+
`${ENDPOINTS.SAFE_DEPLOY}?chainId=${config.chainId}`,
|
|
1696
|
+
{ ...body, ...strategyBody }
|
|
1697
|
+
);
|
|
1698
|
+
if (!deployResponse.success) {
|
|
1699
|
+
throw new Error(
|
|
1700
|
+
`Safe deployment failed: ${JSON.stringify(deployResponse)}`
|
|
1701
|
+
);
|
|
1702
|
+
}
|
|
1703
|
+
return {
|
|
1704
|
+
safeAddress: deployResponse.safeAddress || "0x",
|
|
1705
|
+
txHash: deployResponse.txHash,
|
|
1706
|
+
isDeployed: true
|
|
1707
|
+
};
|
|
1708
|
+
};
|
|
1682
1709
|
var deploySafeAccount = async (config) => {
|
|
1683
1710
|
try {
|
|
1684
|
-
const {
|
|
1711
|
+
const {
|
|
1712
|
+
owner,
|
|
1713
|
+
httpClient,
|
|
1714
|
+
chainId,
|
|
1715
|
+
publicClient,
|
|
1716
|
+
safeOwnerAddress,
|
|
1717
|
+
strategy
|
|
1718
|
+
} = config;
|
|
1685
1719
|
if (!owner || !owner.account) {
|
|
1686
1720
|
throw new Error(
|
|
1687
1721
|
"Wallet not connected. Please connect your wallet first."
|
|
1688
1722
|
);
|
|
1689
1723
|
}
|
|
1690
1724
|
console.log("strategy", strategy);
|
|
1725
|
+
const strategyBody = strategy ? { strategy } : {};
|
|
1691
1726
|
const prepareResponse = await httpClient.post(
|
|
1692
1727
|
`${ENDPOINTS.SAFE_DEPLOY}?chainId=${chainId}`,
|
|
1693
|
-
|
|
1728
|
+
strategyBody
|
|
1694
1729
|
);
|
|
1695
1730
|
console.log("prepareResponse", prepareResponse);
|
|
1731
|
+
if (prepareResponse.success && !prepareResponse.userOpHashToSign && prepareResponse.signingMode !== "typed-data") {
|
|
1732
|
+
return {
|
|
1733
|
+
safeAddress: prepareResponse.safeAddress || "0x",
|
|
1734
|
+
txHash: "0x0",
|
|
1735
|
+
isDeployed: true
|
|
1736
|
+
};
|
|
1737
|
+
}
|
|
1738
|
+
if (prepareResponse.signingMode === "typed-data") {
|
|
1739
|
+
if (!prepareResponse.typedData) {
|
|
1740
|
+
throw new Error(
|
|
1741
|
+
"Backend returned signingMode typed-data without typedData. Response: " + JSON.stringify(prepareResponse)
|
|
1742
|
+
);
|
|
1743
|
+
}
|
|
1744
|
+
const parentSafe = (0, import_viem6.getAddress)(safeOwnerAddress);
|
|
1745
|
+
console.info(
|
|
1746
|
+
`[ZyfaiSDK] safe-deploy typed-data chainId=${chainId} owner=${parentSafe}`
|
|
1747
|
+
);
|
|
1748
|
+
const typedDataSignature = await signNestedSafeDeployTypedData(
|
|
1749
|
+
owner,
|
|
1750
|
+
parentSafe,
|
|
1751
|
+
prepareResponse.typedData
|
|
1752
|
+
);
|
|
1753
|
+
return completeSafeDeploy(config, strategyBody, {
|
|
1754
|
+
typedDataSignature
|
|
1755
|
+
});
|
|
1756
|
+
}
|
|
1696
1757
|
if (!prepareResponse.userOpHashToSign) {
|
|
1697
1758
|
throw new Error(
|
|
1698
|
-
"Backend did not return userOpHashToSign. Response: " + JSON.stringify(prepareResponse)
|
|
1759
|
+
"Backend did not return userOpHashToSign or typed-data to sign. Response: " + JSON.stringify(prepareResponse)
|
|
1699
1760
|
);
|
|
1700
1761
|
}
|
|
1701
1762
|
const userOpHashToSign = prepareResponse.userOpHashToSign;
|
|
@@ -1703,20 +1764,29 @@ var deploySafeAccount = async (config) => {
|
|
|
1703
1764
|
account: owner.account,
|
|
1704
1765
|
message: { raw: userOpHashToSign }
|
|
1705
1766
|
});
|
|
1706
|
-
const
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
if (!deployResponse.success) {
|
|
1711
|
-
throw new Error(
|
|
1712
|
-
`Safe deployment failed: ${JSON.stringify(deployResponse)}`
|
|
1767
|
+
const ownerAddress = (0, import_viem6.getAddress)(safeOwnerAddress);
|
|
1768
|
+
const ownerCode = await publicClient.getCode({ address: ownerAddress }).catch((error) => {
|
|
1769
|
+
console.warn(
|
|
1770
|
+
`[ZyfaiSDK] Could not read code at owner ${ownerAddress}: ${error.message}. Submitting the owner signature unwrapped; a contract owner will be rejected with AA24.`
|
|
1713
1771
|
);
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1772
|
+
return void 0;
|
|
1773
|
+
});
|
|
1774
|
+
const delegatedEoa = isEip7702Delegation(ownerCode);
|
|
1775
|
+
const wrapContractSignature = hasPersistentContractCode(ownerCode);
|
|
1776
|
+
const ownerKind = delegatedEoa ? "eip7702" : wrapContractSignature ? "contract" : "eoa";
|
|
1777
|
+
console.info(
|
|
1778
|
+
`[ZyfaiSDK] safe-deploy signature chainId=${chainId} owner=${ownerAddress} ownerKind=${ownerKind} wrapped=${wrapContractSignature}`
|
|
1779
|
+
);
|
|
1780
|
+
const signatureToSubmit = wrapContractSignature ? buildSafeSignatureBytes([
|
|
1781
|
+
{
|
|
1782
|
+
signer: ownerAddress,
|
|
1783
|
+
data: userOpSignature,
|
|
1784
|
+
isContractSignature: true
|
|
1785
|
+
}
|
|
1786
|
+
]) : userOpSignature;
|
|
1787
|
+
return completeSafeDeploy(config, strategyBody, {
|
|
1788
|
+
userOpSignature: signatureToSubmit
|
|
1789
|
+
});
|
|
1720
1790
|
} catch (error) {
|
|
1721
1791
|
throw new Error(
|
|
1722
1792
|
`Failed to deploy Safe account: ${error.message}`
|
|
@@ -1778,6 +1848,33 @@ var signSessionKey = async (config, sessions, allPublicClients, signingParams) =
|
|
|
1778
1848
|
sessionNonces
|
|
1779
1849
|
};
|
|
1780
1850
|
};
|
|
1851
|
+
var IS_MODULE_INSTALLED_ABI = [
|
|
1852
|
+
{
|
|
1853
|
+
type: "function",
|
|
1854
|
+
name: "isModuleInstalled",
|
|
1855
|
+
stateMutability: "view",
|
|
1856
|
+
inputs: [
|
|
1857
|
+
{ name: "moduleTypeId", type: "uint256" },
|
|
1858
|
+
{ name: "module", type: "address" },
|
|
1859
|
+
{ name: "additionalContext", type: "bytes" }
|
|
1860
|
+
],
|
|
1861
|
+
outputs: [{ type: "bool" }]
|
|
1862
|
+
}
|
|
1863
|
+
];
|
|
1864
|
+
async function resolveOwnerValidator(publicClient, childSafeAddress) {
|
|
1865
|
+
for (const candidate of OWNABLE_VALIDATOR_DEPLOYMENTS) {
|
|
1866
|
+
const installed = await publicClient.readContract({
|
|
1867
|
+
address: childSafeAddress,
|
|
1868
|
+
abi: IS_MODULE_INSTALLED_ABI,
|
|
1869
|
+
functionName: "isModuleInstalled",
|
|
1870
|
+
args: [1n, candidate, "0x"]
|
|
1871
|
+
}).catch(() => false);
|
|
1872
|
+
if (installed) return candidate;
|
|
1873
|
+
}
|
|
1874
|
+
throw new Error(
|
|
1875
|
+
`No known OwnableValidator is installed on Safe ${childSafeAddress}. A nested Safe owner signature cannot be routed to a validator.`
|
|
1876
|
+
);
|
|
1877
|
+
}
|
|
1781
1878
|
async function signNestedOwnerSessionKey(config, sessions, allPublicClients, signingParams) {
|
|
1782
1879
|
const { owner, publicClient, safeOwnerAddress } = config;
|
|
1783
1880
|
const derivedChild = (await getSafeAccount(config)).address;
|
|
@@ -1809,17 +1906,15 @@ async function signNestedOwnerSessionKey(config, sessions, allPublicClients, sig
|
|
|
1809
1906
|
sessionNonces,
|
|
1810
1907
|
permitGenericPolicy
|
|
1811
1908
|
});
|
|
1812
|
-
const
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
);
|
|
1817
|
-
}
|
|
1909
|
+
const ownerValidator = await resolveOwnerValidator(
|
|
1910
|
+
publicClient,
|
|
1911
|
+
childSafeAddress
|
|
1912
|
+
);
|
|
1818
1913
|
const rawSignature = await signMultiChainSessionTypedData(owner, typedData);
|
|
1819
1914
|
const signature = wrapNestedSafeOwnerSignature(
|
|
1820
1915
|
safeOwnerAddress,
|
|
1821
1916
|
rawSignature,
|
|
1822
|
-
|
|
1917
|
+
ownerValidator
|
|
1823
1918
|
);
|
|
1824
1919
|
return {
|
|
1825
1920
|
signature,
|
|
@@ -2513,7 +2608,7 @@ var findBlockingAsyncRedemption = (portfolio, chainId, tokenSymbol) => {
|
|
|
2513
2608
|
};
|
|
2514
2609
|
|
|
2515
2610
|
// src/core/ZyfaiSDK.ts
|
|
2516
|
-
var import_siwe = require("siwe");
|
|
2611
|
+
var import_siwe = require("viem/siwe");
|
|
2517
2612
|
var DepositCreditTimeoutError = class extends Error {
|
|
2518
2613
|
constructor(depositId) {
|
|
2519
2614
|
super(`Timed out waiting for deposit ${depositId} to be credited`);
|
|
@@ -2585,6 +2680,10 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2585
2680
|
this.currentChainId = null;
|
|
2586
2681
|
/** EOA of the active user session (bearer or SIWE). Cleared on disconnect. */
|
|
2587
2682
|
this.sessionEoa = null;
|
|
2683
|
+
/** Serialize concurrent connectAccount calls (e.g. React StrictMode double mount). */
|
|
2684
|
+
this.connectAccountInFlight = null;
|
|
2685
|
+
/** One SIWE prompt at a time. Register must not send a second personal_sign. */
|
|
2686
|
+
this.authenticateUserInFlight = null;
|
|
2588
2687
|
const sdkConfig = typeof config === "string" ? { apiKey: config } : config;
|
|
2589
2688
|
const {
|
|
2590
2689
|
apiKey,
|
|
@@ -2709,16 +2808,33 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2709
2808
|
*
|
|
2710
2809
|
* @returns Promise that resolves when authentication is complete
|
|
2711
2810
|
*/
|
|
2712
|
-
|
|
2811
|
+
authenticateUser() {
|
|
2812
|
+
if (this.authenticatedUserId !== null || this.httpClient.hasAuthToken()) {
|
|
2813
|
+
return Promise.resolve();
|
|
2814
|
+
}
|
|
2815
|
+
if (!this.authenticateUserInFlight) {
|
|
2816
|
+
this.authenticateUserInFlight = this.performAuthenticateUser().finally(
|
|
2817
|
+
() => {
|
|
2818
|
+
this.authenticateUserInFlight = null;
|
|
2819
|
+
}
|
|
2820
|
+
);
|
|
2821
|
+
}
|
|
2822
|
+
return this.authenticateUserInFlight;
|
|
2823
|
+
}
|
|
2824
|
+
async performAuthenticateUser() {
|
|
2713
2825
|
try {
|
|
2714
|
-
if (this.authenticatedUserId !== null) {
|
|
2715
|
-
return;
|
|
2716
|
-
}
|
|
2717
|
-
if (this.httpClient.hasAuthToken()) {
|
|
2826
|
+
if (this.authenticatedUserId !== null || this.httpClient.hasAuthToken()) {
|
|
2718
2827
|
return;
|
|
2719
2828
|
}
|
|
2720
2829
|
const walletClient = this.getWalletClient();
|
|
2721
|
-
const
|
|
2830
|
+
const rawAccount = walletClient.account;
|
|
2831
|
+
const connected = typeof rawAccount === "string" ? rawAccount : rawAccount?.address;
|
|
2832
|
+
if (!connected) {
|
|
2833
|
+
throw new Error(
|
|
2834
|
+
"Wallet account address is missing. Call connectAccount() after the wallet is connected."
|
|
2835
|
+
);
|
|
2836
|
+
}
|
|
2837
|
+
const userAddress = (0, import_viem8.getAddress)(connected);
|
|
2722
2838
|
const chainId = this.currentChainId || walletClient.chain?.id || 8453;
|
|
2723
2839
|
const challengeResponse = await this.httpClient.post(ENDPOINTS.AUTH_CHALLENGE, {});
|
|
2724
2840
|
let uri;
|
|
@@ -2732,7 +2848,8 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2732
2848
|
uri = this.executionApiUrl;
|
|
2733
2849
|
domain = new URL(this.executionApiUrl).host;
|
|
2734
2850
|
}
|
|
2735
|
-
const
|
|
2851
|
+
const issuedAt = /* @__PURE__ */ new Date();
|
|
2852
|
+
const messageFields = {
|
|
2736
2853
|
address: userAddress,
|
|
2737
2854
|
chainId,
|
|
2738
2855
|
domain,
|
|
@@ -2740,11 +2857,15 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2740
2857
|
statement: "Sign in with Ethereum",
|
|
2741
2858
|
uri,
|
|
2742
2859
|
version: "1",
|
|
2743
|
-
issuedAt
|
|
2744
|
-
}
|
|
2745
|
-
const messageString =
|
|
2860
|
+
issuedAt
|
|
2861
|
+
};
|
|
2862
|
+
const messageString = (0, import_siwe.createSiweMessage)(messageFields);
|
|
2863
|
+
const messageObj = {
|
|
2864
|
+
...messageFields,
|
|
2865
|
+
issuedAt: issuedAt.toISOString()
|
|
2866
|
+
};
|
|
2746
2867
|
const signature = await walletClient.signMessage({
|
|
2747
|
-
account:
|
|
2868
|
+
account: userAddress,
|
|
2748
2869
|
message: messageString
|
|
2749
2870
|
});
|
|
2750
2871
|
const loginResponse = await this.httpClient.post(
|
|
@@ -2832,6 +2953,11 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2832
2953
|
[internalAsset]: assetSettings
|
|
2833
2954
|
}
|
|
2834
2955
|
};
|
|
2956
|
+
if (request.smartWallet !== void 0)
|
|
2957
|
+
payload.smartWallet = request.smartWallet;
|
|
2958
|
+
if (request.executorProxy !== void 0)
|
|
2959
|
+
payload.executorProxy = request.executorProxy;
|
|
2960
|
+
if (request.chains !== void 0) payload.chains = request.chains;
|
|
2835
2961
|
if (request.omniAccount !== void 0)
|
|
2836
2962
|
payload.omniAccount = request.omniAccount;
|
|
2837
2963
|
if (request.agentName !== void 0)
|
|
@@ -3137,82 +3263,167 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3137
3263
|
* const provider = await connector.getProvider();
|
|
3138
3264
|
* await sdk.connectAccount(provider);
|
|
3139
3265
|
*/
|
|
3140
|
-
async
|
|
3141
|
-
if (!isSupportedChain(chainId)) {
|
|
3142
|
-
throw new Error(`Unsupported chain ID: ${chainId}`);
|
|
3143
|
-
}
|
|
3144
|
-
this.authenticatedUserId = null;
|
|
3145
|
-
this.isPredeployed = false;
|
|
3146
|
-
this.predeployedResolved = false;
|
|
3147
|
-
this.connectedSmartWallet = null;
|
|
3148
|
-
this.currentChainId = null;
|
|
3149
|
-
this.sessionEoa = null;
|
|
3150
|
-
this.sessionChannel = void 0;
|
|
3151
|
-
this.httpClient.clearAuthToken();
|
|
3152
|
-
if (this.currentProvider?.removeAllListeners) {
|
|
3153
|
-
try {
|
|
3154
|
-
this.currentProvider.removeAllListeners("accountsChanged");
|
|
3155
|
-
} catch (error) {
|
|
3156
|
-
}
|
|
3157
|
-
}
|
|
3158
|
-
const chainConfig = getChainConfig(chainId, this.rpcUrls);
|
|
3159
|
-
let connectedAddress;
|
|
3266
|
+
async resolveConnectAddress(account) {
|
|
3160
3267
|
if (typeof account === "string") {
|
|
3161
3268
|
let privateKey = account;
|
|
3162
3269
|
if (!privateKey.startsWith("0x")) {
|
|
3163
3270
|
privateKey = `0x${privateKey}`;
|
|
3164
3271
|
}
|
|
3165
|
-
|
|
3166
|
-
|
|
3167
|
-
|
|
3168
|
-
|
|
3169
|
-
|
|
3170
|
-
|
|
3272
|
+
return (0, import_accounts2.privateKeyToAccount)(privateKey).address;
|
|
3273
|
+
}
|
|
3274
|
+
const provider = account;
|
|
3275
|
+
if (!provider) {
|
|
3276
|
+
throw new Error(
|
|
3277
|
+
"Invalid account parameter. Expected private key string or wallet provider."
|
|
3278
|
+
);
|
|
3279
|
+
}
|
|
3280
|
+
if (provider.account && provider.transport) {
|
|
3281
|
+
const accountField = provider.account;
|
|
3282
|
+
const resolvedAddress = typeof accountField === "string" ? accountField : accountField.address;
|
|
3283
|
+
if (!resolvedAddress) {
|
|
3284
|
+
throw new Error("Wallet client is missing account address");
|
|
3285
|
+
}
|
|
3286
|
+
return (0, import_viem8.getAddress)(resolvedAddress);
|
|
3287
|
+
}
|
|
3288
|
+
if (provider.request) {
|
|
3289
|
+
const eip1193 = typeof provider.request === "function" ? provider : typeof provider.provider?.request === "function" ? provider.provider : null;
|
|
3290
|
+
if (!eip1193) {
|
|
3291
|
+
throw new Error(
|
|
3292
|
+
"Wallet provider has no EIP-1193 request method. Pass the connector getProvider() result."
|
|
3293
|
+
);
|
|
3294
|
+
}
|
|
3295
|
+
const accounts = await eip1193.request({
|
|
3296
|
+
method: "eth_accounts"
|
|
3171
3297
|
});
|
|
3172
|
-
|
|
3173
|
-
|
|
3174
|
-
|
|
3175
|
-
const
|
|
3176
|
-
|
|
3298
|
+
const fallbackAccounts = accounts?.length > 0 ? accounts : await eip1193.request({
|
|
3299
|
+
method: "eth_requestAccounts"
|
|
3300
|
+
});
|
|
3301
|
+
const raw = fallbackAccounts?.[0];
|
|
3302
|
+
const accountAddress = typeof raw === "string" ? raw : typeof raw?.address === "string" ? raw.address : void 0;
|
|
3303
|
+
if (!accountAddress) {
|
|
3177
3304
|
throw new Error(
|
|
3178
|
-
"
|
|
3305
|
+
"Wallet returned an account without an address. Reconnect via WalletConnect."
|
|
3179
3306
|
);
|
|
3180
3307
|
}
|
|
3181
|
-
|
|
3182
|
-
|
|
3183
|
-
|
|
3184
|
-
|
|
3185
|
-
|
|
3186
|
-
|
|
3308
|
+
return (0, import_viem8.getAddress)(accountAddress);
|
|
3309
|
+
}
|
|
3310
|
+
throw new Error(
|
|
3311
|
+
"Invalid wallet provider. Expected EIP-1193 provider or viem WalletClient."
|
|
3312
|
+
);
|
|
3313
|
+
}
|
|
3314
|
+
async connectAccount(account, chainId = 8453) {
|
|
3315
|
+
if (!isSupportedChain(chainId)) {
|
|
3316
|
+
throw new Error(`Unsupported chain ID: ${chainId}`);
|
|
3317
|
+
}
|
|
3318
|
+
const resolvedAddress = await this.resolveConnectAddress(account);
|
|
3319
|
+
if (this.sessionEoa && this.httpClient.hasAuthToken() && this.authenticatedUserId !== null && (0, import_viem8.isAddressEqual)(resolvedAddress, this.sessionEoa)) {
|
|
3320
|
+
this.currentChainId = chainId;
|
|
3321
|
+
return resolvedAddress;
|
|
3322
|
+
}
|
|
3323
|
+
if (this.connectAccountInFlight) {
|
|
3324
|
+
return this.connectAccountInFlight;
|
|
3325
|
+
}
|
|
3326
|
+
const connect = (async () => {
|
|
3327
|
+
this.authenticatedUserId = null;
|
|
3328
|
+
this.isPredeployed = false;
|
|
3329
|
+
this.predeployedResolved = false;
|
|
3330
|
+
this.connectedSmartWallet = null;
|
|
3331
|
+
this.currentChainId = null;
|
|
3332
|
+
this.sessionEoa = null;
|
|
3333
|
+
this.sessionChannel = void 0;
|
|
3334
|
+
this.httpClient.clearAuthToken();
|
|
3335
|
+
if (this.currentProvider?.removeAllListeners) {
|
|
3336
|
+
try {
|
|
3337
|
+
this.currentProvider.removeAllListeners("accountsChanged");
|
|
3338
|
+
} catch (error) {
|
|
3187
3339
|
}
|
|
3188
|
-
|
|
3189
|
-
|
|
3190
|
-
|
|
3191
|
-
|
|
3192
|
-
|
|
3193
|
-
|
|
3194
|
-
|
|
3195
|
-
this.currentChainId = chainId;
|
|
3196
|
-
if (provider.on) {
|
|
3197
|
-
provider.on("accountsChanged", this.handleAccountsChanged.bind(this));
|
|
3340
|
+
}
|
|
3341
|
+
const chainConfig = getChainConfig(chainId, this.rpcUrls);
|
|
3342
|
+
let connectedAddress;
|
|
3343
|
+
if (typeof account === "string") {
|
|
3344
|
+
let privateKey = account;
|
|
3345
|
+
if (!privateKey.startsWith("0x")) {
|
|
3346
|
+
privateKey = `0x${privateKey}`;
|
|
3198
3347
|
}
|
|
3199
|
-
|
|
3348
|
+
this.signer = (0, import_accounts2.privateKeyToAccount)(privateKey);
|
|
3349
|
+
this.currentChainId = chainId;
|
|
3200
3350
|
this.walletClient = (0, import_viem8.createWalletClient)({
|
|
3201
|
-
account:
|
|
3351
|
+
account: this.signer,
|
|
3202
3352
|
chain: chainConfig.chain,
|
|
3203
|
-
transport:
|
|
3353
|
+
transport: (0, import_viem8.http)(chainConfig.rpcUrl)
|
|
3204
3354
|
});
|
|
3205
|
-
connectedAddress =
|
|
3355
|
+
connectedAddress = this.signer.address;
|
|
3206
3356
|
this.currentProvider = null;
|
|
3207
|
-
this.currentChainId = chainId;
|
|
3208
3357
|
} else {
|
|
3209
|
-
|
|
3210
|
-
|
|
3211
|
-
|
|
3358
|
+
const provider = account;
|
|
3359
|
+
if (!provider) {
|
|
3360
|
+
throw new Error(
|
|
3361
|
+
"Invalid account parameter. Expected private key string or wallet provider."
|
|
3362
|
+
);
|
|
3363
|
+
}
|
|
3364
|
+
if (provider.account && provider.transport) {
|
|
3365
|
+
const accountField = provider.account;
|
|
3366
|
+
const resolvedAddress2 = typeof accountField === "string" ? accountField : accountField.address;
|
|
3367
|
+
if (!resolvedAddress2) {
|
|
3368
|
+
throw new Error("Wallet client is missing account address");
|
|
3369
|
+
}
|
|
3370
|
+
const hasSignMessage = typeof provider.signMessage === "function" && typeof provider.request === "function";
|
|
3371
|
+
this.walletClient = hasSignMessage ? provider : (0, import_viem8.createWalletClient)({
|
|
3372
|
+
account: provider.account,
|
|
3373
|
+
chain: chainConfig.chain,
|
|
3374
|
+
transport: provider.transport
|
|
3375
|
+
});
|
|
3376
|
+
connectedAddress = (0, import_viem8.getAddress)(resolvedAddress2);
|
|
3377
|
+
this.currentProvider = null;
|
|
3378
|
+
this.currentChainId = chainId;
|
|
3379
|
+
} else if (provider.request) {
|
|
3380
|
+
const eip1193 = typeof provider.request === "function" ? provider : typeof provider.provider?.request === "function" ? provider.provider : null;
|
|
3381
|
+
if (!eip1193) {
|
|
3382
|
+
throw new Error(
|
|
3383
|
+
"Wallet provider has no EIP-1193 request method. Pass the connector getProvider() result."
|
|
3384
|
+
);
|
|
3385
|
+
}
|
|
3386
|
+
const accounts = await eip1193.request({
|
|
3387
|
+
method: "eth_requestAccounts"
|
|
3388
|
+
});
|
|
3389
|
+
if (!accounts || accounts.length === 0) {
|
|
3390
|
+
throw new Error("No accounts found in wallet provider");
|
|
3391
|
+
}
|
|
3392
|
+
const raw = accounts[0];
|
|
3393
|
+
const accountAddress = typeof raw === "string" ? raw : typeof raw?.address === "string" ? raw.address : void 0;
|
|
3394
|
+
if (!accountAddress) {
|
|
3395
|
+
throw new Error(
|
|
3396
|
+
"Wallet returned an account without an address. Reconnect via WalletConnect."
|
|
3397
|
+
);
|
|
3398
|
+
}
|
|
3399
|
+
connectedAddress = (0, import_viem8.getAddress)(accountAddress);
|
|
3400
|
+
this.walletClient = (0, import_viem8.createWalletClient)({
|
|
3401
|
+
account: connectedAddress,
|
|
3402
|
+
chain: chainConfig.chain,
|
|
3403
|
+
transport: (0, import_viem8.custom)(eip1193)
|
|
3404
|
+
});
|
|
3405
|
+
this.currentProvider = eip1193;
|
|
3406
|
+
this.currentChainId = chainId;
|
|
3407
|
+
if (provider.on) {
|
|
3408
|
+
provider.on("accountsChanged", this.handleAccountsChanged.bind(this));
|
|
3409
|
+
}
|
|
3410
|
+
} else {
|
|
3411
|
+
throw new Error(
|
|
3412
|
+
"Invalid wallet provider. Expected EIP-1193 provider or viem WalletClient."
|
|
3413
|
+
);
|
|
3414
|
+
}
|
|
3415
|
+
}
|
|
3416
|
+
await this.authenticateUser();
|
|
3417
|
+
return connectedAddress;
|
|
3418
|
+
})();
|
|
3419
|
+
this.connectAccountInFlight = connect;
|
|
3420
|
+
try {
|
|
3421
|
+
return await connect;
|
|
3422
|
+
} finally {
|
|
3423
|
+
if (this.connectAccountInFlight === connect) {
|
|
3424
|
+
this.connectAccountInFlight = null;
|
|
3212
3425
|
}
|
|
3213
3426
|
}
|
|
3214
|
-
await this.authenticateUser();
|
|
3215
|
-
return connectedAddress;
|
|
3216
3427
|
}
|
|
3217
3428
|
/**
|
|
3218
3429
|
* Clear the connected wallet, SIWE session, and cached smart-wallet state.
|
|
@@ -3278,6 +3489,51 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3278
3489
|
const connected = this.signer?.address ?? this.walletClient?.account?.address ?? this.sessionEoa;
|
|
3279
3490
|
return !!connected && connected.toLowerCase() === userAddress.toLowerCase();
|
|
3280
3491
|
}
|
|
3492
|
+
/**
|
|
3493
|
+
* Nested Safe owners use intent sessions: finish any pending safe-deploy typed-data
|
|
3494
|
+
* (install or emissary enable), then register with chainId only.
|
|
3495
|
+
* @private
|
|
3496
|
+
*/
|
|
3497
|
+
async registerNestedIntentSessionKey(userAddress, chainId, strategy) {
|
|
3498
|
+
if (!this.authenticatedUserId) {
|
|
3499
|
+
throw new Error(
|
|
3500
|
+
"User ID not available. Please ensure authentication completed successfully."
|
|
3501
|
+
);
|
|
3502
|
+
}
|
|
3503
|
+
const chainConfig = getChainConfig(chainId, this.rpcUrls);
|
|
3504
|
+
await deploySafeAccount({
|
|
3505
|
+
owner: this.getWalletClient(chainId),
|
|
3506
|
+
safeOwnerAddress: userAddress,
|
|
3507
|
+
chain: chainConfig.chain,
|
|
3508
|
+
publicClient: chainConfig.publicClient,
|
|
3509
|
+
chainId,
|
|
3510
|
+
httpClient: this.httpClient,
|
|
3511
|
+
strategy
|
|
3512
|
+
});
|
|
3513
|
+
const activation = await this.httpClient.post(
|
|
3514
|
+
ENDPOINTS.SESSION_KEYS_ADD,
|
|
3515
|
+
{ chainId }
|
|
3516
|
+
);
|
|
3517
|
+
this.hasActiveSessionKey = true;
|
|
3518
|
+
return {
|
|
3519
|
+
success: true,
|
|
3520
|
+
userId: this.authenticatedUserId,
|
|
3521
|
+
message: "Intent session key registered",
|
|
3522
|
+
alreadyActive: false,
|
|
3523
|
+
sessionActivation: activation
|
|
3524
|
+
};
|
|
3525
|
+
}
|
|
3526
|
+
async registerNestedChildProfile(childAddress, chainId, strategy) {
|
|
3527
|
+
await this.updateUserProfile({
|
|
3528
|
+
smartWallet: childAddress,
|
|
3529
|
+
executorProxy: true,
|
|
3530
|
+
chains: [chainId],
|
|
3531
|
+
strategy,
|
|
3532
|
+
protocols: [],
|
|
3533
|
+
autoSelectProtocols: false,
|
|
3534
|
+
asset: "USDC"
|
|
3535
|
+
});
|
|
3536
|
+
}
|
|
3281
3537
|
/**
|
|
3282
3538
|
* SIWE login sets predeployed. Agent sessions (`forUser`) do not, so read it
|
|
3283
3539
|
* from the user record before allowing a transfer to an undeployed pool Safe.
|
|
@@ -3301,26 +3557,24 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3301
3557
|
chainId
|
|
3302
3558
|
);
|
|
3303
3559
|
const childAddress = await this.getSafeAddressFor(userAddress, chainId);
|
|
3304
|
-
|
|
3305
|
-
throw new Error("Child smart wallet address is not available");
|
|
3306
|
-
}
|
|
3307
|
-
const childDeployed = await isSafeDeployed(
|
|
3308
|
-
childAddress,
|
|
3309
|
-
chainConfig.publicClient
|
|
3310
|
-
);
|
|
3560
|
+
const childDeployed = childAddress ? await isSafeDeployed(childAddress, chainConfig.publicClient) : false;
|
|
3311
3561
|
if (!childDeployed) {
|
|
3312
3562
|
await this.deploySafe(userAddress, chainId, strategy, true);
|
|
3313
3563
|
return;
|
|
3314
3564
|
}
|
|
3565
|
+
console.info(
|
|
3566
|
+
`[ZyfaiSDK] nested deposit signing pending enable only chainId=${chainId} owner=${userAddress} sessionActive=${this.hasActiveSessionKey}`
|
|
3567
|
+
);
|
|
3568
|
+
await deploySafeAccount({
|
|
3569
|
+
owner: this.getWalletClient(chainId),
|
|
3570
|
+
safeOwnerAddress: userAddress,
|
|
3571
|
+
chain: chainConfig.chain,
|
|
3572
|
+
publicClient: chainConfig.publicClient,
|
|
3573
|
+
chainId,
|
|
3574
|
+
httpClient: this.httpClient
|
|
3575
|
+
});
|
|
3315
3576
|
if (!this.hasActiveSessionKey) {
|
|
3316
|
-
|
|
3317
|
-
await this.createSessionKey(userAddress, chainId);
|
|
3318
|
-
} catch (sessionError) {
|
|
3319
|
-
console.warn(
|
|
3320
|
-
"Failed to create session key for nested owner:",
|
|
3321
|
-
sessionError.message
|
|
3322
|
-
);
|
|
3323
|
-
}
|
|
3577
|
+
await this.createSessionKey(userAddress, chainId);
|
|
3324
3578
|
}
|
|
3325
3579
|
}
|
|
3326
3580
|
async hydrateSessionWallet() {
|
|
@@ -3356,6 +3610,25 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3356
3610
|
}
|
|
3357
3611
|
if (!deriveIfMissing) return null;
|
|
3358
3612
|
const chainConfig = getChainConfig(chainId, this.rpcUrls);
|
|
3613
|
+
const ownerType = await getAccountType(
|
|
3614
|
+
userAddress,
|
|
3615
|
+
chainConfig.publicClient
|
|
3616
|
+
);
|
|
3617
|
+
if (ownerType === "Safe") {
|
|
3618
|
+
if (this.isConnectedUser(userAddress) && this.connectedSmartWallet) {
|
|
3619
|
+
return this.connectedSmartWallet;
|
|
3620
|
+
}
|
|
3621
|
+
try {
|
|
3622
|
+
if (this.httpClient.hasAuthToken()) {
|
|
3623
|
+
const me = await this.httpClient.get(
|
|
3624
|
+
ENDPOINTS.USER_ME
|
|
3625
|
+
);
|
|
3626
|
+
if (me?.smartWallet) return (0, import_viem8.getAddress)(me.smartWallet);
|
|
3627
|
+
}
|
|
3628
|
+
} catch {
|
|
3629
|
+
}
|
|
3630
|
+
return null;
|
|
3631
|
+
}
|
|
3359
3632
|
return getDeterministicSafeAddress({
|
|
3360
3633
|
safeOwnerAddress: userAddress,
|
|
3361
3634
|
chain: chainConfig.chain,
|
|
@@ -3385,8 +3658,15 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3385
3658
|
throw new Error(`Unsupported chain ID: ${chainId}`);
|
|
3386
3659
|
}
|
|
3387
3660
|
const chainConfig = getChainConfig(chainId, this.rpcUrls);
|
|
3661
|
+
const ownerType = await getAccountType(
|
|
3662
|
+
userAddress,
|
|
3663
|
+
chainConfig.publicClient
|
|
3664
|
+
);
|
|
3388
3665
|
const safeAddress = await this.getSafeAddressFor(userAddress, chainId);
|
|
3389
3666
|
if (!safeAddress) {
|
|
3667
|
+
if (ownerType === "Safe") {
|
|
3668
|
+
throw new Error("NESTED_CHILD_UNREGISTERED");
|
|
3669
|
+
}
|
|
3390
3670
|
throw new Error("Smart wallet address is not available");
|
|
3391
3671
|
}
|
|
3392
3672
|
const isDeployed = await isSafeDeployed(
|
|
@@ -3471,16 +3751,33 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3471
3751
|
}
|
|
3472
3752
|
const walletClient = this.getWalletClient(chainId);
|
|
3473
3753
|
const chainConfig = getChainConfig(chainId, this.rpcUrls);
|
|
3474
|
-
const
|
|
3475
|
-
|
|
3476
|
-
chain: chainConfig.chain,
|
|
3477
|
-
publicClient: chainConfig.publicClient
|
|
3478
|
-
});
|
|
3479
|
-
const alreadyDeployed = await isSafeDeployed(
|
|
3480
|
-
safeAddress,
|
|
3754
|
+
const ownerAccountType = await getAccountType(
|
|
3755
|
+
userAddress,
|
|
3481
3756
|
chainConfig.publicClient
|
|
3482
3757
|
);
|
|
3483
|
-
|
|
3758
|
+
const nestedSafeOwner = ownerAccountType === "Safe";
|
|
3759
|
+
const safeAddress = await this.getSafeAddressFor(userAddress, chainId);
|
|
3760
|
+
if (!safeAddress && !nestedSafeOwner) {
|
|
3761
|
+
throw new Error(
|
|
3762
|
+
ownerAccountType === "EOA" ? "For nested deploy, connect WalletConnect as the parent Safe contract address, not the owner EOA." : "Smart wallet address is not available"
|
|
3763
|
+
);
|
|
3764
|
+
}
|
|
3765
|
+
const alreadyDeployed = safeAddress ? await isSafeDeployed(safeAddress, chainConfig.publicClient) : false;
|
|
3766
|
+
if (alreadyDeployed && safeAddress) {
|
|
3767
|
+
let enableTxHash = "0x0";
|
|
3768
|
+
if (!this.isPredeployed) {
|
|
3769
|
+
const enabled = await deploySafeAccount({
|
|
3770
|
+
owner: walletClient,
|
|
3771
|
+
safeOwnerAddress: userAddress,
|
|
3772
|
+
chain: chainConfig.chain,
|
|
3773
|
+
publicClient: chainConfig.publicClient,
|
|
3774
|
+
chainId,
|
|
3775
|
+
httpClient: this.httpClient,
|
|
3776
|
+
strategy: strategy ? toInternalStrategy(strategy) : "safe_strategy",
|
|
3777
|
+
childSafeAddress: safeAddress
|
|
3778
|
+
});
|
|
3779
|
+
enableTxHash = enabled.txHash || "0x0";
|
|
3780
|
+
}
|
|
3484
3781
|
await this.updateUserProtocols(strategy);
|
|
3485
3782
|
let sessionKeyCreated2 = false;
|
|
3486
3783
|
if (createSessionKey) {
|
|
@@ -3497,7 +3794,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3497
3794
|
return {
|
|
3498
3795
|
success: true,
|
|
3499
3796
|
safeAddress,
|
|
3500
|
-
txHash:
|
|
3797
|
+
txHash: enableTxHash,
|
|
3501
3798
|
status: "deployed",
|
|
3502
3799
|
sessionKeyCreated: sessionKeyCreated2
|
|
3503
3800
|
};
|
|
@@ -3510,7 +3807,8 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3510
3807
|
publicClient: chainConfig.publicClient,
|
|
3511
3808
|
chainId,
|
|
3512
3809
|
httpClient: this.httpClient,
|
|
3513
|
-
strategy: internalStrategy
|
|
3810
|
+
strategy: internalStrategy,
|
|
3811
|
+
childSafeAddress: safeAddress ?? void 0
|
|
3514
3812
|
});
|
|
3515
3813
|
this.hasActiveSessionKey = false;
|
|
3516
3814
|
try {
|
|
@@ -3521,7 +3819,26 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3521
3819
|
initError.message
|
|
3522
3820
|
);
|
|
3523
3821
|
}
|
|
3822
|
+
if (nestedSafeOwner) {
|
|
3823
|
+
await this.registerNestedChildProfile(
|
|
3824
|
+
deploymentResult.safeAddress,
|
|
3825
|
+
chainId,
|
|
3826
|
+
strategy
|
|
3827
|
+
);
|
|
3828
|
+
}
|
|
3524
3829
|
await this.updateUserProtocols(strategy);
|
|
3830
|
+
if (!this.isPredeployed && !nestedSafeOwner) {
|
|
3831
|
+
await deploySafeAccount({
|
|
3832
|
+
owner: walletClient,
|
|
3833
|
+
safeOwnerAddress: userAddress,
|
|
3834
|
+
chain: chainConfig.chain,
|
|
3835
|
+
publicClient: chainConfig.publicClient,
|
|
3836
|
+
chainId,
|
|
3837
|
+
httpClient: this.httpClient,
|
|
3838
|
+
strategy: internalStrategy,
|
|
3839
|
+
childSafeAddress: deploymentResult.safeAddress
|
|
3840
|
+
});
|
|
3841
|
+
}
|
|
3525
3842
|
let sessionKeyCreated = false;
|
|
3526
3843
|
if (createSessionKey) {
|
|
3527
3844
|
try {
|
|
@@ -3622,14 +3939,15 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3622
3939
|
chainConfig.publicClient
|
|
3623
3940
|
);
|
|
3624
3941
|
if (accountType === "Safe") {
|
|
3625
|
-
|
|
3942
|
+
return this.registerNestedIntentSessionKey(
|
|
3626
3943
|
userAddress,
|
|
3627
|
-
|
|
3944
|
+
chainId,
|
|
3945
|
+
void 0
|
|
3628
3946
|
);
|
|
3629
3947
|
}
|
|
3630
3948
|
const signingParams = {
|
|
3631
3949
|
permitGenericPolicy,
|
|
3632
|
-
ignoreSecurityAttestations:
|
|
3950
|
+
ignoreSecurityAttestations: false
|
|
3633
3951
|
};
|
|
3634
3952
|
const signatureResult = await this.signSessionKey(
|
|
3635
3953
|
userAddress,
|
|
@@ -3659,38 +3977,10 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3659
3977
|
}
|
|
3660
3978
|
}
|
|
3661
3979
|
/**
|
|
3662
|
-
* Internal method to sign session key
|
|
3663
|
-
* @
|
|
3664
|
-
*/
|
|
3665
|
-
/**
|
|
3666
|
-
* A nested Safe owner signs one MultiChainSession covering every chain in the
|
|
3667
|
-
* session configuration, and the child validates that signature against the
|
|
3668
|
-
* owner through ERC-1271 on each of them. A chain where the owner has no code
|
|
3669
|
-
* would accept the signature nowhere, so refuse rather than sign it.
|
|
3670
|
-
*
|
|
3671
|
-
* The session list is not filtered here: the execution API rebuilds the digest
|
|
3672
|
-
* from its own configuration, so dropping a chain client-side would change the
|
|
3673
|
-
* hash and the signature would be rejected everywhere.
|
|
3980
|
+
* Internal method to sign session key for an EOA owner.
|
|
3981
|
+
* Nested Safe owners register through {@link registerNestedIntentSessionKey}.
|
|
3674
3982
|
* @private
|
|
3675
3983
|
*/
|
|
3676
|
-
async assertOwnerSafeOnSessionChains(ownerAddress, sessions) {
|
|
3677
|
-
const chainIds = [
|
|
3678
|
-
...new Set(sessions.map((session) => Number(session.chainId)))
|
|
3679
|
-
].filter(isSupportedChain);
|
|
3680
|
-
const missing = [];
|
|
3681
|
-
await Promise.all(
|
|
3682
|
-
chainIds.map(async (id) => {
|
|
3683
|
-
const { publicClient } = getChainConfig(id, this.rpcUrls);
|
|
3684
|
-
const code = await publicClient.getCode({ address: ownerAddress });
|
|
3685
|
-
if (!code || code === "0x") missing.push(id);
|
|
3686
|
-
})
|
|
3687
|
-
);
|
|
3688
|
-
if (missing.length > 0) {
|
|
3689
|
-
throw new Error(
|
|
3690
|
-
`Owner Safe ${ownerAddress} has no code on chain(s) ${missing.sort((a, b) => a - b).join(", ")} covered by the session configuration. Deploy the Safe there, or restrict the user's chains, before signing the session.`
|
|
3691
|
-
);
|
|
3692
|
-
}
|
|
3693
|
-
}
|
|
3694
3984
|
async signSessionKey(userAddress, chainId, sessions, signingParams) {
|
|
3695
3985
|
try {
|
|
3696
3986
|
if (!userAddress) {
|
|
@@ -8220,371 +8510,6 @@ function createBankrProvider(config) {
|
|
|
8220
8510
|
};
|
|
8221
8511
|
return provider;
|
|
8222
8512
|
}
|
|
8223
|
-
|
|
8224
|
-
// src/providers/safe-owner.ts
|
|
8225
|
-
var import_viem11 = require("viem");
|
|
8226
|
-
var EIP1271_MAGIC_VALUE = "0x1626ba7e";
|
|
8227
|
-
var ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
|
|
8228
|
-
var SAFE_ABI2 = (0, import_viem11.parseAbi)([
|
|
8229
|
-
"function getThreshold() view returns (uint256)",
|
|
8230
|
-
"function getOwners() view returns (address[])",
|
|
8231
|
-
"function nonce() view returns (uint256)",
|
|
8232
|
-
"function getMessageHash(bytes message) view returns (bytes32)",
|
|
8233
|
-
"function getTransactionHash(address to, uint256 value, bytes data, uint8 operation, uint256 safeTxGas, uint256 baseGas, uint256 gasPrice, address gasToken, address refundReceiver, uint256 _nonce) view returns (bytes32)",
|
|
8234
|
-
"function execTransaction(address to, uint256 value, bytes data, uint8 operation, uint256 safeTxGas, uint256 baseGas, uint256 gasPrice, address gasToken, address refundReceiver, bytes signatures) payable returns (bool)",
|
|
8235
|
-
"function isValidSignature(bytes32 _dataHash, bytes _signature) view returns (bytes4)"
|
|
8236
|
-
]);
|
|
8237
|
-
async function createSafeOwnerProvider(config) {
|
|
8238
|
-
if (!(0, import_viem11.isAddress)(config.safeAddress)) {
|
|
8239
|
-
throw new Error("safeAddress must be an Ethereum address");
|
|
8240
|
-
}
|
|
8241
|
-
const safeAddress = (0, import_viem11.getAddress)(config.safeAddress);
|
|
8242
|
-
const publicClient = (0, import_viem11.createPublicClient)({
|
|
8243
|
-
chain: config.chain,
|
|
8244
|
-
transport: (0, import_viem11.http)()
|
|
8245
|
-
});
|
|
8246
|
-
const ownerWallet = await resolveOwnerWallet(config.owner, config.chain);
|
|
8247
|
-
const code = await publicClient.getCode({ address: safeAddress });
|
|
8248
|
-
if (!code || code === "0x") {
|
|
8249
|
-
throw new Error(
|
|
8250
|
-
`Safe ${safeAddress} has no code on chain ${config.chain.id}`
|
|
8251
|
-
);
|
|
8252
|
-
}
|
|
8253
|
-
const [threshold, owners] = await Promise.all([
|
|
8254
|
-
publicClient.readContract({
|
|
8255
|
-
address: safeAddress,
|
|
8256
|
-
abi: SAFE_ABI2,
|
|
8257
|
-
functionName: "getThreshold"
|
|
8258
|
-
}),
|
|
8259
|
-
publicClient.readContract({
|
|
8260
|
-
address: safeAddress,
|
|
8261
|
-
abi: SAFE_ABI2,
|
|
8262
|
-
functionName: "getOwners"
|
|
8263
|
-
})
|
|
8264
|
-
]);
|
|
8265
|
-
if (threshold !== 1n) {
|
|
8266
|
-
throw new Error(
|
|
8267
|
-
`Safe ${safeAddress} threshold is ${threshold.toString()}. createSafeOwnerProvider can execute only a threshold-1 Safe. For a multisig such as 2-of-4, connect the Safe with WalletConnect or the Safe App and pass that provider to connectAccount. sendDeposit waits while the other owners confirm.`
|
|
8268
|
-
);
|
|
8269
|
-
}
|
|
8270
|
-
const ownerAddress = ownerWallet.address;
|
|
8271
|
-
if (!owners.some((owner) => (0, import_viem11.getAddress)(owner) === ownerAddress)) {
|
|
8272
|
-
throw new Error(`${ownerAddress} is not an owner of Safe ${safeAddress}`);
|
|
8273
|
-
}
|
|
8274
|
-
const signSafeDigest = async (dataHash) => {
|
|
8275
|
-
const encoded = (0, import_viem11.encodeAbiParameters)([{ type: "bytes32" }], [dataHash]);
|
|
8276
|
-
const messageHash = await publicClient.readContract({
|
|
8277
|
-
address: safeAddress,
|
|
8278
|
-
abi: SAFE_ABI2,
|
|
8279
|
-
functionName: "getMessageHash",
|
|
8280
|
-
args: [encoded]
|
|
8281
|
-
});
|
|
8282
|
-
return ownerWallet.signHash(messageHash);
|
|
8283
|
-
};
|
|
8284
|
-
const probeHash = (0, import_viem11.hashMessage)("zyfai safe owner");
|
|
8285
|
-
const probeSignature = await signSafeDigest(probeHash);
|
|
8286
|
-
const magic = await publicClient.readContract({
|
|
8287
|
-
address: safeAddress,
|
|
8288
|
-
abi: SAFE_ABI2,
|
|
8289
|
-
functionName: "isValidSignature",
|
|
8290
|
-
args: [probeHash, probeSignature]
|
|
8291
|
-
});
|
|
8292
|
-
if (magic.toLowerCase() !== EIP1271_MAGIC_VALUE) {
|
|
8293
|
-
throw new Error(
|
|
8294
|
-
`Safe ${safeAddress} rejected the owner signature (isValidSignature returned ${magic})`
|
|
8295
|
-
);
|
|
8296
|
-
}
|
|
8297
|
-
const executeFromSafe = async (tx) => {
|
|
8298
|
-
const nonce = await publicClient.readContract({
|
|
8299
|
-
address: safeAddress,
|
|
8300
|
-
abi: SAFE_ABI2,
|
|
8301
|
-
functionName: "nonce"
|
|
8302
|
-
});
|
|
8303
|
-
const safeTxHash = await publicClient.readContract({
|
|
8304
|
-
address: safeAddress,
|
|
8305
|
-
abi: SAFE_ABI2,
|
|
8306
|
-
functionName: "getTransactionHash",
|
|
8307
|
-
args: [
|
|
8308
|
-
tx.to,
|
|
8309
|
-
tx.value,
|
|
8310
|
-
tx.data,
|
|
8311
|
-
0,
|
|
8312
|
-
0n,
|
|
8313
|
-
0n,
|
|
8314
|
-
0n,
|
|
8315
|
-
ZERO_ADDRESS,
|
|
8316
|
-
ZERO_ADDRESS,
|
|
8317
|
-
nonce
|
|
8318
|
-
]
|
|
8319
|
-
});
|
|
8320
|
-
const signature = await ownerWallet.signHash(safeTxHash);
|
|
8321
|
-
const data = (0, import_viem11.encodeFunctionData)({
|
|
8322
|
-
abi: SAFE_ABI2,
|
|
8323
|
-
functionName: "execTransaction",
|
|
8324
|
-
args: [
|
|
8325
|
-
tx.to,
|
|
8326
|
-
tx.value,
|
|
8327
|
-
tx.data,
|
|
8328
|
-
0,
|
|
8329
|
-
0n,
|
|
8330
|
-
0n,
|
|
8331
|
-
0n,
|
|
8332
|
-
ZERO_ADDRESS,
|
|
8333
|
-
ZERO_ADDRESS,
|
|
8334
|
-
signature
|
|
8335
|
-
]
|
|
8336
|
-
});
|
|
8337
|
-
const hash = await ownerWallet.sendTransaction({
|
|
8338
|
-
to: safeAddress,
|
|
8339
|
-
data,
|
|
8340
|
-
value: 0n
|
|
8341
|
-
});
|
|
8342
|
-
const receipt = await publicClient.waitForTransactionReceipt({ hash });
|
|
8343
|
-
if (receipt.status !== "success") {
|
|
8344
|
-
throw new Error(`Safe transaction reverted: ${hash}`);
|
|
8345
|
-
}
|
|
8346
|
-
return hash;
|
|
8347
|
-
};
|
|
8348
|
-
return {
|
|
8349
|
-
async request({ method, params }) {
|
|
8350
|
-
if (method === "eth_requestAccounts" || method === "eth_accounts") {
|
|
8351
|
-
return [safeAddress];
|
|
8352
|
-
}
|
|
8353
|
-
if (method === "personal_sign" || method === "eth_sign") {
|
|
8354
|
-
return signSafeDigest(personalSignDigest(params));
|
|
8355
|
-
}
|
|
8356
|
-
if (method === "eth_signTypedData_v4" || method === "eth_signTypedData") {
|
|
8357
|
-
return signSafeDigest(typedDataDigest(params));
|
|
8358
|
-
}
|
|
8359
|
-
if (method === "eth_sendTransaction" || method === "wallet_sendTransaction") {
|
|
8360
|
-
return executeFromSafe(readSafeTransaction(params));
|
|
8361
|
-
}
|
|
8362
|
-
return publicClient.request({
|
|
8363
|
-
method,
|
|
8364
|
-
params: params ?? []
|
|
8365
|
-
});
|
|
8366
|
-
}
|
|
8367
|
-
};
|
|
8368
|
-
}
|
|
8369
|
-
async function resolveOwnerWallet(owner, chain) {
|
|
8370
|
-
if (isHashSigner(owner)) {
|
|
8371
|
-
const address = (0, import_viem11.getAddress)(owner.address);
|
|
8372
|
-
const wallet = (0, import_viem11.createWalletClient)({
|
|
8373
|
-
account: owner,
|
|
8374
|
-
chain,
|
|
8375
|
-
transport: (0, import_viem11.http)()
|
|
8376
|
-
});
|
|
8377
|
-
return {
|
|
8378
|
-
address,
|
|
8379
|
-
signHash: (hash) => owner.sign({ hash }),
|
|
8380
|
-
signTypedData: (params) => signAccountTypedData(owner, params),
|
|
8381
|
-
sendTransaction: (tx) => wallet.sendTransaction({
|
|
8382
|
-
account: owner,
|
|
8383
|
-
chain,
|
|
8384
|
-
to: tx.to,
|
|
8385
|
-
data: tx.data,
|
|
8386
|
-
value: tx.value
|
|
8387
|
-
})
|
|
8388
|
-
};
|
|
8389
|
-
}
|
|
8390
|
-
if (isWalletClient(owner)) {
|
|
8391
|
-
const address = await walletClientAddress(owner);
|
|
8392
|
-
return {
|
|
8393
|
-
address,
|
|
8394
|
-
signHash: (hash) => signOwnerHash(looseRequest(owner.request), owner.account, address, hash),
|
|
8395
|
-
signTypedData: (params) => {
|
|
8396
|
-
const { domain, types, primaryType, message } = parseTypedDataPayload(params);
|
|
8397
|
-
return owner.signTypedData({
|
|
8398
|
-
account: owner.account ?? address,
|
|
8399
|
-
domain,
|
|
8400
|
-
types,
|
|
8401
|
-
primaryType,
|
|
8402
|
-
message
|
|
8403
|
-
});
|
|
8404
|
-
},
|
|
8405
|
-
sendTransaction: (tx) => owner.sendTransaction({
|
|
8406
|
-
account: owner.account ?? address,
|
|
8407
|
-
chain: owner.chain ?? chain,
|
|
8408
|
-
to: tx.to,
|
|
8409
|
-
data: tx.data,
|
|
8410
|
-
value: tx.value
|
|
8411
|
-
})
|
|
8412
|
-
};
|
|
8413
|
-
}
|
|
8414
|
-
if (isEip1193(owner)) {
|
|
8415
|
-
const address = await eip1193Address(owner);
|
|
8416
|
-
return {
|
|
8417
|
-
address,
|
|
8418
|
-
signHash: (hash) => signOwnerHash(looseRequest(owner.request), void 0, address, hash),
|
|
8419
|
-
signTypedData: async (params) => {
|
|
8420
|
-
const { requestPayload } = parseTypedDataPayload(params);
|
|
8421
|
-
const signature = await owner.request({
|
|
8422
|
-
method: "eth_signTypedData_v4",
|
|
8423
|
-
params: [address, JSON.stringify(requestPayload)]
|
|
8424
|
-
});
|
|
8425
|
-
if (typeof signature !== "string" || !signature.startsWith("0x")) {
|
|
8426
|
-
throw new Error("Owner wallet did not return a typed-data signature");
|
|
8427
|
-
}
|
|
8428
|
-
return signature;
|
|
8429
|
-
},
|
|
8430
|
-
sendTransaction: async (tx) => {
|
|
8431
|
-
const hash = await owner.request({
|
|
8432
|
-
method: "eth_sendTransaction",
|
|
8433
|
-
params: [
|
|
8434
|
-
{
|
|
8435
|
-
from: address,
|
|
8436
|
-
to: tx.to,
|
|
8437
|
-
data: tx.data,
|
|
8438
|
-
value: tx.value === 0n ? "0x0" : `0x${tx.value.toString(16)}`
|
|
8439
|
-
}
|
|
8440
|
-
]
|
|
8441
|
-
});
|
|
8442
|
-
if (typeof hash !== "string" || !hash.startsWith("0x")) {
|
|
8443
|
-
throw new Error("Owner wallet did not return a transaction hash");
|
|
8444
|
-
}
|
|
8445
|
-
return hash;
|
|
8446
|
-
}
|
|
8447
|
-
};
|
|
8448
|
-
}
|
|
8449
|
-
throw new Error(
|
|
8450
|
-
"owner must be a viem wallet client, an account that can sign hashes, or an EIP-1193 provider"
|
|
8451
|
-
);
|
|
8452
|
-
}
|
|
8453
|
-
function isWalletClient(owner) {
|
|
8454
|
-
return typeof owner === "object" && owner !== null && "transport" in owner && "sendTransaction" in owner && typeof owner.sendTransaction === "function";
|
|
8455
|
-
}
|
|
8456
|
-
function isHashSigner(owner) {
|
|
8457
|
-
return typeof owner === "object" && owner !== null && "address" in owner && "sign" in owner && typeof owner.sign === "function" && !("transport" in owner);
|
|
8458
|
-
}
|
|
8459
|
-
function isEip1193(owner) {
|
|
8460
|
-
return typeof owner === "object" && owner !== null && "request" in owner && typeof owner.request === "function" && !("transport" in owner) && !isHashSigner(owner);
|
|
8461
|
-
}
|
|
8462
|
-
function looseRequest(request) {
|
|
8463
|
-
return request;
|
|
8464
|
-
}
|
|
8465
|
-
async function walletClientAddress(owner) {
|
|
8466
|
-
if (owner.account?.address && (0, import_viem11.isAddress)(owner.account.address)) {
|
|
8467
|
-
return (0, import_viem11.getAddress)(owner.account.address);
|
|
8468
|
-
}
|
|
8469
|
-
return requestAccount(looseRequest(owner.request));
|
|
8470
|
-
}
|
|
8471
|
-
async function eip1193Address(owner) {
|
|
8472
|
-
return requestAccount(looseRequest(owner.request));
|
|
8473
|
-
}
|
|
8474
|
-
async function requestAccount(request) {
|
|
8475
|
-
const accounts = await request({ method: "eth_requestAccounts" });
|
|
8476
|
-
const first = Array.isArray(accounts) ? accounts[0] : void 0;
|
|
8477
|
-
if (typeof first !== "string" || !(0, import_viem11.isAddress)(first)) {
|
|
8478
|
-
throw new Error("Owner wallet did not return an account");
|
|
8479
|
-
}
|
|
8480
|
-
return (0, import_viem11.getAddress)(first);
|
|
8481
|
-
}
|
|
8482
|
-
async function signOwnerHash(request, account, address, hash) {
|
|
8483
|
-
if (account?.sign) {
|
|
8484
|
-
return account.sign({ hash });
|
|
8485
|
-
}
|
|
8486
|
-
const signature = await request({
|
|
8487
|
-
method: "personal_sign",
|
|
8488
|
-
params: [hash, address]
|
|
8489
|
-
});
|
|
8490
|
-
if (typeof signature !== "string" || !signature.startsWith("0x")) {
|
|
8491
|
-
throw new Error("Owner wallet did not return a signature");
|
|
8492
|
-
}
|
|
8493
|
-
return asSafeEthSign(signature);
|
|
8494
|
-
}
|
|
8495
|
-
function asSafeEthSign(signature) {
|
|
8496
|
-
const sig = (0, import_viem11.hexToSignature)(signature);
|
|
8497
|
-
const current = sig.v === void 0 ? sig.yParity === 1 ? 28 : 27 : Number(sig.v);
|
|
8498
|
-
const v = current >= 27 ? current + 4 : current + 31;
|
|
8499
|
-
return (0, import_viem11.signatureToHex)({ r: sig.r, s: sig.s, v: BigInt(v) });
|
|
8500
|
-
}
|
|
8501
|
-
function personalSignDigest(params) {
|
|
8502
|
-
const [first, second] = params ?? [];
|
|
8503
|
-
const message = typeof first === "string" && (0, import_viem11.isAddress)(first) ? second : first;
|
|
8504
|
-
const hex = typeof message === "string" && message.startsWith("0x") ? message : (0, import_viem11.stringToHex)(String(message ?? ""));
|
|
8505
|
-
return (0, import_viem11.hashMessage)({ raw: hex });
|
|
8506
|
-
}
|
|
8507
|
-
function parseTypedDataPayload(params) {
|
|
8508
|
-
const [first, second] = params ?? [];
|
|
8509
|
-
const payload = typeof first === "string" && (0, import_viem11.isAddress)(first) ? second : first;
|
|
8510
|
-
const parsed = typeof payload === "string" ? JSON.parse(payload) : payload;
|
|
8511
|
-
const types = { ...parsed.types };
|
|
8512
|
-
delete types.EIP712Domain;
|
|
8513
|
-
const domain = { ...parsed.domain ?? {} };
|
|
8514
|
-
if (typeof domain.chainId === "string") {
|
|
8515
|
-
domain.chainId = BigInt(domain.chainId);
|
|
8516
|
-
}
|
|
8517
|
-
const message = coerceEip712Value(
|
|
8518
|
-
parsed.primaryType,
|
|
8519
|
-
parsed.message,
|
|
8520
|
-
types
|
|
8521
|
-
);
|
|
8522
|
-
return {
|
|
8523
|
-
domain,
|
|
8524
|
-
types,
|
|
8525
|
-
primaryType: parsed.primaryType,
|
|
8526
|
-
message,
|
|
8527
|
-
requestPayload: parsed
|
|
8528
|
-
};
|
|
8529
|
-
}
|
|
8530
|
-
function typedDataDigest(params) {
|
|
8531
|
-
const { domain, types, primaryType, message } = parseTypedDataPayload(params);
|
|
8532
|
-
return (0, import_viem11.hashTypedData)({ domain, types, primaryType, message });
|
|
8533
|
-
}
|
|
8534
|
-
async function signAccountTypedData(account, params) {
|
|
8535
|
-
const { domain, types, primaryType, message } = parseTypedDataPayload(params);
|
|
8536
|
-
if (!account.signTypedData) {
|
|
8537
|
-
throw new Error("Owner account cannot sign typed data");
|
|
8538
|
-
}
|
|
8539
|
-
return account.signTypedData({
|
|
8540
|
-
domain,
|
|
8541
|
-
types,
|
|
8542
|
-
primaryType,
|
|
8543
|
-
message
|
|
8544
|
-
});
|
|
8545
|
-
}
|
|
8546
|
-
function coerceEip712Value(type, value, types) {
|
|
8547
|
-
if (value == null) return value;
|
|
8548
|
-
const array = type.match(/^(.*)\[(\d*)\]$/);
|
|
8549
|
-
if (array) {
|
|
8550
|
-
if (!Array.isArray(value)) return value;
|
|
8551
|
-
return value.map((item) => coerceEip712Value(array[1], item, types));
|
|
8552
|
-
}
|
|
8553
|
-
const struct = types[type];
|
|
8554
|
-
if (struct) {
|
|
8555
|
-
if (typeof value !== "object" || Array.isArray(value)) return value;
|
|
8556
|
-
const record = value;
|
|
8557
|
-
const next = {};
|
|
8558
|
-
for (const field of struct) {
|
|
8559
|
-
next[field.name] = coerceEip712Value(
|
|
8560
|
-
field.type,
|
|
8561
|
-
record[field.name],
|
|
8562
|
-
types
|
|
8563
|
-
);
|
|
8564
|
-
}
|
|
8565
|
-
return next;
|
|
8566
|
-
}
|
|
8567
|
-
if (/^u?int\d*$/.test(type)) {
|
|
8568
|
-
if (typeof value === "bigint") return value;
|
|
8569
|
-
if (typeof value === "string" || typeof value === "number") {
|
|
8570
|
-
return BigInt(value);
|
|
8571
|
-
}
|
|
8572
|
-
}
|
|
8573
|
-
return value;
|
|
8574
|
-
}
|
|
8575
|
-
function readSafeTransaction(params) {
|
|
8576
|
-
const tx = Array.isArray(params) ? params[0] : void 0;
|
|
8577
|
-
if (!tx?.to || !(0, import_viem11.isAddress)(tx.to)) {
|
|
8578
|
-
throw new Error("Safe transaction is missing a destination");
|
|
8579
|
-
}
|
|
8580
|
-
const data = typeof tx.data === "string" && tx.data.startsWith("0x") ? tx.data : "0x";
|
|
8581
|
-
let value = 0n;
|
|
8582
|
-
if (typeof tx.value === "bigint") value = tx.value;
|
|
8583
|
-
else if (typeof tx.value === "number" || typeof tx.value === "string") {
|
|
8584
|
-
value = BigInt(tx.value);
|
|
8585
|
-
}
|
|
8586
|
-
return { to: (0, import_viem11.getAddress)(tx.to), data, value };
|
|
8587
|
-
}
|
|
8588
8513
|
// Annotate the CommonJS export names for ESM import in node:
|
|
8589
8514
|
0 && (module.exports = {
|
|
8590
8515
|
ALLOWED_HISTORY_DAYS,
|
|
@@ -8618,7 +8543,6 @@ function readSafeTransaction(params) {
|
|
|
8618
8543
|
ZyfaiSDK,
|
|
8619
8544
|
buildV1ChainSessions,
|
|
8620
8545
|
createBankrProvider,
|
|
8621
|
-
createSafeOwnerProvider,
|
|
8622
8546
|
decodeUserOperationCalls,
|
|
8623
8547
|
getChainConfig,
|
|
8624
8548
|
getDataApiBaseUrl,
|