Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

fix(economy-ledger): canonicalize master_sol_after in the chain hash

fudjia1fdfba9d473b
diff --git a/api/_lib/economy-ledger.js b/api/_lib/economy-ledger.js
index f068923a8..c0a4cce49 100644
--- a/api/_lib/economy-ledger.js
+++ b/api/_lib/economy-ledger.js
@@ -89,12 +89,28 @@ export function hashEntry(prevHash, r) {
r.lamports == null ? '' : String(r.lamports),
r.tx_signature || '',
r.reason || '',
- r.master_sol_after == null ? '' : String(r.master_sol_after),
+ // master_sol_after is numeric(20,9). Canonicalize to a fixed 9-decimal
+ // string so the WRITE side (which hashes the JS number, e.g. 0.30223827)
+ // and the VERIFY side (which reads the DB's scale-9 string "0.302238270")
+ // produce the SAME hash. Before this, `String(value)` dropped the JS
+ // number's trailing zeros while Postgres kept them, so every row's content
+ // hash mismatched — verifyChain broke at the first row and stopped, masking
+ // it as a single "tamper at seq 5". Number().toFixed(9) is stable for both
+ // a JS number and the DB decimal string.
+ canonicalSol9(r.master_sol_after),
prevHash || '',
].join('|');
return sha256hex(payload);
}
+// Fixed 9-decimal canonical form for a SOL amount that round-trips a JS number
+// and a Postgres numeric(_,9) string to the same string. Empty for null.
+function canonicalSol9(v) {
+ if (v == null || v === '') return '';
+ const n = Number(v);
+ return Number.isFinite(n) ? n.toFixed(9) : String(v);
+}
+
function round9(n) {
return Math.round(Number(n) * 1e9) / 1e9;
}
diff --git a/tests/economy-ledger-hash-canonical.test.js b/tests/economy-ledger-hash-canonical.test.js
new file mode 100644
index 000000000..2717878e5
--- /dev/null
+++ b/tests/economy-ledger-hash-canonical.test.js
@@ -0,0 +1,48 @@
+// hashEntry must produce the SAME hash whether master_sol_after arrives as the
+// JS number the write path holds (0.30223827) or the Postgres numeric(_,9)
+// string the verify path reads ("0.302238270"). Before canonicalization the
+// trailing-zero difference made every ledger row's content hash mismatch, which
+// verifyChain surfaced as a spurious "tamper".
+
+import { describe, it, expect } from 'vitest';
+import { hashEntry } from '../api/_lib/economy-ledger.js';
+
+const base = {
+ seq: 42,
+ ts: '2026-07-12T09:35:00.000Z',
+ master_pubkey: 'MASTER',
+ event: 'sweep',
+ target_pubkey: null,
+ lamports: 0,
+ tx_signature: null,
+ reason: null,
+};
+
+describe('hashEntry — master_sol_after canonicalization', () => {
+ it('hashes a JS number and its DB scale-9 string identically', () => {
+ const fromWrite = hashEntry('prev', { ...base, master_sol_after: 0.30223827 });
+ const fromDb = hashEntry('prev', { ...base, master_sol_after: '0.302238270' });
+ expect(fromWrite).toBe(fromDb);
+ });
+
+ it('treats a whole number and its padded form identically', () => {
+ expect(hashEntry('p', { ...base, master_sol_after: 0 })).toBe(
+ hashEntry('p', { ...base, master_sol_after: '0.000000000' }),
+ );
+ expect(hashEntry('p', { ...base, master_sol_after: 1 })).toBe(
+ hashEntry('p', { ...base, master_sol_after: '1.000000000' }),
+ );
+ });
+
+ it('null and empty hash the same (absent value)', () => {
+ expect(hashEntry('p', { ...base, master_sol_after: null })).toBe(
+ hashEntry('p', { ...base, master_sol_after: '' }),
+ );
+ });
+
+ it('distinguishes genuinely different amounts', () => {
+ expect(hashEntry('p', { ...base, master_sol_after: 0.5 })).not.toBe(
+ hashEntry('p', { ...base, master_sol_after: 0.6 }),
+ );
+ });
+});