-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcli-auth.ts
More file actions
164 lines (148 loc) · 5.41 KB
/
cli-auth.ts
File metadata and controls
164 lines (148 loc) · 5.41 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
/**
* CLI Authentication Helpers
*
* Shared utilities for parsing authentication options from CLI commands
* and preparing them for use with the Synapse SDK.
*/
import type { Chain, Synapse } from '@filoz/synapse-sdk'
import { getConfiguredChain, getRpcUrl } from '../common/get-rpc-url.js'
import type { SynapseSetupConfig } from '../core/synapse/index.js'
import { initializeSynapse } from '../core/synapse/index.js'
import { createLogger } from '../logger.js'
/**
* Common CLI authentication options interface
* Used across all commands that require authentication
*/
export interface CLIAuthOptions {
/** Private key for standard authentication */
privateKey?: string | undefined
/** Wallet address for session key mode */
walletAddress?: string | undefined
/** Session key private key */
sessionKey?: string | undefined
/** View-only wallet address (no signing) */
viewAddress?: string | undefined
/** Filecoin network: mainnet or calibration */
network?: string | undefined
/** Commander shorthand for --network mainnet */
mainnet?: boolean | undefined
/** RPC endpoint URL (overrides network if specified) */
rpcUrl?: string | undefined
/** Optional provider ID overrides (comma-separated) */
providerIds?: string | undefined
/** Optional data set ID overrides (comma-separated) */
dataSetIds?: string | undefined
}
/**
* Parse CLI authentication options into SynapseSetupConfig
*
* This function handles reading from CLI options and environment variables,
* and returns a config ready for initializeSynapse().
*
* Note: Validation is performed by initializeSynapse() via validateAuthConfig()
*
* @param options - CLI authentication options
* @returns Synapse setup config (validation happens in initializeSynapse)
*/
export function parseCLIAuth(options: CLIAuthOptions): SynapseSetupConfig {
// Read from CLI options or environment variables
const privateKey = options.privateKey || process.env.PRIVATE_KEY
const walletAddress = options.walletAddress || process.env.WALLET_ADDRESS
const sessionKey = options.sessionKey || process.env.SESSION_KEY
const viewAddress = options.viewAddress || process.env.VIEW_ADDRESS
const rpcUrl = getRpcUrl(options)
const chain = getConfiguredChain(options)
// Build config incrementally; initializeSynapse() validates the final shape
const config: {
privateKey?: string
walletAddress?: string
sessionKey?: string
readOnly?: boolean
rpcUrl?: string
chain?: Chain
} = {}
if (privateKey) config.privateKey = privateKey
if (viewAddress) {
config.walletAddress = viewAddress
config.readOnly = true
} else if (walletAddress) {
config.walletAddress = walletAddress
}
if (sessionKey) config.sessionKey = sessionKey
if (rpcUrl) config.rpcUrl = rpcUrl
if (chain) config.chain = chain
return config as SynapseSetupConfig
}
/**
* Context selection options for upload (provider IDs and/or data set IDs)
*/
export interface ContextSelectionOptions {
/** Provider ID overrides for targeting specific providers */
providerIds?: bigint[]
/** Data set ID overrides for targeting specific data sets */
dataSetIds?: bigint[]
}
/**
* Parse a comma-separated list of numeric IDs, validating and deduplicating.
* Returns bigint[] since all downstream consumers (SDK, contracts) use bigint.
* Throws on non-numeric values or duplicate IDs.
*/
function parseIdList(raw: string, label: string): bigint[] {
const parts = raw
.split(',')
.map((s) => s.trim())
.filter((s) => s !== '')
const ids: bigint[] = []
for (const part of parts) {
try {
ids.push(BigInt(part))
} catch {
throw new Error(`Invalid ${label}: "${raw}". Provide comma-separated numeric IDs.`)
}
}
const unique = [...new Set(ids)]
if (unique.length !== ids.length) {
const dupes = ids.filter((id, i) => ids.indexOf(id) !== i)
throw new Error(`Duplicate ${label}: ${[...new Set(dupes)].join(', ')}`)
}
return ids
}
/**
* Parse context selection from CLI options and environment variables.
*
* Reads provider IDs from --provider-ids / PROVIDER_IDS and
* data set IDs from --data-set-ids / DATA_SET_IDS. Both accept
* comma-separated numeric values. They are mutually exclusive.
*
* @param options - CLI authentication options (may contain provider/data-set fields)
* @returns Context selection options
*/
export function parseContextSelectionOptions(options?: CLIAuthOptions): ContextSelectionOptions {
const providerRaw = (options?.providerIds || process.env.PROVIDER_IDS)?.trim()
const dataSetRaw = (options?.dataSetIds || process.env.DATA_SET_IDS)?.trim()
const hasProviders = providerRaw != null && providerRaw !== ''
const hasDataSets = dataSetRaw != null && dataSetRaw !== ''
if (hasProviders && hasDataSets) {
throw new Error('Cannot specify both --provider-ids and --data-set-ids. Use one or the other.')
}
if (hasProviders) {
return { providerIds: parseIdList(providerRaw, 'provider ID(s)') }
}
if (hasDataSets) {
return { dataSetIds: parseIdList(dataSetRaw, 'data set ID(s)') }
}
return {}
}
/**
* Get a logger instance for use in CLI commands
*
* @returns Logger configured for CLI use
*/
export function getCLILogger() {
return createLogger({ logLevel: process.env.LOG_LEVEL })
}
export async function getCliSynapse(options: CLIAuthOptions): Promise<Synapse> {
const authConfig = parseCLIAuth(options)
const logger = getCLILogger()
return initializeSynapse(authConfig, logger)
}