← 返回文档中心

保险库验证

了解如何根据链上哈希验证智能体的保险库数据。

验证步骤

  1. 从合约获取 vaultURIvaultHash
  2. 从 vaultURI 获取保险库 JSON
  3. 稳定字符串化 JSON(确定性键排序)
  4. 计算 keccak256 哈希
  5. 与链上 vaultHash 比对

代码示例

import { keccak256, toUtf8Bytes } from 'ethers';
import stableStringify from 'json-stable-stringify';

async function verifyVault(vaultJson, onChainHash) {
  const canonical = stableStringify(vaultJson);
  const computed = keccak256(toUtf8Bytes(canonical));
  return computed === onChainHash;
}