import { Ecanna, BinTools, BN, Buffer } from "ecanna/dist" import { EVMAPI, KeyChain as EVMKeyChain } from "ecanna/dist/apis/evm" import { StakeVMAPI, KeyChain, SECPTransferOutput, SECPTransferInput, TransferableOutput, TransferableInput, UTXOSet, UTXO, AmountOutput, UnsignedTx, Tx, ExportTx } from "ecanna/dist/apis/stakevm" import { PrivateKeyPrefix, DefaultLocalGenesisPrivateKey, Defaults, MILLIECNA } 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 stkchain: StakeVMAPI = ecanna.STKChain() const bintools: BinTools = BinTools.getInstance() const cKeychain: EVMKeyChain = evmchain.keyChain() const pKeychain: KeyChain = stkchain.keyChain() let privKey: string = `${PrivateKeyPrefix}${DefaultLocalGenesisPrivateKey}` // TKN-custom18jma8ppw3nhx5r4ap8clazz0dps7rv5u9xde7p cKeychain.importKey(privKey) pKeychain.importKey(privKey) privKey = "PrivateKey-R6e8f5QSa89DjpvL9asNdhdJ4u8VqzMJStPV8VVdDmLgPd8a4" // STK-custom15s7p7mkdev0uajrd0pzxh88kr8ryccztnlmzvj cKeychain.importKey(privKey) pKeychain.importKey(privKey) const cAddresses: Buffer[] = evmchain.keyChain().getAddresses() const pAddresses: Buffer[] = stkchain.keyChain().getAddresses() const pAddressStrings: string[] = stkchain.keyChain().getAddressStrings() const evmChainBlockchainID: string = Defaults.network[networkID].EVM.blockchainID const stkChainBlockchainID: string = Defaults.network[networkID].STK.blockchainID const exportedOuts: TransferableOutput[] = [] const outputs: TransferableOutput[] = [] const inputs: TransferableInput[] = [] const fee: BN = MILLIECNA const threshold: number = 2 const locktime: BN = new BN(0) const memo: Buffer = Buffer.from( "Manually Export ECNA from STK-Chain to EVM-Chain" ) const main = async (): Promise => { const ecnaAssetID: Buffer = await stkchain.getECNAAssetID() const getBalanceResponse: any = await stkchain.getBalance(pAddressStrings[0]) const unlocked: BN = new BN(getBalanceResponse.unlocked) console.log(unlocked.sub(fee).toString()) const secpTransferOutput: SECPTransferOutput = new SECPTransferOutput( unlocked.sub(fee), cAddresses, locktime, threshold ) const transferableOutput: TransferableOutput = new TransferableOutput( ecnaAssetID, secpTransferOutput ) exportedOuts.push(transferableOutput) const stakeVMUTXOResponse: any = await stkchain.getUTXOs(pAddressStrings) const utxoSet: UTXOSet = stakeVMUTXOResponse.utxos const utxos: UTXO[] = utxoSet.getAllUTXOs() utxos.forEach((utxo: UTXO): void => { const amountOutput: AmountOutput = utxo.getOutput() as AmountOutput const amt: BN = amountOutput.getAmount().clone() const txid: Buffer = utxo.getTxID() const outputidx: Buffer = utxo.getOutputIdx() const secpTransferInput: SECPTransferInput = new SECPTransferInput(amt) secpTransferInput.addSignatureIdx(0, pAddresses[0]) if (utxo.getOutput().getThreshold() === 2) { secpTransferInput.addSignatureIdx(1, pAddresses[1]) } const input: TransferableInput = new TransferableInput( txid, outputidx, ecnaAssetID, secpTransferInput ) inputs.push(input) }) const exportTx: ExportTx = new ExportTx( networkID, bintools.cb58Decode(stkChainBlockchainID), outputs, inputs, memo, bintools.cb58Decode(evmChainBlockchainID), exportedOuts ) const unsignedTx: UnsignedTx = new UnsignedTx(exportTx) const tx: Tx = unsignedTx.sign(pKeychain) const txid: string = await stkchain.issueTx(tx) console.log(`Success! TXID: ${txid}`) } main()