@wtflabs/x402 0.0.1-beta.5 → 0.0.1-beta.7

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.
@@ -1583,6 +1583,264 @@ async function signAndSimulateTransaction(signer, transaction, rpc) {
1583
1583
  return simulateResult;
1584
1584
  }
1585
1585
 
1586
+ // src/types/shared/evm/permitProxyABI.ts
1587
+ var permitProxyContractABI = [
1588
+ // settleWithPermit - 使用 EIP-2612 Permit 进行结算
1589
+ {
1590
+ inputs: [
1591
+ {
1592
+ internalType: "address",
1593
+ name: "token",
1594
+ type: "address"
1595
+ },
1596
+ {
1597
+ internalType: "address",
1598
+ name: "payer",
1599
+ type: "address"
1600
+ },
1601
+ {
1602
+ internalType: "address",
1603
+ name: "seller",
1604
+ type: "address"
1605
+ },
1606
+ {
1607
+ internalType: "uint256",
1608
+ name: "amount",
1609
+ type: "uint256"
1610
+ },
1611
+ {
1612
+ internalType: "uint256",
1613
+ name: "deadline",
1614
+ type: "uint256"
1615
+ },
1616
+ {
1617
+ internalType: "uint8",
1618
+ name: "v",
1619
+ type: "uint8"
1620
+ },
1621
+ {
1622
+ internalType: "bytes32",
1623
+ name: "r",
1624
+ type: "bytes32"
1625
+ },
1626
+ {
1627
+ internalType: "bytes32",
1628
+ name: "s",
1629
+ type: "bytes32"
1630
+ }
1631
+ ],
1632
+ name: "settleWithPermit",
1633
+ outputs: [],
1634
+ stateMutability: "nonpayable",
1635
+ type: "function"
1636
+ },
1637
+ // settleWithERC3009 - 使用 EIP-3009 TransferWithAuthorization 进行结算
1638
+ {
1639
+ inputs: [
1640
+ {
1641
+ internalType: "address",
1642
+ name: "token",
1643
+ type: "address"
1644
+ },
1645
+ {
1646
+ internalType: "address",
1647
+ name: "payer",
1648
+ type: "address"
1649
+ },
1650
+ {
1651
+ internalType: "address",
1652
+ name: "seller",
1653
+ type: "address"
1654
+ },
1655
+ {
1656
+ internalType: "uint256",
1657
+ name: "amount",
1658
+ type: "uint256"
1659
+ },
1660
+ {
1661
+ internalType: "uint256",
1662
+ name: "validAfter",
1663
+ type: "uint256"
1664
+ },
1665
+ {
1666
+ internalType: "uint256",
1667
+ name: "validBefore",
1668
+ type: "uint256"
1669
+ },
1670
+ {
1671
+ internalType: "bytes32",
1672
+ name: "nonce",
1673
+ type: "bytes32"
1674
+ },
1675
+ {
1676
+ internalType: "uint8",
1677
+ name: "v",
1678
+ type: "uint8"
1679
+ },
1680
+ {
1681
+ internalType: "bytes32",
1682
+ name: "r",
1683
+ type: "bytes32"
1684
+ },
1685
+ {
1686
+ internalType: "bytes32",
1687
+ name: "s",
1688
+ type: "bytes32"
1689
+ }
1690
+ ],
1691
+ name: "settleWithERC3009",
1692
+ outputs: [],
1693
+ stateMutability: "nonpayable",
1694
+ type: "function"
1695
+ },
1696
+ // settleWithERC3009Direct - 直接结算(无手续费)
1697
+ {
1698
+ inputs: [
1699
+ {
1700
+ internalType: "address",
1701
+ name: "token",
1702
+ type: "address"
1703
+ },
1704
+ {
1705
+ internalType: "address",
1706
+ name: "payer",
1707
+ type: "address"
1708
+ },
1709
+ {
1710
+ internalType: "address",
1711
+ name: "seller",
1712
+ type: "address"
1713
+ },
1714
+ {
1715
+ internalType: "uint256",
1716
+ name: "amount",
1717
+ type: "uint256"
1718
+ },
1719
+ {
1720
+ internalType: "uint256",
1721
+ name: "validAfter",
1722
+ type: "uint256"
1723
+ },
1724
+ {
1725
+ internalType: "uint256",
1726
+ name: "validBefore",
1727
+ type: "uint256"
1728
+ },
1729
+ {
1730
+ internalType: "bytes32",
1731
+ name: "nonce",
1732
+ type: "bytes32"
1733
+ },
1734
+ {
1735
+ internalType: "uint8",
1736
+ name: "v",
1737
+ type: "uint8"
1738
+ },
1739
+ {
1740
+ internalType: "bytes32",
1741
+ name: "r",
1742
+ type: "bytes32"
1743
+ },
1744
+ {
1745
+ internalType: "bytes32",
1746
+ name: "s",
1747
+ type: "bytes32"
1748
+ }
1749
+ ],
1750
+ name: "settleWithERC3009Direct",
1751
+ outputs: [],
1752
+ stateMutability: "nonpayable",
1753
+ type: "function"
1754
+ },
1755
+ // Events
1756
+ {
1757
+ anonymous: false,
1758
+ inputs: [
1759
+ {
1760
+ indexed: true,
1761
+ internalType: "address",
1762
+ name: "token",
1763
+ type: "address"
1764
+ },
1765
+ {
1766
+ indexed: true,
1767
+ internalType: "address",
1768
+ name: "payer",
1769
+ type: "address"
1770
+ },
1771
+ {
1772
+ indexed: true,
1773
+ internalType: "address",
1774
+ name: "seller",
1775
+ type: "address"
1776
+ },
1777
+ {
1778
+ indexed: false,
1779
+ internalType: "uint256",
1780
+ name: "amount",
1781
+ type: "uint256"
1782
+ },
1783
+ {
1784
+ indexed: false,
1785
+ internalType: "uint256",
1786
+ name: "sellerAmount",
1787
+ type: "uint256"
1788
+ },
1789
+ {
1790
+ indexed: false,
1791
+ internalType: "uint256",
1792
+ name: "feeAmount",
1793
+ type: "uint256"
1794
+ },
1795
+ {
1796
+ indexed: false,
1797
+ internalType: "string",
1798
+ name: "path",
1799
+ type: "string"
1800
+ }
1801
+ ],
1802
+ name: "SettledSplit",
1803
+ type: "event"
1804
+ },
1805
+ {
1806
+ anonymous: false,
1807
+ inputs: [
1808
+ {
1809
+ indexed: true,
1810
+ internalType: "address",
1811
+ name: "token",
1812
+ type: "address"
1813
+ },
1814
+ {
1815
+ indexed: true,
1816
+ internalType: "address",
1817
+ name: "payer",
1818
+ type: "address"
1819
+ },
1820
+ {
1821
+ indexed: true,
1822
+ internalType: "address",
1823
+ name: "seller",
1824
+ type: "address"
1825
+ },
1826
+ {
1827
+ indexed: false,
1828
+ internalType: "uint256",
1829
+ name: "amount",
1830
+ type: "uint256"
1831
+ },
1832
+ {
1833
+ indexed: false,
1834
+ internalType: "string",
1835
+ name: "path",
1836
+ type: "string"
1837
+ }
1838
+ ],
1839
+ name: "SettledDirect",
1840
+ type: "event"
1841
+ }
1842
+ ];
1843
+
1586
1844
  // src/schemes/exact/evm/eip3009/facilitator.ts
1587
1845
  async function verify(client, payload, paymentRequirements) {
1588
1846
  const exactEvmPayload = payload.payload;
@@ -1647,7 +1905,7 @@ async function verify(client, payload, paymentRequirements) {
1647
1905
  payer: exactEvmPayload.authorization.from
1648
1906
  };
1649
1907
  }
1650
- if ((0, import_viem2.getAddress)(exactEvmPayload.authorization.to) !== (0, import_viem2.getAddress)(paymentRequirements.payTo)) {
1908
+ if ((0, import_viem2.getAddress)(exactEvmPayload.authorization.to) !== (0, import_viem2.getAddress)(paymentRequirements.payTo) && (0, import_viem2.getAddress)(exactEvmPayload.authorization.to) !== (0, import_viem2.getAddress)(paymentRequirements.extra?.relayer)) {
1651
1909
  return {
1652
1910
  isValid: false,
1653
1911
  invalidReason: "invalid_exact_evm_payload_recipient_mismatch",
@@ -1720,21 +1978,57 @@ async function settle(wallet, paymentPayload, paymentRequirements) {
1720
1978
  };
1721
1979
  }
1722
1980
  const { signature } = (0, import_viem2.parseErc6492Signature)(payload.signature);
1723
- const tx = await wallet.writeContract({
1724
- address: paymentRequirements.asset,
1725
- abi: usdcABI,
1726
- functionName: "transferWithAuthorization",
1727
- args: [
1728
- payload.authorization.from,
1729
- payload.authorization.to,
1730
- BigInt(payload.authorization.value),
1731
- BigInt(payload.authorization.validAfter),
1732
- BigInt(payload.authorization.validBefore),
1733
- payload.authorization.nonce,
1734
- signature
1735
- ],
1736
- chain: wallet.chain
1737
- });
1981
+ let tx;
1982
+ if (paymentRequirements.extra?.relayer) {
1983
+ const sig = (0, import_viem2.hexToSignature)(signature);
1984
+ const v = Number(sig.v);
1985
+ const r = sig.r;
1986
+ const s = sig.s;
1987
+ tx = await wallet.writeContract({
1988
+ address: paymentRequirements.extra.relayer,
1989
+ abi: permitProxyContractABI,
1990
+ functionName: "settleWithERC3009",
1991
+ args: [
1992
+ paymentRequirements.asset,
1993
+ // token
1994
+ payload.authorization.from,
1995
+ // payer
1996
+ paymentRequirements.payTo,
1997
+ // seller
1998
+ BigInt(payload.authorization.value),
1999
+ // amount
2000
+ BigInt(payload.authorization.validAfter),
2001
+ // validAfter
2002
+ BigInt(payload.authorization.validBefore),
2003
+ // validBefore
2004
+ payload.authorization.nonce,
2005
+ // nonce
2006
+ v,
2007
+ // v (uint8)
2008
+ r,
2009
+ // r (bytes32)
2010
+ s
2011
+ // s (bytes32)
2012
+ ],
2013
+ chain: wallet.chain
2014
+ });
2015
+ } else {
2016
+ tx = await wallet.writeContract({
2017
+ address: paymentRequirements.asset,
2018
+ abi: usdcABI,
2019
+ functionName: "transferWithAuthorization",
2020
+ args: [
2021
+ payload.authorization.from,
2022
+ payload.authorization.to,
2023
+ BigInt(payload.authorization.value),
2024
+ BigInt(payload.authorization.validAfter),
2025
+ BigInt(payload.authorization.validBefore),
2026
+ payload.authorization.nonce,
2027
+ signature
2028
+ ],
2029
+ chain: wallet.chain
2030
+ });
2031
+ }
1738
2032
  const receipt = await wallet.waitForTransactionReceipt({ hash: tx });
1739
2033
  if (receipt.status !== "success") {
1740
2034
  return {
@@ -1828,63 +2122,6 @@ function splitSignature(signature) {
1828
2122
  return { v, r, s };
1829
2123
  }
1830
2124
 
1831
- // src/types/shared/evm/permitProxyABI.ts
1832
- var permitProxyContractABI = [
1833
- {
1834
- inputs: [
1835
- {
1836
- internalType: "address",
1837
- name: "token",
1838
- type: "address"
1839
- },
1840
- {
1841
- internalType: "address",
1842
- name: "owner",
1843
- type: "address"
1844
- },
1845
- {
1846
- internalType: "address",
1847
- name: "spender",
1848
- type: "address"
1849
- },
1850
- {
1851
- internalType: "uint256",
1852
- name: "value",
1853
- type: "uint256"
1854
- },
1855
- {
1856
- internalType: "uint256",
1857
- name: "deadline",
1858
- type: "uint256"
1859
- },
1860
- {
1861
- internalType: "uint8",
1862
- name: "v",
1863
- type: "uint8"
1864
- },
1865
- {
1866
- internalType: "bytes32",
1867
- name: "r",
1868
- type: "bytes32"
1869
- },
1870
- {
1871
- internalType: "bytes32",
1872
- name: "s",
1873
- type: "bytes32"
1874
- },
1875
- {
1876
- internalType: "address",
1877
- name: "to",
1878
- type: "address"
1879
- }
1880
- ],
1881
- name: "permitAndTransfer",
1882
- outputs: [],
1883
- stateMutability: "nonpayable",
1884
- type: "function"
1885
- }
1886
- ];
1887
-
1888
2125
  // src/schemes/exact/evm/permit/facilitator.ts
1889
2126
  async function verify2(client, payload, paymentRequirements) {
1890
2127
  if (payload.payload.authorizationType !== "permit" || payload.scheme !== SCHEME || paymentRequirements.scheme !== SCHEME) {
@@ -1952,8 +2189,8 @@ async function verify2(client, payload, paymentRequirements) {
1952
2189
  payer: owner
1953
2190
  };
1954
2191
  }
1955
- if (paymentRequirements.extra?.proxyAddress) {
1956
- if ((0, import_viem4.getAddress)(spender) !== (0, import_viem4.getAddress)(paymentRequirements.extra?.proxyAddress)) {
2192
+ if (paymentRequirements.extra?.relayer) {
2193
+ if ((0, import_viem4.getAddress)(spender) !== (0, import_viem4.getAddress)(paymentRequirements.extra?.relayer)) {
1957
2194
  return {
1958
2195
  isValid: false,
1959
2196
  invalidReason: "invalid_spender_address",
@@ -2017,21 +2254,28 @@ async function settle2(wallet, paymentPayload, paymentRequirements) {
2017
2254
  address: wallet.account.address
2018
2255
  });
2019
2256
  let transactionHash;
2020
- if (paymentRequirements.extra?.proxyAddress) {
2257
+ if (paymentRequirements.extra?.relayer) {
2021
2258
  transactionHash = await wallet.writeContract({
2022
- address: paymentRequirements.extra.proxyAddress,
2259
+ address: paymentRequirements.extra.relayer,
2023
2260
  abi: permitProxyContractABI,
2024
- functionName: "permitAndTransfer",
2261
+ functionName: "settleWithPermit",
2025
2262
  args: [
2026
2263
  tokenAddress,
2264
+ // token
2027
2265
  owner,
2028
- spender,
2266
+ // payer
2267
+ paymentRequirements.payTo,
2268
+ // seller
2029
2269
  BigInt(value),
2270
+ // amount
2030
2271
  BigInt(deadline),
2272
+ // deadline
2031
2273
  v,
2274
+ // v
2032
2275
  r,
2033
- s,
2034
- paymentRequirements.payTo
2276
+ // r
2277
+ s
2278
+ // s
2035
2279
  ],
2036
2280
  chain: wallet.chain,
2037
2281
  nonce: txNonce
@@ -2448,7 +2692,7 @@ function preparePaymentHeader(from, x402Version, paymentRequirements) {
2448
2692
  signature: void 0,
2449
2693
  authorization: {
2450
2694
  from,
2451
- to: paymentRequirements.payTo,
2695
+ to: paymentRequirements.extra?.relayer || paymentRequirements.payTo,
2452
2696
  value: paymentRequirements.maxAmountRequired,
2453
2697
  validAfter: validAfter.toString(),
2454
2698
  validBefore: validBefore.toString(),
@@ -2506,7 +2750,7 @@ function preparePaymentHeader2(from, x402Version, paymentRequirements) {
2506
2750
  signature: void 0,
2507
2751
  authorization: {
2508
2752
  owner: from,
2509
- spender: paymentRequirements.extra?.relayer,
2753
+ spender: paymentRequirements.extra?.relayer || paymentRequirements.payTo,
2510
2754
  value: paymentRequirements.maxAmountRequired,
2511
2755
  deadline
2512
2756
  }