src/server.js · 139–144 · Source excerpt
`INSERT INTO entities (
entity_id, rin, agent_type, agent_name, status, issued_at,
claim_token_hash, claim_token_issued_at, issued_by_agent_name
)
VALUES ($1, $2, $3, $4, 'UNCLAIMED', $5, $6, $7, $8)
ON CONFLICT (rin) DO NOTHING`,
src/server.js · 156–156 · Source excerpt
return result.rowCount === 1 ? { entityId, issuedAt } : null;
src/server.js · 159–178 · Source excerpt
async function createUniqueRin(agentType, agentName, issuedByAgentName) {
for (let i = 0; i < 12; i += 1) {
const rin = generateRin();
const claimToken = generateClaimToken();
const claimTokenHash = hashClaimToken(claimToken, CLAIM_TOKEN_PEPPER);
const claimTokenIssuedAt = new Date().toISOString();
const inserted = await insertEntity({
rin,
agentType,
agentName,
claimTokenHash,
claimTokenIssuedAt,
issuedByAgentName,
});
if (inserted) {
return { rin, issued_at: inserted.issuedAt, claim_token: claimToken };
}
}
throw new Error('Failed to generate unique RIN');
}