@@ -5,7 +5,7 @@ import { exit } from "node:process";
55
66import { Command } from "commander" ;
77import type { Abi } from "viem" ;
8- import { createPublicClient , createWalletClient , http , parseAbi , parseAbiItem , parseEther , toHex } from "viem" ;
8+ import { createPublicClient , createWalletClient , http , isAddress , parseAbi , parseAbiItem , parseEther , toHex } from "viem" ;
99import { privateKeyToAccount } from "viem/accounts" ;
1010import { foundry , baseSepolia } from "viem/chains" ;
1111import { DopplerSDK } from "@whetstone-research/doppler-sdk/evm" ;
@@ -21,6 +21,8 @@ import { createWallet } from "./wallet/create.js";
2121import { DEFAULT_PRIVATE_KEY_SECRET_NAME , linkWallet } from "./wallet/link.js" ;
2222import { getLocalWallet , walletFilePath } from "./wallet/store.js" ;
2323import { wallet } from "viem/tempo/actions" ;
24+ import { getConfig , type Config } from "@/utils/config.js" ;
25+ import { get } from "node:http" ;
2426
2527const COMMAND_NAME = "fcf" ;
2628const COMMAND_DESCRIPTION = "FCF CLI tool." ;
@@ -190,7 +192,7 @@ function addRegisterCommand(program: Command): void {
190192 . command ( "register" )
191193 . option ( "--oidc-token <token>" , "GitHub's OIDC repository token" )
192194 // needs to be removed in prod or be a default
193- . requiredOption ( "--contract <addr>" , "deployed RIK address" )
195+ . option ( "--contract <addr>" , "deployed RIK address" )
194196 . action ( async ( opts ) => {
195197 const { account, publicClient, walletClient } = clients ( ) ;
196198 const oidcToken = opts . oidcToken ??
@@ -208,8 +210,12 @@ function addRegisterCommand(program: Command): void {
208210 const ownerId = BigInt ( jwt . payload . repository_owner_id ) ;
209211 if ( ! jwt . header . kid ) die ( new Error ( "jwt kid missing" ) ) ;
210212
213+ const { rikContractAddress } = getConfig ( ) ;
214+ if ( ! rikContractAddress && ! opts . contract ) die ( new Error ( "failed to get RIK contract address" ) ) ;
215+ if ( ! isAddress ( rikContractAddress as `0x${ string } ` ) && ! opts . contract ) die ( new Error ( "invalid RIK contract address" ) ) ;
216+
211217 const hash = await walletClient . writeContract ( {
212- address : opts . contract as `0x${ string } ` ,
218+ address : ( opts . contract ?? rikContractAddress ) as `0x${ string } ` ,
213219 abi,
214220 functionName : "register" ,
215221 args : [
@@ -233,18 +239,22 @@ function addKeysSyncCommand(program: Command): void {
233239 . command ( "keys" )
234240 . command ( "sync" )
235241 // needs to be removed in prod or be a default
236- . requiredOption ( "--contract <addr>" , "deployed RIK address" )
242+ . option ( "--contract <addr>" , "deployed RIK address" )
237243 . action ( async ( opts ) => {
238244 const { account, publicClient, walletClient } = clients ( ) ;
239245 const config = await fetchJson ( `${ GITHUB_ISSUER } /.well-known/openid-configuration` ) ;
240246 const jwks = await fetchJson ( config . jwks_uri ) ;
241247
248+ const { rikContractAddress } = getConfig ( ) ;
249+ if ( ! rikContractAddress && ! opts . contract ) die ( new Error ( "failed to get RIK contract address" ) ) ;
250+ if ( ! isAddress ( rikContractAddress as `0x${ string } ` ) && ! opts . contract ) die ( new Error ( "invalid RIK contract address" ) ) ;
251+
242252 for ( const key of jwks . keys ?? [ ] ) {
243253 if ( key . kty !== "RSA" || ! key . kid || ! key . n || ! key . e ) continue ;
244254
245255 const kid = jwtKid ( key . kid ) ;
246256 const hash = await walletClient . writeContract ( {
247- address : opts . contract as `0x${ string } ` ,
257+ address : ( opts . contract ?? rikContractAddress ) as `0x${ string } ` ,
248258 abi,
249259 functionName : "addKey" ,
250260 args : [ kid , b64urlToHex ( key . n ) , b64urlToHex ( key . e ) ] ,
@@ -260,16 +270,20 @@ function addListCommand(program: Command): void {
260270 program
261271 . command ( "list" )
262272 // needs to be removed in prod or be a default
263- . requiredOption ( "--contract <addr>" , "deployed RIK address" )
273+ . option ( "--contract <addr>" , "deployed RIK address" )
264274 . option ( "--from-block <n>" , "starting block" )
265275 . action ( async ( opts ) => {
266276 const { publicClient } = clients ( ) ;
267277 const fromBlock = opts . fromBlock
268278 ? BigInt ( opts . fromBlock )
269279 : await publicClient . getBlockNumber ( ) - DEFAULT_LIST_BLOCK_RANGE ;
270280
281+ const { rikContractAddress } = getConfig ( ) ;
282+ if ( ! rikContractAddress && ! opts . contract ) die ( new Error ( "failed to get RIK contract address" ) ) ;
283+ if ( ! isAddress ( rikContractAddress as `0x${ string } ` ) && ! opts . contract ) die ( new Error ( "invalid RIK contract address" ) ) ;
284+
271285 const logs = await publicClient . getLogs ( {
272- address : opts . contract as `0x${ string } ` ,
286+ address : ( opts . contract ?? rikContractAddress ) as `0x${ string } ` ,
273287 event : RepoRegisteredEvent ,
274288 fromBlock,
275289 toBlock : "latest" ,
0 commit comments