Skip to content

Commit a37bbba

Browse files
authored
fix: sandbox scripts (#803)
1 parent dfdb996 commit a37bbba

5 files changed

Lines changed: 67 additions & 18 deletions

File tree

docker-compose.databases.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ services:
1010
environment:
1111
- POSTGRES_USER=${POSTGRES_USER:-postgres}
1212
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-postgres}
13-
- POSTGRES_MULTIPLE_DATABASES=registry
13+
- POSTGRES_MULTIPLE_DATABASES=registry,provisioner
1414
volumes:
1515
- postgres_data:/var/lib/postgresql/data
1616
- ./db/init-multiple-databases.sh:/docker-entrypoint-initdb.d/init-multiple-databases.sh

infrastructure/dev-sandbox/src/lib/PersistingWebCryptoAdapter.ts

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,40 @@ export class PersistingWebCryptoAdapter implements CryptoAdapter {
114114
return { keyId, publicKey: bufferToMultibaseHex(spki) };
115115
}
116116

117-
private async ensureKey(keyId: string): Promise<CryptoKeyPair> {
117+
async getPublicKey(keyId: string, _context?: string): Promise<string> {
118+
const pair = await this.ensureKeyInMemory(keyId);
119+
const spki = await crypto.subtle.exportKey("spki", pair.publicKey);
120+
return bufferToMultibaseHex(spki);
121+
}
122+
123+
async sign(keyId: string, payload: string): Promise<string> {
124+
const pair = await this.ensureKeyInMemory(keyId);
125+
const data = new TextEncoder().encode(payload);
126+
const sig = await crypto.subtle.sign(SIGN_ALG, pair.privateKey, data);
127+
return bufferToBase64(sig);
128+
}
129+
130+
/** CryptoAdapter: sign with keyId and context (context ignored). */
131+
async signPayload(
132+
keyId: string,
133+
_context: string,
134+
payload: string,
135+
): Promise<string> {
136+
return this.sign(keyId, payload);
137+
}
138+
139+
/** CryptoAdapter: ensure key exists (load from storage); return created: false (we don't create on demand by keyId). */
140+
async ensureKey(keyId: string, _context: string): Promise<{ created: boolean }> {
141+
if (keyStore.has(keyId)) return { created: false };
142+
const stored = readStoredKeys()[keyId];
143+
if (!stored?.privateKeyPkcs8Base64) {
144+
throw new Error(`Key not found: ${keyId}. Provision an eVault first to create a key.`);
145+
}
146+
await this.ensureKeyInMemory(keyId);
147+
return { created: false };
148+
}
149+
150+
private async ensureKeyInMemory(keyId: string): Promise<CryptoKeyPair> {
118151
let pair = keyStore.get(keyId);
119152
if (pair) return pair;
120153
const stored = readStoredKeys()[keyId];
@@ -137,17 +170,4 @@ export class PersistingWebCryptoAdapter implements CryptoAdapter {
137170
keyStore.set(keyId, pair);
138171
return pair;
139172
}
140-
141-
async getPublicKey(keyId: string): Promise<string> {
142-
const pair = await this.ensureKey(keyId);
143-
const spki = await crypto.subtle.exportKey("spki", pair.publicKey);
144-
return bufferToMultibaseHex(spki);
145-
}
146-
147-
async sign(keyId: string, payload: string): Promise<string> {
148-
const pair = await this.ensureKey(keyId);
149-
const data = new TextEncoder().encode(payload);
150-
const sig = await crypto.subtle.sign(SIGN_ALG, pair.privateKey, data);
151-
return bufferToBase64(sig);
152-
}
153173
}

infrastructure/dev-sandbox/src/routes/+page.svelte

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
platformName: env.PUBLIC_DEV_SANDBOX_PLATFORM_NAME ?? "dev-sandbox",
1818
};
1919
20+
/** Demo verification code accepted by evault-core when DEMO_CODE_W3DS is set. */
21+
const DEMO_VERIFICATION_ID = "d66b7138-538a-465f-a6ce-f6985854c3f4";
22+
2023
const IDENTITIES_STORAGE_KEY = "dev-sandbox-identities";
2124
const TOKEN_REFRESH_THRESHOLD_MS = 5 * 60 * 1000;
2225
@@ -158,15 +161,19 @@
158161
provisionSuccess = null;
159162
addLog("info", "Provisioning new eVault…");
160163
try {
161-
const result = await provision({
162-
cryptoAdapter: adapter,
164+
const { keyId } = await adapter.generateKeyPair();
165+
const result = await provision(adapter, {
163166
registryUrl: config.registryUrl,
164167
provisionerUrl: config.provisionerUrl,
168+
namespace: crypto.randomUUID(),
169+
verificationId: DEMO_VERIFICATION_ID,
170+
keyId,
171+
context: "onboarding",
165172
});
166173
const identity: Identity = {
167174
w3id: result.w3id,
168175
uri: result.uri,
169-
keyId: result.keyId,
176+
keyId,
170177
};
171178
identities = [...identities, identity];
172179
selectedIndex = identities.length - 1;
16 KB
Binary file not shown.

scripts/start-dev.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,28 @@ for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
5151
done
5252
echo "Postgres and Neo4j are up."
5353

54+
# Create registry and provisioner databases if they don't exist
55+
echo "Ensuring registry and provisioner databases exist..."
56+
for db in registry provisioner; do
57+
EXISTS=$(docker compose -f docker-compose.databases.yml exec -T postgres psql -U "${POSTGRES_USER:-postgres}" -d postgres -tAc "SELECT 1 FROM pg_database WHERE datname='$db'" 2>/dev/null | tr -d '[:space:]' || echo "")
58+
if [ "$EXISTS" != "1" ]; then
59+
echo "Creating database: $db"
60+
docker compose -f docker-compose.databases.yml exec -T postgres psql -U "${POSTGRES_USER:-postgres}" -d postgres -c "CREATE DATABASE \"$db\""
61+
else
62+
echo "Database $db already exists."
63+
fi
64+
done
65+
66+
# Run registry migrations
67+
echo "Running registry migrations..."
68+
pnpm --filter registry migration:run
69+
70+
# Build and run evault-core (provisioner) migrations
71+
# Unset REGISTRY_DATABASE_URL so evault-core uses PROVISIONER_DATABASE_URL for its migrations
72+
echo "Building evault-core and running provisioner migrations..."
73+
pnpm --filter evault-core build
74+
REGISTRY_DATABASE_URL= pnpm --filter evault-core migration:run
75+
5476
echo "Starting registry, evault-core, dev-sandbox (logs prefixed by service)..."
5577
pnpm exec concurrently -n registry,evault,sandbox \
5678
"pnpm --filter registry dev" \

0 commit comments

Comments
 (0)