@zyfai/sdk 1.5.0 → 1.8.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 +40 -4
- package/dist/index.d.mts +171 -15
- package/dist/index.d.ts +171 -15
- package/dist/index.js +960 -97
- package/dist/index.mjs +932 -51
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -61,6 +61,7 @@ __export(index_exports, {
|
|
|
61
61
|
ZyfaiSDK: () => ZyfaiSDK,
|
|
62
62
|
buildV1ChainSessions: () => buildV1ChainSessions,
|
|
63
63
|
createBankrProvider: () => createBankrProvider,
|
|
64
|
+
createSafeOwnerProvider: () => createSafeOwnerProvider,
|
|
64
65
|
decodeUserOperationCalls: () => decodeUserOperationCalls,
|
|
65
66
|
getChainConfig: () => getChainConfig,
|
|
66
67
|
getDataApiBaseUrl: () => getDataApiBaseUrl,
|
|
@@ -915,7 +916,7 @@ function isMainnetMinPortfolioExempt(chainId, ...addresses) {
|
|
|
915
916
|
|
|
916
917
|
// src/core/ZyfaiSDK.ts
|
|
917
918
|
var import_accounts2 = require("viem/accounts");
|
|
918
|
-
var
|
|
919
|
+
var import_viem8 = require("viem");
|
|
919
920
|
var import_relay_sdk2 = require("@relayprotocol/relay-sdk");
|
|
920
921
|
|
|
921
922
|
// src/config/chains.ts
|
|
@@ -1400,9 +1401,9 @@ function getManagedAssets() {
|
|
|
1400
1401
|
}
|
|
1401
1402
|
|
|
1402
1403
|
// src/utils/safe-account.ts
|
|
1403
|
-
var
|
|
1404
|
+
var import_module_sdk2 = require("@rhinestone/module-sdk");
|
|
1404
1405
|
var import_accounts = require("permissionless/accounts");
|
|
1405
|
-
var
|
|
1406
|
+
var import_viem6 = require("viem");
|
|
1406
1407
|
var import_account_abstraction = require("viem/account-abstraction");
|
|
1407
1408
|
|
|
1408
1409
|
// src/config/modules.ts
|
|
@@ -1423,6 +1424,142 @@ var POOL_MODULE_ADDRESSES = [
|
|
|
1423
1424
|
OWNABLE_VALIDATOR
|
|
1424
1425
|
];
|
|
1425
1426
|
|
|
1427
|
+
// src/utils/safe-signature.ts
|
|
1428
|
+
var import_module_sdk = require("@rhinestone/module-sdk");
|
|
1429
|
+
var import_viem5 = require("viem");
|
|
1430
|
+
var EMPTY_DATA = "0x";
|
|
1431
|
+
var SIGNATURE_LENGTH_BYTES = 65;
|
|
1432
|
+
function safeSigStaticPart(sig, dynamicOffset) {
|
|
1433
|
+
if (sig.isContractSignature) {
|
|
1434
|
+
return `${sig.signer.slice(2).padStart(64, "0")}${dynamicOffset ?? ""}00`;
|
|
1435
|
+
}
|
|
1436
|
+
return sig.data.slice(2);
|
|
1437
|
+
}
|
|
1438
|
+
function safeSigDynamicPart(sig) {
|
|
1439
|
+
if (!sig.isContractSignature) return "";
|
|
1440
|
+
const dynamicPartLength = (sig.data.slice(2).length / 2).toString(16).padStart(64, "0");
|
|
1441
|
+
return `${dynamicPartLength}${sig.data.slice(2)}`;
|
|
1442
|
+
}
|
|
1443
|
+
function buildSafeSignatureBytes(signatures) {
|
|
1444
|
+
const sorted = [...signatures].sort(
|
|
1445
|
+
(left, right) => left.signer.toLowerCase().localeCompare(right.signer.toLowerCase())
|
|
1446
|
+
);
|
|
1447
|
+
let signatureBytes = EMPTY_DATA.slice(2);
|
|
1448
|
+
let dynamicBytes = "";
|
|
1449
|
+
for (const signature of sorted) {
|
|
1450
|
+
if (signature.isContractSignature) {
|
|
1451
|
+
const dynamicPartPosition = (sorted.length * SIGNATURE_LENGTH_BYTES + dynamicBytes.length / 2).toString(16).padStart(64, "0");
|
|
1452
|
+
signatureBytes += safeSigStaticPart(signature, dynamicPartPosition);
|
|
1453
|
+
dynamicBytes += safeSigDynamicPart(signature);
|
|
1454
|
+
} else {
|
|
1455
|
+
signatureBytes += signature.data.slice(2);
|
|
1456
|
+
}
|
|
1457
|
+
}
|
|
1458
|
+
return `0x${signatureBytes}${dynamicBytes}`;
|
|
1459
|
+
}
|
|
1460
|
+
function wrapNestedSafeOwnerSignature(parentSafe, rawSignature, validator) {
|
|
1461
|
+
const normalizedParent = (0, import_viem5.getAddress)(parentSafe);
|
|
1462
|
+
const normalizedValidator = (0, import_viem5.getAddress)(validator);
|
|
1463
|
+
const inner = rawSignature.toLowerCase().startsWith(normalizedValidator.toLowerCase()) ? `0x${rawSignature.slice(42)}` : rawSignature;
|
|
1464
|
+
const safeSignature = {
|
|
1465
|
+
signer: normalizedParent,
|
|
1466
|
+
data: inner,
|
|
1467
|
+
isContractSignature: true
|
|
1468
|
+
};
|
|
1469
|
+
return (0, import_viem5.concat)([
|
|
1470
|
+
normalizedValidator,
|
|
1471
|
+
buildSafeSignatureBytes([safeSignature])
|
|
1472
|
+
]);
|
|
1473
|
+
}
|
|
1474
|
+
function buildMultiChainSessionTypedData(params) {
|
|
1475
|
+
const smartSession = params.smartSessionAddress ?? import_module_sdk.SMART_SESSIONS_ADDRESS;
|
|
1476
|
+
return {
|
|
1477
|
+
domain: {
|
|
1478
|
+
name: "SmartSession",
|
|
1479
|
+
version: "1"
|
|
1480
|
+
},
|
|
1481
|
+
types: {
|
|
1482
|
+
PolicyData: [
|
|
1483
|
+
{ name: "policy", type: "address" },
|
|
1484
|
+
{ name: "initData", type: "bytes" }
|
|
1485
|
+
],
|
|
1486
|
+
ActionData: [
|
|
1487
|
+
{ name: "actionTargetSelector", type: "bytes4" },
|
|
1488
|
+
{ name: "actionTarget", type: "address" },
|
|
1489
|
+
{ name: "actionPolicies", type: "PolicyData[]" }
|
|
1490
|
+
],
|
|
1491
|
+
ERC7739Context: [
|
|
1492
|
+
{ name: "appDomainSeparator", type: "bytes32" },
|
|
1493
|
+
{ name: "contentName", type: "string[]" }
|
|
1494
|
+
],
|
|
1495
|
+
ERC7739Data: [
|
|
1496
|
+
{ name: "allowedERC7739Content", type: "ERC7739Context[]" },
|
|
1497
|
+
{ name: "erc1271Policies", type: "PolicyData[]" }
|
|
1498
|
+
],
|
|
1499
|
+
SignedPermissions: [
|
|
1500
|
+
{ name: "permitGenericPolicy", type: "bool" },
|
|
1501
|
+
{ name: "permitAdminAccess", type: "bool" },
|
|
1502
|
+
{ name: "ignoreSecurityAttestations", type: "bool" },
|
|
1503
|
+
{ name: "permitERC4337Paymaster", type: "bool" },
|
|
1504
|
+
{ name: "userOpPolicies", type: "PolicyData[]" },
|
|
1505
|
+
{ name: "erc7739Policies", type: "ERC7739Data" },
|
|
1506
|
+
{ name: "actions", type: "ActionData[]" }
|
|
1507
|
+
],
|
|
1508
|
+
SignedSession: [
|
|
1509
|
+
{ name: "account", type: "address" },
|
|
1510
|
+
{ name: "permissions", type: "SignedPermissions" },
|
|
1511
|
+
{ name: "sessionValidator", type: "address" },
|
|
1512
|
+
{ name: "sessionValidatorInitData", type: "bytes" },
|
|
1513
|
+
{ name: "salt", type: "bytes32" },
|
|
1514
|
+
{ name: "smartSession", type: "address" },
|
|
1515
|
+
{ name: "nonce", type: "uint256" }
|
|
1516
|
+
],
|
|
1517
|
+
ChainSession: [
|
|
1518
|
+
{ name: "chainId", type: "uint64" },
|
|
1519
|
+
{ name: "session", type: "SignedSession" }
|
|
1520
|
+
],
|
|
1521
|
+
MultiChainSession: [
|
|
1522
|
+
{ name: "sessionsAndChainIds", type: "ChainSession[]" }
|
|
1523
|
+
]
|
|
1524
|
+
},
|
|
1525
|
+
primaryType: "MultiChainSession",
|
|
1526
|
+
message: {
|
|
1527
|
+
sessionsAndChainIds: params.sessions.map((session, index) => ({
|
|
1528
|
+
chainId: BigInt(session.chainId),
|
|
1529
|
+
session: {
|
|
1530
|
+
account: params.childSafeAddress,
|
|
1531
|
+
permissions: {
|
|
1532
|
+
permitGenericPolicy: params.permitGenericPolicy,
|
|
1533
|
+
permitAdminAccess: false,
|
|
1534
|
+
ignoreSecurityAttestations: true,
|
|
1535
|
+
permitERC4337Paymaster: session.permitERC4337Paymaster,
|
|
1536
|
+
userOpPolicies: session.userOpPolicies,
|
|
1537
|
+
erc7739Policies: session.erc7739Policies,
|
|
1538
|
+
actions: session.actions
|
|
1539
|
+
},
|
|
1540
|
+
sessionValidator: session.sessionValidator,
|
|
1541
|
+
sessionValidatorInitData: session.sessionValidatorInitData,
|
|
1542
|
+
salt: session.salt,
|
|
1543
|
+
smartSession,
|
|
1544
|
+
nonce: params.sessionNonces[index]
|
|
1545
|
+
}
|
|
1546
|
+
}))
|
|
1547
|
+
}
|
|
1548
|
+
};
|
|
1549
|
+
}
|
|
1550
|
+
async function signMultiChainSessionTypedData(walletClient, typedData) {
|
|
1551
|
+
if (!walletClient.account) {
|
|
1552
|
+
throw new Error("Wallet not connected. Please connect your wallet first.");
|
|
1553
|
+
}
|
|
1554
|
+
return walletClient.signTypedData({
|
|
1555
|
+
account: walletClient.account,
|
|
1556
|
+
domain: typedData.domain,
|
|
1557
|
+
types: typedData.types,
|
|
1558
|
+
primaryType: typedData.primaryType,
|
|
1559
|
+
message: typedData.message
|
|
1560
|
+
});
|
|
1561
|
+
}
|
|
1562
|
+
|
|
1426
1563
|
// src/utils/safe-account.ts
|
|
1427
1564
|
var SAFE_7579_ADDRESS2 = "0x7579EE8307284F293B1927136486880611F20002";
|
|
1428
1565
|
var ERC7579_LAUNCHPAD_ADDRESS2 = "0x7579011aB74c46090561ea277Ba79D510c6C00ff";
|
|
@@ -1432,12 +1569,12 @@ var getSafeAccount = async (config) => {
|
|
|
1432
1569
|
if (!safeOwnerAddress) {
|
|
1433
1570
|
throw new Error("Safe owner address is required");
|
|
1434
1571
|
}
|
|
1435
|
-
const formattedOwnerAddress = (0,
|
|
1436
|
-
const ownableValidator = (0,
|
|
1572
|
+
const formattedOwnerAddress = (0, import_viem6.getAddress)(safeOwnerAddress);
|
|
1573
|
+
const ownableValidator = (0, import_module_sdk2.getOwnableValidator)({
|
|
1437
1574
|
owners: [formattedOwnerAddress],
|
|
1438
1575
|
threshold: 1
|
|
1439
1576
|
});
|
|
1440
|
-
const saltHex = (0,
|
|
1577
|
+
const saltHex = (0, import_viem6.fromHex)((0, import_viem6.toHex)(ACCOUNT_SALT), "bigint");
|
|
1441
1578
|
const tempOwner = {
|
|
1442
1579
|
address: formattedOwnerAddress,
|
|
1443
1580
|
type: "json-rpc"
|
|
@@ -1452,7 +1589,7 @@ var getSafeAccount = async (config) => {
|
|
|
1452
1589
|
},
|
|
1453
1590
|
safe4337ModuleAddress: SAFE_7579_ADDRESS2,
|
|
1454
1591
|
erc7579LaunchpadAddress: ERC7579_LAUNCHPAD_ADDRESS2,
|
|
1455
|
-
attesters: [
|
|
1592
|
+
attesters: [import_module_sdk2.RHINESTONE_ATTESTER_ADDRESS],
|
|
1456
1593
|
attestersThreshold: 1,
|
|
1457
1594
|
validators: [
|
|
1458
1595
|
{
|
|
@@ -1484,12 +1621,12 @@ var isSafeDeployed = async (address, publicClient) => {
|
|
|
1484
1621
|
}
|
|
1485
1622
|
};
|
|
1486
1623
|
var isOwnableValidatorOwner = async (safeAddress, userAddress, publicClient) => {
|
|
1487
|
-
const normalizedUser = (0,
|
|
1488
|
-
const normalizedSafe = (0,
|
|
1624
|
+
const normalizedUser = (0, import_viem6.getAddress)(userAddress);
|
|
1625
|
+
const normalizedSafe = (0, import_viem6.getAddress)(safeAddress);
|
|
1489
1626
|
const validatorAddresses = Array.from(
|
|
1490
1627
|
/* @__PURE__ */ new Set([
|
|
1491
|
-
(0,
|
|
1492
|
-
(0,
|
|
1628
|
+
(0, import_viem6.getAddress)(OWNABLE_VALIDATOR),
|
|
1629
|
+
(0, import_viem6.getAddress)(import_module_sdk2.OWNABLE_VALIDATOR_ADDRESS)
|
|
1493
1630
|
])
|
|
1494
1631
|
);
|
|
1495
1632
|
for (const validatorAddress of validatorAddresses) {
|
|
@@ -1500,7 +1637,7 @@ var isOwnableValidatorOwner = async (safeAddress, userAddress, publicClient) =>
|
|
|
1500
1637
|
functionName: "getOwners",
|
|
1501
1638
|
args: [normalizedSafe]
|
|
1502
1639
|
});
|
|
1503
|
-
if (owners.some((owner) => (0,
|
|
1640
|
+
if (owners.some((owner) => (0, import_viem6.getAddress)(owner) === normalizedUser)) {
|
|
1504
1641
|
return true;
|
|
1505
1642
|
}
|
|
1506
1643
|
} catch {
|
|
@@ -1587,12 +1724,24 @@ var deploySafeAccount = async (config) => {
|
|
|
1587
1724
|
}
|
|
1588
1725
|
};
|
|
1589
1726
|
var signSessionKey = async (config, sessions, allPublicClients, signingParams) => {
|
|
1590
|
-
const { owner, publicClient } = config;
|
|
1727
|
+
const { owner, publicClient, safeOwnerAddress } = config;
|
|
1591
1728
|
if (!owner || !owner.account) {
|
|
1592
1729
|
throw new Error("Wallet not connected. Please connect your wallet first.");
|
|
1593
1730
|
}
|
|
1731
|
+
const ownerAccountType = await getAccountType(
|
|
1732
|
+
safeOwnerAddress,
|
|
1733
|
+
publicClient
|
|
1734
|
+
);
|
|
1735
|
+
if (ownerAccountType === "Safe") {
|
|
1736
|
+
return signNestedOwnerSessionKey(
|
|
1737
|
+
config,
|
|
1738
|
+
sessions,
|
|
1739
|
+
allPublicClients,
|
|
1740
|
+
signingParams
|
|
1741
|
+
);
|
|
1742
|
+
}
|
|
1594
1743
|
const safeAccount = await getSafeAccount(config);
|
|
1595
|
-
const account = (0,
|
|
1744
|
+
const account = (0, import_module_sdk2.getAccount)({
|
|
1596
1745
|
address: safeAccount.address,
|
|
1597
1746
|
type: "safe"
|
|
1598
1747
|
});
|
|
@@ -1601,16 +1750,16 @@ var signSessionKey = async (config, sessions, allPublicClients, signingParams) =
|
|
|
1601
1750
|
sessions.map((session) => {
|
|
1602
1751
|
const sessionChainId = Number(session.chainId);
|
|
1603
1752
|
const client = clients.find((c) => c.chain?.id === sessionChainId) || publicClient;
|
|
1604
|
-
return (0,
|
|
1753
|
+
return (0, import_module_sdk2.getSessionNonce)({
|
|
1605
1754
|
client,
|
|
1606
1755
|
account,
|
|
1607
|
-
permissionId: (0,
|
|
1756
|
+
permissionId: (0, import_module_sdk2.getPermissionId)({
|
|
1608
1757
|
session
|
|
1609
1758
|
})
|
|
1610
1759
|
});
|
|
1611
1760
|
})
|
|
1612
1761
|
);
|
|
1613
|
-
const sessionDetails = await (0,
|
|
1762
|
+
const sessionDetails = await (0, import_module_sdk2.getEnableSessionDetails)({
|
|
1614
1763
|
sessions,
|
|
1615
1764
|
account,
|
|
1616
1765
|
clients,
|
|
@@ -1629,6 +1778,192 @@ var signSessionKey = async (config, sessions, allPublicClients, signingParams) =
|
|
|
1629
1778
|
sessionNonces
|
|
1630
1779
|
};
|
|
1631
1780
|
};
|
|
1781
|
+
async function signNestedOwnerSessionKey(config, sessions, allPublicClients, signingParams) {
|
|
1782
|
+
const { owner, publicClient, safeOwnerAddress } = config;
|
|
1783
|
+
const derivedChild = (await getSafeAccount(config)).address;
|
|
1784
|
+
const childSafeAddress = (0, import_viem6.getAddress)(
|
|
1785
|
+
config.childSafeAddress ?? derivedChild
|
|
1786
|
+
);
|
|
1787
|
+
const account = (0, import_module_sdk2.getAccount)({
|
|
1788
|
+
address: childSafeAddress,
|
|
1789
|
+
type: "safe"
|
|
1790
|
+
});
|
|
1791
|
+
const clients = allPublicClients || [publicClient];
|
|
1792
|
+
const sessionNonces = await Promise.all(
|
|
1793
|
+
sessions.map((session) => {
|
|
1794
|
+
const sessionChainId = Number(session.chainId);
|
|
1795
|
+
const client = clients.find((c) => c.chain?.id === sessionChainId) || publicClient;
|
|
1796
|
+
return (0, import_module_sdk2.getSessionNonce)({
|
|
1797
|
+
client,
|
|
1798
|
+
account,
|
|
1799
|
+
permissionId: (0, import_module_sdk2.getPermissionId)({
|
|
1800
|
+
session
|
|
1801
|
+
})
|
|
1802
|
+
});
|
|
1803
|
+
})
|
|
1804
|
+
);
|
|
1805
|
+
const permitGenericPolicy = signingParams?.permitGenericPolicy ?? true;
|
|
1806
|
+
const typedData = buildMultiChainSessionTypedData({
|
|
1807
|
+
childSafeAddress,
|
|
1808
|
+
sessions,
|
|
1809
|
+
sessionNonces,
|
|
1810
|
+
permitGenericPolicy
|
|
1811
|
+
});
|
|
1812
|
+
const sessionValidator = sessions[0]?.sessionValidator;
|
|
1813
|
+
if (!sessionValidator) {
|
|
1814
|
+
throw new Error(
|
|
1815
|
+
"Session configuration has no sessionValidator. The nested Safe signature cannot be routed to a validator."
|
|
1816
|
+
);
|
|
1817
|
+
}
|
|
1818
|
+
const rawSignature = await signMultiChainSessionTypedData(owner, typedData);
|
|
1819
|
+
const signature = wrapNestedSafeOwnerSignature(
|
|
1820
|
+
safeOwnerAddress,
|
|
1821
|
+
rawSignature,
|
|
1822
|
+
sessionValidator
|
|
1823
|
+
);
|
|
1824
|
+
return {
|
|
1825
|
+
signature,
|
|
1826
|
+
sessionNonces
|
|
1827
|
+
};
|
|
1828
|
+
}
|
|
1829
|
+
|
|
1830
|
+
// src/utils/nested-safe-deposit.ts
|
|
1831
|
+
var import_viem7 = require("viem");
|
|
1832
|
+
var NESTED_SAFE_DEPOSIT_TIMEOUT_MS = 20 * 60 * 1e3;
|
|
1833
|
+
var NESTED_SAFE_DEPOSIT_POLL_MS = 1e4;
|
|
1834
|
+
var NESTED_SAFE_RESUME_WINDOW_MS = 24 * 60 * 60 * 1e3;
|
|
1835
|
+
var SAFE_TX_SERVICE_NETWORK = {
|
|
1836
|
+
1: "eth",
|
|
1837
|
+
42161: "arb1",
|
|
1838
|
+
8453: "base",
|
|
1839
|
+
10: "oeth",
|
|
1840
|
+
59144: "linea",
|
|
1841
|
+
146: "sonic",
|
|
1842
|
+
9745: "plasma"
|
|
1843
|
+
};
|
|
1844
|
+
function safeTxServiceNetworkName(chainId) {
|
|
1845
|
+
return SAFE_TX_SERVICE_NETWORK[chainId];
|
|
1846
|
+
}
|
|
1847
|
+
function safeTxServiceNetworkNameOrThrow(chainId) {
|
|
1848
|
+
const networkName = safeTxServiceNetworkName(chainId);
|
|
1849
|
+
if (!networkName) {
|
|
1850
|
+
throw new Error(
|
|
1851
|
+
`Chain ${chainId} has no Safe Transaction Service. A nested Safe deposit cannot be tracked there.`
|
|
1852
|
+
);
|
|
1853
|
+
}
|
|
1854
|
+
return networkName;
|
|
1855
|
+
}
|
|
1856
|
+
async function assertParentSafeDeployedOnChain(parentSafe, publicClient, chainId) {
|
|
1857
|
+
const code = await publicClient.getCode({ address: (0, import_viem7.getAddress)(parentSafe) });
|
|
1858
|
+
if (!code || code === "0x") {
|
|
1859
|
+
throw new Error(
|
|
1860
|
+
`Parent Safe ${parentSafe} has no code on chain ${chainId}. Choose a chain where the Safe is deployed.`
|
|
1861
|
+
);
|
|
1862
|
+
}
|
|
1863
|
+
}
|
|
1864
|
+
function sameAddress(left, right) {
|
|
1865
|
+
if (!left || !right) return false;
|
|
1866
|
+
try {
|
|
1867
|
+
return (0, import_viem7.getAddress)(left) === (0, import_viem7.getAddress)(right);
|
|
1868
|
+
} catch {
|
|
1869
|
+
return false;
|
|
1870
|
+
}
|
|
1871
|
+
}
|
|
1872
|
+
function transferMatches(transaction, params) {
|
|
1873
|
+
if (!sameAddress(transaction.to, params.tokenAddress)) return false;
|
|
1874
|
+
if (transaction.isSuccessful === false) return false;
|
|
1875
|
+
const decoded = transaction.dataDecoded;
|
|
1876
|
+
if (decoded?.method?.toLowerCase() !== "transfer") return false;
|
|
1877
|
+
const parameters = decoded.parameters;
|
|
1878
|
+
if (!parameters) return false;
|
|
1879
|
+
const destination = parameters.find(
|
|
1880
|
+
(p) => (p.name === "to" || p.name === "dst" || p.name === "_to") && p.type === "address"
|
|
1881
|
+
);
|
|
1882
|
+
if (!sameAddress(destination?.value, params.childSafe)) return false;
|
|
1883
|
+
if (params.amount === void 0) return true;
|
|
1884
|
+
const valueParam = parameters.find(
|
|
1885
|
+
(p) => (p.name === "value" || p.name === "amount" || p.name === "_value" || p.name === "wad") && p.type.startsWith("uint")
|
|
1886
|
+
);
|
|
1887
|
+
if (!valueParam) return false;
|
|
1888
|
+
try {
|
|
1889
|
+
return BigInt(valueParam.value) === params.amount;
|
|
1890
|
+
} catch {
|
|
1891
|
+
return false;
|
|
1892
|
+
}
|
|
1893
|
+
}
|
|
1894
|
+
function isWithinWindow(transaction, since) {
|
|
1895
|
+
if (!since) return true;
|
|
1896
|
+
const stamp = transaction.executionDate ?? transaction.submissionDate;
|
|
1897
|
+
if (!stamp) return false;
|
|
1898
|
+
const at = Date.parse(stamp);
|
|
1899
|
+
return Number.isFinite(at) && at >= since.getTime();
|
|
1900
|
+
}
|
|
1901
|
+
async function findSafeTransferByHash(params) {
|
|
1902
|
+
const networkName = safeTxServiceNetworkNameOrThrow(params.chainId);
|
|
1903
|
+
const url = `https://api.safe.global/tx-service/${networkName}/api/v2/multisig-transactions/${params.safeTxHash}/`;
|
|
1904
|
+
const response = await fetch(url);
|
|
1905
|
+
if (!response.ok) return null;
|
|
1906
|
+
const transaction = await response.json();
|
|
1907
|
+
if (!transaction.safeTxHash) return null;
|
|
1908
|
+
if (transaction.isSuccessful === false) {
|
|
1909
|
+
throw new Error(
|
|
1910
|
+
`Safe transaction ${params.safeTxHash} on chain ${params.chainId} reverted on-chain. No deposit was registered.`
|
|
1911
|
+
);
|
|
1912
|
+
}
|
|
1913
|
+
return {
|
|
1914
|
+
safeTxHash: transaction.safeTxHash,
|
|
1915
|
+
transactionHash: transaction.transactionHash ?? null,
|
|
1916
|
+
isExecuted: transaction.isExecuted === true && !!transaction.transactionHash
|
|
1917
|
+
};
|
|
1918
|
+
}
|
|
1919
|
+
async function findSafeTransferProposal(params) {
|
|
1920
|
+
const networkName = safeTxServiceNetworkNameOrThrow(params.chainId);
|
|
1921
|
+
const url = `https://api.safe.global/tx-service/${networkName}/api/v2/safes/${(0, import_viem7.getAddress)(
|
|
1922
|
+
params.parentSafe
|
|
1923
|
+
)}/multisig-transactions/?ordering=-nonce`;
|
|
1924
|
+
const response = await fetch(url);
|
|
1925
|
+
if (!response.ok) return null;
|
|
1926
|
+
const body = await response.json();
|
|
1927
|
+
const match = (body.results ?? []).find(
|
|
1928
|
+
(transaction) => !!transaction.safeTxHash && isWithinWindow(transaction, params.since) && transferMatches(transaction, {
|
|
1929
|
+
childSafe: params.childSafe,
|
|
1930
|
+
tokenAddress: params.tokenAddress,
|
|
1931
|
+
amount: params.amount
|
|
1932
|
+
})
|
|
1933
|
+
);
|
|
1934
|
+
if (!match) return null;
|
|
1935
|
+
return {
|
|
1936
|
+
safeTxHash: match.safeTxHash,
|
|
1937
|
+
transactionHash: match.transactionHash ?? null,
|
|
1938
|
+
isExecuted: match.isExecuted === true && !!match.transactionHash
|
|
1939
|
+
};
|
|
1940
|
+
}
|
|
1941
|
+
async function waitForSafeTransferExecution(params) {
|
|
1942
|
+
safeTxServiceNetworkNameOrThrow(params.chainId);
|
|
1943
|
+
const timeoutMs = params.timeoutMs ?? NESTED_SAFE_DEPOSIT_TIMEOUT_MS;
|
|
1944
|
+
const pollMs = params.pollMs ?? NESTED_SAFE_DEPOSIT_POLL_MS;
|
|
1945
|
+
const started = Date.now();
|
|
1946
|
+
do {
|
|
1947
|
+
const proposal = params.safeTxHash ? await findSafeTransferByHash({
|
|
1948
|
+
chainId: params.chainId,
|
|
1949
|
+
safeTxHash: params.safeTxHash
|
|
1950
|
+
}) : await findSafeTransferProposal({
|
|
1951
|
+
parentSafe: params.parentSafe,
|
|
1952
|
+
childSafe: params.childSafe,
|
|
1953
|
+
chainId: params.chainId,
|
|
1954
|
+
tokenAddress: params.tokenAddress,
|
|
1955
|
+
amount: params.amount,
|
|
1956
|
+
since: params.since
|
|
1957
|
+
});
|
|
1958
|
+
if (proposal?.isExecuted && proposal.transactionHash) {
|
|
1959
|
+
return proposal.transactionHash;
|
|
1960
|
+
}
|
|
1961
|
+
await new Promise((resolve) => setTimeout(resolve, pollMs));
|
|
1962
|
+
} while (Date.now() - started < timeoutMs);
|
|
1963
|
+
throw new Error(
|
|
1964
|
+
`Timed out waiting for Safe transfer from parent ${params.parentSafe} to child ${params.childSafe} on chain ${params.chainId} for token ${params.tokenAddress} amount ${params.amount.toString()}${params.safeTxHash ? ` (safeTxHash ${params.safeTxHash})` : ""}. Call sendDeposit again once co-signers have executed the proposal; it resumes that transfer instead of proposing another.`
|
|
1965
|
+
);
|
|
1966
|
+
}
|
|
1632
1967
|
|
|
1633
1968
|
// src/utils/strategy.ts
|
|
1634
1969
|
var PUBLIC_STRATEGIES = `"conservative", "aggressive" or "yieldmaxxing"`;
|
|
@@ -2296,7 +2631,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2296
2631
|
}
|
|
2297
2632
|
this.httpClient.setAuthToken(session.accessToken);
|
|
2298
2633
|
this.authenticatedUserId = session.userId ?? "bearer-session";
|
|
2299
|
-
this.sessionEoa = session.eoa ? (0,
|
|
2634
|
+
this.sessionEoa = session.eoa ? (0, import_viem8.getAddress)(session.eoa) : null;
|
|
2300
2635
|
this.sessionChannel = session.channel;
|
|
2301
2636
|
this.isPredeployed = false;
|
|
2302
2637
|
this.predeployedResolved = false;
|
|
@@ -2345,19 +2680,28 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2345
2680
|
this.isPredeployed = loginResponse.predeployed || false;
|
|
2346
2681
|
this.predeployedResolved = true;
|
|
2347
2682
|
this.connectedSmartWallet = loginResponse.smartWallet || null;
|
|
2348
|
-
this.sessionEoa = (0,
|
|
2683
|
+
this.sessionEoa = (0, import_viem8.getAddress)(params.message.address);
|
|
2349
2684
|
this.sessionChannel = void 0;
|
|
2350
2685
|
return loginResponse;
|
|
2351
2686
|
}
|
|
2352
|
-
/**
|
|
2353
|
-
* Warn that a legacy method is deprecated in favor of sendDeposit.
|
|
2354
|
-
* Predeployed wallets no longer require deploySafe / createSessionKey.
|
|
2355
|
-
*/
|
|
2687
|
+
/** Warn that a deprecated compatibility method was called. */
|
|
2356
2688
|
warnDeprecatedOnboardingMethod(methodName) {
|
|
2357
2689
|
console.warn(
|
|
2358
2690
|
`[ZyfaiSDK] ${methodName}() is deprecated for partner integrations. Prefer sendDeposit() \u2014 predeployed Safes and session keys are handled automatically on first deposit.`
|
|
2359
2691
|
);
|
|
2360
2692
|
}
|
|
2693
|
+
/**
|
|
2694
|
+
* Warn only a pool wallet, which sendDeposit onboards on its own. A contract
|
|
2695
|
+
* owner is never predeployed and reaches these methods through sendDeposit,
|
|
2696
|
+
* so warning unconditionally tells that caller to use what it just called.
|
|
2697
|
+
* Call after authentication, once `isPredeployed` is resolved.
|
|
2698
|
+
*/
|
|
2699
|
+
warnPredeployedOnboardingMethod(methodName) {
|
|
2700
|
+
if (!this.isPredeployed) return;
|
|
2701
|
+
console.warn(
|
|
2702
|
+
`[ZyfaiSDK] ${methodName}() is not needed for a predeployed wallet \u2014 sendDeposit() deploys the Safe and enables the agent session on first deposit.`
|
|
2703
|
+
);
|
|
2704
|
+
}
|
|
2361
2705
|
/**
|
|
2362
2706
|
* Authenticate user with SIWE (Sign-In with Ethereum) & JWT token
|
|
2363
2707
|
* This is required for accessing user-specific endpoints like session-keys/config
|
|
@@ -2374,7 +2718,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2374
2718
|
return;
|
|
2375
2719
|
}
|
|
2376
2720
|
const walletClient = this.getWalletClient();
|
|
2377
|
-
const userAddress = (0,
|
|
2721
|
+
const userAddress = (0, import_viem8.getAddress)(walletClient.account.address);
|
|
2378
2722
|
const chainId = this.currentChainId || walletClient.chain?.id || 8453;
|
|
2379
2723
|
const challengeResponse = await this.httpClient.post(ENDPOINTS.AUTH_CHALLENGE, {});
|
|
2380
2724
|
let uri;
|
|
@@ -2752,10 +3096,10 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2752
3096
|
this.walletClient.chain?.id || 8453,
|
|
2753
3097
|
this.rpcUrls
|
|
2754
3098
|
);
|
|
2755
|
-
this.walletClient = (0,
|
|
3099
|
+
this.walletClient = (0, import_viem8.createWalletClient)({
|
|
2756
3100
|
account: newAddress,
|
|
2757
3101
|
chain: chainConfig.chain,
|
|
2758
|
-
transport: (0,
|
|
3102
|
+
transport: (0, import_viem8.custom)(this.currentProvider)
|
|
2759
3103
|
});
|
|
2760
3104
|
this.currentChainId = this.walletClient.chain?.id || null;
|
|
2761
3105
|
try {
|
|
@@ -2820,10 +3164,10 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2820
3164
|
}
|
|
2821
3165
|
this.signer = (0, import_accounts2.privateKeyToAccount)(privateKey);
|
|
2822
3166
|
this.currentChainId = chainId;
|
|
2823
|
-
this.walletClient = (0,
|
|
3167
|
+
this.walletClient = (0, import_viem8.createWalletClient)({
|
|
2824
3168
|
account: this.signer,
|
|
2825
3169
|
chain: chainConfig.chain,
|
|
2826
|
-
transport: (0,
|
|
3170
|
+
transport: (0, import_viem8.http)(chainConfig.rpcUrl)
|
|
2827
3171
|
});
|
|
2828
3172
|
connectedAddress = this.signer.address;
|
|
2829
3173
|
this.currentProvider = null;
|
|
@@ -2841,10 +3185,10 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2841
3185
|
if (!accounts || accounts.length === 0) {
|
|
2842
3186
|
throw new Error("No accounts found in wallet provider");
|
|
2843
3187
|
}
|
|
2844
|
-
this.walletClient = (0,
|
|
3188
|
+
this.walletClient = (0, import_viem8.createWalletClient)({
|
|
2845
3189
|
account: accounts[0],
|
|
2846
3190
|
chain: chainConfig.chain,
|
|
2847
|
-
transport: (0,
|
|
3191
|
+
transport: (0, import_viem8.custom)(provider)
|
|
2848
3192
|
});
|
|
2849
3193
|
connectedAddress = accounts[0];
|
|
2850
3194
|
this.currentProvider = provider;
|
|
@@ -2853,7 +3197,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2853
3197
|
provider.on("accountsChanged", this.handleAccountsChanged.bind(this));
|
|
2854
3198
|
}
|
|
2855
3199
|
} else if (provider.account && provider.transport) {
|
|
2856
|
-
this.walletClient = (0,
|
|
3200
|
+
this.walletClient = (0, import_viem8.createWalletClient)({
|
|
2857
3201
|
account: provider.account,
|
|
2858
3202
|
chain: chainConfig.chain,
|
|
2859
3203
|
transport: provider.transport
|
|
@@ -2914,10 +3258,10 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2914
3258
|
targetChainId,
|
|
2915
3259
|
this.rpcUrls
|
|
2916
3260
|
);
|
|
2917
|
-
return (0,
|
|
3261
|
+
return (0, import_viem8.createWalletClient)({
|
|
2918
3262
|
account: this.signer,
|
|
2919
3263
|
chain: targetChainConfig.chain,
|
|
2920
|
-
transport: (0,
|
|
3264
|
+
transport: (0, import_viem8.http)(targetChainConfig.rpcUrl)
|
|
2921
3265
|
});
|
|
2922
3266
|
} else {
|
|
2923
3267
|
if (!this.walletClient) {
|
|
@@ -2939,6 +3283,46 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2939
3283
|
* from the user record before allowing a transfer to an undeployed pool Safe.
|
|
2940
3284
|
* @private
|
|
2941
3285
|
*/
|
|
3286
|
+
/**
|
|
3287
|
+
* Deploy the child Zyfai Safe and session for a nested (contract) owner.
|
|
3288
|
+
* @private
|
|
3289
|
+
*/
|
|
3290
|
+
async ensureNestedOwnerReadyForDeposit(userAddress, chainId, strategy) {
|
|
3291
|
+
if (this.isPredeployed) return;
|
|
3292
|
+
const chainConfig = getChainConfig(chainId, this.rpcUrls);
|
|
3293
|
+
const ownerType = await getAccountType(
|
|
3294
|
+
userAddress,
|
|
3295
|
+
chainConfig.publicClient
|
|
3296
|
+
);
|
|
3297
|
+
if (ownerType !== "Safe") return;
|
|
3298
|
+
await assertParentSafeDeployedOnChain(
|
|
3299
|
+
userAddress,
|
|
3300
|
+
chainConfig.publicClient,
|
|
3301
|
+
chainId
|
|
3302
|
+
);
|
|
3303
|
+
const childAddress = await this.getSafeAddressFor(userAddress, chainId);
|
|
3304
|
+
if (!childAddress) {
|
|
3305
|
+
throw new Error("Child smart wallet address is not available");
|
|
3306
|
+
}
|
|
3307
|
+
const childDeployed = await isSafeDeployed(
|
|
3308
|
+
childAddress,
|
|
3309
|
+
chainConfig.publicClient
|
|
3310
|
+
);
|
|
3311
|
+
if (!childDeployed) {
|
|
3312
|
+
await this.deploySafe(userAddress, chainId, strategy, true);
|
|
3313
|
+
return;
|
|
3314
|
+
}
|
|
3315
|
+
if (!this.hasActiveSessionKey) {
|
|
3316
|
+
try {
|
|
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
|
+
}
|
|
3324
|
+
}
|
|
3325
|
+
}
|
|
2942
3326
|
async hydrateSessionWallet() {
|
|
2943
3327
|
if (this.predeployedResolved || !this.sessionEoa) return;
|
|
2944
3328
|
const me = await this.httpClient.get(ENDPOINTS.USER_ME);
|
|
@@ -3023,9 +3407,10 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3023
3407
|
/**
|
|
3024
3408
|
* Deploy Safe Smart Wallet for a user
|
|
3025
3409
|
*
|
|
3026
|
-
*
|
|
3027
|
-
*
|
|
3028
|
-
* legacy
|
|
3410
|
+
* Not needed for a predeployed (pool) wallet: {@link ZyfaiSDK.sendDeposit} deploys the Safe and
|
|
3411
|
+
* enables the agent session on first deposit. Required for an owner that is not predeployed —
|
|
3412
|
+
* a contract owner such as a nested Safe, or a legacy self-deployed wallet — where `sendDeposit`
|
|
3413
|
+
* calls this method internally.
|
|
3029
3414
|
*
|
|
3030
3415
|
* @see {@link ZyfaiSDK.sendDeposit}
|
|
3031
3416
|
*
|
|
@@ -3039,16 +3424,15 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3039
3424
|
*
|
|
3040
3425
|
* @example
|
|
3041
3426
|
* ```typescript
|
|
3042
|
-
* // Preferred: sendDeposit handles
|
|
3427
|
+
* // Preferred: sendDeposit handles onboarding for every owner type
|
|
3043
3428
|
* const sent = await sdk.sendDeposit(userAddress, 8453, amount, "USDC");
|
|
3044
3429
|
* await sdk.waitForDepositCredit(sent.registration.id, 8453);
|
|
3045
3430
|
*
|
|
3046
|
-
* //
|
|
3431
|
+
* // Explicit: deploy with default conservative strategy
|
|
3047
3432
|
* await sdk.deploySafe(userAddress, 8453);
|
|
3048
3433
|
* ```
|
|
3049
3434
|
*/
|
|
3050
3435
|
async deploySafe(userAddress, chainId, strategy, createSessionKey) {
|
|
3051
|
-
this.warnDeprecatedOnboardingMethod("deploySafe");
|
|
3052
3436
|
try {
|
|
3053
3437
|
if (!userAddress) {
|
|
3054
3438
|
throw new Error("User address is required");
|
|
@@ -3057,6 +3441,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3057
3441
|
throw new Error(`Unsupported chain ID: ${chainId}`);
|
|
3058
3442
|
}
|
|
3059
3443
|
await this.authenticateUser();
|
|
3444
|
+
this.warnPredeployedOnboardingMethod("deploySafe");
|
|
3060
3445
|
if (this.isPredeployed && this.isConnectedUser(userAddress)) {
|
|
3061
3446
|
const predeployedAddress = await this.getSafeAddressFor(
|
|
3062
3447
|
userAddress,
|
|
@@ -3158,18 +3543,17 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3158
3543
|
};
|
|
3159
3544
|
} catch (error) {
|
|
3160
3545
|
console.error("Safe deployment failed:", error);
|
|
3161
|
-
throw new Error(
|
|
3162
|
-
`Safe deployment failed: ${error.message}. deploySafe() is deprecated \u2014 prefer sendDeposit() for predeployed wallet onboarding.`
|
|
3163
|
-
);
|
|
3546
|
+
throw new Error(`Safe deployment failed: ${error.message}`);
|
|
3164
3547
|
}
|
|
3165
3548
|
}
|
|
3166
3549
|
/**
|
|
3167
3550
|
* Create session key with auto-fetched configuration from Zyfai API
|
|
3168
3551
|
* This is the simplified method that automatically fetches session configuration
|
|
3169
3552
|
*
|
|
3170
|
-
*
|
|
3171
|
-
*
|
|
3172
|
-
*
|
|
3553
|
+
* Not needed for a predeployed (pool) wallet: its agent session is already enabled on-chain and
|
|
3554
|
+
* {@link ZyfaiSDK.sendDeposit} records it after the first deposit. Required for an owner that is
|
|
3555
|
+
* not predeployed — a contract owner such as a nested Safe, or a legacy self-deployed wallet —
|
|
3556
|
+
* where `sendDeposit` calls this method internally.
|
|
3173
3557
|
*
|
|
3174
3558
|
* @see {@link ZyfaiSDK.sendDeposit}
|
|
3175
3559
|
*
|
|
@@ -3181,18 +3565,18 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3181
3565
|
*
|
|
3182
3566
|
* @example
|
|
3183
3567
|
* ```typescript
|
|
3184
|
-
* // Preferred: sendDeposit handles session onboarding for
|
|
3568
|
+
* // Preferred: sendDeposit handles session onboarding for every owner type
|
|
3185
3569
|
* const sent = await sdk.sendDeposit(userAddress, 8453, amount, "USDC");
|
|
3186
3570
|
* await sdk.waitForDepositCredit(sent.registration.id, 8453);
|
|
3187
3571
|
*
|
|
3188
|
-
* //
|
|
3572
|
+
* // Explicit:
|
|
3189
3573
|
* const result = await sdk.createSessionKey(userAddress, 8453);
|
|
3190
3574
|
* ```
|
|
3191
3575
|
*/
|
|
3192
3576
|
async createSessionKey(userAddress, chainId) {
|
|
3193
|
-
this.warnDeprecatedOnboardingMethod("createSessionKey");
|
|
3194
3577
|
try {
|
|
3195
3578
|
await this.authenticateUser();
|
|
3579
|
+
this.warnPredeployedOnboardingMethod("createSessionKey");
|
|
3196
3580
|
if (!this.authenticatedUserId) {
|
|
3197
3581
|
throw new Error(
|
|
3198
3582
|
"User ID not available. Please ensure authentication completed successfully."
|
|
@@ -3237,6 +3621,12 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3237
3621
|
userAddress,
|
|
3238
3622
|
chainConfig.publicClient
|
|
3239
3623
|
);
|
|
3624
|
+
if (accountType === "Safe") {
|
|
3625
|
+
await this.assertOwnerSafeOnSessionChains(
|
|
3626
|
+
userAddress,
|
|
3627
|
+
sessions
|
|
3628
|
+
);
|
|
3629
|
+
}
|
|
3240
3630
|
const signingParams = {
|
|
3241
3631
|
permitGenericPolicy,
|
|
3242
3632
|
ignoreSecurityAttestations: accountType === "Safe"
|
|
@@ -3264,7 +3654,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3264
3654
|
};
|
|
3265
3655
|
} catch (error) {
|
|
3266
3656
|
throw new Error(
|
|
3267
|
-
`Failed to create session key: ${error.message}
|
|
3657
|
+
`Failed to create session key: ${error.message}`
|
|
3268
3658
|
);
|
|
3269
3659
|
}
|
|
3270
3660
|
}
|
|
@@ -3272,6 +3662,35 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3272
3662
|
* Internal method to sign session key
|
|
3273
3663
|
* @private
|
|
3274
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.
|
|
3674
|
+
* @private
|
|
3675
|
+
*/
|
|
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
|
+
}
|
|
3275
3694
|
async signSessionKey(userAddress, chainId, sessions, signingParams) {
|
|
3276
3695
|
try {
|
|
3277
3696
|
if (!userAddress) {
|
|
@@ -3285,14 +3704,13 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3285
3704
|
}
|
|
3286
3705
|
const walletClient = this.getWalletClient();
|
|
3287
3706
|
const chainConfig = getChainConfig(chainId, this.rpcUrls);
|
|
3288
|
-
|
|
3707
|
+
await this.hydrateSessionWallet();
|
|
3708
|
+
const childSafeAddress = await this.getSafeAddressFor(
|
|
3289
3709
|
userAddress,
|
|
3290
|
-
|
|
3710
|
+
chainId
|
|
3291
3711
|
);
|
|
3292
|
-
if (
|
|
3293
|
-
throw new Error(
|
|
3294
|
-
`Invalid account type for ${userAddress}. Must be an EOA.`
|
|
3295
|
-
);
|
|
3712
|
+
if (!childSafeAddress) {
|
|
3713
|
+
throw new Error("Child smart wallet address is not available");
|
|
3296
3714
|
}
|
|
3297
3715
|
const sessionChainIds = [
|
|
3298
3716
|
...new Set(sessions.map((s) => Number(s.chainId)))
|
|
@@ -3302,6 +3720,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3302
3720
|
{
|
|
3303
3721
|
owner: walletClient,
|
|
3304
3722
|
safeOwnerAddress: userAddress,
|
|
3723
|
+
childSafeAddress,
|
|
3305
3724
|
chain: chainConfig.chain,
|
|
3306
3725
|
publicClient: chainConfig.publicClient
|
|
3307
3726
|
},
|
|
@@ -3793,6 +4212,15 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3793
4212
|
throw new Error("Smart wallet address is not available");
|
|
3794
4213
|
}
|
|
3795
4214
|
const amountBigInt = BigInt(amount);
|
|
4215
|
+
const ownerType = await getAccountType(
|
|
4216
|
+
userAddress,
|
|
4217
|
+
chainConfig.publicClient
|
|
4218
|
+
);
|
|
4219
|
+
if (ownerType !== "EOA" && ownerType !== "Safe") {
|
|
4220
|
+
throw new Error(
|
|
4221
|
+
`Could not resolve the account type of ${userAddress} on chain ${chainId}: it is neither an EOA nor a Safe, or the RPC read failed. No deposit was sent.`
|
|
4222
|
+
);
|
|
4223
|
+
}
|
|
3796
4224
|
const [isDeployed, currentSafeBalance] = await Promise.all([
|
|
3797
4225
|
isSafeDeployed(safeAddress, chainConfig.publicClient),
|
|
3798
4226
|
minRequired !== void 0 ? chainConfig.publicClient.readContract({
|
|
@@ -3804,7 +4232,18 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3804
4232
|
this.prepareFirstDeposit(strategy)
|
|
3805
4233
|
]).then(([deployed, balance]) => [deployed, balance]);
|
|
3806
4234
|
if (!isDeployed) {
|
|
3807
|
-
if (
|
|
4235
|
+
if (ownerType === "Safe" && !this.isPredeployed) {
|
|
4236
|
+
await assertParentSafeDeployedOnChain(
|
|
4237
|
+
userAddress,
|
|
4238
|
+
chainConfig.publicClient,
|
|
4239
|
+
chainId
|
|
4240
|
+
);
|
|
4241
|
+
await this.ensureNestedOwnerReadyForDeposit(
|
|
4242
|
+
userAddress,
|
|
4243
|
+
chainId,
|
|
4244
|
+
strategy
|
|
4245
|
+
);
|
|
4246
|
+
} else if (!(this.isPredeployed && this.isConnectedUser(userAddress))) {
|
|
3808
4247
|
throw new Error(
|
|
3809
4248
|
`Safe not available for ${userAddress} on chain ${chainId}.`
|
|
3810
4249
|
);
|
|
@@ -3827,16 +4266,74 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3827
4266
|
);
|
|
3828
4267
|
}
|
|
3829
4268
|
}
|
|
4269
|
+
if (ownerType === "Safe" && isDeployed) {
|
|
4270
|
+
await assertParentSafeDeployedOnChain(
|
|
4271
|
+
userAddress,
|
|
4272
|
+
chainConfig.publicClient,
|
|
4273
|
+
chainId
|
|
4274
|
+
);
|
|
4275
|
+
await this.ensureNestedOwnerReadyForDeposit(
|
|
4276
|
+
userAddress,
|
|
4277
|
+
chainId,
|
|
4278
|
+
strategy
|
|
4279
|
+
);
|
|
4280
|
+
}
|
|
3830
4281
|
if (allowPermit && this.isPredeployed && this.isConnectedUser(userAddress)) {
|
|
3831
4282
|
const sponsored = await this.sendFirstPermitDeposit({
|
|
3832
|
-
owner: (0,
|
|
4283
|
+
owner: (0, import_viem8.getAddress)(userAddress),
|
|
3833
4284
|
safe: safeAddress,
|
|
3834
4285
|
chainId,
|
|
3835
|
-
token: (0,
|
|
4286
|
+
token: (0, import_viem8.getAddress)(token),
|
|
3836
4287
|
amount: amountBigInt
|
|
3837
4288
|
});
|
|
3838
4289
|
if (sponsored) return sponsored;
|
|
3839
4290
|
}
|
|
4291
|
+
if (ownerType === "Safe") {
|
|
4292
|
+
const since = new Date(Date.now() - NESTED_SAFE_RESUME_WINDOW_MS);
|
|
4293
|
+
const inFlight = await findSafeTransferProposal({
|
|
4294
|
+
parentSafe: userAddress,
|
|
4295
|
+
childSafe: safeAddress,
|
|
4296
|
+
chainId,
|
|
4297
|
+
tokenAddress: token,
|
|
4298
|
+
amount: amountBigInt,
|
|
4299
|
+
since
|
|
4300
|
+
});
|
|
4301
|
+
let safeTxHash = inFlight?.safeTxHash;
|
|
4302
|
+
if (!inFlight) {
|
|
4303
|
+
const proposed = await walletClient.writeContract({
|
|
4304
|
+
address: token,
|
|
4305
|
+
abi: erc20TransferWriteAbi(assetSymbol),
|
|
4306
|
+
functionName: "transfer",
|
|
4307
|
+
args: [safeAddress, amountBigInt],
|
|
4308
|
+
chain: chainConfig.chain,
|
|
4309
|
+
account: walletClient.account
|
|
4310
|
+
});
|
|
4311
|
+
safeTxHash = (await findSafeTransferByHash({ chainId, safeTxHash: proposed }))?.safeTxHash;
|
|
4312
|
+
}
|
|
4313
|
+
const executedHash = inFlight?.isExecuted && inFlight.transactionHash ? inFlight.transactionHash : await waitForSafeTransferExecution({
|
|
4314
|
+
parentSafe: userAddress,
|
|
4315
|
+
childSafe: safeAddress,
|
|
4316
|
+
chainId,
|
|
4317
|
+
tokenAddress: token,
|
|
4318
|
+
amount: amountBigInt,
|
|
4319
|
+
safeTxHash,
|
|
4320
|
+
since
|
|
4321
|
+
});
|
|
4322
|
+
const registration2 = await this.registerDeposit(
|
|
4323
|
+
chainId,
|
|
4324
|
+
executedHash,
|
|
4325
|
+
amount,
|
|
4326
|
+
token
|
|
4327
|
+
);
|
|
4328
|
+
return {
|
|
4329
|
+
success: true,
|
|
4330
|
+
txHash: executedHash,
|
|
4331
|
+
smartWallet: safeAddress,
|
|
4332
|
+
amount: amountBigInt.toString(),
|
|
4333
|
+
route: "direct",
|
|
4334
|
+
registration: registration2
|
|
4335
|
+
};
|
|
4336
|
+
}
|
|
3840
4337
|
const txHash = await walletClient.writeContract({
|
|
3841
4338
|
address: token,
|
|
3842
4339
|
abi: erc20TransferWriteAbi(assetSymbol),
|
|
@@ -3889,7 +4386,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3889
4386
|
* ```
|
|
3890
4387
|
*/
|
|
3891
4388
|
async getFirstPermitDepositStatus(userAddress) {
|
|
3892
|
-
if (!this.sessionEoa || !(0,
|
|
4389
|
+
if (!this.sessionEoa || !(0, import_viem8.isAddressEqual)((0, import_viem8.getAddress)(userAddress), this.sessionEoa)) {
|
|
3893
4390
|
throw new Error("userAddress does not match authenticated session EOA");
|
|
3894
4391
|
}
|
|
3895
4392
|
return (await this.getFirstPermitDepositAvailability()).intent;
|
|
@@ -4360,16 +4857,16 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
4360
4857
|
method: "wallet_switchEthereumChain",
|
|
4361
4858
|
params: [{ chainId: `0x${originChainId.toString(16)}` }]
|
|
4362
4859
|
});
|
|
4363
|
-
const chain = isSupportedChain(originChainId) ? getChainConfig(originChainId, this.rpcUrls).chain : (0,
|
|
4860
|
+
const chain = isSupportedChain(originChainId) ? getChainConfig(originChainId, this.rpcUrls).chain : (0, import_viem8.defineChain)({
|
|
4364
4861
|
id: originChainId,
|
|
4365
4862
|
name: `Chain ${originChainId}`,
|
|
4366
4863
|
nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
|
|
4367
4864
|
rpcUrls: { default: { http: ["http://127.0.0.1"] } }
|
|
4368
4865
|
});
|
|
4369
|
-
this.walletClient = (0,
|
|
4866
|
+
this.walletClient = (0, import_viem8.createWalletClient)({
|
|
4370
4867
|
account: this.walletClient.account,
|
|
4371
4868
|
chain,
|
|
4372
|
-
transport: (0,
|
|
4869
|
+
transport: (0, import_viem8.custom)(this.currentProvider)
|
|
4373
4870
|
});
|
|
4374
4871
|
if (isSupportedChain(originChainId)) {
|
|
4375
4872
|
this.currentChainId = originChainId;
|
|
@@ -4545,7 +5042,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
4545
5042
|
safeAddress,
|
|
4546
5043
|
tokenAddress,
|
|
4547
5044
|
to: tokenAddress,
|
|
4548
|
-
data: (0,
|
|
5045
|
+
data: (0, import_viem8.encodeFunctionData)({
|
|
4549
5046
|
abi: ERC20_ABI,
|
|
4550
5047
|
functionName: "transfer",
|
|
4551
5048
|
args: [safeAddress, amountBigInt]
|
|
@@ -4574,7 +5071,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
4574
5071
|
if (!userAddress) {
|
|
4575
5072
|
throw new Error("A user address is required");
|
|
4576
5073
|
}
|
|
4577
|
-
const owner = (0,
|
|
5074
|
+
const owner = (0, import_viem8.getAddress)(userAddress);
|
|
4578
5075
|
const chainId = 8453;
|
|
4579
5076
|
await this.hydrateSessionWallet();
|
|
4580
5077
|
const safe = await this.getSafeAddressFor(owner, chainId);
|
|
@@ -4596,7 +5093,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
4596
5093
|
]).catch(() => {
|
|
4597
5094
|
throw new Error(`Safe ${safe} is not deployed on Base`);
|
|
4598
5095
|
});
|
|
4599
|
-
if (threshold !== 1n || !owners.some((o) => (0,
|
|
5096
|
+
if (threshold !== 1n || !owners.some((o) => (0, import_viem8.getAddress)(o) === owner)) {
|
|
4600
5097
|
throw new Error(
|
|
4601
5098
|
`${owner} cannot execute Safe ${safe} alone (not an owner, or threshold above 1)`
|
|
4602
5099
|
);
|
|
@@ -4685,7 +5182,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
4685
5182
|
if (!userAddress) {
|
|
4686
5183
|
throw new Error("A user address is required");
|
|
4687
5184
|
}
|
|
4688
|
-
if (this.sessionEoa && (0,
|
|
5185
|
+
if (this.sessionEoa && (0, import_viem8.getAddress)(userAddress) !== this.sessionEoa) {
|
|
4689
5186
|
throw new Error("userAddress does not match authenticated session EOA");
|
|
4690
5187
|
}
|
|
4691
5188
|
}
|
|
@@ -4831,7 +5328,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
4831
5328
|
*/
|
|
4832
5329
|
async prepareDeposit(params) {
|
|
4833
5330
|
const { userAddress, chainId, amount, asset } = params;
|
|
4834
|
-
if (this.sessionEoa && (0,
|
|
5331
|
+
if (this.sessionEoa && (0, import_viem8.getAddress)(userAddress) !== this.sessionEoa) {
|
|
4835
5332
|
throw new Error("userAddress does not match authenticated session EOA");
|
|
4836
5333
|
}
|
|
4837
5334
|
const setup = await this.ensureDepositProfileForPrepare({
|
|
@@ -6890,7 +7387,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
6890
7387
|
if (!tokenUriResponse.tokenUri) {
|
|
6891
7388
|
throw new Error("API did not return a tokenUri");
|
|
6892
7389
|
}
|
|
6893
|
-
const callData = (0,
|
|
7390
|
+
const callData = (0, import_viem8.encodeFunctionData)({
|
|
6894
7391
|
abi: IDENTITY_REGISTRY_ABI,
|
|
6895
7392
|
functionName: "register",
|
|
6896
7393
|
args: [tokenUriResponse.tokenUri]
|
|
@@ -7359,8 +7856,8 @@ _ZyfaiSDK.IDENTITY_REGISTRY_CHAIN_IDS = [8453, 42161];
|
|
|
7359
7856
|
var ZyfaiSDK = _ZyfaiSDK;
|
|
7360
7857
|
|
|
7361
7858
|
// src/utils/session-verification.ts
|
|
7362
|
-
var
|
|
7363
|
-
var
|
|
7859
|
+
var import_module_sdk3 = require("@rhinestone/module-sdk");
|
|
7860
|
+
var import_viem9 = require("viem");
|
|
7364
7861
|
function buildV1ChainSessions(inputs) {
|
|
7365
7862
|
const chainSessions = inputs.map(({ account, smartSession, nonce, session, permitGenericPolicy, permitAdminAccess, ignoreSecurityAttestations }) => ({
|
|
7366
7863
|
chainId: session.chainId,
|
|
@@ -7389,8 +7886,8 @@ function validateV1ChainSessions(sessions) {
|
|
|
7389
7886
|
const fail = () => {
|
|
7390
7887
|
throw new Error("Invalid v1 chain session payload");
|
|
7391
7888
|
};
|
|
7392
|
-
const address = (v) => typeof v === "string" && (0,
|
|
7393
|
-
const hex = (v, bytes) => typeof v === "string" && (0,
|
|
7889
|
+
const address = (v) => typeof v === "string" && (0, import_viem9.isAddress)(v);
|
|
7890
|
+
const hex = (v, bytes) => typeof v === "string" && (0, import_viem9.isHex)(v, { strict: true }) && v.length % 2 === 0 && (bytes === void 0 || (0, import_viem9.size)(v) === bytes);
|
|
7394
7891
|
const policies = (v) => Array.isArray(v) && v.every((p) => p && address(p.policy) && hex(p.initData));
|
|
7395
7892
|
if (!Array.isArray(sessions) || !sessions.length) fail();
|
|
7396
7893
|
for (const entry of sessions) {
|
|
@@ -7422,14 +7919,14 @@ function parseV1ChainSessions(json) {
|
|
|
7422
7919
|
}
|
|
7423
7920
|
function hashV1SessionEnable(chainSessions) {
|
|
7424
7921
|
validateV1ChainSessions(chainSessions);
|
|
7425
|
-
const permissionEnableHash = (0,
|
|
7426
|
-
return { permissionEnableHash, personalSignDigest: (0,
|
|
7922
|
+
const permissionEnableHash = (0, import_module_sdk3.hashChainSessions)(chainSessions);
|
|
7923
|
+
return { permissionEnableHash, personalSignDigest: (0, import_viem9.hashMessage)({ raw: permissionEnableHash }) };
|
|
7427
7924
|
}
|
|
7428
7925
|
|
|
7429
7926
|
// src/utils/userop-verification.ts
|
|
7430
|
-
var
|
|
7927
|
+
var import_viem10 = require("viem");
|
|
7431
7928
|
var ENTRY_POINT_V07_ADDRESS = "0x0000000071727De22E5E9d8BAf0edAc6f37da032";
|
|
7432
|
-
var KNOWN_CALLS_ABI = (0,
|
|
7929
|
+
var KNOWN_CALLS_ABI = (0, import_viem10.parseAbi)([
|
|
7433
7930
|
"function setupSafe((address singleton, address[] owners, uint256 threshold, address setupTo, bytes setupData, address safe7579, (address module, bytes initData)[] validators, bytes callData) initData)",
|
|
7434
7931
|
"function execute(bytes32 mode, bytes executionCalldata)",
|
|
7435
7932
|
"function setRegistry(address registry, address[] attesters, uint8 threshold)",
|
|
@@ -7456,8 +7953,8 @@ function parseUserOperationJson(json) {
|
|
|
7456
7953
|
if (!raw || typeof raw !== "object") fail();
|
|
7457
7954
|
const input = raw;
|
|
7458
7955
|
const integer = (v) => typeof v === "string" && /^(0|[1-9][0-9]*|0x[0-9a-fA-F]+)$/.test(v) ? BigInt(v) : fail();
|
|
7459
|
-
const bytes = (v) => typeof v === "string" && (0,
|
|
7460
|
-
const address = (v) => typeof v === "string" && (0,
|
|
7956
|
+
const bytes = (v) => typeof v === "string" && (0, import_viem10.isHex)(v, { strict: true }) && v.length % 2 === 0 ? v : fail();
|
|
7957
|
+
const address = (v) => typeof v === "string" && (0, import_viem10.isAddress)(v, { strict: false }) ? v : fail();
|
|
7461
7958
|
const op = {
|
|
7462
7959
|
sender: address(input.sender),
|
|
7463
7960
|
nonce: integer(input.nonce),
|
|
@@ -7484,16 +7981,16 @@ function parseUserOperationJson(json) {
|
|
|
7484
7981
|
}
|
|
7485
7982
|
return op;
|
|
7486
7983
|
}
|
|
7487
|
-
var uint128 = (value) => (0,
|
|
7984
|
+
var uint128 = (value) => (0, import_viem10.numberToHex)(value, { size: 16 });
|
|
7488
7985
|
function hashUserOperationV07(userOp, chainId, entryPoint = ENTRY_POINT_V07_ADDRESS) {
|
|
7489
|
-
const initCode = userOp.factory ? (0,
|
|
7490
|
-
const paymasterAndData = userOp.paymaster ? (0,
|
|
7986
|
+
const initCode = userOp.factory ? (0, import_viem10.concat)([userOp.factory, userOp.factoryData ?? "0x"]) : "0x";
|
|
7987
|
+
const paymasterAndData = userOp.paymaster ? (0, import_viem10.concat)([
|
|
7491
7988
|
userOp.paymaster,
|
|
7492
7989
|
uint128(userOp.paymasterVerificationGasLimit ?? 0n),
|
|
7493
7990
|
uint128(userOp.paymasterPostOpGasLimit ?? 0n),
|
|
7494
7991
|
userOp.paymasterData ?? "0x"
|
|
7495
7992
|
]) : "0x";
|
|
7496
|
-
const packed = (0,
|
|
7993
|
+
const packed = (0, import_viem10.encodeAbiParameters)(
|
|
7497
7994
|
[
|
|
7498
7995
|
{ type: "address" },
|
|
7499
7996
|
{ type: "uint256" },
|
|
@@ -7507,26 +8004,26 @@ function hashUserOperationV07(userOp, chainId, entryPoint = ENTRY_POINT_V07_ADDR
|
|
|
7507
8004
|
[
|
|
7508
8005
|
userOp.sender,
|
|
7509
8006
|
userOp.nonce,
|
|
7510
|
-
(0,
|
|
7511
|
-
(0,
|
|
7512
|
-
(0,
|
|
8007
|
+
(0, import_viem10.keccak256)(initCode),
|
|
8008
|
+
(0, import_viem10.keccak256)(userOp.callData),
|
|
8009
|
+
(0, import_viem10.concat)([uint128(userOp.verificationGasLimit), uint128(userOp.callGasLimit)]),
|
|
7513
8010
|
userOp.preVerificationGas,
|
|
7514
|
-
(0,
|
|
7515
|
-
(0,
|
|
8011
|
+
(0, import_viem10.concat)([uint128(userOp.maxPriorityFeePerGas), uint128(userOp.maxFeePerGas)]),
|
|
8012
|
+
(0, import_viem10.keccak256)(paymasterAndData)
|
|
7516
8013
|
]
|
|
7517
8014
|
);
|
|
7518
|
-
return (0,
|
|
7519
|
-
(0,
|
|
8015
|
+
return (0, import_viem10.keccak256)(
|
|
8016
|
+
(0, import_viem10.encodeAbiParameters)(
|
|
7520
8017
|
[{ type: "bytes32" }, { type: "address" }, { type: "uint256" }],
|
|
7521
|
-
[(0,
|
|
8018
|
+
[(0, import_viem10.keccak256)(packed), entryPoint, BigInt(chainId)]
|
|
7522
8019
|
)
|
|
7523
8020
|
);
|
|
7524
8021
|
}
|
|
7525
8022
|
function decodeCall(target, value, data) {
|
|
7526
|
-
const call = { target, value, selector: (0,
|
|
8023
|
+
const call = { target, value, selector: (0, import_viem10.size)(data) >= 4 ? (0, import_viem10.slice)(data, 0, 4) : "0x" };
|
|
7527
8024
|
let decoded;
|
|
7528
8025
|
try {
|
|
7529
|
-
decoded = (0,
|
|
8026
|
+
decoded = (0, import_viem10.decodeFunctionData)({ abi: KNOWN_CALLS_ABI, data });
|
|
7530
8027
|
} catch {
|
|
7531
8028
|
return call;
|
|
7532
8029
|
}
|
|
@@ -7537,18 +8034,18 @@ function decodeCall(target, value, data) {
|
|
|
7537
8034
|
call.calls = [decodeCall(target, 0n, initData.callData)];
|
|
7538
8035
|
} else if (decoded.functionName === "execute") {
|
|
7539
8036
|
const [mode, executionCalldata] = decoded.args;
|
|
7540
|
-
const callType = (0,
|
|
8037
|
+
const callType = (0, import_viem10.slice)(mode, 0, 1);
|
|
7541
8038
|
if (callType === ERC7579_CALLTYPE_BATCH) {
|
|
7542
|
-
const [executions] = (0,
|
|
8039
|
+
const [executions] = (0, import_viem10.decodeAbiParameters)(
|
|
7543
8040
|
[{ type: "tuple[]", components: [{ type: "address", name: "target" }, { type: "uint256", name: "value" }, { type: "bytes", name: "callData" }] }],
|
|
7544
8041
|
executionCalldata
|
|
7545
8042
|
);
|
|
7546
8043
|
call.calls = executions.map((e) => decodeCall(e.target, e.value, e.callData));
|
|
7547
8044
|
} else if (callType === ERC7579_CALLTYPE_SINGLE) {
|
|
7548
8045
|
call.calls = [decodeCall(
|
|
7549
|
-
(0,
|
|
7550
|
-
BigInt((0,
|
|
7551
|
-
(0,
|
|
8046
|
+
(0, import_viem10.slice)(executionCalldata, 0, 20),
|
|
8047
|
+
BigInt((0, import_viem10.slice)(executionCalldata, 20, 52)),
|
|
8048
|
+
(0, import_viem10.size)(executionCalldata) > 52 ? (0, import_viem10.slice)(executionCalldata, 52) : "0x"
|
|
7552
8049
|
)];
|
|
7553
8050
|
}
|
|
7554
8051
|
}
|
|
@@ -7723,6 +8220,371 @@ function createBankrProvider(config) {
|
|
|
7723
8220
|
};
|
|
7724
8221
|
return provider;
|
|
7725
8222
|
}
|
|
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
|
+
}
|
|
7726
8588
|
// Annotate the CommonJS export names for ESM import in node:
|
|
7727
8589
|
0 && (module.exports = {
|
|
7728
8590
|
ALLOWED_HISTORY_DAYS,
|
|
@@ -7756,6 +8618,7 @@ function createBankrProvider(config) {
|
|
|
7756
8618
|
ZyfaiSDK,
|
|
7757
8619
|
buildV1ChainSessions,
|
|
7758
8620
|
createBankrProvider,
|
|
8621
|
+
createSafeOwnerProvider,
|
|
7759
8622
|
decodeUserOperationCalls,
|
|
7760
8623
|
getChainConfig,
|
|
7761
8624
|
getDataApiBaseUrl,
|