Last active
May 7, 2022 21:53
-
-
Save cgcardona/47970058c13e90f30aaddaa162a1c832 to your computer and use it in GitHub Desktop.
This script correctly consumes and signs the 1-of-2 output from step1. It also creates an AVAX UTXO which is a 1-of-2 multisig with keypairs A and B. Lastly it instantiates a virtual machine as a blockchain on a subnet by correctly signing the 2-of-3 Subnet Auth.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
Avalanche, | |
BinTools, | |
BN, | |
Buffer, | |
GenesisAsset, | |
GenesisData | |
} from "avalanche" | |
import { InitialStates } from "avalanche/dist/apis/avm" | |
import { | |
PlatformVMAPI, | |
KeyChain, | |
SECPTransferOutput, | |
SECPTransferInput, | |
TransferableOutput, | |
TransferableInput, | |
UTXOSet, | |
UTXO, | |
AmountOutput, | |
UnsignedTx, | |
CreateChainTx, | |
Tx | |
} from "avalanche/dist/apis/platformvm" | |
import { Output } from "avalanche/dist/common" | |
import { | |
PrivateKeyPrefix, | |
DefaultLocalGenesisPrivateKey, | |
ONEAVAX | |
} from "avalanche/dist/utils" | |
const bintools: BinTools = BinTools.getInstance() | |
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 pchain: PlatformVMAPI = avalanche.PChain() | |
// Keychain with 4 keys-A, B, C, and D | |
const pKeychain: KeyChain = pchain.keyChain() | |
// Keypair A | |
let privKey: string = `${PrivateKeyPrefix}${DefaultLocalGenesisPrivateKey}` | |
// 'P-custom18jma8ppw3nhx5r4ap8clazz0dps7rv5u9xde7p' | |
pKeychain.importKey(privKey) | |
// Keypair B | |
privKey = "PrivateKey-R6e8f5QSa89DjpvL9asNdhdJ4u8VqzMJStPV8VVdDmLgPd8a4" | |
// 'P-custom15s7p7mkdev0uajrd0pzxh88kr8ryccztnlmzvj' | |
pKeychain.importKey(privKey) | |
// Keypair C | |
privKey = "PrivateKey-24gdABgapjnsJfnYkfev6YPyQhTaCU72T9bavtDNTYivBLp2eW" | |
// 'P-custom1u6eth2fg33ye63mnyu5jswtj326jaypvhyar45' | |
pKeychain.importKey(privKey) | |
// Keypair D | |
privKey = "PrivateKey-2uWuEQbY5t7NPzgqzDrXSgGPhi3uyKj2FeAvPUHYo6CmENHJfn" | |
// P-custom1t3qjau2pf3ys83yallqt4y5xc3l6ya5f7wr6aq | |
pKeychain.importKey(privKey) | |
const pAddresses: Buffer[] = pchain.keyChain().getAddresses() | |
const pAddressStrings: string[] = pchain.keyChain().getAddressStrings() | |
const pChainBlockchainID: string = "11111111111111111111111111111111LpoYY" | |
const outputs: TransferableOutput[] = [] | |
const inputs: TransferableInput[] = [] | |
const fee: BN = ONEAVAX | |
const threshold: number = 1 | |
const locktime: BN = new BN(0) | |
const avaxUTXOKeychain: Buffer[] = [pAddresses[0], pAddresses[1]] | |
const avaxUTXOKeychainStrings: string[] = [pAddressStrings[0], pAddressStrings[1]] | |
const main = async (): Promise<any> => { | |
const assetAlias: string = "AssetAliasTest" | |
const name: string = "Test Asset" | |
const symbol: string = "TEST" | |
const denomination: number = 0 | |
const amount: BN = new BN(507) | |
const vcapSecpOutput = new SECPTransferOutput( | |
amount, | |
avaxUTXOKeychain, | |
locktime, | |
threshold | |
) | |
const initialStates: InitialStates = new InitialStates() | |
initialStates.addOutput(vcapSecpOutput) | |
const memo: Buffer = Buffer.from("Manually crete") | |
const genesisAsset = new GenesisAsset( | |
assetAlias, | |
name, | |
symbol, | |
denomination, | |
initialStates, | |
memo | |
) | |
const genesisAssets: GenesisAsset[] = [] | |
genesisAssets.push(genesisAsset) | |
const genesisData: GenesisData = new GenesisData(genesisAssets, networkID) | |
const avaxAssetID: Buffer = await pchain.getAVAXAssetID() | |
const getBalanceResponse: any = await pchain.getBalance(pAddressStrings[0]) | |
const unlocked: BN = new BN(getBalanceResponse.unlocked) | |
const secpTransferOutput: SECPTransferOutput = new SECPTransferOutput( | |
unlocked.sub(fee), | |
avaxUTXOKeychain, | |
locktime, | |
threshold | |
) | |
const transferableOutput: TransferableOutput = new TransferableOutput( | |
avaxAssetID, | |
secpTransferOutput | |
) | |
outputs.push(transferableOutput) | |
const platformVMUTXOResponse: any = await pchain.getUTXOs(avaxUTXOKeychainStrings) | |
const utxoSet: UTXOSet = platformVMUTXOResponse.utxos | |
const utxos: UTXO[] = utxoSet.getAllUTXOs() | |
utxos.forEach((utxo: UTXO) => { | |
const output: Output = utxo.getOutput() | |
if (output.getOutputID() === 7) { | |
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]) | |
const input: TransferableInput = new TransferableInput( | |
txid, | |
outputidx, | |
avaxAssetID, | |
secpTransferInput | |
) | |
inputs.push(input) | |
} | |
}) | |
const subnetID: Buffer = bintools.cb58Decode( | |
"2e93TxpEo8wakrEV3T3oESdsUXbnTW7reecDZGHDXWsHrkFnxz" | |
) | |
const chainName: string = "EPIC AVM" | |
const vmID: string = "avm" | |
const fxIDs: string[] = ["secp256k1fx", "nftfx", "propertyfx"] | |
fxIDs.sort() | |
const blockchainID: Buffer = bintools.cb58Decode(pChainBlockchainID) | |
const createChainTx: CreateChainTx = new CreateChainTx( | |
networkID, | |
blockchainID, | |
outputs, | |
inputs, | |
memo, | |
subnetID, | |
chainName, | |
vmID, | |
fxIDs, | |
genesisData | |
) | |
createChainTx.addSignatureIdx(0, pAddresses[3]) | |
createChainTx.addSignatureIdx(2, pAddresses[2]) | |
const unsignedTx: UnsignedTx = new UnsignedTx(createChainTx) | |
const tx: Tx = unsignedTx.sign(pKeychain) | |
const txid: string = await pchain.issueTx(tx) | |
console.log(`Success! TXID: ${txid}`) | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment