@ocap/client 1.31.9 → 1.32.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/dist/extension.js CHANGED
@@ -14,7 +14,8 @@ const { bytesToHex, toBase58, toBase64, toHex, toBuffer, fromTokenToUnit, fromUn
14
14
  const { DEFAULT_TOKEN_DECIMAL } = require('@ocap/util/lib/constant');
15
15
 
16
16
  const { formatFactoryState, preMintFromFactory: preMintFromFactoryCbor } = require('@ocap/asset/mint/client');
17
- const { verifyTxInclusion } = require('./verify');
17
+ const { verifyTxInclusion, parseEntryHashV2From, computeEntryHash } = require('./verify');
18
+ const ENTRY_HASH_V2_FROM_SYM = Symbol.for('@ocap/client.entryHashV2From');
18
19
  const { createBridgeGovernance } = require('./bridge');
19
20
  const {
20
21
  toDelegateAddress,
@@ -1730,6 +1731,66 @@ const createExtensionMethods = (client, deps = defaultDeps) => {
1730
1731
  return get(res, 'getLedgerEntry') || { code: 'ERROR', entry: null };
1731
1732
  };
1732
1733
 
1734
+ const normalizeHexLocal = h => String(h || '').replace(/^0x/i, '').toLowerCase();
1735
+
1736
+ const hasAuthenticatedTrustAnchor = p => {
1737
+ if (!p) return false;
1738
+ if (typeof p.verifier === 'function') return true;
1739
+ if (typeof p.proposerPublicKey === 'string') {
1740
+ const t = p.proposerPublicKey.trim();
1741
+ if (t.length > 0 && !/^0x$/i.test(t)) return true;
1742
+ } else if (p.proposerPublicKey instanceof Uint8Array && p.proposerPublicKey.length > 0) {
1743
+ return true;
1744
+ }
1745
+ const cpHeight = get(p, 'checkpoint.height');
1746
+ const actHeight = p.checkpointSigActivationHeight;
1747
+ if (typeof cpHeight === 'number' && Number.isSafeInteger(cpHeight) && cpHeight >= 1 && typeof actHeight === 'number' && Number.isSafeInteger(actHeight) && actHeight >= 1 && cpHeight < actHeight && typeof p.activationCheckpointHash === 'string' && /^[0-9a-f]{64}$/.test(normalizeHexLocal(p.activationCheckpointHash.trim()))) {
1748
+ return true;
1749
+ }
1750
+ return false;
1751
+ };
1752
+
1753
+ const maybeRatchetPinnedV2From = (p, result) => {
1754
+ if (!result || result.verified !== true) return;
1755
+ if (!hasAuthenticatedTrustAnchor(p)) return;
1756
+ const entry = get(p, 'entry');
1757
+ if (!entry || typeof entry.sequence !== 'number' || !Number.isSafeInteger(entry.sequence) || entry.sequence < 1) {
1758
+ return;
1759
+ }
1760
+ const v2Hash = computeEntryHash(entry.sequence, entry.txRaw, entry.stateCommitHash || '');
1761
+ if (normalizeHexLocal(entry.entryHash) !== normalizeHexLocal(v2Hash)) {
1762
+ return;
1763
+ }
1764
+ if (client._pinnedEntryHashV2From === undefined || entry.sequence < client._pinnedEntryHashV2From) {
1765
+ client._pinnedEntryHashV2From = entry.sequence;
1766
+ }
1767
+ };
1768
+
1769
+ /**
1770
+ * Query active Ledger metadata (`entryHashV2From`, `proposer`, `proposerPublicKey`, `checkpointSigningEnabled`).
1771
+ *
1772
+ * @memberof GraphQLClient
1773
+ * @function
1774
+ * @name GraphQLClient#getLedgerInfo
1775
+ * @returns {Promise<object>}
1776
+ */
1777
+ client.getLedgerInfo = async () => {
1778
+ const res = await client.doRawQuery(`{
1779
+ getLedgerInfo {
1780
+ code
1781
+ info {
1782
+ chainId
1783
+ latestSequence
1784
+ entryHashV2From
1785
+ proposer
1786
+ proposerPublicKey
1787
+ checkpointSigningEnabled
1788
+ }
1789
+ }
1790
+ }`);
1791
+ return get(res, 'getLedgerInfo') || { code: 'ERROR', info: null };
1792
+ };
1793
+
1733
1794
  /**
1734
1795
  * Query a Ledger Merkle inclusion proof and its covering Checkpoint by sequence number.
1735
1796
  *
@@ -1741,9 +1802,10 @@ const createExtensionMethods = (client, deps = defaultDeps) => {
1741
1802
  * @returns {Promise<object>}
1742
1803
  */
1743
1804
  client.getMerkleProof = async ({ sequence } = {}) => {
1744
- const res = await client.doRawQuery(`{
1745
- getMerkleProof(sequence: ${Number(sequence)}) {
1746
- code
1805
+ const seqNum = Number(sequence);
1806
+ const buildQuery = withV2From => `{
1807
+ getMerkleProof(sequence: ${seqNum}) {
1808
+ code${withV2From ? '\n entryHashV2From' : ''}
1747
1809
  proof {
1748
1810
  index
1749
1811
  leaf
@@ -1764,12 +1826,49 @@ const createExtensionMethods = (client, deps = defaultDeps) => {
1764
1826
  signature
1765
1827
  }
1766
1828
  }
1767
- }`);
1768
- return get(res, 'getMerkleProof') || { code: 'ERROR', proof: null, checkpoint: null };
1829
+ }`;
1830
+
1831
+ let res;
1832
+ if (client._supportsMerkleProofV2From === false) {
1833
+ res = await client.doRawQuery(buildQuery(false));
1834
+ } else {
1835
+ try {
1836
+ res = await client.doRawQuery(buildQuery(true));
1837
+ } catch (err) {
1838
+ const msg = String(get(err, 'message') || err);
1839
+ if (msg.indexOf('entryHashV2From') !== -1) {
1840
+ client._supportsMerkleProofV2From = false;
1841
+ res = await client.doRawQuery(buildQuery(false));
1842
+ } else {
1843
+ throw err;
1844
+ }
1845
+ }
1846
+ }
1847
+
1848
+ const payload = get(res, 'getMerkleProof') || { code: 'ERROR', proof: null, checkpoint: null };
1849
+ if (payload && payload.entryHashV2From !== undefined && payload.entryHashV2From !== null) {
1850
+ if (payload.proof && typeof payload.proof === 'object') {
1851
+ Object.defineProperty(payload.proof, ENTRY_HASH_V2_FROM_SYM, {
1852
+ value: payload.entryHashV2From,
1853
+ enumerable: false,
1854
+ configurable: true
1855
+ });
1856
+ }
1857
+ if (payload.checkpoint && typeof payload.checkpoint === 'object') {
1858
+ Object.defineProperty(payload.checkpoint, ENTRY_HASH_V2_FROM_SYM, {
1859
+ value: payload.entryHashV2From,
1860
+ enumerable: false,
1861
+ configurable: true
1862
+ });
1863
+ }
1864
+ }
1865
+ return payload;
1769
1866
  };
1770
1867
 
1771
1868
  /**
1772
- * Cryptographically verify transaction inclusion in a signed Checkpoint (< 1ms, zero network I/O).
1869
+ * Cryptographically verify transaction inclusion in a signed Checkpoint (#1407, #1707).
1870
+ * Enforces the node's advertised `entryHashV2From` per-call and ratchets `client._pinnedEntryHashV2From`
1871
+ * ONLY upon cryptographically authenticated v2 entry proofs (preventing unauthenticated MITM poisoning).
1773
1872
  *
1774
1873
  * @memberof GraphQLClient
1775
1874
  * @function
@@ -1777,7 +1876,51 @@ const createExtensionMethods = (client, deps = defaultDeps) => {
1777
1876
  * @param {object} params
1778
1877
  * @returns {Promise<{ verified: boolean, reason?: string }>}
1779
1878
  */
1780
- client.verifyTxInclusion = params => verifyTxInclusion(params);
1879
+ client.verifyTxInclusion = async params => {
1880
+ if (!params || params.entryHashV2From !== undefined) {
1881
+ const explicitRes = await verifyTxInclusion(params);
1882
+ maybeRatchetPinnedV2From(params, explicitRes);
1883
+ return explicitRes;
1884
+ }
1885
+
1886
+ const entrySeq = params.entry && typeof params.entry.sequence === 'number' && Number.isSafeInteger(params.entry.sequence) ? params.entry.sequence : undefined;
1887
+ const pinnedV2From = client._pinnedEntryHashV2From;
1888
+
1889
+ let rawCandidate;
1890
+ if (params.proof && typeof params.proof === 'object' && params.proof[ENTRY_HASH_V2_FROM_SYM] !== undefined) {
1891
+ rawCandidate = params.proof[ENTRY_HASH_V2_FROM_SYM];
1892
+ } else if (params.checkpoint && typeof params.checkpoint === 'object' && params.checkpoint[ENTRY_HASH_V2_FROM_SYM] !== undefined) {
1893
+ rawCandidate = params.checkpoint[ENTRY_HASH_V2_FROM_SYM];
1894
+ } else if ((pinnedV2From === undefined || entrySeq !== undefined && entrySeq < pinnedV2From) && typeof client.doRawQuery === 'function' && client._supportsGetLedgerInfo !== false) {
1895
+ try {
1896
+ const infoRes = await client.getLedgerInfo();
1897
+ if (infoRes && infoRes.info && infoRes.info.entryHashV2From !== undefined && infoRes.info.entryHashV2From !== null) {
1898
+ rawCandidate = infoRes.info.entryHashV2From;
1899
+ }
1900
+ } catch (err) {
1901
+ const msg = String(get(err, 'message') || err);
1902
+ if (/Cannot query field.*getLedgerInfo|Unknown field.*getLedgerInfo/i.test(msg)) {
1903
+ client._supportsGetLedgerInfo = false;
1904
+ }
1905
+ }
1906
+ }
1907
+
1908
+ let effectiveV2From = pinnedV2From;
1909
+ if (rawCandidate !== undefined) {
1910
+ const parsed = parseEntryHashV2From(rawCandidate);
1911
+ if (!parsed.ok) {
1912
+ return verifyTxInclusion(Object.assign({}, params, { entryHashV2From: rawCandidate }));
1913
+ }
1914
+ if (parsed.value !== undefined) {
1915
+ effectiveV2From = effectiveV2From === undefined ? parsed.value : Math.min(effectiveV2From, parsed.value);
1916
+ }
1917
+ }
1918
+
1919
+ const effectiveParams = effectiveV2From !== undefined ? Object.assign({}, params, { entryHashV2From: effectiveV2From }) : params;
1920
+ const verifyRes = await verifyTxInclusion(effectiveParams);
1921
+ maybeRatchetPinnedV2From(effectiveParams, verifyRes);
1922
+ return verifyRes;
1923
+ };
1781
1924
 
1782
1925
  /**
1783
1926
  * On-chain bridge governance helpers (create, update, join, leave, pause, resume, close, migrate).