import createHash from "create-hash" import { Ecanna, BN, Buffer } from "ecanna/dist" import { TKNVMAPI, KeyChain as TKNVMKeyChain, UTXOSet, UnsignedTx, Tx } from "ecanna/dist/apis/tknvm" import { KeyChain as StakeVMKeyChain, StakeVMAPI } from "ecanna/dist/apis/stakevm" import { PrivateKeyPrefix, DefaultLocalGenesisPrivateKey, Defaults, UnixNow, SerializedType } from "ecanna/dist/utils" import { Serialization } from "ecanna/dist/utils" const serialization: Serialization = Serialization.getInstance() 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 tknchain: TKNVMAPI = ecanna.TKNChain() const stkchain: StakeVMAPI = ecanna.STKChain() const xKeychain: TKNVMKeyChain = tknchain.keyChain() const pKeychain: StakeVMKeyChain = stkchain.keyChain() const privKey: string = `${PrivateKeyPrefix}${DefaultLocalGenesisPrivateKey}` xKeychain.importKey(privKey) pKeychain.importKey(privKey) const xAddressStrings: string[] = tknchain.keyChain().getAddressStrings() const pAddressStrings: string[] = stkchain.keyChain().getAddressStrings() const stkChainBlockchainID: string = Defaults.network[networkID].STK.blockchainID const ecnaAssetID: string = Defaults.network[networkID].TKN.ecnaAssetID const locktime: BN = new BN(0) const asOf: BN = UnixNow() const memo: Buffer = Buffer.from( "TKNVM utility method buildExportTx to export ECNA to the STK-Chain from the TKN-Chain" ) const fee: BN = tknchain.getDefaultTxFee() const cb58: SerializedType = "cb58" const main = async (): Promise => { const tknvmUTXOResponse: any = await tknchain.getUTXOs(xAddressStrings) const utxoSet: UTXOSet = tknvmUTXOResponse.utxos const getBalanceResponse: any = await tknchain.getBalance( xAddressStrings[0], ecnaAssetID ) const balance: BN = new BN(getBalanceResponse.balance) const amount: BN = balance.sub(fee) const unsignedTx: UnsignedTx = await tknchain.buildExportTx( utxoSet, amount, stkChainBlockchainID, pAddressStrings, xAddressStrings, xAddressStrings, memo, asOf, locktime ) const tx: Tx = unsignedTx.sign(xKeychain) const buffer: Buffer = Buffer.from( createHash("sha256").update(tx.toBuffer()).digest().buffer ) const txid: string = serialization.bufferToType(buffer, cb58) console.log(txid) // APfkX9NduHkZtghRpQASNZJjLut4ZAkVhkTGeazQerLSRa36t } main()