import { Ecanna, BinTools, BN, Buffer } from "ecanna/dist" import { TKNVMAPI, KeyChain, SECPTransferOutput, SECPTransferInput, TransferableOutput, TransferableInput, UTXOSet, UTXO, AmountOutput, UnsignedTx, Tx, BaseTx } from "ecanna/dist/apis/tknvm" import { Defaults, PrivateKeyPrefix, DefaultLocalGenesisPrivateKey } 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 tknchain: TKNVMAPI = ecanna.TKNChain() const bintools: BinTools = BinTools.getInstance() const xKeychain: KeyChain = tknchain.keyChain() const privKey: string = `${PrivateKeyPrefix}${DefaultLocalGenesisPrivateKey}` xKeychain.importKey(privKey) const xAddresses: Buffer[] = tknchain.keyChain().getAddresses() const xAddressStrings: string[] = tknchain.keyChain().getAddressStrings() const blockchainID: string = Defaults.network[networkID].TKN.blockchainID const ecnaAssetID: string = Defaults.network[networkID].TKN.ecnaAssetID const ecnaAssetIDBuf: Buffer = bintools.cb58Decode(ecnaAssetID) const outputs: TransferableOutput[] = [] const inputs: TransferableInput[] = [] const fee: BN = tknchain.getDefaultTxFee() const threshold: number = 1 const locktime: BN = new BN(0) const memo: Buffer = Buffer.from("TKNVM manual BaseTx to send ECNA and ANT") // Uncomment for codecID 00 01 // const codecID: number = 1 const main = async (): Promise => { const tknvmUTXOResponse: any = await tknchain.getUTXOs(xAddressStrings) const utxoSet: UTXOSet = tknvmUTXOResponse.utxos const utxos: UTXO[] = utxoSet.getAllUTXOs() utxos.forEach((utxo: UTXO): void => { const typeID: number = utxo.getOutput().getTypeID() if (typeID != 6) { const amountOutput: AmountOutput = utxo.getOutput() as AmountOutput const amt: BN = amountOutput.getAmount().clone() const txID: Buffer = utxo.getTxID() const outputIDX: Buffer = utxo.getOutputIdx() const assetID: Buffer = utxo.getAssetID() if (assetID.toString("hex") === ecnaAssetIDBuf.toString("hex")) { const secpTransferOutput: SECPTransferOutput = new SECPTransferOutput( amt.sub(fee), xAddresses, locktime, threshold ) // Uncomment for codecID 00 01 // secpTransferOutput.setCodecID(codecID) const transferableOutput: TransferableOutput = new TransferableOutput( ecnaAssetIDBuf, secpTransferOutput ) outputs.push(transferableOutput) const secpTransferInput: SECPTransferInput = new SECPTransferInput(amt) // Uncomment for codecID 00 01 // secpTransferInput.setCodecID(codecID) secpTransferInput.addSignatureIdx(0, xAddresses[0]) const input: TransferableInput = new TransferableInput( txID, outputIDX, ecnaAssetIDBuf, secpTransferInput ) inputs.push(input) } else { const secpTransferOutput: SECPTransferOutput = new SECPTransferOutput( amt, xAddresses, locktime, threshold ) // Uncomment for codecID 00 01 // secpTransferOutput.setCodecID(codecID) const transferableOutput: TransferableOutput = new TransferableOutput( assetID, secpTransferOutput ) outputs.push(transferableOutput) const secpTransferInput: SECPTransferInput = new SECPTransferInput(amt) // Uncomment for codecID 00 01 // secpTransferInput.setCodecID(codecID) secpTransferInput.addSignatureIdx(0, xAddresses[0]) const input: TransferableInput = new TransferableInput( txID, outputIDX, assetID, secpTransferInput ) inputs.push(input) } } }) const baseTx: BaseTx = new BaseTx( networkID, bintools.cb58Decode(blockchainID), outputs, inputs, memo ) // Uncomment for codecID 00 01 // baseTx.setCodecID(codecID) const unsignedTx: UnsignedTx = new UnsignedTx(baseTx) const tx: Tx = unsignedTx.sign(xKeychain) const txid: string = await tknchain.issueTx(tx) console.log(`Success! TXID: ${txid}`) } main()