import type { NextApiRequest , NextApiResponse } from 'next' ; import { getAvalancheClient } from '@figment-avalanche/lib' ; import { BinTools } from 'avalanche' ; import { Address } from 'ethereumjs-util' ; export default async function ( req , res ) { try { const { secret , network } = req . body ; const client = getAvalancheClient ( network ) ; const [ xChain , cChain ] = [ client . XChain ( ) , client . CChain ( ) ] ; const [ xKeychain , cKeychain ] = [ xChain . keyChain ( ) , cChain . keyChain ( ) ] ; const [ xKeypair , cKeypair ] = [ xKeychain . importKey ( secret ) , cKeychain . importKey ( secret ) , ] ; const [ cAddress ] = [ cKeypair . getAddressString ( ) ] ; const xChainId = undefined ; const { utxos } = await cChain . getUTXOs ( cAddress , xChainId ) ; const binTools = BinTools . getInstance ( ) ; const keyBuff = binTools . cb58Decode ( secret . split ( '-' ) [ 1 ] ) ; const ethAddr = Address . fromPrivateKey ( Buffer . from ( keyBuff . toString ( 'hex' ) , 'hex' ) ) . toString ( ) ; console . log ( 'Ethereum-style address: ' , ethAddr ) ; const importTx = await cChain . buildImportTx ( undefined ) ; const hash = await cChain . issueTx ( importTx . sign ( cKeychain ) ) ; res . status ( 200 ) . json ( hash ) ; } catch ( error ) { let errorMessage = error instanceof Error ? error . message : 'Unknown Error' ; res . status ( 500 ) . json ( errorMessage ) ; } }