import { Ecanna, BinTools, BN, Buffer } from "ecanna/dist" import { EVMAPI, EVMOutput, ImportTx, TransferableInput, KeyChain, UTXO, UTXOSet, SECPTransferInput, AmountOutput, UnsignedTx, Tx } from "ecanna/dist/apis/evm" import { PrivateKeyPrefix, DefaultLocalGenesisPrivateKey, Defaults } from "ecanna/dist/utils" const ip: string = "localhost" const port: number = 9650 const protocol: string = "http" const networkID: number = 1337 const ecanna: Ecanna = new Ecanna(ip, port, protocol, networkID) const evmchain: EVMAPI = ecanna.EVMChain() const bintools: BinTools = BinTools.getInstance() const cKeychain: KeyChain = evmchain.keyChain() const cHexAddress: string = "0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC" const privKey: string = `${PrivateKeyPrefix}${DefaultLocalGenesisPrivateKey}` cKeychain.importKey(privKey) const cAddresses: Buffer[] = evmchain.keyChain().getAddresses() const cAddressStrings: string[] = evmchain.keyChain().getAddressStrings() const evmChainBlockchainIdStr: string = Defaults.network[networkID].EVM.blockchainID const evmChainBlockchainIdBuf: Buffer = bintools.cb58Decode( evmChainBlockchainIdStr ) const tknChainBlockchainIdStr: string = Defaults.network[networkID].TKN.blockchainID const tknChainBlockchainIdBuf: Buffer = bintools.cb58Decode( tknChainBlockchainIdStr ) const importedIns: TransferableInput[] = [] const evmOutputs: EVMOutput[] = [] const fee: BN = evmchain.getDefaultTxFee() const main = async (): Promise => { const u: any = await evmchain.getUTXOs(cAddressStrings[0], "TKN") const utxoSet: UTXOSet = u.utxos const utxos: UTXO[] = utxoSet.getAllUTXOs() utxos.forEach((utxo: UTXO) => { const assetID: Buffer = utxo.getAssetID() const txid: Buffer = utxo.getTxID() const outputidx: Buffer = utxo.getOutputIdx() const output: AmountOutput = utxo.getOutput() as AmountOutput const amt: BN = output.getAmount().clone() const input: SECPTransferInput = new SECPTransferInput(amt) input.addSignatureIdx(0, cAddresses[0]) const xferin: TransferableInput = new TransferableInput( txid, outputidx, assetID, input ) importedIns.push(xferin) const evmOutput: EVMOutput = new EVMOutput( cHexAddress, amt.sub(fee), assetID ) evmOutputs.push(evmOutput) }) const importTx: ImportTx = new ImportTx( networkID, evmChainBlockchainIdBuf, tknChainBlockchainIdBuf, importedIns, evmOutputs ) const unsignedTx: UnsignedTx = new UnsignedTx(importTx) const tx: Tx = unsignedTx.sign(cKeychain) const txid: string = await evmchain.issueTx(tx) console.log(`Success! TXID: ${txid}`) } main()