import { Lux, BinTools, BN, Buffer } from "lux/dist" import { EVMAPI, KeyChain as EVMKeyChain } from "lux/dist/apis/evm" import { PlatformVMAPI, KeyChain, SECPTransferOutput, SECPTransferInput, TransferableOutput, TransferableInput, UTXOSet, UTXO, AmountOutput, UnsignedTx, Tx, ExportTx } from "lux/dist/apis/platformvm" import { PrivateKeyPrefix, DefaultLocalGenesisPrivateKey, Defaults, MILLILUX } from "lux/dist/utils" const ip: string = "localhost" const port: number = 9650 const protocol: string = "http" const networkID: number = 1337 const lux: Lux = new Lux(ip, port, protocol, networkID) const cchain: EVMAPI = lux.CChain() const pchain: PlatformVMAPI = lux.PChain() const bintools: BinTools = BinTools.getInstance() const cKeychain: EVMKeyChain = cchain.keyChain() const pKeychain: KeyChain = pchain.keyChain() let privKey: string = `${PrivateKeyPrefix}${DefaultLocalGenesisPrivateKey}` // X-custom18jma8ppw3nhx5r4ap8clazz0dps7rv5u9xde7p cKeychain.importKey(privKey) pKeychain.importKey(privKey) privKey = "PrivateKey-R6e8f5QSa89DjpvL9asNdhdJ4u8VqzMJStPV8VVdDmLgPd8a4" // P-custom15s7p7mkdev0uajrd0pzxh88kr8ryccztnlmzvj cKeychain.importKey(privKey) pKeychain.importKey(privKey) const cAddresses: Buffer[] = cchain.keyChain().getAddresses() const pAddresses: Buffer[] = pchain.keyChain().getAddresses() const pAddressStrings: string[] = pchain.keyChain().getAddressStrings() const cChainBlockchainID: string = Defaults.network[networkID].C.blockchainID const pChainBlockchainID: string = Defaults.network[networkID].P.blockchainID const exportedOuts: TransferableOutput[] = [] const outputs: TransferableOutput[] = [] const inputs: TransferableInput[] = [] const fee: BN = MILLILUX const threshold: number = 2 const locktime: BN = new BN(0) const memo: Buffer = Buffer.from("Manually Export LUX from P-Chain to C-Chain") const main = async (): Promise => { const luxAssetID: Buffer = await pchain.getLUXAssetID() const getBalanceResponse: any = await pchain.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( luxAssetID, secpTransferOutput ) exportedOuts.push(transferableOutput) const platformVMUTXOResponse: any = await pchain.getUTXOs(pAddressStrings) const utxoSet: UTXOSet = platformVMUTXOResponse.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, luxAssetID, secpTransferInput ) inputs.push(input) }) const exportTx: ExportTx = new ExportTx( networkID, bintools.cb58Decode(pChainBlockchainID), outputs, inputs, memo, bintools.cb58Decode(cChainBlockchainID), exportedOuts ) const unsignedTx: UnsignedTx = new UnsignedTx(exportTx) const tx: Tx = unsignedTx.sign(pKeychain) const txid: string = await pchain.issueTx(tx) console.log(`Success! TXID: ${txid}`) } main()