@@ -15,6 +15,8 @@ import {
1515 InsufficientAmountError ,
1616 InsufficientFundsError ,
1717} from "@dfinity/nns" ;
18+ import { Principal } from "@dfinity/principal" ;
19+ import type { Secp256k1PublicKey } from "src/ledger/secp256k1" ;
1820import { Agent , AnonymousIdentity , HttpAgent , Identity } from "@dfinity/agent" ;
1921import chalk from "chalk" ;
2022
@@ -24,7 +26,7 @@ import "node-window-polyfill/register";
2426// Add polyfill for `window.fetch` for agent-js to work.
2527// @ts -ignore (no types are available)
2628import fetch from "node-fetch" ;
27- import { Principal } from "@dfinity/principal" ;
29+
2830global . fetch = fetch ;
2931window . fetch = fetch ;
3032
@@ -285,10 +287,20 @@ async function listNeurons() {
285287 }
286288}
287289
290+ const buf2hex = ( buffer : ArrayBuffer ) : string => {
291+ return [ ...new Uint8Array ( buffer ) ]
292+ . map ( ( x ) => x . toString ( 16 ) . padStart ( 2 , "0" ) )
293+ . join ( "" ) ;
294+ } ;
295+
288296/**
289297 * Fetches the balance of the main account on the wallet.
290298 */
291- async function claimNeurons ( hexPubKey : string ) {
299+ async function claimNeurons ( ) {
300+ const identity = await LedgerIdentity . create ( ) ;
301+
302+ const bufferKey = identity . getPublicKey ( ) as Secp256k1PublicKey ;
303+ const hexPubKey = buf2hex ( bufferKey . toRaw ( ) ) ;
292304 const isHex = hexPubKey . match ( "^[0-9a-fA-F]+$" ) ;
293305 if ( ! isHex ) {
294306 throw new Error ( `${ hexPubKey } is not a hex string.` ) ;
@@ -298,7 +310,6 @@ async function claimNeurons(hexPubKey: string) {
298310 throw new Error ( `The key must be >= 130 characters and <= 150 characters.` ) ;
299311 }
300312
301- const identity = await LedgerIdentity . create ( ) ;
302313 const governance = await GenesisTokenCanister . create ( {
303314 agent : await getAgent ( identity ) ,
304315 } ) ;
@@ -477,11 +488,10 @@ async function main() {
477488 )
478489 . addCommand (
479490 new Command ( "claim" )
480- . requiredOption (
481- "--hex-public-key <public-key>" ,
491+ . description (
482492 "Claim the caller's GTC neurons."
483493 )
484- . action ( ( args ) => run ( ( ) => claimNeurons ( args . hexPublicKey ) ) )
494+ . action ( ( args ) => run ( ( ) => claimNeurons ( ) ) )
485495 ) ;
486496
487497 const icp = new Command ( "icp" )
0 commit comments