# Verify it yourself

> One script that checks the latest signed checkpoint, proves a block is in the log and recomputes the block's hash and signature.

This script uses two small, audited libraries and the public API. It trusts nothing it does not check.

```bash title="Terminal"
npm install @noble/hashes @noble/curves
```

```js title="verify.mjs"
import { sha256 } from '@noble/hashes/sha2.js';
import { ed25519 } from '@noble/curves/ed25519.js';

const API = 'https://usd.net/explorer/testnet/api/v1';
const enc = new TextEncoder();
const hex = (s) => Uint8Array.from(s.match(/../g).map((b) => parseInt(b, 16)));
const toHex = (u) => [...u].map((b) => b.toString(16).padStart(2, '0')).join('');
const b64 = (s) => Uint8Array.from(atob(s), (c) => c.charCodeAt(0));
const cat = (...a) => { const o = new Uint8Array(a.reduce((n, x) => n + x.length, 0)); let i = 0; for (const x of a) { o.set(x, i); i += x.length; } return o; };
const leafHash = (d) => sha256(cat(new Uint8Array([0]), d));
const nodeHash = (l, r) => sha256(cat(new Uint8Array([1]), l, r));
const canonical = (v) => v === null || typeof v !== 'object' ? JSON.stringify(v)
  : Array.isArray(v) ? '[' + v.map(canonical).join(',') + ']'
  : '{' + Object.keys(v).filter((k) => v[k] !== undefined).sort().map((k) => JSON.stringify(k) + ':' + canonical(v[k])).join(',') + '}';

function verifyInclusion(index, size, leaf, path, root) {
  if (index >= size) return false;
  let fn = index, sn = size - 1, r = leafHash(hex(leaf));
  for (const p of path.map(hex)) {
    if (sn === 0) return false;
    if (fn & 1 || fn === sn) {
      r = nodeHash(p, r);
      if (!(fn & 1)) while (!(fn & 1) && fn !== 0) { fn >>= 1; sn >>= 1; }
    } else r = nodeHash(r, p);
    fn >>= 1; sn >>= 1;
  }
  return sn === 0 && toHex(r) === root;
}

// 1. The network key and the latest signed checkpoint
const { sequencerKey, chain } = await fetch(API + '/info').then((r) => r.json());
const note = await fetch(API + '/checkpoint?format=note').then((r) => r.text());
const [body, sigs] = note.split('\n\n');
const [origin, size, rootB64] = body.split('\n');
const sigLine = sigs.split('\n').find((l) => l.startsWith('— usd.net '));
const noteOk = ed25519.verify(b64(sigLine.split(' ')[2]).slice(4), enc.encode(body + '\n'), hex(sequencerKey));
const root = toHex(b64(rootB64));
console.log(`${origin} size ${size}: signature ${noteOk ? 'valid' : 'INVALID'}`);

// 2. A block: its hash, its signature and its place in the log
const height = Number(process.argv[2] ?? 0);
const { hash, sig, transactions, ...block } = await fetch(`${API}/block/${height}`).then((r) => r.json());
const hashOk = toHex(sha256(enc.encode(canonical({ ...block, logRoot: '', chain })))) === hash;
const sigOk = ed25519.verify(hex(sig), hex(hash), hex(sequencerKey));
const proof = await fetch(`${API}/proof/inclusion/${height}`).then((r) => r.json());
const inLog = proof.leaf === hash && proof.size === Number(size) && proof.root === root
  && verifyInclusion(height, proof.size, proof.leaf, proof.path, root);
console.log(`block ${height}: hash ${hashOk}, signature ${sigOk}, in signed log ${inLog}`);
```

```bash title="Terminal"
node verify.mjs 5
# explorer.usd.net/usdnet-testnet-1 size 6: signature valid
# block 5: hash true, signature true, in signed log true
```

> [!TIP]
> If a new block is sealed between fetching the checkpoint and fetching the proof, the sizes will differ and the check reports `false`. Run it again, or fetch the proof first and the checkpoint second.

## In the browser

[usd.net/explorer/verify](https://usd.net/explorer/verify) runs the same checks in your browser, with no install.
