-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-c.ts
More file actions
401 lines (368 loc) · 12.1 KB
/
Copy pathsetup-c.ts
File metadata and controls
401 lines (368 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/**
* Local Dev — Council Setup (multi-council)
*
* Creates THREE councils against the local stack, each with its own
* channel-auth + privacy-channel contracts, name, and accepted jurisdictions.
* Drives the full production API surface for every council.
*
* US Banks — US
* EU Banks — FR, DE, ES, NL
* Stellar Wallets — US, VE
*
* Steps per council:
* 1. Deploy Channel Auth contract (deterministic salt per council index)
* 2. Deploy Privacy Channel contract (deterministic salt per council index)
* 3. PUT /council/metadata (name + description)
* 4. POST /council/channels (XLM channel)
* 5. POST /council/jurisdictions (one per jurisdiction)
*
* Shared:
* - Native XLM SAC is deployed once and reused for all 3 councils
* - Admin keypair is shared
* - JWT is per-council via SEP-43 challenge/verify
*
* State file format (.local-dev-state) keys:
* ADMIN_PK, ADMIN_SK, ASSET_ID, COUNCIL_URL, NETWORK_PASSPHRASE,
* RPC_URL, FRIENDBOT_URL,
* COUNCIL_COUNT,
* COUNCIL_<i>_ID, COUNCIL_<i>_NAME, COUNCIL_<i>_CHANNEL,
* COUNCIL_<i>_JURISDICTIONS (comma-separated, e.g. AR,BR,UY,PY)
*
* Prereqs:
* - up.sh has run (Stellar quickstart on :8000, council-platform on :3015)
*
* Usage:
* ./setup-c.sh
*/
import { Keypair } from "npm:@stellar/stellar-sdk@14.2.0";
import { Buffer } from "node:buffer";
import { createServer } from "./lib/soroban.ts";
import {
deployChannelAuth,
deployPrivacyChannel,
getOrDeployNativeSac,
uploadWasm,
} from "./lib/deploy.ts";
import { extractEvents, verifyEvent } from "./lib/events.ts";
const RPC_URL = Deno.env.get("STELLAR_RPC_URL") ??
"http://localhost:8000/soroban/rpc";
const FRIENDBOT_URL = Deno.env.get("FRIENDBOT_URL") ??
"http://localhost:8000/friendbot";
const NETWORK_PASSPHRASE = Deno.env.get("STELLAR_NETWORK_PASSPHRASE") ??
"Standalone Network ; February 2017";
const COUNCIL_URL = Deno.env.get("COUNCIL_URL") ?? "http://localhost:3015";
const STATE_FILE = Deno.env.get("STATE_FILE") ??
new URL("./.local-dev-state", import.meta.url).pathname;
const CHANNEL_AUTH_WASM = Deno.env.get("CHANNEL_AUTH_WASM") ??
new URL("./e2e/wasms/channel_auth_contract.wasm", import.meta.url).pathname;
const PRIVACY_CHANNEL_WASM = Deno.env.get("PRIVACY_CHANNEL_WASM") ??
new URL("./e2e/wasms/privacy_channel.wasm", import.meta.url).pathname;
const ADMIN_SECRET = Deno.env.get("ADMIN_SECRET") ??
"SAQCGLJ2JISI67QGG457IBN2DY6YW5GGS2OMQU5KNLXB3TWVUIR2RD74";
const CHANNEL_AUTH_SALT_BASE = Deno.env.get("CHANNEL_AUTH_SALT_HEX") ??
"4c4f43414c5f43415554";
const PRIVACY_CHANNEL_SALT_BASE = Deno.env.get("PRIVACY_CHANNEL_SALT_HEX") ??
"4c4f43414c5f50434843";
interface CouncilSpec {
name: string;
jurisdictions: string[];
}
const COUNCILS: CouncilSpec[] = [
{
name: "US Banks",
jurisdictions: ["US"],
},
{
name: "EU Banks",
jurisdictions: ["FR", "DE", "ES", "NL"],
},
{
name: "Stellar Wallets",
jurisdictions: ["US", "VE"],
},
];
function hexToFixedBuffer(hex: string, length = 32): Buffer {
const bytes = Buffer.alloc(length);
const data = Buffer.from(hex, "hex");
data.copy(bytes, length - data.length);
return bytes;
}
function saltForCouncil(baseHex: string, index: number): Buffer {
// Append 4 hex digits of the index so each council gets a distinct salt
// (and therefore a distinct deterministic contract ID).
const idxHex = index.toString(16).padStart(4, "0");
return hexToFixedBuffer(baseHex + idxHex);
}
async function fundAccount(publicKey: string): Promise<void> {
const res = await fetch(`${FRIENDBOT_URL}?addr=${publicKey}`);
if (!res.ok && res.status !== 400) {
throw new Error(
`Friendbot failed for ${publicKey}: ${res.status} ${await res.text()}`,
);
}
}
async function warmupCouncil(): Promise<void> {
for (let i = 0; i < 30; i++) {
try {
const res = await fetch(`${COUNCIL_URL}/api/v1/health`);
if (res.ok) return;
} catch { /* retry */ }
await new Promise((r) => setTimeout(r, 1000));
}
throw new Error(`council-platform not reachable at ${COUNCIL_URL}`);
}
async function walletAuth(keypair: Keypair): Promise<string> {
const challengeRes = await fetch(
`${COUNCIL_URL}/api/v1/admin/auth/challenge`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ publicKey: keypair.publicKey() }),
},
);
if (!challengeRes.ok) {
throw new Error(
`Council auth challenge failed: ${challengeRes.status} ${await challengeRes
.text()}`,
);
}
const { data: { nonce } } = await challengeRes.json();
const nonceBytes = Uint8Array.from(atob(nonce), (c) => c.charCodeAt(0));
const sig = keypair.sign(Buffer.from(nonceBytes));
const signature = btoa(String.fromCharCode(...new Uint8Array(sig)));
const verifyRes = await fetch(`${COUNCIL_URL}/api/v1/admin/auth/verify`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ nonce, signature, publicKey: keypair.publicKey() }),
});
if (!verifyRes.ok) {
throw new Error(
`Council auth verify failed: ${verifyRes.status} ${await verifyRes
.text()}`,
);
}
const { data: { token } } = await verifyRes.json();
return token;
}
async function writeStateFile(state: Record<string, string>): Promise<void> {
const lines = [
"# Generated by setup-c.sh — regenerated on every run.",
"# Consumed by setup-pp.sh, setup-pay.sh, send-loop.sh.",
`# Created: ${new Date().toISOString()}`,
"",
...Object.entries(state).map(([k, v]) => `${k}=${v}`),
"",
];
await Deno.writeTextFile(STATE_FILE, lines.join("\n"));
}
interface DeployedCouncil {
spec: CouncilSpec;
index: number;
channelAuthId: string;
channelContractId: string;
}
async function setupOneCouncil(
spec: CouncilSpec,
index: number,
admin: Keypair,
// deno-lint-ignore no-explicit-any
server: any,
assetContractId: string,
channelAuthWasmHash: Buffer,
privacyChannelWasmHash: Buffer,
): Promise<DeployedCouncil> {
const tag = `[${index + 1}/${COUNCILS.length}] ${spec.name}`;
console.log(`\n=== ${tag} ===`);
console.log(" Deploying Channel Auth…");
const channelAuthSalt = saltForCouncil(CHANNEL_AUTH_SALT_BASE, index);
const { contractId: channelAuthId, txResponse: authDeployTx } =
await deployChannelAuth(
server,
admin,
NETWORK_PASSPHRASE,
channelAuthWasmHash,
channelAuthSalt,
);
if (
verifyEvent(extractEvents(authDeployTx), "contract_initialized", true).found
) {
console.log(" contract_initialized event verified");
}
console.log(` Channel Auth: ${channelAuthId}`);
console.log(" Deploying Privacy Channel…");
const privacyChannelSalt = saltForCouncil(PRIVACY_CHANNEL_SALT_BASE, index);
const channelContractId = await deployPrivacyChannel(
server,
admin,
NETWORK_PASSPHRASE,
privacyChannelWasmHash,
channelAuthId,
assetContractId,
privacyChannelSalt,
);
console.log(` Privacy Channel: ${channelContractId}`);
console.log(" Authenticating admin to council-platform…");
const adminJwt = await walletAuth(admin);
console.log(" Creating council metadata…");
const createRes = await fetch(`${COUNCIL_URL}/api/v1/council/metadata`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${adminJwt}`,
},
body: JSON.stringify({
councilId: channelAuthId,
name: spec.name,
description: `Local-dev council: ${spec.name}`,
contactEmail: "local-dev@moonlight.test",
}),
});
if (!createRes.ok) {
throw new Error(
`Create council failed: ${createRes.status} ${await createRes.text()}`,
);
}
console.log(` Council created: ${channelAuthId}`);
console.log(" Adding XLM channel…");
const addChannelRes = await fetch(
`${COUNCIL_URL}/api/v1/council/channels?councilId=${
encodeURIComponent(channelAuthId)
}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${adminJwt}`,
},
body: JSON.stringify({
channelContractId,
assetCode: "XLM",
assetContractId,
label: "XLM channel",
}),
},
);
if (!addChannelRes.ok) {
throw new Error(
`Add channel failed: ${addChannelRes.status} ${await addChannelRes
.text()}`,
);
}
console.log(" Adding jurisdictions…");
for (const j of spec.jurisdictions) {
const r = await fetch(
`${COUNCIL_URL}/api/v1/council/jurisdictions?councilId=${
encodeURIComponent(channelAuthId)
}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${adminJwt}`,
},
body: JSON.stringify({ countryCode: j }),
},
);
if (!r.ok) {
throw new Error(
`Add jurisdiction ${j} failed: ${r.status} ${await r.text()}`,
);
}
console.log(` + ${j}`);
}
return { spec, index, channelAuthId, channelContractId };
}
async function main() {
const startTime = Date.now();
console.log("\n=== local-dev — Council Setup (multi-council) ===\n");
console.log(` RPC: ${RPC_URL}`);
console.log(` Friendbot: ${FRIENDBOT_URL}`);
console.log(` Council: ${COUNCIL_URL}`);
console.log(` State file: ${STATE_FILE}`);
console.log(` Councils to create: ${COUNCILS.length}`);
console.log("\n[1/4] Warmup council-platform");
await warmupCouncil();
console.log(" council-platform reachable");
const admin = Keypair.fromSecret(ADMIN_SECRET);
console.log(` Admin: ${admin.publicKey()}`);
console.log("\n[2/4] Funding admin via Friendbot");
await fundAccount(admin.publicKey());
console.log("\n[3/4] Pre-deploy: WASM upload + native XLM SAC");
const server = createServer(RPC_URL, true);
const channelAuthWasm = await Deno.readFile(CHANNEL_AUTH_WASM);
const channelAuthHash = await uploadWasm(
server,
admin,
NETWORK_PASSPHRASE,
channelAuthWasm,
);
console.log(` Channel Auth WASM hash: ${channelAuthHash.toString("hex")}`);
const privacyChannelWasm = await Deno.readFile(PRIVACY_CHANNEL_WASM);
const privacyChannelHash = await uploadWasm(
server,
admin,
NETWORK_PASSPHRASE,
privacyChannelWasm,
);
console.log(
` Privacy Channel WASM hash: ${privacyChannelHash.toString("hex")}`,
);
const assetContractId = await getOrDeployNativeSac(
server,
admin,
NETWORK_PASSPHRASE,
);
console.log(` XLM SAC: ${assetContractId}`);
console.log(`\n[4/4] Setting up ${COUNCILS.length} councils`);
const deployed: DeployedCouncil[] = [];
for (let i = 0; i < COUNCILS.length; i++) {
deployed.push(
await setupOneCouncil(
COUNCILS[i],
i,
admin,
server,
assetContractId,
channelAuthHash,
privacyChannelHash,
),
);
}
console.log("\nWriting state file…");
const state: Record<string, string> = {
ADMIN_PK: admin.publicKey(),
ADMIN_SK: admin.secret(),
ASSET_ID: assetContractId,
COUNCIL_URL,
NETWORK_PASSPHRASE,
RPC_URL,
FRIENDBOT_URL,
COUNCIL_COUNT: String(deployed.length),
};
for (const d of deployed) {
const i = d.index + 1;
state[`COUNCIL_${i}_ID`] = d.channelAuthId;
state[`COUNCIL_${i}_NAME`] = d.spec.name;
state[`COUNCIL_${i}_CHANNEL`] = d.channelContractId;
state[`COUNCIL_${i}_JURISDICTIONS`] = d.spec.jurisdictions.join(",");
}
await writeStateFile(state);
console.log(` State written to ${STATE_FILE}`);
const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
console.log(`\n=== Council setup complete in ${elapsed}s ===\n`);
for (const d of deployed) {
console.log(` ${d.spec.name}`);
console.log(` Council ID: ${d.channelAuthId}`);
console.log(` Privacy Channel: ${d.channelContractId}`);
console.log(` Jurisdictions: ${d.spec.jurisdictions.join(", ")}`);
}
console.log("");
console.log(
"Next: ./setup-pp.sh to register the 12 PPs across the councils.",
);
}
main().catch((err) => {
console.error("\n=== Council setup FAILED ===");
console.error(err);
Deno.exit(1);
});