Skip to content

Commit

Permalink
Generate and store station_id Ed25119 keypair and pass to Zinnia via …
Browse files Browse the repository at this point in the history
…env var
  • Loading branch information
PatrickNercessian committed Apr 12, 2024
1 parent ad71623 commit 1c5af33
Show file tree
Hide file tree
Showing 5 changed files with 419 additions and 104 deletions.
2 changes: 2 additions & 0 deletions commands/station.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { startPingLoop } from '../lib/telemetry.js'
import fs from 'node:fs/promises'
import { metrics } from '../lib/metrics.js'
import { paths } from '../lib/paths.js'
import { getStationId } from '../lib/cryptoOperations.js'
import pRetry from 'p-retry'
import { fetch } from 'undici'
import { ethAddressFromDelegated } from '@glif/filecoin-address'
Expand Down Expand Up @@ -82,6 +83,7 @@ export const station = async ({ json, experimental }) => {

const modules = [
zinniaRuntime.run({
STATION_ID: (await getStationId()).publicKey,
FIL_WALLET_ADDRESS,
ethAddress,
STATE_ROOT: join(paths.moduleState, 'zinnia'),
Expand Down
28 changes: 28 additions & 0 deletions lib/cryptoOperations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const { subtle } = globalThis.crypto;
import keytar from 'keytar';

const SERVICE_NAME = 'filecoin-station-core';

export async function getStationId() {
const storedKeys = await keytar.getPassword(SERVICE_NAME, 'station_id');
if (storedKeys) {
return JSON.parse(storedKeys);
}

const keyPair = await generateEd25519KeyPair();
const publicKey = await subtle.exportKey("spki", keyPair.publicKey);
const privateKey = await subtle.exportKey("pkcs8", keyPair.privateKey);

const keysToStore = JSON.stringify({ publicKey, privateKey });
await keytar.setPassword(SERVICE_NAME, 'station_id', keysToStore);

return { publicKey, privateKey };
}

async function generateEd25519KeyPair() {
return await subtle.generateKey(
{ name: "ED25519" },
true,
["sign", "verify", "decrypt", "encrypt"] // TODO do we want decrypt/encrypt?
);
}
3 changes: 3 additions & 0 deletions lib/zinnia.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ const catchChildProcessExit = async ({
}

export async function run ({
STATION_ID,
FIL_WALLET_ADDRESS,
ethAddress,
STATE_ROOT,
Expand Down Expand Up @@ -350,6 +351,7 @@ export async function run ({
{
cwd: moduleSourcesDir,
env: {
STATION_ID,
FIL_WALLET_ADDRESS,
STATE_ROOT,
CACHE_ROOT
Expand Down Expand Up @@ -406,6 +408,7 @@ export async function run ({
// This infinite recursion has no risk of exceeding the maximum call stack
// size, as awaiting promises unwinds the stack
return run({
STATION_ID,
FIL_WALLET_ADDRESS,
ethAddress,
STATE_ROOT,
Expand Down
Loading

0 comments on commit 1c5af33

Please sign in to comment.