PHP WebShell
Текущая директория: /usr/lib/node_modules/bitgo/node_modules/avalanche/examples/platformvm
Просмотр файла: exportTx-cchain.ts
import { Avalanche, BinTools, BN, Buffer } from "avalanche/dist"
import { EVMAPI, KeyChain as EVMKeyChain } from "avalanche/dist/apis/evm"
import {
PlatformVMAPI,
KeyChain,
SECPTransferOutput,
SECPTransferInput,
TransferableOutput,
TransferableInput,
UTXOSet,
UTXO,
AmountOutput,
UnsignedTx,
Tx,
ExportTx
} from "avalanche/dist/apis/platformvm"
import {
PrivateKeyPrefix,
DefaultLocalGenesisPrivateKey,
Defaults,
MILLIAVAX
} from "avalanche/dist/utils"
const ip: string = "localhost"
const port: number = 9650
const protocol: string = "http"
const networkID: number = 1337
const avalanche: Avalanche = new Avalanche(ip, port, protocol, networkID)
const cchain: EVMAPI = avalanche.CChain()
const pchain: PlatformVMAPI = avalanche.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 = MILLIAVAX
const threshold: number = 2
const locktime: BN = new BN(0)
const memo: Buffer = Buffer.from("Manually Export AVAX from P-Chain to C-Chain")
const main = async (): Promise<any> => {
const avaxAssetID: Buffer = await pchain.getAVAXAssetID()
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(
avaxAssetID,
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,
avaxAssetID,
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()
Выполнить команду
Для локальной разработки. Не используйте в интернете!