import { Ecanna, BinTools, BN, Buffer } from "ecanna/dist" import { StakeVMAPI, KeyChain, SECPTransferOutput, SECPTransferInput, TransferableOutput, TransferableInput, UTXOSet, UTXO, AmountOutput, UnsignedTx, CreateSubnetTx, Tx, SECPOwnerOutput } from "ecanna/dist/apis/stakevm" import { PrivateKeyPrefix, DefaultLocalGenesisPrivateKey, Defaults } 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 stkchain: StakeVMAPI = ecanna.STKChain() const bintools: BinTools = BinTools.getInstance() // Keychain with 4 keys-A, B, C, and D const pKeychain: KeyChain = stkchain.keyChain() // Keypair A let privKey: string = `${PrivateKeyPrefix}${DefaultLocalGenesisPrivateKey}` // STK-custom18jma8ppw3nhx5r4ap8clazz0dps7rv5u9xde7p pKeychain.importKey(privKey) // Keypair B privKey = "PrivateKey-R6e8f5QSa89DjpvL9asNdhdJ4u8VqzMJStPV8VVdDmLgPd8a4" // STK-custom15s7p7mkdev0uajrd0pzxh88kr8ryccztnlmzvj pKeychain.importKey(privKey) // Keypair C privKey = "PrivateKey-24gdABgapjnsJfnYkfev6YPyQhTaCU72T9bavtDNTYivBLp2eW" // STK-custom1u6eth2fg33ye63mnyu5jswtj326jaypvhyar45 pKeychain.importKey(privKey) // Keypair D privKey = "PrivateKey-2uWuEQbY5t7NPzgqzDrXSgGPhi3uyKj2FeAvPUHYo6CmENHJfn" // STK-custom1t3qjau2pf3ys83yallqt4y5xc3l6ya5f7wr6aq pKeychain.importKey(privKey) const pAddresses: Buffer[] = stkchain.keyChain().getAddresses() const pAddressStrings: string[] = stkchain.keyChain().getAddressStrings() const stkChainBlockchainID: string = Defaults.network[networkID].STK.blockchainID const stkChainBlockchainIDBuf: Buffer = bintools.cb58Decode(stkChainBlockchainID) const outputs: TransferableOutput[] = [] const inputs: TransferableInput[] = [] const fee: BN = stkchain.getCreateSubnetTxFee() const threshold: number = 1 const threshold2: number = 2 const locktime: BN = new BN(0) const memo: Buffer = Buffer.from( "Manually create a CreateSubnetTx which creates a 1-of-2 ECNA utxo and a 2-of-3 SubnetAuth" ) const ecnaUTXOKeychain: Buffer[] = [pAddresses[0], pAddresses[1]] const subnetAuthKeychain: Buffer[] = [ pAddresses[1], pAddresses[2], pAddresses[3] ] 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) const secpTransferOutput: SECPTransferOutput = new SECPTransferOutput( unlocked.sub(fee), ecnaUTXOKeychain, locktime, threshold ) const transferableOutput: TransferableOutput = new TransferableOutput( ecnaAssetID, secpTransferOutput ) outputs.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 = 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]) const input: TransferableInput = new TransferableInput( txid, outputidx, ecnaAssetID, secpTransferInput ) inputs.push(input) }) const subnetOwner: SECPOwnerOutput = new SECPOwnerOutput( subnetAuthKeychain, locktime, threshold2 ) const createSubnetTx: CreateSubnetTx = new CreateSubnetTx( networkID, stkChainBlockchainIDBuf, outputs, inputs, memo, subnetOwner ) const unsignedTx: UnsignedTx = new UnsignedTx(createSubnetTx) const tx: Tx = unsignedTx.sign(pKeychain) const txid: string = await stkchain.issueTx(tx) console.log(`Success! TXID: ${txid}`) } main()