Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/commands/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export const serverCommand = new Command('server')
.option('--host <string>', 'server host', '127.0.0.1')
.option('--car-storage <path>', 'path for CAR file storage', './cars')
.option('--database <path>', 'path to SQLite database', './pins.db')
.option('--private-key <key>', 'private key for Synapse (or use PRIVATE_KEY env var)')
.option('--private-key <key>', 'private key for standard auth (or use PRIVATE_KEY env var)')
.option('--wallet-address <address>', 'wallet address for session key auth (or use WALLET_ADDRESS env var)')
.option('--session-key <key>', 'session key for session key auth (or use SESSION_KEY env var)')

addNetworkOptions(serverCommand)
.addOption(
Expand All @@ -20,6 +22,12 @@ addNetworkOptions(serverCommand)
if (options.privateKey) {
process.env.PRIVATE_KEY = options.privateKey
}
if (options.walletAddress) {
process.env.WALLET_ADDRESS = options.walletAddress
}
if (options.sessionKey) {
process.env.SESSION_KEY = options.sessionKey
}
// RPC URL takes precedence over network flag
if (options.rpcUrl) {
process.env.RPC_URL = options.rpcUrl
Expand Down
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export function createConfig(): Config {

// Synapse SDK configuration
privateKey: process.env.PRIVATE_KEY, // Required: Ethereum-compatible private key
walletAddress: process.env.WALLET_ADDRESS,
sessionKey: process.env.SESSION_KEY,
viewAddress: process.env.VIEW_ADDRESS,
rpcUrl, // Determined from RPC_URL, NETWORK, or default to calibration
// Storage paths
databasePath: process.env.DATABASE_PATH ?? join(dataDir, 'pins.db'),
Expand Down
3 changes: 3 additions & 0 deletions src/core/synapse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ export interface Config {
port: number
host: string
privateKey: string | undefined
walletAddress: string | undefined
sessionKey: string | undefined
viewAddress: string | undefined
rpcUrl: string
databasePath: string
carStoragePath: string
Expand Down
33 changes: 24 additions & 9 deletions src/filecoin-pinning-server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import fastify, { type FastifyInstance, type FastifyRequest } from 'fastify'
import { CID } from 'multiformats/cid'
import type { Logger } from 'pino'
import type { Address } from 'viem'
import type { Config } from './core/synapse/index.js'
import { initializeSynapse, type PrivateKeyConfig } from './core/synapse/index.js'
import { initializeSynapse, type SynapseSetupConfig } from './core/synapse/index.js'
import { FilecoinPinStore, type PinOptions } from './filecoin-pin-store.js'
import type { ServiceInfo } from './server.js'

Expand All @@ -20,20 +21,34 @@ const DEFAULT_USER_INFO = {
name: 'Default User',
}

function buildSynapseConfig(config: Config): SynapseSetupConfig {
const base = { rpcUrl: config.rpcUrl }

if (config.walletAddress && config.sessionKey) {
return {
...base,
walletAddress: config.walletAddress as Address,
sessionKey: config.sessionKey as Address,
}
}

if (config.privateKey) {
return { ...base, privateKey: config.privateKey as Address }
}

throw new Error(
'No authentication configured. Provide a private key (--private-key / PRIVATE_KEY) ' +
'or session key (--wallet-address + --session-key / WALLET_ADDRESS + SESSION_KEY).'
)
}

export async function createFilecoinPinningServer(
config: Config,
logger: Logger,
serviceInfo: ServiceInfo
): Promise<{ server: FastifyInstance; pinStore: FilecoinPinStore }> {
// Set up Synapse service
if (!config.privateKey) {
throw new Error('PRIVATE_KEY environment variable is required to start the pinning server')
}

const synapseConfig: PrivateKeyConfig = {
privateKey: config.privateKey as `0x${string}`,
rpcUrl: config.rpcUrl,
}
const synapseConfig = buildSynapseConfig(config)
const synapse = await initializeSynapse(synapseConfig, logger)

const filecoinPinStore = new FilecoinPinStore({
Expand Down
9 changes: 5 additions & 4 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ export async function startServer(): Promise<void> {
)

// Also print a user-friendly message to stderr for clarity
if (errorMessage.includes('PRIVATE_KEY')) {
console.error('\n❌ Error: PRIVATE_KEY environment variable is required')
console.error(' Please set your private key: export PRIVATE_KEY=0x...')
console.error(' Or run with: PRIVATE_KEY=0x... filecoin-pin server\n')
if (errorMessage.includes('No authentication')) {
console.error('\n❌ Error: Authentication is required to start the pinning server')
console.error(' Private key: --private-key <key> or PRIVATE_KEY=0x...')
console.error(' Session key: --wallet-address <addr> --session-key <key>')
console.error(' or WALLET_ADDRESS=0x... SESSION_KEY=0x...\n')
}

process.exit(1)
Expand Down
Loading