Separate identity-only pilot custody foundation
This commit is contained in:
@@ -18,6 +18,7 @@ import { migrateCustody, grantCustodyRuntime, grantMoneroObserverRuntime, CUSTOD
|
||||
grantMoneroDepositCreditRuntime, grantMoneroDepositAllocatorRuntime, grantMoneroWithdrawalRuntime, CUSTODY_MIGRATION_V2,
|
||||
CUSTODY_SCHEMA_V2_DIGEST } from '../../server/market/postgres-schema.ts';
|
||||
import { parseMainnetCustodianId } from '../../server/market/mainnet-custody-identity.ts';
|
||||
import { initializePilotCustodyIdentity, parsePilotCustodianId } from '../../server/market/pilot-custody-identity.ts';
|
||||
import { readMainnetCustodyBalance, reserveMainnetWithdrawal, cancelMainnetWithdrawal } from '../../server/market/mainnet-withdrawal.ts';
|
||||
import { mainnetCustodyBalanceMessage, mainnetWithdrawalCancellationMessage, mainnetWithdrawalMessage,
|
||||
type MainnetCustodyBalanceRequestUnsigned, type MainnetWithdrawalRequestUnsigned,
|
||||
@@ -205,7 +206,7 @@ test('explicit PostgreSQL migrations are idempotent and runtime role cannot alte
|
||||
assert.equal((await f.market.audit()).assetsAtomicXmr, '1000000000000');
|
||||
});
|
||||
|
||||
test('custody schema v1 upgrades additively through v9 without dropping existing metadata', async t => {
|
||||
test('custody schema v1 upgrades additively through v10 without dropping existing metadata', async t => {
|
||||
const admin = new Pool(config);
|
||||
await admin.query('DROP SCHEMA IF EXISTS wk_custody CASCADE');
|
||||
const client = await admin.connect();
|
||||
@@ -219,13 +220,14 @@ test('custody schema v1 upgrades additively through v9 without dropping existing
|
||||
try {
|
||||
await migrateCustody(admin); await migrateCustody(admin);
|
||||
const versions = await admin.query('SELECT version FROM wk_custody.schema_versions ORDER BY version');
|
||||
assert.deepEqual(versions.rows.map(row => Number(row.version)), [1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
||||
assert.deepEqual(versions.rows.map(row => Number(row.version)), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
|
||||
assert.equal((await admin.query("SELECT value FROM wk_custody.custody_meta WHERE key='v1-preserved-marker'")).rows[0].value,
|
||||
'existing-test-data');
|
||||
for (const table of ['monero_observer_state', 'monero_transfer_observations', 'monero_observation_events', 'monero_observation_scans']) {
|
||||
assert.ok((await admin.query('SELECT to_regclass($1) AS name', [`wk_custody.${table}`])).rows[0].name);
|
||||
}
|
||||
assert.ok((await admin.query("SELECT to_regclass('wk_custody.monero_deposit_allocations') AS name")).rows[0].name);
|
||||
assert.ok((await admin.query("SELECT to_regclass('wk_custody.pilot_custody_identity') AS name")).rows[0].name);
|
||||
for (const table of ['monero_deposit_credit_events', 'monero_deposit_reorg_holds',
|
||||
'monero_deposit_release_events', 'monero_deposit_reorg_resolutions']) {
|
||||
assert.ok((await admin.query('SELECT to_regclass($1) AS name', [`wk_custody.${table}`])).rows[0].name);
|
||||
@@ -233,7 +235,7 @@ test('custody schema v1 upgrades additively through v9 without dropping existing
|
||||
} finally { await admin.end(); }
|
||||
});
|
||||
|
||||
test('custody schema v2 upgrades explicitly through v9 deposit and withdrawal lifecycle tables', async t => {
|
||||
test('custody schema v2 upgrades explicitly through v10 deposit and withdrawal lifecycle tables', async t => {
|
||||
const admin = new Pool(config);
|
||||
await admin.query('DROP SCHEMA IF EXISTS wk_custody CASCADE');
|
||||
const client = await admin.connect();
|
||||
@@ -246,14 +248,82 @@ test('custody schema v2 upgrades explicitly through v9 deposit and withdrawal li
|
||||
await client.query('COMMIT');
|
||||
await migrateCustody(admin); await migrateCustody(admin);
|
||||
const versions = await admin.query('SELECT version FROM wk_custody.schema_versions ORDER BY version');
|
||||
assert.deepEqual(versions.rows.map(row => Number(row.version)), [1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
||||
assert.deepEqual(versions.rows.map(row => Number(row.version)), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
|
||||
assert.equal((await admin.query("SELECT value FROM wk_custody.custody_meta WHERE key='v2-marker'")).rows[0].value, 'preserve');
|
||||
assert.ok((await admin.query("SELECT to_regclass('wk_custody.monero_deposit_release_events') AS name")).rows[0].name);
|
||||
assert.ok((await admin.query("SELECT to_regclass('wk_custody.monero_withdrawal_intents') AS name")).rows[0].name);
|
||||
assert.ok((await admin.query("SELECT to_regclass('wk_custody.pilot_custody_identity') AS name")).rows[0].name);
|
||||
} catch (error) { await client.query('ROLLBACK').catch(() => {}); throw error; }
|
||||
finally { client.release(); await admin.end(); }
|
||||
});
|
||||
|
||||
test('pilot custody identity binds the private market chain to Monero Mainnet without enabling funds', async t => {
|
||||
assert.equal(parsePilotCustodianId('AAAAAAAA-AAAA-4AAA-8AAA-AAAAAAAAAAAA'), 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa');
|
||||
assert.throws(() => parsePilotCustodianId('not-a-uuid'), /Invalid pilot custodian UUID/);
|
||||
const admin = new Pool(config), database = `pilot_identity_${randomUUID().replaceAll('-', '')}`;
|
||||
await admin.query(`CREATE DATABASE "${database}"`);
|
||||
const target = new Pool({ ...config, database });
|
||||
const run = promisify(execFile), custodianId = randomUUID(), runtimeRole = `pilot_identity_${randomUUID().replaceAll('-', '')}`;
|
||||
const code = `import { Pool } from 'pg';
|
||||
import { initializePilotCustodyIdentity } from './server/market/pilot-custody-identity.ts';
|
||||
const pool=new Pool({host:'127.0.0.1',database:process.env.TEST_DATABASE,user:'postgres',ssl:false});
|
||||
try { console.log(JSON.stringify(await initializePilotCustodyIdentity(pool,process.env.TEST_CUSTODIAN_ID,1234))); }
|
||||
finally { await pool.end(); }`;
|
||||
const env = { ...process.env, WIKIDECK_NETWORK: 'pilot', WIKIDECK_PILOT_MODE: 'private-mock-only',
|
||||
TEST_DATABASE: database, TEST_CUSTODIAN_ID: custodianId };
|
||||
try {
|
||||
await migrateCustody(target);
|
||||
await admin.query(`CREATE ROLE "${runtimeRole}" LOGIN`);
|
||||
assert.equal((await target.query('SELECT has_table_privilege($1,$2,$3) AS allowed',
|
||||
[runtimeRole, 'wk_custody.pilot_custody_identity', 'SELECT'])).rows[0].allowed, false);
|
||||
const first = JSON.parse((await run(process.execPath, ['--experimental-strip-types', '--input-type=module', '-e', code], { env })).stdout.trim());
|
||||
const replay = JSON.parse((await run(process.execPath, ['--experimental-strip-types', '--input-type=module', '-e', code], { env })).stdout.trim());
|
||||
assert.equal(first.mode, 'pilot-custody');
|
||||
assert.equal(first.marketNetwork, 'wikideck-randomx-pilot-1');
|
||||
assert.equal(first.moneroNetwork, 'mainnet');
|
||||
assert.equal(first.activationStatus, 'identity-only');
|
||||
assert.equal(first.custodianId, custodianId);
|
||||
assert.deepEqual(replay, first);
|
||||
await assert.rejects(run(process.execPath, ['--experimental-strip-types', '--input-type=module', '-e', code], {
|
||||
env: { ...env, TEST_CUSTODIAN_ID: randomUUID() },
|
||||
}));
|
||||
await assert.rejects(run(process.execPath, ['--experimental-strip-types', '--input-type=module', '-e', code], {
|
||||
env: { ...env, WIKIDECK_NETWORK: 'testnet' },
|
||||
}));
|
||||
const stored = await target.query('SELECT * FROM wk_custody.pilot_custody_identity');
|
||||
assert.equal(stored.rowCount, 1);
|
||||
assert.equal((await target.query('SELECT count(*)::int AS n FROM wk_custody.journals')).rows[0].n, 0);
|
||||
assert.equal((await target.query('SELECT count(*)::int AS n FROM wk_custody.monero_deposit_allocations')).rows[0].n, 0);
|
||||
assert.equal((await target.query('SELECT count(*)::int AS n FROM wk_custody.monero_withdrawal_intents')).rows[0].n, 0);
|
||||
assert.equal((await target.query("SELECT count(*)::int AS n FROM wk_custody.custody_meta WHERE key='identity'")).rows[0].n, 0);
|
||||
} finally {
|
||||
await target.end();
|
||||
await admin.query(`DROP DATABASE IF EXISTS "${database}" WITH (FORCE)`);
|
||||
await admin.query(`DROP ROLE IF EXISTS "${runtimeRole}"`);
|
||||
await admin.end();
|
||||
}
|
||||
});
|
||||
|
||||
test('pilot custody identity refuses a database containing pre-existing mock or financial data', async t => {
|
||||
const admin = new Pool(config), database = `pilot_identity_dirty_${randomUUID().replaceAll('-', '')}`;
|
||||
await admin.query(`CREATE DATABASE "${database}"`);
|
||||
const target = new Pool({ ...config, database }), run = promisify(execFile);
|
||||
try {
|
||||
await migrateCustody(target);
|
||||
await target.query("INSERT INTO wk_custody.accounts(id,normal) VALUES('pre-existing-mock','credit')");
|
||||
const code = `import { Pool } from 'pg';
|
||||
import { initializePilotCustodyIdentity } from './server/market/pilot-custody-identity.ts';
|
||||
const p=new Pool({host:'127.0.0.1',database:process.env.TEST_DATABASE,user:'postgres',ssl:false});
|
||||
try { await initializePilotCustodyIdentity(p,process.env.TEST_CUSTODIAN_ID,1234); }
|
||||
finally { await p.end(); }`;
|
||||
await assert.rejects(run(process.execPath, ['--experimental-strip-types', '--input-type=module', '-e', code], {
|
||||
env: { ...process.env, WIKIDECK_NETWORK: 'pilot', WIKIDECK_PILOT_MODE: 'private-mock-only',
|
||||
TEST_DATABASE: database, TEST_CUSTODIAN_ID: randomUUID() },
|
||||
}), /separate empty custody database/);
|
||||
assert.equal((await target.query('SELECT count(*)::int AS n FROM wk_custody.pilot_custody_identity')).rows[0].n, 0);
|
||||
} finally { await target.end(); await admin.query(`DROP DATABASE IF EXISTS "${database}" WITH (FORCE)`); await admin.end(); }
|
||||
});
|
||||
|
||||
test('mainnet custody identity bootstrap is one-time, chain-bound, and does not open withdrawals', async t => {
|
||||
assert.equal(parseMainnetCustodianId('AAAAAAAA-AAAA-4AAA-8AAA-AAAAAAAAAAAA'), 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa');
|
||||
assert.throws(() => parseMainnetCustodianId('not-a-uuid'), /Invalid mainnet custodian UUID/);
|
||||
|
||||
Reference in New Issue
Block a user