@ocap/client 1.31.0 → 1.31.2
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/base.js +1 -1
- package/dist/bridge.js +288 -0
- package/dist/bridge.js.map +1 -0
- package/dist/browser.d.ts +51 -1
- package/dist/bundle.js +1 -1
- package/dist/extension.js +111 -4
- package/dist/extension.js.map +1 -1
- package/dist/report.html +2 -2
- package/dist/schema/graphql.json +238 -1
- package/dist/schema/graphql.txt +2 -2
- package/dist/types.js +64 -2
- package/dist/types.js.map +1 -1
- package/dist/verify.js +9 -0
- package/dist/verify.js.map +1 -1
- package/docs/QUERIES.md +33 -0
- package/docs/README.md +84 -10
- package/docs/api-reference-transaction-helpers.ja.md +1 -1
- package/docs/api-reference-transaction-helpers.md +1 -1
- package/docs/api-reference-transaction-helpers.zh-TW.md +1 -1
- package/docs/api-reference-transaction-helpers.zh.md +1 -1
- package/lib/base.js +1 -1
- package/lib/bridge.js +290 -0
- package/lib/bridge.js.map +1 -0
- package/lib/extension.js +111 -4
- package/lib/extension.js.map +1 -1
- package/lib/node.d.ts +51 -1
- package/lib/schema/graphql.json +238 -1
- package/lib/schema/graphql.txt +2 -2
- package/lib/types.js +64 -2
- package/lib/types.js.map +1 -1
- package/lib/verify.js +9 -0
- package/lib/verify.js.map +1 -1
- package/package.json +14 -11
package/dist/verify.js
CHANGED
|
@@ -292,14 +292,23 @@ function verifyMerkleProof(leaf, index, proof, root, treeSize) {
|
|
|
292
292
|
}
|
|
293
293
|
let currentHash = leaf;
|
|
294
294
|
let currentIndex = index;
|
|
295
|
+
let levelWidth = treeSize;
|
|
295
296
|
for (let i = 0; i < proof.length; i++) {
|
|
296
297
|
const sibling = proof[i];
|
|
298
|
+
if (levelWidth !== undefined && levelWidth % 2 === 1 && currentIndex === levelWidth - 1) {
|
|
299
|
+
if (!sibling.equals(currentHash)) {
|
|
300
|
+
return false;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
297
303
|
if (currentIndex % 2 === 0) {
|
|
298
304
|
currentHash = hashPair(currentHash, sibling);
|
|
299
305
|
} else {
|
|
300
306
|
currentHash = hashPair(sibling, currentHash);
|
|
301
307
|
}
|
|
302
308
|
currentIndex = Math.floor(currentIndex / 2);
|
|
309
|
+
if (levelWidth !== undefined) {
|
|
310
|
+
levelWidth = Math.ceil(levelWidth / 2);
|
|
311
|
+
}
|
|
303
312
|
}
|
|
304
313
|
if (currentIndex !== 0) {
|
|
305
314
|
return false;
|
package/dist/verify.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/verify.js"],"names":["Hasher","Signer","toTxHash","require","formatMessage","toTypeUrl","CANONICAL_SELF_DESCRIBE_PREFIX","decodeTx","decodeCbor","encodeTx","encodeCbor","decodeProto","encodeProto","normalizeHex","hex","String","replace","toLowerCase","isPlainObject","value","Array","isArray","proto","Object","getPrototypeOf","prototype","stableStringify","JSON","stringify","map","item","join","keys","filter","k","undefined","sort","hasCborSelfDescribeTag","buf","length","sameBytes","a","b","i","validatePresentEntryPrimitives","entry","stringFields","field","val","nonce","isNum","Number","isFinite","isStr","tryDecodeOcapWireTx","txRaw","trimmed","trim","startsWith","kind","rawBuf","Buffer","from","_err","isCanonicalBase64","toString","rawBytes","Uint8Array","buffer","byteOffset","byteLength","reason","decoded","reencoded","formattedTx","hasTxField","Boolean","pk","signature","chainId","itx","typeUrl","reencodeInput","assign","rawItx","authenticateEntryDecodedFields","strict","primitiveErr","ok","rawStr","parsedRaw","parsed","parse","tx","entryTxObj","rawFrom","spenderDid","rawPk","spenderPublicKey","rawSig","spenderSignature","txType","decodedTx","decodedResult","expectedFrom","expectedNonce","expectedPk","expectedSig","expectedChainId","itxObj","rawItxType","type","mappedTypeUrl","includes","expectedTypeUrl","sha256Buf","data","SHA2","hash256","hashPair","left","right","concat","merkleProofLength","treeSize","depth","width","Math","ceil","verifyMerkleProof","leaf","index","proof","root","isInteger","equals","currentHash","currentIndex","sibling","floor","computeEntryHashV1","sequence","stateCommitHash","computeEntryHash","seqBytes","txBytes","stateBytes","computeCheckpointHash","checkpoint","height","prevSequence","txRoot","txCount","stateRoot","timestamp","prevCheckpoint","proposer","verifyCheckpointSignature","publicKeyOrVerifier","expectedProposer","hash","recomputedHash","hashBytes","Ed25519","verify","sha3Digest","SHA3","sha2Digest","NO_TRUST_ANCHOR_REASON","resolveTrustAnchor","params","verifier","proposerPublicKey","bare","test","verifyTxInclusion","verified","txHash","trustAnchor","allowUnauthenticated","verifyTxRawHash","seq","prevSeq","cpSeq","expectedIndex","v2Hash","v1Hash","pinned","entryHashV2From","legacyAllowed","allowLegacyEntryHash","givenHash","entryHash","recomputedEntryHash","expectedLeafHash","leafBuf","proofBufs","p","rootBuf","recomputedCpHash","sigValid","verifyDecodedEntry","authFields","res","defineProperty","enumerable","configurable","writable","err","Error","message","module","exports"],"mappings":"AAAA,MAAM,EAAEA,MAAF,EAAUC,MAAV,EAAkBC,QAAlB,KAA+BC,QAAQ,eAAR,CAArC;AACA,MAAM,EAAEC,aAAF,EAAiBC,SAAjB,KAA+BF,QAAQ,eAAR,CAArC;AACA,MAAM,EAAEG,8BAAF,EAAkCC,UAAUC,UAA5C,EAAwDC,UAAUC,UAAlE,KAAiFP,QAAQ,oBAAR,CAAvF;AACA,MAAM,EAAEI,UAAUI,WAAZ,EAAyBF,UAAUG,WAAnC,KAAmDT,QAAQ,wBAAR,CAAzD;;AAEA,SAASU,YAAT,CAAsBC,GAAtB,EAA2B;AACzB,SAAOC,OAAOD,OAAO,EAAd,EACJE,OADI,CACI,MADJ,EACY,EADZ,EAEJC,WAFI,EAAP;AAGD;;AAED,SAASC,aAAT,CAAuBC,KAAvB,EAA8B;AAC5B,MAAIA,UAAU,IAAV,IAAkB,OAAOA,KAAP,KAAiB,QAAnC,IAA+CC,MAAMC,OAAN,CAAcF,KAAd,CAAnD,EAAyE;AACvE,WAAO,KAAP;AACD;AACD,QAAMG,QAAQC,OAAOC,cAAP,CAAsB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EAAsC6H,OAAO+B,kBAAP,KAA8B,IAApE,CAAnB;AACA,UAAI,CAACC,WAAWlH,EAAhB,EAAoB;AAClB,eAAO,EAAEwF,UAAU,KAAZ,EAAmBzG,QAAQmI,WAAWnI,MAAtC,EAAP;AACD;AACD+B,kBAAYoG,WAAWpG,SAAvB;AACD;;AAED,UAAMqG,MAAM,EAAE3B,UAAU,IAAZ,EAAZ;AACA,QAAI1E,cAAcnE,SAAlB,EAA6B;AAC3BZ,aAAOqL,cAAP,CAAsBD,GAAtB,EAA2B,WAA3B,EAAwC;AACtCxL,eAAOmF,SAD+B;AAEtCuG,oBAAY,KAF0B;AAGtCC,sBAAc,IAHwB;AAItCC,kBAAU;AAJ4B,OAAxC;AAMD;AACD,WAAOJ,GAAP;AACD,GA5ID,CA4IE,OAAOK,GAAP,EAAY;AACZ,WAAO;AACLhC,gBAAU,KADL;AAELzG,cAAQyI,eAAeC,KAAf,GAAuBD,IAAIE,OAA3B,GAAqCnM,OAAOiM,GAAP;AAFxC,KAAP;AAID;AACF;;AAEDG,OAAOC,OAAP,GAAiB;AACfrC,mBADe;AAEflB,2BAFe;AAGfd,kBAHe;AAIfH,oBAJe;AAKfO;AALe,CAAjB","file":"verify.js","sourcesContent":["const { Hasher, Signer, toTxHash } = require('@ocap/mcrypto');\nconst { formatMessage, toTypeUrl } = require('@ocap/message');\nconst { CANONICAL_SELF_DESCRIBE_PREFIX, decodeTx: decodeCbor, encodeTx: encodeCbor } = require('@ocap/message/cbor');\nconst { decodeTx: decodeProto, encodeTx: encodeProto } = require('@ocap/message/protobuf');\n\nfunction normalizeHex(hex) {\n return String(hex || '')\n .replace(/^0x/i, '')\n .toLowerCase();\n}\n\nfunction isPlainObject(value) {\n if (value === null || typeof value !== 'object' || Array.isArray(value)) {\n return false;\n }\n const proto = Object.getPrototypeOf(value);\n return proto === Object.prototype || proto === null;\n}\n\nfunction stableStringify(value) {\n if (value === null || typeof value !== 'object') {\n return JSON.stringify(value);\n }\n if (Array.isArray(value)) {\n return `[${value.map((item) => stableStringify(item)).join(',')}]`;\n }\n if (!isPlainObject(value)) {\n return JSON.stringify(String(value));\n }\n const keys = Object.keys(value)\n .filter((k) => value[k] !== undefined)\n .sort();\n return `{${keys.map((k) => `${JSON.stringify(k)}:${stableStringify(value[k])}`).join(',')}}`;\n}\n\nfunction hasCborSelfDescribeTag(buf) {\n return (\n buf.length >= CANONICAL_SELF_DESCRIBE_PREFIX.length &&\n buf[0] === CANONICAL_SELF_DESCRIBE_PREFIX[0] &&\n buf[1] === CANONICAL_SELF_DESCRIBE_PREFIX[1] &&\n buf[2] === CANONICAL_SELF_DESCRIBE_PREFIX[2]\n );\n}\n\nfunction sameBytes(a, b) {\n if (a.length !== b.length) return false;\n for (let i = 0; i < a.length; i++) {\n if (a[i] !== b[i]) return false;\n }\n return true;\n}\n\nfunction validatePresentEntryPrimitives(entry) {\n const stringFields = ['from', 'pk', 'signature', 'chainId', 'txType'];\n for (let i = 0; i < stringFields.length; i++) {\n const field = stringFields[i];\n const val = entry[field];\n if (val !== undefined && val !== null && typeof val !== 'string') {\n return `Entry ${field} must be a string`;\n }\n }\n if (entry.nonce !== undefined && entry.nonce !== null) {\n const isNum = typeof entry.nonce === 'number' && Number.isFinite(entry.nonce);\n const isStr = typeof entry.nonce === 'string';\n if (!isNum && !isStr) {\n return 'Entry nonce must be a finite number or string';\n }\n }\n return null;\n}\n\nfunction tryDecodeOcapWireTx(txRaw) {\n const trimmed = String(txRaw || '').trim();\n if (!trimmed || trimmed.startsWith('{')) return { kind: 'none' };\n let rawBuf;\n try {\n rawBuf = Buffer.from(trimmed, 'base64');\n } catch (_err) {\n return { kind: 'none' };\n }\n if (rawBuf.length === 0) {\n return { kind: 'none' };\n }\n const isCanonicalBase64 = rawBuf.toString('base64') === trimmed;\n const rawBytes = new Uint8Array(rawBuf.buffer, rawBuf.byteOffset, rawBuf.byteLength);\n\n if (hasCborSelfDescribeTag(rawBytes)) {\n if (!isCanonicalBase64) {\n return { kind: 'invalid', reason: 'Non-canonical base64 encoding in entry txRaw' };\n }\n try {\n const decoded = decodeCbor(rawBytes);\n const reencoded = encodeCbor(decoded);\n if (!sameBytes(rawBytes, reencoded)) {\n return { kind: 'invalid', reason: 'Invalid canonical CBOR transaction in entry txRaw' };\n }\n return { kind: 'ocap', formattedTx: formatMessage('Transaction', decoded) };\n } catch (_err) {\n return { kind: 'invalid', reason: 'Failed to decode CBOR transaction in entry txRaw' };\n }\n }\n\n try {\n const decoded = decodeProto(rawBytes);\n const hasTxField = Boolean(\n decoded.from ||\n (decoded.pk && decoded.pk.length > 0) ||\n (decoded.signature && decoded.signature.length > 0) ||\n decoded.chainId ||\n (decoded.itx && typeof decoded.itx === 'object' && decoded.itx.typeUrl)\n );\n if (!hasTxField) return { kind: 'none' };\n\n const formattedTx = formatMessage('Transaction', decoded);\n const reencodeInput = Object.assign({}, decoded);\n const rawItx = decoded.itx;\n if (rawItx && typeof rawItx === 'object' && rawItx.typeUrl) {\n if (rawItx.typeUrl === 'json' || rawItx.typeUrl === 'vc') {\n reencodeInput.itx = formattedTx.itx;\n } else if (!rawItx.value) {\n reencodeInput.itx = { typeUrl: rawItx.typeUrl, value: new Uint8Array(0) };\n }\n }\n\n const reencoded = encodeProto(reencodeInput);\n if (!sameBytes(rawBytes, reencoded)) return { kind: 'none' };\n if (!isCanonicalBase64) {\n return { kind: 'invalid', reason: 'Non-canonical base64 encoding in entry txRaw' };\n }\n return { kind: 'ocap', formattedTx };\n } catch (_err) {\n return { kind: 'none' };\n }\n}\n\nfunction authenticateEntryDecodedFields(entry, strict) {\n const primitiveErr = validatePresentEntryPrimitives(entry);\n if (primitiveErr) {\n return { ok: false, reason: primitiveErr };\n }\n\n const rawStr = String(entry.txRaw !== undefined && entry.txRaw !== null ? entry.txRaw : '').trim();\n\n if (rawStr.startsWith('{')) {\n let parsedRaw;\n try {\n const parsed = JSON.parse(rawStr);\n if (!isPlainObject(parsed)) {\n return { ok: false, reason: 'Entry txRaw JSON is not an object' };\n }\n parsedRaw = parsed;\n } catch (_err) {\n return { ok: false, reason: 'Entry txRaw is not valid JSON' };\n }\n\n if (entry.tx !== undefined && entry.tx !== null) {\n let entryTxObj = entry.tx;\n if (typeof entryTxObj === 'string') {\n try {\n entryTxObj = JSON.parse(entryTxObj);\n } catch (_err) {\n return { ok: false, reason: 'Entry tx is not valid JSON' };\n }\n }\n if (!isPlainObject(entryTxObj) || stableStringify(entryTxObj) !== stableStringify(parsedRaw)) {\n return { ok: false, reason: 'Entry tx does not match authenticated txRaw' };\n }\n }\n\n const rawFrom = parsedRaw.from !== undefined && parsedRaw.from !== null ? parsedRaw.from : parsedRaw.spenderDid;\n if (rawFrom !== undefined && rawFrom !== null && entry.from !== undefined && entry.from !== null) {\n if (String(entry.from) !== String(rawFrom)) {\n return { ok: false, reason: `Entry from mismatch: expected ${rawFrom}, got ${entry.from}` };\n }\n }\n if (\n parsedRaw.nonce !== undefined &&\n parsedRaw.nonce !== null &&\n entry.nonce !== undefined &&\n entry.nonce !== null\n ) {\n if (String(entry.nonce) !== String(parsedRaw.nonce)) {\n return { ok: false, reason: `Entry nonce mismatch: expected ${parsedRaw.nonce}, got ${entry.nonce}` };\n }\n }\n const rawPk = parsedRaw.pk !== undefined && parsedRaw.pk !== null ? parsedRaw.pk : parsedRaw.spenderPublicKey;\n if (rawPk !== undefined && rawPk !== null && entry.pk !== undefined && entry.pk !== null) {\n if (String(entry.pk) !== String(rawPk)) {\n return { ok: false, reason: `Entry pk mismatch: expected ${rawPk}, got ${entry.pk}` };\n }\n }\n const rawSig =\n parsedRaw.signature !== undefined && parsedRaw.signature !== null\n ? parsedRaw.signature\n : parsedRaw.spenderSignature;\n if (rawSig !== undefined && rawSig !== null && entry.signature !== undefined && entry.signature !== null) {\n if (String(entry.signature) !== String(rawSig)) {\n return { ok: false, reason: `Entry signature mismatch: expected ${rawSig}, got ${entry.signature}` };\n }\n }\n if (\n parsedRaw.chainId !== undefined &&\n parsedRaw.chainId !== null &&\n entry.chainId !== undefined &&\n entry.chainId !== null\n ) {\n if (String(entry.chainId) !== String(parsedRaw.chainId)) {\n return { ok: false, reason: `Entry chainId mismatch: expected ${parsedRaw.chainId}, got ${entry.chainId}` };\n }\n }\n if (\n parsedRaw.txType !== undefined &&\n parsedRaw.txType !== null &&\n entry.txType !== undefined &&\n entry.txType !== null\n ) {\n if (String(entry.txType) !== String(parsedRaw.txType)) {\n return { ok: false, reason: `Entry txType mismatch: expected ${parsedRaw.txType}, got ${entry.txType}` };\n }\n }\n\n return { ok: true, decodedTx: parsedRaw };\n }\n\n const decodedResult = tryDecodeOcapWireTx(rawStr);\n if (decodedResult.kind === 'invalid') {\n return { ok: false, reason: decodedResult.reason };\n }\n if (decodedResult.kind === 'none') {\n if (strict) {\n return { ok: false, reason: 'Failed to decode entry txRaw as canonical transaction' };\n }\n return { ok: true };\n }\n const formattedTx = decodedResult.formattedTx;\n\n const expectedFrom = String(formattedTx.from || '');\n if (entry.from !== undefined && entry.from !== null && String(entry.from) !== expectedFrom) {\n return { ok: false, reason: `Entry from mismatch: expected ${expectedFrom}, got ${entry.from}` };\n }\n\n const expectedNonce = String(formattedTx.nonce !== undefined && formattedTx.nonce !== null ? formattedTx.nonce : 0);\n if (entry.nonce !== undefined && entry.nonce !== null && String(entry.nonce) !== expectedNonce) {\n return { ok: false, reason: `Entry nonce mismatch: expected ${expectedNonce}, got ${entry.nonce}` };\n }\n\n const expectedPk = String(formattedTx.pk || '');\n if (entry.pk !== undefined && entry.pk !== null && String(entry.pk) !== expectedPk) {\n return { ok: false, reason: `Entry pk mismatch: expected ${expectedPk}, got ${entry.pk}` };\n }\n\n const expectedSig = String(formattedTx.signature || '');\n if (entry.signature !== undefined && entry.signature !== null && String(entry.signature) !== expectedSig) {\n return { ok: false, reason: `Entry signature mismatch: expected ${expectedSig}, got ${entry.signature}` };\n }\n\n const expectedChainId = String(formattedTx.chainId || '');\n if (entry.chainId !== undefined && entry.chainId !== null && String(entry.chainId) !== expectedChainId) {\n return { ok: false, reason: `Entry chainId mismatch: expected ${expectedChainId}, got ${entry.chainId}` };\n }\n\n const itxObj = formattedTx.itx;\n const rawItxType = (itxObj && (itxObj.type || itxObj.typeUrl)) || '';\n if (entry.txType !== undefined && entry.txType !== null) {\n if (!rawItxType) {\n if (String(entry.txType) !== '') {\n return { ok: false, reason: `Entry txType mismatch: expected empty, got ${entry.txType}` };\n }\n } else {\n const mappedTypeUrl = rawItxType.includes(':') ? rawItxType : toTypeUrl(rawItxType);\n const expectedTypeUrl = mappedTypeUrl || rawItxType;\n if (String(entry.txType) !== rawItxType && String(entry.txType) !== expectedTypeUrl) {\n return { ok: false, reason: `Entry txType mismatch: expected ${expectedTypeUrl}, got ${entry.txType}` };\n }\n }\n }\n\n if (entry.tx !== undefined && entry.tx !== null) {\n let entryTxObj = entry.tx;\n if (typeof entryTxObj === 'string') {\n try {\n entryTxObj = JSON.parse(entryTxObj);\n } catch (_err) {\n return { ok: false, reason: 'Entry tx is not valid JSON' };\n }\n }\n if (!isPlainObject(entryTxObj) || stableStringify(entryTxObj) !== stableStringify(formattedTx)) {\n return { ok: false, reason: 'Entry tx does not match authenticated txRaw' };\n }\n }\n\n return { ok: true, decodedTx: formattedTx };\n}\n\nfunction sha256Buf(data) {\n const hex = Hasher.SHA2.hash256(data, 1, 'hex');\n return Buffer.from(String(hex).replace(/^0x/i, ''), 'hex');\n}\n\nfunction hashPair(left, right) {\n return sha256Buf(Buffer.concat([left, right]));\n}\n\n// Proof length for every leaf of a tree with `treeSize` leaves (odd last node pairs with itself)\nfunction merkleProofLength(treeSize) {\n let depth = 0;\n for (let width = treeSize; width > 1; width = Math.ceil(width / 2)) {\n depth++;\n }\n return depth;\n}\n\nfunction verifyMerkleProof(leaf, index, proof, root, treeSize) {\n if (!Number.isInteger(index) || index < 0) {\n return false;\n }\n // #972: bind the proof to the checkpoint's tree size\n if (treeSize !== undefined) {\n if (\n !Number.isInteger(treeSize) ||\n treeSize < 1 ||\n index >= treeSize ||\n proof.length !== merkleProofLength(treeSize)\n ) {\n return false;\n }\n }\n if (proof.length === 0) {\n return index === 0 && leaf.equals(root);\n }\n let currentHash = leaf;\n let currentIndex = index;\n for (let i = 0; i < proof.length; i++) {\n const sibling = proof[i];\n if (currentIndex % 2 === 0) {\n currentHash = hashPair(currentHash, sibling);\n } else {\n currentHash = hashPair(sibling, currentHash);\n }\n currentIndex = Math.floor(currentIndex / 2);\n }\n if (currentIndex !== 0) {\n return false;\n }\n return currentHash.equals(root);\n}\n\n// Entry hash versions (#1337). v1 = legacy plain concatenation written by nodes <= 1.30.26,\n// v2 = length-prefixed framing (#1325). The version is a function of the sequence on the node side.\nfunction computeEntryHashV1(sequence, txRaw, stateCommitHash = '') {\n return Hasher.SHA2.hash256(\n Buffer.concat([\n Buffer.from(String(sequence), 'utf8'),\n Buffer.from(String(txRaw == null ? '' : txRaw), 'utf8'),\n Buffer.from(String(stateCommitHash == null ? '' : stateCommitHash), 'utf8'),\n ]),\n 1,\n 'hex'\n );\n}\n\nfunction computeEntryHash(sequence, txRaw, stateCommitHash = '') {\n const seqBytes = Buffer.from(String(sequence), 'utf8');\n const txBytes = Buffer.from(String(txRaw == null ? '' : txRaw), 'utf8');\n const stateBytes = Buffer.from(String(stateCommitHash == null ? '' : stateCommitHash), 'utf8');\n const data = Buffer.concat([\n Buffer.from(`seq:${seqBytes.length}:`, 'utf8'),\n seqBytes,\n Buffer.from(`|tx:${txBytes.length}:`, 'utf8'),\n txBytes,\n Buffer.from(`|state:${stateBytes.length}:`, 'utf8'),\n stateBytes,\n ]);\n return Hasher.SHA2.hash256(data, 1, 'hex');\n}\n\nfunction computeCheckpointHash(checkpoint) {\n const data = Buffer.from(\n JSON.stringify({\n height: Number(checkpoint.height),\n sequence: Number(checkpoint.sequence),\n prevSequence: Number(checkpoint.prevSequence),\n txRoot: checkpoint.txRoot,\n txCount: Number(checkpoint.txCount),\n stateRoot: checkpoint.stateRoot || undefined,\n timestamp: Number(checkpoint.timestamp),\n prevCheckpoint: checkpoint.prevCheckpoint || null,\n proposer: checkpoint.proposer,\n })\n );\n return Hasher.SHA2.hash256(data, 1, 'hex');\n}\n\nasync function verifyCheckpointSignature(checkpoint, publicKeyOrVerifier, expectedProposer) {\n if (!checkpoint || !checkpoint.hash || !checkpoint.signature || checkpoint.signature === 'unsigned') {\n return false;\n }\n if (expectedProposer && checkpoint.proposer !== expectedProposer) {\n return false;\n }\n const recomputedHash = computeCheckpointHash(checkpoint);\n if (normalizeHex(recomputedHash) !== normalizeHex(checkpoint.hash)) {\n return false;\n }\n if (!publicKeyOrVerifier) {\n return false;\n }\n const hashBytes = Buffer.from(normalizeHex(checkpoint.hash), 'hex');\n if (typeof publicKeyOrVerifier === 'function') {\n try {\n // Only a literal `true` authenticates: `{ verified: false }`, 1 or 'false' must not pass as truthy.\n return (await publicKeyOrVerifier(hashBytes, checkpoint.signature, checkpoint.proposer)) === true;\n } catch (_err) {\n return false;\n }\n }\n try {\n if (Signer.Ed25519.verify(hashBytes, checkpoint.signature, publicKeyOrVerifier)) {\n return true;\n }\n const sha3Digest = Hasher.SHA3.hash256(hashBytes, 1);\n if (Signer.Ed25519.verify(sha3Digest, checkpoint.signature, publicKeyOrVerifier)) {\n return true;\n }\n const sha2Digest = Hasher.SHA2.hash256(hashBytes, 1);\n if (Signer.Ed25519.verify(sha2Digest, checkpoint.signature, publicKeyOrVerifier)) {\n return true;\n }\n return false;\n } catch (_err) {\n return false;\n }\n}\n\nconst NO_TRUST_ANCHOR_REASON = 'no trust anchor (proposerPublicKey/verifier) supplied';\n\n// Pick the trust anchor that authenticates the checkpoint signature, or undefined when none is usable.\n// Empty / whitespace / bare \"0x\" strings and zero-length byte arrays do not count as an anchor (#1133).\n// Hex strings are normalised to bytes whether or not they carry a 0x prefix.\nfunction resolveTrustAnchor(params) {\n if (typeof params.verifier === 'function') return params.verifier;\n const pk = params.proposerPublicKey;\n if (typeof pk === 'string') {\n const bare = pk.trim().replace(/^0x/i, '');\n if (bare.length === 0) return undefined;\n // Hex keys are accepted with or without 0x and in any case; decode them to bytes so both\n // implementations hand the same value to the signature check.\n return /^[0-9a-fA-F]+$/.test(bare) && bare.length % 2 === 0 ? Buffer.from(bare, 'hex') : pk.trim();\n }\n if (pk instanceof Uint8Array) {\n return pk.length > 0 ? pk : undefined;\n }\n return undefined;\n}\n\n/**\n * Pure client-side verification of transaction inclusion in a signed Checkpoint (< 1ms, zero network I/O).\n *\n * When `txRaw` is an OCAP wire transaction (CBOR/Protobuf) or canonical JSON payload, `verifyTxInclusion`\n * decodes `txRaw` and authenticates any present `entry.tx`, `from`, `pk`, `nonce`, `signature`, `txType`,\n * and `chainId` fields against the decoded payload (#1139), attaching `decodedTx` on the result.\n *\n * @param {object} params\n * @param {object} params.checkpoint\n * @param {object} params.proof\n * @param {object} params.entry\n * @param {string} [params.txHash]\n * @param {boolean} [params.verifyTxRawHash=true] - require entry.hash === toTxHash(txRaw) (#976)\n * @param {boolean} [params.verifyDecodedEntry] - controls decoded entry field verification (#1139)\n * @param {string} [params.expectedProposer]\n * @param {string|Buffer} [params.proposerPublicKey] - trust anchor; one of proposerPublicKey / verifier is\n * required, otherwise the result is `{ verified: false }` (#1133)\n * @param {Function} [params.verifier]\n * @param {boolean} [params.allowUnauthenticated=false] - UNSAFE: skip the signature check when no trust anchor\n * is supplied (pre-#1133 behavior). A malicious RPC can then forge inclusion.\n * @returns {Promise<{ verified: boolean, reason?: string, decodedTx?: object }>}\n */\nasync function verifyTxInclusion(params) {\n try {\n if (!params || !params.checkpoint || !params.proof || !params.entry) {\n return { verified: false, reason: 'Missing checkpoint, proof, or entry' };\n }\n\n const { checkpoint, proof, entry, txHash, expectedProposer } = params;\n\n // Fail closed (#1133): every check below is structural and a forging RPC can satisfy them all;\n // only the checkpoint signature against a trust anchor authenticates the bundle.\n const trustAnchor = resolveTrustAnchor(params);\n if (!trustAnchor && params.allowUnauthenticated !== true) {\n return { verified: false, reason: NO_TRUST_ANCHOR_REASON };\n }\n\n // entry.hash is not covered by entryHash or the Merkle leaf, so bind it to txRaw (#976).\n // On by default: this client only talks to OCAP chains, where hash = toTxHash(txRaw).\n if (\n params.verifyTxRawHash !== false &&\n normalizeHex(toTxHash(Buffer.from(String(entry.txRaw || ''), 'base64'))) !== normalizeHex(entry.hash)\n ) {\n return { verified: false, reason: `Transaction hash ${entry.hash} does not match entry txRaw` };\n }\n\n if (txHash && entry.hash !== txHash) {\n return {\n verified: false,\n reason: `Transaction hash mismatch: expected ${txHash}, got ${entry.hash}`,\n };\n }\n\n if (expectedProposer && checkpoint.proposer !== expectedProposer) {\n return {\n verified: false,\n reason: `Checkpoint proposer mismatch: expected ${expectedProposer}, got ${checkpoint.proposer}`,\n };\n }\n\n const seq = Number(entry.sequence);\n const prevSeq = Number(checkpoint.prevSequence);\n const cpSeq = Number(checkpoint.sequence);\n\n if (seq <= prevSeq || seq > cpSeq) {\n return {\n verified: false,\n reason: `Entry sequence ${seq} is outside checkpoint range (${prevSeq}, ${cpSeq}]`,\n };\n }\n\n const expectedIndex = seq - (prevSeq + 1);\n if (Number(proof.index) !== expectedIndex) {\n return {\n verified: false,\n reason: `Merkle proof index mismatch: expected ${expectedIndex}, got ${proof.index}`,\n };\n }\n\n // v2 (length-prefixed) first, then legacy v1 so proofs from 1.30.26 nodes keep verifying (#1337).\n // With params.entryHashV2From the version is exact: seq >= v2From -> v2, below -> v1.\n // allowLegacyEntryHash:false (without a pin) refuses v1. v1's boundary ambiguity cannot be\n // exploited here because entry.hash == toTxHash(txRaw) (default on) and the Merkle index pin\n // txRaw and sequence; pass entryHashV2From if you also disable verifyTxRawHash.\n const v2Hash = computeEntryHash(seq, entry.txRaw, entry.stateCommitHash || '');\n const v1Hash = computeEntryHashV1(seq, entry.txRaw, entry.stateCommitHash || '');\n const pinned = params.entryHashV2From === undefined ? undefined : seq >= Number(params.entryHashV2From) ? 2 : 1;\n const legacyAllowed = pinned === 1 || (pinned === undefined && params.allowLegacyEntryHash !== false);\n const givenHash = normalizeHex(entry.entryHash);\n let recomputedEntryHash = v2Hash;\n if (pinned !== 2 && legacyAllowed && givenHash !== normalizeHex(v2Hash) && givenHash === normalizeHex(v1Hash)) {\n recomputedEntryHash = v1Hash;\n } else if (pinned === 1) {\n recomputedEntryHash = v1Hash;\n }\n if (givenHash !== normalizeHex(recomputedEntryHash)) {\n return {\n verified: false,\n reason: `Entry hash mismatch: expected ${recomputedEntryHash}, got ${entry.entryHash}`,\n };\n }\n\n const expectedLeafHash = Hasher.SHA2.hash256(Buffer.from(recomputedEntryHash), 1, 'hex');\n if (normalizeHex(proof.leaf) !== normalizeHex(expectedLeafHash)) {\n return {\n verified: false,\n reason: `Merkle proof leaf mismatch: expected ${expectedLeafHash}, got ${proof.leaf}`,\n };\n }\n\n if (normalizeHex(proof.root) !== normalizeHex(checkpoint.txRoot)) {\n return {\n verified: false,\n reason: `Merkle proof root mismatch: expected ${checkpoint.txRoot}, got ${proof.root}`,\n };\n }\n\n const leafBuf = Buffer.from(normalizeHex(proof.leaf), 'hex');\n const proofBufs = (proof.proof || []).map((p) => Buffer.from(normalizeHex(p), 'hex'));\n const rootBuf = Buffer.from(normalizeHex(proof.root), 'hex');\n\n if (!verifyMerkleProof(leafBuf, Number(proof.index), proofBufs, rootBuf, Number(checkpoint.txCount))) {\n return { verified: false, reason: 'Invalid Merkle inclusion proof' };\n }\n\n const recomputedCpHash = computeCheckpointHash(checkpoint);\n if (normalizeHex(recomputedCpHash) !== normalizeHex(checkpoint.hash)) {\n return {\n verified: false,\n reason: `Checkpoint hash mismatch: expected ${recomputedCpHash}, got ${checkpoint.hash}`,\n };\n }\n\n if (!checkpoint.signature || checkpoint.signature === 'unsigned') {\n return { verified: false, reason: 'Checkpoint is unsigned' };\n }\n\n if (trustAnchor) {\n const sigValid = await verifyCheckpointSignature(checkpoint, trustAnchor, expectedProposer);\n if (!sigValid) {\n return { verified: false, reason: 'Invalid checkpoint signature' };\n }\n }\n\n let decodedTx;\n if (params.verifyDecodedEntry !== false) {\n const authFields = authenticateEntryDecodedFields(entry, params.verifyDecodedEntry === true);\n if (!authFields.ok) {\n return { verified: false, reason: authFields.reason };\n }\n decodedTx = authFields.decodedTx;\n }\n\n const res = { verified: true };\n if (decodedTx !== undefined) {\n Object.defineProperty(res, 'decodedTx', {\n value: decodedTx,\n enumerable: false,\n configurable: true,\n writable: true,\n });\n }\n return res;\n } catch (err) {\n return {\n verified: false,\n reason: err instanceof Error ? err.message : String(err),\n };\n }\n}\n\nmodule.exports = {\n verifyTxInclusion,\n verifyCheckpointSignature,\n computeEntryHash,\n computeEntryHashV1,\n computeCheckpointHash,\n};\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/verify.js"],"names":["Hasher","Signer","toTxHash","require","formatMessage","toTypeUrl","CANONICAL_SELF_DESCRIBE_PREFIX","decodeTx","decodeCbor","encodeTx","encodeCbor","decodeProto","encodeProto","normalizeHex","hex","String","replace","toLowerCase","isPlainObject","value","Array","isArray","proto","Object","getPrototypeOf","prototype","stableStringify","JSON","stringify","map","item","join","keys","filter","k","undefined","sort","hasCborSelfDescribeTag","buf","length","sameBytes","a","b","i","validatePresentEntryPrimitives","entry","stringFields","field","val","nonce","isNum","Number","isFinite","isStr","tryDecodeOcapWireTx","txRaw","trimmed","trim","startsWith","kind","rawBuf","Buffer","from","_err","isCanonicalBase64","toString","rawBytes","Uint8Array","buffer","byteOffset","byteLength","reason","decoded","reencoded","formattedTx","hasTxField","Boolean","pk","signature","chainId","itx","typeUrl","reencodeInput","assign","rawItx","authenticateEntryDecodedFields","strict","primitiveErr","ok","rawStr","parsedRaw","parsed","parse","tx","entryTxObj","rawFrom","spenderDid","rawPk","spenderPublicKey","rawSig","spenderSignature","txType","decodedTx","decodedResult","expectedFrom","expectedNonce","expectedPk","expectedSig","expectedChainId","itxObj","rawItxType","type","mappedTypeUrl","includes","expectedTypeUrl","sha256Buf","data","SHA2","hash256","hashPair","left","right","concat","merkleProofLength","treeSize","depth","width","Math","ceil","verifyMerkleProof","leaf","index","proof","root","isInteger","equals","currentHash","currentIndex","levelWidth","sibling","floor","computeEntryHashV1","sequence","stateCommitHash","computeEntryHash","seqBytes","txBytes","stateBytes","computeCheckpointHash","checkpoint","height","prevSequence","txRoot","txCount","stateRoot","timestamp","prevCheckpoint","proposer","verifyCheckpointSignature","publicKeyOrVerifier","expectedProposer","hash","recomputedHash","hashBytes","Ed25519","verify","sha3Digest","SHA3","sha2Digest","NO_TRUST_ANCHOR_REASON","resolveTrustAnchor","params","verifier","proposerPublicKey","bare","test","verifyTxInclusion","verified","txHash","trustAnchor","allowUnauthenticated","verifyTxRawHash","seq","prevSeq","cpSeq","expectedIndex","v2Hash","v1Hash","pinned","entryHashV2From","legacyAllowed","allowLegacyEntryHash","givenHash","entryHash","recomputedEntryHash","expectedLeafHash","leafBuf","proofBufs","p","rootBuf","recomputedCpHash","sigValid","verifyDecodedEntry","authFields","res","defineProperty","enumerable","configurable","writable","err","Error","message","module","exports"],"mappings":"AAAA,MAAM,EAAEA,MAAF,EAAUC,MAAV,EAAkBC,QAAlB,KAA+BC,QAAQ,eAAR,CAArC;AACA,MAAM,EAAEC,aAAF,EAAiBC,SAAjB,KAA+BF,QAAQ,eAAR,CAArC;AACA,MAAM,EAAEG,8BAAF,EAAkCC,UAAUC,UAA5C,EAAwDC,UAAUC,UAAlE,KAAiFP,QAAQ,oBAAR,CAAvF;AACA,MAAM,EAAEI,UAAUI,WAAZ,EAAyBF,UAAUG,WAAnC,KAAmDT,QAAQ,wBAAR,CAAzD;;AAEA,SAASU,YAAT,CAAsBC,GAAtB,EAA2B;AACzB,SAAOC,OAAOD,OAAO,EAAd,EACJE,OADI,CACI,MADJ,EACY,EADZ,EAEJC,WAFI,EAAP;AAGD;;AAED,SAASC,aAAT,CAAuBC,KAAvB,EAA8B;AAC5B,MAAIA,UAAU,IAAV,IAAkB,OAAOA,KAAP,KAAiB,QAAnC,IAA+CC,MAAMC,OAAN,CAAcF,KAAd,CAAnD,EAAyE;AACvE,WAAO,KAAP;AACD;AACD,QAAMG,QAAQC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3B,EAAP;AACD;;AAED,QAAI4G,WAAJ,EAAiB;AACf,YAAMsB,WAAW,MAAM3C,0BAA0BT,UAA1B,EAAsC8B,WAAtC,EAAmDnB,gBAAnD,CAAvB;AACA,UAAI,CAACyC,QAAL,EAAe;AACb,eAAO,EAAExB,UAAU,KAAZ,EAAmB1G,QAAQ,8BAA3B,EAAP;AACD;AACF;;AAED,QAAI+B,SAAJ;AACA,QAAIqE,OAAO+B,kBAAP,KAA8B,KAAlC,EAAyC;AACvC,YAAMC,aAAatH,+BAA+BxC,KAA/B,EAAsC8H,OAAO+B,kBAAP,KAA8B,IAApE,CAAnB;AACA,UAAI,CAACC,WAAWnH,EAAhB,EAAoB;AAClB,eAAO,EAAEyF,UAAU,KAAZ,EAAmB1G,QAAQoI,WAAWpI,MAAtC,EAAP;AACD;AACD+B,kBAAYqG,WAAWrG,SAAvB;AACD;;AAED,UAAMsG,MAAM,EAAE3B,UAAU,IAAZ,EAAZ;AACA,QAAI3E,cAAcnE,SAAlB,EAA6B;AAC3BZ,aAAOsL,cAAP,CAAsBD,GAAtB,EAA2B,WAA3B,EAAwC;AACtCzL,eAAOmF,SAD+B;AAEtCwG,oBAAY,KAF0B;AAGtCC,sBAAc,IAHwB;AAItCC,kBAAU;AAJ4B,OAAxC;AAMD;AACD,WAAOJ,GAAP;AACD,GA5ID,CA4IE,OAAOK,GAAP,EAAY;AACZ,WAAO;AACLhC,gBAAU,KADL;AAEL1G,cAAQ0I,eAAeC,KAAf,GAAuBD,IAAIE,OAA3B,GAAqCpM,OAAOkM,GAAP;AAFxC,KAAP;AAID;AACF;;AAEDG,OAAOC,OAAP,GAAiB;AACfrC,mBADe;AAEflB,2BAFe;AAGfd,kBAHe;AAIfH,oBAJe;AAKfO;AALe,CAAjB","file":"verify.js","sourcesContent":["const { Hasher, Signer, toTxHash } = require('@ocap/mcrypto');\nconst { formatMessage, toTypeUrl } = require('@ocap/message');\nconst { CANONICAL_SELF_DESCRIBE_PREFIX, decodeTx: decodeCbor, encodeTx: encodeCbor } = require('@ocap/message/cbor');\nconst { decodeTx: decodeProto, encodeTx: encodeProto } = require('@ocap/message/protobuf');\n\nfunction normalizeHex(hex) {\n return String(hex || '')\n .replace(/^0x/i, '')\n .toLowerCase();\n}\n\nfunction isPlainObject(value) {\n if (value === null || typeof value !== 'object' || Array.isArray(value)) {\n return false;\n }\n const proto = Object.getPrototypeOf(value);\n return proto === Object.prototype || proto === null;\n}\n\nfunction stableStringify(value) {\n if (value === null || typeof value !== 'object') {\n return JSON.stringify(value);\n }\n if (Array.isArray(value)) {\n return `[${value.map((item) => stableStringify(item)).join(',')}]`;\n }\n if (!isPlainObject(value)) {\n return JSON.stringify(String(value));\n }\n const keys = Object.keys(value)\n .filter((k) => value[k] !== undefined)\n .sort();\n return `{${keys.map((k) => `${JSON.stringify(k)}:${stableStringify(value[k])}`).join(',')}}`;\n}\n\nfunction hasCborSelfDescribeTag(buf) {\n return (\n buf.length >= CANONICAL_SELF_DESCRIBE_PREFIX.length &&\n buf[0] === CANONICAL_SELF_DESCRIBE_PREFIX[0] &&\n buf[1] === CANONICAL_SELF_DESCRIBE_PREFIX[1] &&\n buf[2] === CANONICAL_SELF_DESCRIBE_PREFIX[2]\n );\n}\n\nfunction sameBytes(a, b) {\n if (a.length !== b.length) return false;\n for (let i = 0; i < a.length; i++) {\n if (a[i] !== b[i]) return false;\n }\n return true;\n}\n\nfunction validatePresentEntryPrimitives(entry) {\n const stringFields = ['from', 'pk', 'signature', 'chainId', 'txType'];\n for (let i = 0; i < stringFields.length; i++) {\n const field = stringFields[i];\n const val = entry[field];\n if (val !== undefined && val !== null && typeof val !== 'string') {\n return `Entry ${field} must be a string`;\n }\n }\n if (entry.nonce !== undefined && entry.nonce !== null) {\n const isNum = typeof entry.nonce === 'number' && Number.isFinite(entry.nonce);\n const isStr = typeof entry.nonce === 'string';\n if (!isNum && !isStr) {\n return 'Entry nonce must be a finite number or string';\n }\n }\n return null;\n}\n\nfunction tryDecodeOcapWireTx(txRaw) {\n const trimmed = String(txRaw || '').trim();\n if (!trimmed || trimmed.startsWith('{')) return { kind: 'none' };\n let rawBuf;\n try {\n rawBuf = Buffer.from(trimmed, 'base64');\n } catch (_err) {\n return { kind: 'none' };\n }\n if (rawBuf.length === 0) {\n return { kind: 'none' };\n }\n const isCanonicalBase64 = rawBuf.toString('base64') === trimmed;\n const rawBytes = new Uint8Array(rawBuf.buffer, rawBuf.byteOffset, rawBuf.byteLength);\n\n if (hasCborSelfDescribeTag(rawBytes)) {\n if (!isCanonicalBase64) {\n return { kind: 'invalid', reason: 'Non-canonical base64 encoding in entry txRaw' };\n }\n try {\n const decoded = decodeCbor(rawBytes);\n const reencoded = encodeCbor(decoded);\n if (!sameBytes(rawBytes, reencoded)) {\n return { kind: 'invalid', reason: 'Invalid canonical CBOR transaction in entry txRaw' };\n }\n return { kind: 'ocap', formattedTx: formatMessage('Transaction', decoded) };\n } catch (_err) {\n return { kind: 'invalid', reason: 'Failed to decode CBOR transaction in entry txRaw' };\n }\n }\n\n try {\n const decoded = decodeProto(rawBytes);\n const hasTxField = Boolean(\n decoded.from ||\n (decoded.pk && decoded.pk.length > 0) ||\n (decoded.signature && decoded.signature.length > 0) ||\n decoded.chainId ||\n (decoded.itx && typeof decoded.itx === 'object' && decoded.itx.typeUrl)\n );\n if (!hasTxField) return { kind: 'none' };\n\n const formattedTx = formatMessage('Transaction', decoded);\n const reencodeInput = Object.assign({}, decoded);\n const rawItx = decoded.itx;\n if (rawItx && typeof rawItx === 'object' && rawItx.typeUrl) {\n if (rawItx.typeUrl === 'json' || rawItx.typeUrl === 'vc') {\n reencodeInput.itx = formattedTx.itx;\n } else if (!rawItx.value) {\n reencodeInput.itx = { typeUrl: rawItx.typeUrl, value: new Uint8Array(0) };\n }\n }\n\n const reencoded = encodeProto(reencodeInput);\n if (!sameBytes(rawBytes, reencoded)) return { kind: 'none' };\n if (!isCanonicalBase64) {\n return { kind: 'invalid', reason: 'Non-canonical base64 encoding in entry txRaw' };\n }\n return { kind: 'ocap', formattedTx };\n } catch (_err) {\n return { kind: 'none' };\n }\n}\n\nfunction authenticateEntryDecodedFields(entry, strict) {\n const primitiveErr = validatePresentEntryPrimitives(entry);\n if (primitiveErr) {\n return { ok: false, reason: primitiveErr };\n }\n\n const rawStr = String(entry.txRaw !== undefined && entry.txRaw !== null ? entry.txRaw : '').trim();\n\n if (rawStr.startsWith('{')) {\n let parsedRaw;\n try {\n const parsed = JSON.parse(rawStr);\n if (!isPlainObject(parsed)) {\n return { ok: false, reason: 'Entry txRaw JSON is not an object' };\n }\n parsedRaw = parsed;\n } catch (_err) {\n return { ok: false, reason: 'Entry txRaw is not valid JSON' };\n }\n\n if (entry.tx !== undefined && entry.tx !== null) {\n let entryTxObj = entry.tx;\n if (typeof entryTxObj === 'string') {\n try {\n entryTxObj = JSON.parse(entryTxObj);\n } catch (_err) {\n return { ok: false, reason: 'Entry tx is not valid JSON' };\n }\n }\n if (!isPlainObject(entryTxObj) || stableStringify(entryTxObj) !== stableStringify(parsedRaw)) {\n return { ok: false, reason: 'Entry tx does not match authenticated txRaw' };\n }\n }\n\n const rawFrom = parsedRaw.from !== undefined && parsedRaw.from !== null ? parsedRaw.from : parsedRaw.spenderDid;\n if (rawFrom !== undefined && rawFrom !== null && entry.from !== undefined && entry.from !== null) {\n if (String(entry.from) !== String(rawFrom)) {\n return { ok: false, reason: `Entry from mismatch: expected ${rawFrom}, got ${entry.from}` };\n }\n }\n if (\n parsedRaw.nonce !== undefined &&\n parsedRaw.nonce !== null &&\n entry.nonce !== undefined &&\n entry.nonce !== null\n ) {\n if (String(entry.nonce) !== String(parsedRaw.nonce)) {\n return { ok: false, reason: `Entry nonce mismatch: expected ${parsedRaw.nonce}, got ${entry.nonce}` };\n }\n }\n const rawPk = parsedRaw.pk !== undefined && parsedRaw.pk !== null ? parsedRaw.pk : parsedRaw.spenderPublicKey;\n if (rawPk !== undefined && rawPk !== null && entry.pk !== undefined && entry.pk !== null) {\n if (String(entry.pk) !== String(rawPk)) {\n return { ok: false, reason: `Entry pk mismatch: expected ${rawPk}, got ${entry.pk}` };\n }\n }\n const rawSig =\n parsedRaw.signature !== undefined && parsedRaw.signature !== null\n ? parsedRaw.signature\n : parsedRaw.spenderSignature;\n if (rawSig !== undefined && rawSig !== null && entry.signature !== undefined && entry.signature !== null) {\n if (String(entry.signature) !== String(rawSig)) {\n return { ok: false, reason: `Entry signature mismatch: expected ${rawSig}, got ${entry.signature}` };\n }\n }\n if (\n parsedRaw.chainId !== undefined &&\n parsedRaw.chainId !== null &&\n entry.chainId !== undefined &&\n entry.chainId !== null\n ) {\n if (String(entry.chainId) !== String(parsedRaw.chainId)) {\n return { ok: false, reason: `Entry chainId mismatch: expected ${parsedRaw.chainId}, got ${entry.chainId}` };\n }\n }\n if (\n parsedRaw.txType !== undefined &&\n parsedRaw.txType !== null &&\n entry.txType !== undefined &&\n entry.txType !== null\n ) {\n if (String(entry.txType) !== String(parsedRaw.txType)) {\n return { ok: false, reason: `Entry txType mismatch: expected ${parsedRaw.txType}, got ${entry.txType}` };\n }\n }\n\n return { ok: true, decodedTx: parsedRaw };\n }\n\n const decodedResult = tryDecodeOcapWireTx(rawStr);\n if (decodedResult.kind === 'invalid') {\n return { ok: false, reason: decodedResult.reason };\n }\n if (decodedResult.kind === 'none') {\n if (strict) {\n return { ok: false, reason: 'Failed to decode entry txRaw as canonical transaction' };\n }\n return { ok: true };\n }\n const formattedTx = decodedResult.formattedTx;\n\n const expectedFrom = String(formattedTx.from || '');\n if (entry.from !== undefined && entry.from !== null && String(entry.from) !== expectedFrom) {\n return { ok: false, reason: `Entry from mismatch: expected ${expectedFrom}, got ${entry.from}` };\n }\n\n const expectedNonce = String(formattedTx.nonce !== undefined && formattedTx.nonce !== null ? formattedTx.nonce : 0);\n if (entry.nonce !== undefined && entry.nonce !== null && String(entry.nonce) !== expectedNonce) {\n return { ok: false, reason: `Entry nonce mismatch: expected ${expectedNonce}, got ${entry.nonce}` };\n }\n\n const expectedPk = String(formattedTx.pk || '');\n if (entry.pk !== undefined && entry.pk !== null && String(entry.pk) !== expectedPk) {\n return { ok: false, reason: `Entry pk mismatch: expected ${expectedPk}, got ${entry.pk}` };\n }\n\n const expectedSig = String(formattedTx.signature || '');\n if (entry.signature !== undefined && entry.signature !== null && String(entry.signature) !== expectedSig) {\n return { ok: false, reason: `Entry signature mismatch: expected ${expectedSig}, got ${entry.signature}` };\n }\n\n const expectedChainId = String(formattedTx.chainId || '');\n if (entry.chainId !== undefined && entry.chainId !== null && String(entry.chainId) !== expectedChainId) {\n return { ok: false, reason: `Entry chainId mismatch: expected ${expectedChainId}, got ${entry.chainId}` };\n }\n\n const itxObj = formattedTx.itx;\n const rawItxType = (itxObj && (itxObj.type || itxObj.typeUrl)) || '';\n if (entry.txType !== undefined && entry.txType !== null) {\n if (!rawItxType) {\n if (String(entry.txType) !== '') {\n return { ok: false, reason: `Entry txType mismatch: expected empty, got ${entry.txType}` };\n }\n } else {\n const mappedTypeUrl = rawItxType.includes(':') ? rawItxType : toTypeUrl(rawItxType);\n const expectedTypeUrl = mappedTypeUrl || rawItxType;\n if (String(entry.txType) !== rawItxType && String(entry.txType) !== expectedTypeUrl) {\n return { ok: false, reason: `Entry txType mismatch: expected ${expectedTypeUrl}, got ${entry.txType}` };\n }\n }\n }\n\n if (entry.tx !== undefined && entry.tx !== null) {\n let entryTxObj = entry.tx;\n if (typeof entryTxObj === 'string') {\n try {\n entryTxObj = JSON.parse(entryTxObj);\n } catch (_err) {\n return { ok: false, reason: 'Entry tx is not valid JSON' };\n }\n }\n if (!isPlainObject(entryTxObj) || stableStringify(entryTxObj) !== stableStringify(formattedTx)) {\n return { ok: false, reason: 'Entry tx does not match authenticated txRaw' };\n }\n }\n\n return { ok: true, decodedTx: formattedTx };\n}\n\nfunction sha256Buf(data) {\n const hex = Hasher.SHA2.hash256(data, 1, 'hex');\n return Buffer.from(String(hex).replace(/^0x/i, ''), 'hex');\n}\n\nfunction hashPair(left, right) {\n return sha256Buf(Buffer.concat([left, right]));\n}\n\n// Proof length for every leaf of a tree with `treeSize` leaves (odd last node pairs with itself)\nfunction merkleProofLength(treeSize) {\n let depth = 0;\n for (let width = treeSize; width > 1; width = Math.ceil(width / 2)) {\n depth++;\n }\n return depth;\n}\n\nfunction verifyMerkleProof(leaf, index, proof, root, treeSize) {\n if (!Number.isInteger(index) || index < 0) {\n return false;\n }\n // #972: bind the proof to the checkpoint's tree size\n if (treeSize !== undefined) {\n if (\n !Number.isInteger(treeSize) ||\n treeSize < 1 ||\n index >= treeSize ||\n proof.length !== merkleProofLength(treeSize)\n ) {\n return false;\n }\n }\n if (proof.length === 0) {\n return index === 0 && leaf.equals(root);\n }\n let currentHash = leaf;\n let currentIndex = index;\n let levelWidth = treeSize;\n for (let i = 0; i < proof.length; i++) {\n const sibling = proof[i];\n if (levelWidth !== undefined && levelWidth % 2 === 1 && currentIndex === levelWidth - 1) {\n if (!sibling.equals(currentHash)) {\n return false;\n }\n }\n if (currentIndex % 2 === 0) {\n currentHash = hashPair(currentHash, sibling);\n } else {\n currentHash = hashPair(sibling, currentHash);\n }\n currentIndex = Math.floor(currentIndex / 2);\n if (levelWidth !== undefined) {\n levelWidth = Math.ceil(levelWidth / 2);\n }\n }\n if (currentIndex !== 0) {\n return false;\n }\n return currentHash.equals(root);\n}\n\n// Entry hash versions (#1337). v1 = legacy plain concatenation written by nodes <= 1.30.26,\n// v2 = length-prefixed framing (#1325). The version is a function of the sequence on the node side.\nfunction computeEntryHashV1(sequence, txRaw, stateCommitHash = '') {\n return Hasher.SHA2.hash256(\n Buffer.concat([\n Buffer.from(String(sequence), 'utf8'),\n Buffer.from(String(txRaw == null ? '' : txRaw), 'utf8'),\n Buffer.from(String(stateCommitHash == null ? '' : stateCommitHash), 'utf8'),\n ]),\n 1,\n 'hex'\n );\n}\n\nfunction computeEntryHash(sequence, txRaw, stateCommitHash = '') {\n const seqBytes = Buffer.from(String(sequence), 'utf8');\n const txBytes = Buffer.from(String(txRaw == null ? '' : txRaw), 'utf8');\n const stateBytes = Buffer.from(String(stateCommitHash == null ? '' : stateCommitHash), 'utf8');\n const data = Buffer.concat([\n Buffer.from(`seq:${seqBytes.length}:`, 'utf8'),\n seqBytes,\n Buffer.from(`|tx:${txBytes.length}:`, 'utf8'),\n txBytes,\n Buffer.from(`|state:${stateBytes.length}:`, 'utf8'),\n stateBytes,\n ]);\n return Hasher.SHA2.hash256(data, 1, 'hex');\n}\n\nfunction computeCheckpointHash(checkpoint) {\n const data = Buffer.from(\n JSON.stringify({\n height: Number(checkpoint.height),\n sequence: Number(checkpoint.sequence),\n prevSequence: Number(checkpoint.prevSequence),\n txRoot: checkpoint.txRoot,\n txCount: Number(checkpoint.txCount),\n stateRoot: checkpoint.stateRoot || undefined,\n timestamp: Number(checkpoint.timestamp),\n prevCheckpoint: checkpoint.prevCheckpoint || null,\n proposer: checkpoint.proposer,\n })\n );\n return Hasher.SHA2.hash256(data, 1, 'hex');\n}\n\nasync function verifyCheckpointSignature(checkpoint, publicKeyOrVerifier, expectedProposer) {\n if (!checkpoint || !checkpoint.hash || !checkpoint.signature || checkpoint.signature === 'unsigned') {\n return false;\n }\n if (expectedProposer && checkpoint.proposer !== expectedProposer) {\n return false;\n }\n const recomputedHash = computeCheckpointHash(checkpoint);\n if (normalizeHex(recomputedHash) !== normalizeHex(checkpoint.hash)) {\n return false;\n }\n if (!publicKeyOrVerifier) {\n return false;\n }\n const hashBytes = Buffer.from(normalizeHex(checkpoint.hash), 'hex');\n if (typeof publicKeyOrVerifier === 'function') {\n try {\n // Only a literal `true` authenticates: `{ verified: false }`, 1 or 'false' must not pass as truthy.\n return (await publicKeyOrVerifier(hashBytes, checkpoint.signature, checkpoint.proposer)) === true;\n } catch (_err) {\n return false;\n }\n }\n try {\n if (Signer.Ed25519.verify(hashBytes, checkpoint.signature, publicKeyOrVerifier)) {\n return true;\n }\n const sha3Digest = Hasher.SHA3.hash256(hashBytes, 1);\n if (Signer.Ed25519.verify(sha3Digest, checkpoint.signature, publicKeyOrVerifier)) {\n return true;\n }\n const sha2Digest = Hasher.SHA2.hash256(hashBytes, 1);\n if (Signer.Ed25519.verify(sha2Digest, checkpoint.signature, publicKeyOrVerifier)) {\n return true;\n }\n return false;\n } catch (_err) {\n return false;\n }\n}\n\nconst NO_TRUST_ANCHOR_REASON = 'no trust anchor (proposerPublicKey/verifier) supplied';\n\n// Pick the trust anchor that authenticates the checkpoint signature, or undefined when none is usable.\n// Empty / whitespace / bare \"0x\" strings and zero-length byte arrays do not count as an anchor (#1133).\n// Hex strings are normalised to bytes whether or not they carry a 0x prefix.\nfunction resolveTrustAnchor(params) {\n if (typeof params.verifier === 'function') return params.verifier;\n const pk = params.proposerPublicKey;\n if (typeof pk === 'string') {\n const bare = pk.trim().replace(/^0x/i, '');\n if (bare.length === 0) return undefined;\n // Hex keys are accepted with or without 0x and in any case; decode them to bytes so both\n // implementations hand the same value to the signature check.\n return /^[0-9a-fA-F]+$/.test(bare) && bare.length % 2 === 0 ? Buffer.from(bare, 'hex') : pk.trim();\n }\n if (pk instanceof Uint8Array) {\n return pk.length > 0 ? pk : undefined;\n }\n return undefined;\n}\n\n/**\n * Pure client-side verification of transaction inclusion in a signed Checkpoint (< 1ms, zero network I/O).\n *\n * When `txRaw` is an OCAP wire transaction (CBOR/Protobuf) or canonical JSON payload, `verifyTxInclusion`\n * decodes `txRaw` and authenticates any present `entry.tx`, `from`, `pk`, `nonce`, `signature`, `txType`,\n * and `chainId` fields against the decoded payload (#1139), attaching `decodedTx` on the result.\n *\n * @param {object} params\n * @param {object} params.checkpoint\n * @param {object} params.proof\n * @param {object} params.entry\n * @param {string} [params.txHash]\n * @param {boolean} [params.verifyTxRawHash=true] - require entry.hash === toTxHash(txRaw) (#976)\n * @param {boolean} [params.verifyDecodedEntry] - controls decoded entry field verification (#1139)\n * @param {string} [params.expectedProposer]\n * @param {string|Buffer} [params.proposerPublicKey] - trust anchor; one of proposerPublicKey / verifier is\n * required, otherwise the result is `{ verified: false }` (#1133)\n * @param {Function} [params.verifier]\n * @param {boolean} [params.allowUnauthenticated=false] - UNSAFE: skip the signature check when no trust anchor\n * is supplied (pre-#1133 behavior). A malicious RPC can then forge inclusion.\n * @returns {Promise<{ verified: boolean, reason?: string, decodedTx?: object }>}\n */\nasync function verifyTxInclusion(params) {\n try {\n if (!params || !params.checkpoint || !params.proof || !params.entry) {\n return { verified: false, reason: 'Missing checkpoint, proof, or entry' };\n }\n\n const { checkpoint, proof, entry, txHash, expectedProposer } = params;\n\n // Fail closed (#1133): every check below is structural and a forging RPC can satisfy them all;\n // only the checkpoint signature against a trust anchor authenticates the bundle.\n const trustAnchor = resolveTrustAnchor(params);\n if (!trustAnchor && params.allowUnauthenticated !== true) {\n return { verified: false, reason: NO_TRUST_ANCHOR_REASON };\n }\n\n // entry.hash is not covered by entryHash or the Merkle leaf, so bind it to txRaw (#976).\n // On by default: this client only talks to OCAP chains, where hash = toTxHash(txRaw).\n if (\n params.verifyTxRawHash !== false &&\n normalizeHex(toTxHash(Buffer.from(String(entry.txRaw || ''), 'base64'))) !== normalizeHex(entry.hash)\n ) {\n return { verified: false, reason: `Transaction hash ${entry.hash} does not match entry txRaw` };\n }\n\n if (txHash && entry.hash !== txHash) {\n return {\n verified: false,\n reason: `Transaction hash mismatch: expected ${txHash}, got ${entry.hash}`,\n };\n }\n\n if (expectedProposer && checkpoint.proposer !== expectedProposer) {\n return {\n verified: false,\n reason: `Checkpoint proposer mismatch: expected ${expectedProposer}, got ${checkpoint.proposer}`,\n };\n }\n\n const seq = Number(entry.sequence);\n const prevSeq = Number(checkpoint.prevSequence);\n const cpSeq = Number(checkpoint.sequence);\n\n if (seq <= prevSeq || seq > cpSeq) {\n return {\n verified: false,\n reason: `Entry sequence ${seq} is outside checkpoint range (${prevSeq}, ${cpSeq}]`,\n };\n }\n\n const expectedIndex = seq - (prevSeq + 1);\n if (Number(proof.index) !== expectedIndex) {\n return {\n verified: false,\n reason: `Merkle proof index mismatch: expected ${expectedIndex}, got ${proof.index}`,\n };\n }\n\n // v2 (length-prefixed) first, then legacy v1 so proofs from 1.30.26 nodes keep verifying (#1337).\n // With params.entryHashV2From the version is exact: seq >= v2From -> v2, below -> v1.\n // allowLegacyEntryHash:false (without a pin) refuses v1. v1's boundary ambiguity cannot be\n // exploited here because entry.hash == toTxHash(txRaw) (default on) and the Merkle index pin\n // txRaw and sequence; pass entryHashV2From if you also disable verifyTxRawHash.\n const v2Hash = computeEntryHash(seq, entry.txRaw, entry.stateCommitHash || '');\n const v1Hash = computeEntryHashV1(seq, entry.txRaw, entry.stateCommitHash || '');\n const pinned = params.entryHashV2From === undefined ? undefined : seq >= Number(params.entryHashV2From) ? 2 : 1;\n const legacyAllowed = pinned === 1 || (pinned === undefined && params.allowLegacyEntryHash !== false);\n const givenHash = normalizeHex(entry.entryHash);\n let recomputedEntryHash = v2Hash;\n if (pinned !== 2 && legacyAllowed && givenHash !== normalizeHex(v2Hash) && givenHash === normalizeHex(v1Hash)) {\n recomputedEntryHash = v1Hash;\n } else if (pinned === 1) {\n recomputedEntryHash = v1Hash;\n }\n if (givenHash !== normalizeHex(recomputedEntryHash)) {\n return {\n verified: false,\n reason: `Entry hash mismatch: expected ${recomputedEntryHash}, got ${entry.entryHash}`,\n };\n }\n\n const expectedLeafHash = Hasher.SHA2.hash256(Buffer.from(recomputedEntryHash), 1, 'hex');\n if (normalizeHex(proof.leaf) !== normalizeHex(expectedLeafHash)) {\n return {\n verified: false,\n reason: `Merkle proof leaf mismatch: expected ${expectedLeafHash}, got ${proof.leaf}`,\n };\n }\n\n if (normalizeHex(proof.root) !== normalizeHex(checkpoint.txRoot)) {\n return {\n verified: false,\n reason: `Merkle proof root mismatch: expected ${checkpoint.txRoot}, got ${proof.root}`,\n };\n }\n\n const leafBuf = Buffer.from(normalizeHex(proof.leaf), 'hex');\n const proofBufs = (proof.proof || []).map((p) => Buffer.from(normalizeHex(p), 'hex'));\n const rootBuf = Buffer.from(normalizeHex(proof.root), 'hex');\n\n if (!verifyMerkleProof(leafBuf, Number(proof.index), proofBufs, rootBuf, Number(checkpoint.txCount))) {\n return { verified: false, reason: 'Invalid Merkle inclusion proof' };\n }\n\n const recomputedCpHash = computeCheckpointHash(checkpoint);\n if (normalizeHex(recomputedCpHash) !== normalizeHex(checkpoint.hash)) {\n return {\n verified: false,\n reason: `Checkpoint hash mismatch: expected ${recomputedCpHash}, got ${checkpoint.hash}`,\n };\n }\n\n if (!checkpoint.signature || checkpoint.signature === 'unsigned') {\n return { verified: false, reason: 'Checkpoint is unsigned' };\n }\n\n if (trustAnchor) {\n const sigValid = await verifyCheckpointSignature(checkpoint, trustAnchor, expectedProposer);\n if (!sigValid) {\n return { verified: false, reason: 'Invalid checkpoint signature' };\n }\n }\n\n let decodedTx;\n if (params.verifyDecodedEntry !== false) {\n const authFields = authenticateEntryDecodedFields(entry, params.verifyDecodedEntry === true);\n if (!authFields.ok) {\n return { verified: false, reason: authFields.reason };\n }\n decodedTx = authFields.decodedTx;\n }\n\n const res = { verified: true };\n if (decodedTx !== undefined) {\n Object.defineProperty(res, 'decodedTx', {\n value: decodedTx,\n enumerable: false,\n configurable: true,\n writable: true,\n });\n }\n return res;\n } catch (err) {\n return {\n verified: false,\n reason: err instanceof Error ? err.message : String(err),\n };\n }\n}\n\nmodule.exports = {\n verifyTxInclusion,\n verifyCheckpointSignature,\n computeEntryHash,\n computeEntryHashV1,\n computeCheckpointHash,\n};\n"]}
|
package/docs/QUERIES.md
CHANGED
|
@@ -118,6 +118,7 @@
|
|
|
118
118
|
from
|
|
119
119
|
gasFee
|
|
120
120
|
gasPaid
|
|
121
|
+
gasPayer
|
|
121
122
|
itxJson
|
|
122
123
|
nonce
|
|
123
124
|
pk
|
|
@@ -170,6 +171,7 @@
|
|
|
170
171
|
from
|
|
171
172
|
gasFee
|
|
172
173
|
gasPaid
|
|
174
|
+
gasPayer
|
|
173
175
|
itxJson
|
|
174
176
|
nonce
|
|
175
177
|
pk
|
|
@@ -273,6 +275,7 @@
|
|
|
273
275
|
from
|
|
274
276
|
gasFee
|
|
275
277
|
gasPaid
|
|
278
|
+
gasPayer
|
|
276
279
|
itxJson
|
|
277
280
|
nonce
|
|
278
281
|
pk
|
|
@@ -325,6 +328,7 @@
|
|
|
325
328
|
from
|
|
326
329
|
gasFee
|
|
327
330
|
gasPaid
|
|
331
|
+
gasPayer
|
|
328
332
|
itxJson
|
|
329
333
|
nonce
|
|
330
334
|
pk
|
|
@@ -421,6 +425,7 @@
|
|
|
421
425
|
from
|
|
422
426
|
gasFee
|
|
423
427
|
gasPaid
|
|
428
|
+
gasPayer
|
|
424
429
|
itxJson
|
|
425
430
|
nonce
|
|
426
431
|
pk
|
|
@@ -473,6 +478,7 @@
|
|
|
473
478
|
from
|
|
474
479
|
gasFee
|
|
475
480
|
gasPaid
|
|
481
|
+
gasPayer
|
|
476
482
|
itxJson
|
|
477
483
|
nonce
|
|
478
484
|
pk
|
|
@@ -613,6 +619,7 @@
|
|
|
613
619
|
from
|
|
614
620
|
gasFee
|
|
615
621
|
gasPaid
|
|
622
|
+
gasPayer
|
|
616
623
|
itxJson
|
|
617
624
|
nonce
|
|
618
625
|
pk
|
|
@@ -665,6 +672,7 @@
|
|
|
665
672
|
from
|
|
666
673
|
gasFee
|
|
667
674
|
gasPaid
|
|
675
|
+
gasPayer
|
|
668
676
|
itxJson
|
|
669
677
|
nonce
|
|
670
678
|
pk
|
|
@@ -811,6 +819,7 @@
|
|
|
811
819
|
from
|
|
812
820
|
gasFee
|
|
813
821
|
gasPaid
|
|
822
|
+
gasPayer
|
|
814
823
|
itxJson
|
|
815
824
|
nonce
|
|
816
825
|
pk
|
|
@@ -863,6 +872,7 @@
|
|
|
863
872
|
from
|
|
864
873
|
gasFee
|
|
865
874
|
gasPaid
|
|
875
|
+
gasPayer
|
|
866
876
|
itxJson
|
|
867
877
|
nonce
|
|
868
878
|
pk
|
|
@@ -954,6 +964,7 @@
|
|
|
954
964
|
from
|
|
955
965
|
gasFee
|
|
956
966
|
gasPaid
|
|
967
|
+
gasPayer
|
|
957
968
|
itxJson
|
|
958
969
|
nonce
|
|
959
970
|
pk
|
|
@@ -1006,6 +1017,7 @@
|
|
|
1006
1017
|
from
|
|
1007
1018
|
gasFee
|
|
1008
1019
|
gasPaid
|
|
1020
|
+
gasPayer
|
|
1009
1021
|
itxJson
|
|
1010
1022
|
nonce
|
|
1011
1023
|
pk
|
|
@@ -1125,6 +1137,7 @@
|
|
|
1125
1137
|
version
|
|
1126
1138
|
}
|
|
1127
1139
|
vaults {
|
|
1140
|
+
bridgeReserve
|
|
1128
1141
|
slashedStake
|
|
1129
1142
|
txFee
|
|
1130
1143
|
txGas
|
|
@@ -1190,6 +1203,7 @@
|
|
|
1190
1203
|
from
|
|
1191
1204
|
gasFee
|
|
1192
1205
|
gasPaid
|
|
1206
|
+
gasPayer
|
|
1193
1207
|
itxJson
|
|
1194
1208
|
nonce
|
|
1195
1209
|
pk
|
|
@@ -1242,6 +1256,7 @@
|
|
|
1242
1256
|
from
|
|
1243
1257
|
gasFee
|
|
1244
1258
|
gasPaid
|
|
1259
|
+
gasPayer
|
|
1245
1260
|
itxJson
|
|
1246
1261
|
nonce
|
|
1247
1262
|
pk
|
|
@@ -1326,6 +1341,7 @@
|
|
|
1326
1341
|
from
|
|
1327
1342
|
gasFee
|
|
1328
1343
|
gasPaid
|
|
1344
|
+
gasPayer
|
|
1329
1345
|
itxJson
|
|
1330
1346
|
nonce
|
|
1331
1347
|
pk
|
|
@@ -1374,6 +1390,7 @@
|
|
|
1374
1390
|
from
|
|
1375
1391
|
gasFee
|
|
1376
1392
|
gasPaid
|
|
1393
|
+
gasPayer
|
|
1377
1394
|
itxJson
|
|
1378
1395
|
nonce
|
|
1379
1396
|
pk
|
|
@@ -1459,6 +1476,7 @@
|
|
|
1459
1476
|
from
|
|
1460
1477
|
gasFee
|
|
1461
1478
|
gasPaid
|
|
1479
|
+
gasPayer
|
|
1462
1480
|
itxJson
|
|
1463
1481
|
nonce
|
|
1464
1482
|
pk
|
|
@@ -1507,6 +1525,7 @@
|
|
|
1507
1525
|
from
|
|
1508
1526
|
gasFee
|
|
1509
1527
|
gasPaid
|
|
1528
|
+
gasPayer
|
|
1510
1529
|
itxJson
|
|
1511
1530
|
nonce
|
|
1512
1531
|
pk
|
|
@@ -1589,6 +1608,7 @@
|
|
|
1589
1608
|
from
|
|
1590
1609
|
gasFee
|
|
1591
1610
|
gasPaid
|
|
1611
|
+
gasPayer
|
|
1592
1612
|
itxJson
|
|
1593
1613
|
nonce
|
|
1594
1614
|
pk
|
|
@@ -1673,6 +1693,7 @@
|
|
|
1673
1693
|
from
|
|
1674
1694
|
gasFee
|
|
1675
1695
|
gasPaid
|
|
1696
|
+
gasPayer
|
|
1676
1697
|
itxJson
|
|
1677
1698
|
nonce
|
|
1678
1699
|
pk
|
|
@@ -1732,6 +1753,7 @@
|
|
|
1732
1753
|
from
|
|
1733
1754
|
gasFee
|
|
1734
1755
|
gasPaid
|
|
1756
|
+
gasPayer
|
|
1735
1757
|
itxJson
|
|
1736
1758
|
nonce
|
|
1737
1759
|
pk
|
|
@@ -1837,6 +1859,7 @@
|
|
|
1837
1859
|
from
|
|
1838
1860
|
gasFee
|
|
1839
1861
|
gasPaid
|
|
1862
|
+
gasPayer
|
|
1840
1863
|
itxJson
|
|
1841
1864
|
nonce
|
|
1842
1865
|
pk
|
|
@@ -2084,6 +2107,7 @@ No arguments
|
|
|
2084
2107
|
}
|
|
2085
2108
|
transactions {
|
|
2086
2109
|
code
|
|
2110
|
+
gasPayer
|
|
2087
2111
|
hash
|
|
2088
2112
|
receiver
|
|
2089
2113
|
sender
|
|
@@ -2111,6 +2135,7 @@ No arguments
|
|
|
2111
2135
|
from
|
|
2112
2136
|
gasFee
|
|
2113
2137
|
gasPaid
|
|
2138
|
+
gasPayer
|
|
2114
2139
|
itxJson
|
|
2115
2140
|
nonce
|
|
2116
2141
|
pk
|
|
@@ -2297,6 +2322,7 @@ No arguments
|
|
|
2297
2322
|
}
|
|
2298
2323
|
transactions {
|
|
2299
2324
|
code
|
|
2325
|
+
gasPayer
|
|
2300
2326
|
hash
|
|
2301
2327
|
receiver
|
|
2302
2328
|
sender
|
|
@@ -2324,6 +2350,7 @@ No arguments
|
|
|
2324
2350
|
from
|
|
2325
2351
|
gasFee
|
|
2326
2352
|
gasPaid
|
|
2353
|
+
gasPayer
|
|
2327
2354
|
itxJson
|
|
2328
2355
|
nonce
|
|
2329
2356
|
pk
|
|
@@ -2586,6 +2613,7 @@ No arguments
|
|
|
2586
2613
|
from
|
|
2587
2614
|
gasFee
|
|
2588
2615
|
gasPaid
|
|
2616
|
+
gasPayer
|
|
2589
2617
|
itxJson
|
|
2590
2618
|
nonce
|
|
2591
2619
|
pk
|
|
@@ -2638,6 +2666,7 @@ No arguments
|
|
|
2638
2666
|
from
|
|
2639
2667
|
gasFee
|
|
2640
2668
|
gasPaid
|
|
2669
|
+
gasPayer
|
|
2641
2670
|
itxJson
|
|
2642
2671
|
nonce
|
|
2643
2672
|
pk
|
|
@@ -2824,6 +2853,7 @@ No arguments
|
|
|
2824
2853
|
from
|
|
2825
2854
|
gasFee
|
|
2826
2855
|
gasPaid
|
|
2856
|
+
gasPayer
|
|
2827
2857
|
itxJson
|
|
2828
2858
|
nonce
|
|
2829
2859
|
pk
|
|
@@ -2876,6 +2906,7 @@ No arguments
|
|
|
2876
2906
|
from
|
|
2877
2907
|
gasFee
|
|
2878
2908
|
gasPaid
|
|
2909
|
+
gasPayer
|
|
2879
2910
|
itxJson
|
|
2880
2911
|
nonce
|
|
2881
2912
|
pk
|
|
@@ -3090,6 +3121,7 @@ No arguments
|
|
|
3090
3121
|
from
|
|
3091
3122
|
gasFee
|
|
3092
3123
|
gasPaid
|
|
3124
|
+
gasPayer
|
|
3093
3125
|
itxJson
|
|
3094
3126
|
nonce
|
|
3095
3127
|
pk
|
|
@@ -3142,6 +3174,7 @@ No arguments
|
|
|
3142
3174
|
from
|
|
3143
3175
|
gasFee
|
|
3144
3176
|
gasPaid
|
|
3177
|
+
gasPayer
|
|
3145
3178
|
itxJson
|
|
3146
3179
|
nonce
|
|
3147
3180
|
pk
|