fix(token-service): use HTTP API instead of direct DB import for DID lookup
The rspace container doesn't have DATABASE_URL for the encryptid DB. Use the /api/users/directory endpoint via HTTP to resolve jeff's DID, with retry-on-restart if encryptid isn't reachable yet. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
ba3a0018ea
commit
df453d9e00
|
|
@ -8,7 +8,6 @@ import * as Automerge from '@automerge/automerge';
|
|||
import type { SyncServer } from './local-first/sync-server';
|
||||
import { tokenLedgerSchema, tokenDocId } from './token-schemas';
|
||||
import type { TokenLedgerDoc, LedgerEntry } from './token-schemas';
|
||||
import { getUserByUsername } from '../src/encryptid/db';
|
||||
|
||||
let _syncServer: SyncServer | null = null;
|
||||
|
||||
|
|
@ -118,14 +117,15 @@ export async function seedCUSDC() {
|
|||
const tokenId = 'cusdc';
|
||||
const doc = ensureTokenDoc(tokenId);
|
||||
|
||||
// Skip if already seeded
|
||||
if (doc.token.name) {
|
||||
// Skip if already fully seeded (has entries)
|
||||
if (doc.token.name && Object.keys(doc.entries).length > 0) {
|
||||
console.log('[TokenService] cUSDC already seeded, skipping');
|
||||
return;
|
||||
}
|
||||
|
||||
// Set up token definition
|
||||
// Set up token definition (idempotent — only writes if not yet defined)
|
||||
const docId = tokenDocId(tokenId);
|
||||
if (!doc.token.name) {
|
||||
_syncServer!.changeDoc<TokenLedgerDoc>(docId, 'define cUSDC token', (d) => {
|
||||
d.token.id = 'cusdc';
|
||||
d.token.name = 'CRDT USDC';
|
||||
|
|
@ -137,18 +137,33 @@ export async function seedCUSDC() {
|
|||
d.token.createdAt = Date.now();
|
||||
d.token.createdBy = 'system';
|
||||
});
|
||||
}
|
||||
|
||||
// Look up jeff's DID
|
||||
const jeff = await getUserByUsername('jeff');
|
||||
if (!jeff || !jeff.did) {
|
||||
console.warn('[TokenService] Could not find user "jeff" — skipping mint');
|
||||
// Look up jeff's DID via EncryptID user directory API
|
||||
let jeffDid: string | null = null;
|
||||
try {
|
||||
const authBase = process.env.ENCRYPTID_URL || 'http://encryptid:3000';
|
||||
const res = await fetch(`${authBase}/api/users/directory?space=demo`, {
|
||||
signal: AbortSignal.timeout(5000),
|
||||
});
|
||||
if (res.ok) {
|
||||
const data = await res.json() as { users?: Array<{ did?: string; username?: string }> };
|
||||
const jeff = (data.users || []).find((u) => u.username === 'jeff');
|
||||
jeffDid = jeff?.did || null;
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[TokenService] EncryptID not reachable, will retry next startup:', (e as Error).message);
|
||||
}
|
||||
|
||||
if (!jeffDid) {
|
||||
console.warn('[TokenService] Could not resolve jeff DID — will retry next startup');
|
||||
return;
|
||||
}
|
||||
|
||||
// Mint 5 cUSDC (5 × 10^6 base units)
|
||||
const success = mintTokens(tokenId, jeff.did, 'jeff', 5_000_000, 'Initial seed mint', 'system');
|
||||
const success = mintTokens(tokenId, jeffDid, 'jeff', 5_000_000, 'Initial seed mint', 'system');
|
||||
if (success) {
|
||||
console.log(`[TokenService] cUSDC seeded: 5 cUSDC minted to jeff (${jeff.did})`);
|
||||
console.log(`[TokenService] cUSDC seeded: 5 cUSDC minted to jeff (${jeffDid})`);
|
||||
} else {
|
||||
console.error('[TokenService] Failed to mint cUSDC to jeff');
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue