UNPKG

@c4tplatform/caminojs

Version:
727 lines 208 kB
"use strict"; /** * @packageDocumentation * @module API-PlatformVM-Builder */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Builder = void 0; const bn_js_1 = __importDefault(require("bn.js")); const buffer_1 = require("buffer/"); const create_hash_1 = __importDefault(require("create-hash")); const _1 = require("."); const adddepositoffertx_1 = require("../../apis/platformvm/adddepositoffertx"); const common_1 = require("../../common"); const utils_1 = require("../../utils"); const errors_1 = require("../../utils/errors"); const addproposaltx_1 = require("./addproposaltx"); const addvotetx_1 = require("./addvotetx"); const zero = new bn_js_1.default(0); class Builder { constructor(spender, caminoEnabled) { /** * Creates an [[UnsignedTx]] wrapping a [[BaseTx]]. For more granular control, you may create your own * [[UnsignedTx]] wrapping a [[BaseTx]] manually (with their corresponding [[TransferableInput]]s and [[TransferableOutput]]s). * * @param networkID The number representing NetworkID of the node * @param blockchainID The {@link https://github.com/feross/buffer|Buffer} representing the BlockchainID for the transaction * @param amount The amount of the asset to be spent in its smallest denomination, represented as {@link https://github.com/indutny/bn.js/|BN}. * @param assetID {@link https://github.com/feross/buffer|Buffer} of the asset ID for the UTXO * @param toAddresses The addresses to send the funds * @param fromSigner The addresses being used to send and verify the funds from the UTXOs {@link https://github.com/feross/buffer|Buffer} * @param changeAddresses Optional. The addresses that can spend the change remaining from the spent UTXOs. Default: toAddresses * @param fee Optional. The amount of fees to burn in its smallest denomination, represented as {@link https://github.com/indutny/bn.js/|BN} * @param feeAssetID Optional. The assetID of the fees being burned. Default: assetID * @param memo Optional. Contains arbitrary data, up to 256 bytes * @param asOf Optional. The timestamp to verify the transaction against as a {@link https://github.com/indutny/bn.js/|BN} * @param locktime Optional. The locktime field created in the resulting outputs * @param toThreshold Optional. The number of signatures required to spend the funds in the resultant UTXO * @param changeThreshold Optional. The number of signatures required to spend the funds in the resultant change UTXO * * @returns An unsigned transaction created from the passed in parameters. * */ this.buildBaseTx = (networkID, blockchainID, amount, amountAssetID, toAddresses, fromSigner, changeAddresses = undefined, fee = zero, feeAssetID = undefined, memo = undefined, asOf = zero, lockTime = zero, toThreshold = 1, changeThreshold = 1) => __awaiter(this, void 0, void 0, function* () { if (toThreshold > toAddresses.length) { /* istanbul ignore next */ throw new errors_1.ThresholdError("Error - UTXOSet.buildBaseTx: threshold is greater than number of addresses"); } if (typeof changeAddresses === "undefined") { changeAddresses = []; } if (typeof feeAssetID === "undefined") { feeAssetID = amountAssetID; } if (amount.eq(zero)) { return undefined; } const aad = new _1.AssetAmountDestination(toAddresses, toThreshold, fromSigner.from, fromSigner.signer, changeAddresses, changeThreshold); if (amountAssetID.toString("hex") === feeAssetID.toString("hex")) { aad.addAssetAmount(amountAssetID, amount, fee); } else { aad.addAssetAmount(amountAssetID, amount, zero); if (this._feeCheck(fee, feeAssetID)) { aad.addAssetAmount(feeAssetID, zero, fee); } } let ins = []; let outs = []; let owners = []; const minSpendableErr = yield this.spender.getMinimumSpendable(aad, asOf, lockTime, "Unlocked"); if (typeof minSpendableErr === "undefined") { ins = aad.getInputs(); outs = aad.getAllOutputs(); owners = aad.getOutputOwners(); } else { throw minSpendableErr; } const baseTx = new _1.BaseTx(networkID, blockchainID, outs, ins, memo); baseTx.setOutputOwners(owners); return new _1.UnsignedTx(baseTx); }); /** * Creates an unsigned ImportTx transaction. * * @param networkID The number representing NetworkID of the node * @param blockchainID The {@link https://github.com/feross/buffer|Buffer} representing the BlockchainID for the transaction * @param toAddresses The addresses to send the funds * @param fromSigner The addresses being used to send and verify the funds from the UTXOs {@link https://github.com/feross/buffer|Buffer} * @param changeAddresses Optional. The addresses that can spend the change remaining from the spent UTXOs. Default: toAddresses * @param importIns An array of [[TransferableInput]]s being imported * @param sourceChain A {@link https://github.com/feross/buffer|Buffer} for the chainid where the imports are coming from. * @param fee Optional. The amount of fees to burn in its smallest denomination, represented as {@link https://github.com/indutny/bn.js/|BN}. Fee will come from the inputs first, if they can. * @param feeAssetID Optional. The assetID of the fees being burned. * @param memo Optional contains arbitrary bytes, up to 256 bytes * @param locktime Optional. The locktime field created in the resulting outputs * @param toThreshold Optional. The number of signatures required to spend the funds in the received UTXO * @param changeThreshold Optional. The number of signatures required to spend the funds in the resultant change UTXO * * @returns An unsigned transaction created from the passed in parameters. * */ this.buildImportTx = (networkID, blockchainID, toAddresses, fromSigner, changeAddresses, atomics, sourceChain = undefined, fee = zero, feeAssetID = undefined, memo = undefined, asOf = zero, locktime = zero, toThreshold = 1, changeThreshold = 1) => __awaiter(this, void 0, void 0, function* () { let ins = []; let outs = []; let owners = []; const importOwners = []; if (typeof fee === "undefined") { fee = zero.clone(); } const importIns = []; let feepaid = new bn_js_1.default(0); let feeAssetStr = feeAssetID.toString("hex"); for (let i = 0; i < atomics.length; i++) { const utxo = atomics[`${i}`]; const assetID = utxo.getAssetID(); const output = utxo.getOutput(); let amt = output.getAmount().clone(); let infeeamount = amt.clone(); let assetStr = assetID.toString("hex"); if (typeof feeAssetID !== "undefined" && fee.gt(zero) && feepaid.lt(fee) && assetStr === feeAssetStr) { feepaid = feepaid.add(infeeamount); if (feepaid.gte(fee)) { infeeamount = feepaid.sub(fee); feepaid = fee.clone(); } else { infeeamount = zero.clone(); } } const txid = utxo.getTxID(); const outputidx = utxo.getOutputIdx(); const input = new _1.SECPTransferInput(amt); const xferin = new _1.TransferableInput(txid, outputidx, assetID, input); const from = output.getAddresses(); const spenders = output.getSpenders(from); for (let j = 0; j < spenders.length; j++) { const idx = output.getAddressIdx(spenders[`${j}`]); if (idx === -1) { /* istanbul ignore next */ throw new errors_1.AddressError("Error - UTXOSet.buildImportTx: no such " + `address in output: ${spenders[`${j}`]}`); } xferin.getInput().addSignatureIdx(idx, spenders[`${j}`]); } importOwners.push(new common_1.OutputOwners(output.getAddresses(), output.getLocktime(), output.getThreshold())); importIns.push(xferin); //add extra outputs for each amount (calculated from the imported inputs), minus fees if (infeeamount.gt(zero)) { const spendout = (0, _1.SelectOutputClass)(output.getOutputID(), infeeamount, toAddresses, locktime, toThreshold); const xferout = new _1.TransferableOutput(assetID, spendout); outs.push(xferout); } } // get remaining fees from the provided addresses let feeRemaining = fee.sub(feepaid); if (feeRemaining.gt(zero) && this._feeCheck(feeRemaining, feeAssetID)) { const aad = new _1.AssetAmountDestination(toAddresses, toThreshold, fromSigner.from, fromSigner.signer, changeAddresses, changeThreshold); aad.addAssetAmount(feeAssetID, zero, feeRemaining); const minSpendableErr = yield this.spender.getMinimumSpendable(aad, asOf, locktime, "Unlocked"); if (typeof minSpendableErr === "undefined") { ins = aad.getInputs(); outs = aad.getAllOutputs(); owners = aad.getOutputOwners(); owners.push(...importOwners); } else { throw minSpendableErr; } } const baseTx = new _1.ImportTx(networkID, blockchainID, outs, ins, memo, sourceChain, importIns); baseTx.setOutputOwners(owners); return new _1.UnsignedTx(baseTx); }); /** * Creates an unsigned ExportTx transaction. * * @param networkID The number representing NetworkID of the node * @param blockchainID The {@link https://github.com/feross/buffer|Buffer} representing the BlockchainID for the transaction * @param amount The amount being exported as a {@link https://github.com/indutny/bn.js/|BN} * @param avaxAssetID {@link https://github.com/feross/buffer|Buffer} of the asset ID for AVAX * @param toAddresses An array of addresses as {@link https://github.com/feross/buffer|Buffer} who recieves the AVAX * @param fromSigner The addresses being used to send and verify the funds from the UTXOs {@link https://github.com/feross/buffer|Buffer} * @param changeAddresses An array of addresses as {@link https://github.com/feross/buffer|Buffer} who gets the change leftover of the AVAX * @param destinationChain Optional. A {@link https://github.com/feross/buffer|Buffer} for the chainid where to send the asset. * @param fee Optional. The amount of fees to burn in its smallest denomination, represented as {@link https://github.com/indutny/bn.js/|BN} * @param feeAssetID Optional. The assetID of the fees being burned. * @param memo Optional contains arbitrary bytes, up to 256 bytes * @param asOf Optional. The timestamp to verify the transaction against as a {@link https://github.com/indutny/bn.js/|BN} * @param locktime Optional. The locktime field created in the resulting outputs * @param toThreshold Optional. The number of signatures required to spend the funds in the received UTXO * @param changeThreshold Optional. The number of signatures required to spend the funds in the resultant change UTXO * * @returns An unsigned transaction created from the passed in parameters. * */ this.buildExportTx = (networkID, blockchainID, amount, amountAssetID, toAddresses, fromSigner, destinationChain, changeAddresses = undefined, fee = zero, feeAssetID = undefined, memo = undefined, asOf = zero, locktime = zero, toThreshold = 1, changeThreshold = 1) => __awaiter(this, void 0, void 0, function* () { let ins = []; let outs = []; let owners = []; if (typeof changeAddresses === "undefined") { changeAddresses = toAddresses; } if (amount.eq(zero)) { return undefined; } if (typeof feeAssetID === "undefined") { feeAssetID = amountAssetID; } else if (feeAssetID.toString("hex") !== amountAssetID.toString("hex")) { /* istanbul ignore next */ throw new errors_1.FeeAssetError("Error - CaminoExecutor.buildExportTx: " + `feeAssetID must match avaxAssetID`); } const aad = new _1.AssetAmountDestination(toAddresses, toThreshold, fromSigner.from, fromSigner.signer, changeAddresses, changeThreshold); let singleAsset = true; if (amountAssetID.toString("hex") === feeAssetID.toString("hex")) { aad.addAssetAmount(amountAssetID, zero, fee.add(amount)); } else { singleAsset = false; aad.addAssetAmount(amountAssetID, amount, zero); if (this._feeCheck(fee, feeAssetID)) { aad.addAssetAmount(feeAssetID, zero, fee); } } const minSpendableErr = yield this.spender.getMinimumSpendable(aad, asOf, locktime, "Unlocked"); if (typeof minSpendableErr === "undefined") { ins = aad.getInputs(); outs = singleAsset ? aad.getAllOutputs() : aad.getChangeOutputs(); exports = singleAsset ? [] : aad.getOutputs(); owners = aad.getOutputOwners(); } else { throw minSpendableErr; } const baseTx = new _1.ExportTx(networkID, blockchainID, outs, ins, memo, destinationChain, exports.length > 0 ? exports : [ new _1.SECPTransferOutput(amount, toAddresses, locktime, toThreshold).makeTransferable(amountAssetID) ]); baseTx.setOutputOwners(owners); return new _1.UnsignedTx(baseTx); }); /** * Class representing an unsigned [[AddSubnetValidatorTx]] transaction. * * @param networkID Networkid, [[DefaultNetworkID]] * @param blockchainID Blockchainid, default undefined * @param fromSigner The addresses being used to send and verify the funds from the UTXOs {@link https://github.com/feross/buffer|Buffer} * @param changeAddresses An array of addresses as {@link https://github.com/feross/buffer|Buffer} who gets the change leftover from the fee payment * @param nodeID The node ID of the validator being added. * @param startTime The Unix time when the validator starts validating the Primary Network. * @param endTime The Unix time when the validator stops validating the Primary Network (and staked AVAX is returned). * @param weight The amount of weight for this subnet validator. * @param fee Optional. The amount of fees to burn in its smallest denomination, represented as {@link https://github.com/indutny/bn.js/|BN} * @param feeAssetID Optional. The assetID of the fees being burned. * @param memo Optional contains arbitrary bytes, up to 256 bytes * @param asOf Optional. The timestamp to verify the transaction against as a {@link https://github.com/indutny/bn.js/|BN} * @param subnetAuth Optional. An Auth struct which contains the subnet Auth and the signers. * @param changeThreshold Optional. The number of signatures required to spend the funds in the resultant change UTXO * * @returns An unsigned transaction created from the passed in parameters. */ this.buildAddSubnetValidatorTx = (networkID = utils_1.DefaultNetworkID, blockchainID, fromSigner, changeAddresses, nodeID, startTime, endTime, weight, subnetID, fee = zero, feeAssetID = undefined, memo = undefined, asOf = zero, subnetAuth = { addresses: [], threshold: 0, signer: [] }, changeThreshold = 1) => __awaiter(this, void 0, void 0, function* () { let ins = []; let outs = []; let owners = []; const now = (0, utils_1.UnixNow)(); if (startTime.lt(now) || endTime.lte(startTime)) { throw new Error("CaminoExecutor.buildAddSubnetValidatorTx -- startTime must be in the future and endTime must come after startTime"); } if (this._feeCheck(fee, feeAssetID)) { const aad = new _1.AssetAmountDestination([], 0, fromSigner.from, fromSigner.signer, changeAddresses, changeThreshold); aad.addAssetAmount(feeAssetID, zero, fee); const minSpendableErr = yield this.spender.getMinimumSpendable(aad, asOf, zero, this.caminoEnabled ? "Unlocked" : "Stake"); if (typeof minSpendableErr === "undefined") { ins = aad.getInputs(); outs = aad.getAllOutputs(); owners = aad.getOutputOwners(); } else { throw minSpendableErr; } } const baseTx = new _1.AddSubnetValidatorTx(networkID, blockchainID, outs, ins, memo, nodeID, startTime, endTime, weight, subnetID); subnetAuth.signer.forEach((subnetSigner) => { baseTx.addSignatureIdx(subnetSigner[0], subnetSigner[1]); }); // We need to fetch the AUTH for later msig verification // For now we use simply what we get in subnetAuth owners.push(new common_1.OutputOwners(subnetAuth.addresses, common_1.ZeroBN, subnetAuth.threshold)); if (this.caminoEnabled) { baseTx.includeNodeSignature(); owners.push(new common_1.OutputOwners([nodeID], common_1.ZeroBN, 1)); } baseTx.setOutputOwners(owners); return new _1.UnsignedTx(baseTx); }); /** * Class representing an unsigned [[AddDelegatorTx]] transaction. * * @param networkID Networkid, [[DefaultNetworkID]] * @param blockchainID Blockchainid, default undefined * @param avaxAssetID {@link https://github.com/feross/buffer|Buffer} of the asset ID for AVAX * @param toAddresses An array of addresses as {@link https://github.com/feross/buffer|Buffer} recieves the stake at the end of the staking period * @param fromSigner The addresses being used to send and verify the funds from the UTXOs {@link https://github.com/feross/buffer|Buffer} * @param changeAddresses An array of addresses as {@link https://github.com/feross/buffer|Buffer} who gets the change leftover from the staking payment * @param nodeID The node ID of the validator being added. * @param startTime The Unix time when the validator starts validating the Primary Network. * @param endTime The Unix time when the validator stops validating the Primary Network (and staked AVAX is returned). * @param stakeAmount A {@link https://github.com/indutny/bn.js/|BN} for the amount of stake to be delegated in nAVAX. * @param rewardLocktime The locktime field created in the resulting reward outputs * @param rewardThreshold The number of signatures required to spend the funds in the resultant reward UTXO * @param rewardAddresses The addresses the validator reward goes. * @param fee Optional. The amount of fees to burn in its smallest denomination, represented as {@link https://github.com/indutny/bn.js/|BN} * @param feeAssetID Optional. The assetID of the fees being burned. * @param memo Optional contains arbitrary bytes, up to 256 bytes * @param asOf Optional. The timestamp to verify the transaction against as a {@link https://github.com/indutny/bn.js/|BN} * @param toThreshold Optional. The number of signatures required to spend the funds in the stake UTXO * @param changeThreshold Optional. The number of signatures required to spend the funds in the change UTXO * * @returns An unsigned transaction created from the passed in parameters. */ this.buildAddDelegatorTx = (networkID = utils_1.DefaultNetworkID, blockchainID, avaxAssetID, toAddresses, fromSigner, changeAddresses, nodeID, startTime, endTime, stakeAmount, rewardLocktime, rewardThreshold, rewardAddresses, fee = zero, feeAssetID = undefined, memo = undefined, asOf = zero, toThreshold = 1, changeThreshold = 1) => __awaiter(this, void 0, void 0, function* () { if (this.caminoEnabled) { throw new errors_1.ProtocolError("buildAddDelegatorTx - Not supported in Camino mode"); } if (rewardThreshold > rewardAddresses.length) { /* istanbul ignore next */ throw new errors_1.ThresholdError("Error - UTXOSet.buildAddDelegatorTx: reward threshold is greater than number of addresses"); } if (typeof changeAddresses === "undefined") { changeAddresses = toAddresses; } let ins = []; let outs = []; let stakeOuts = []; let owners = []; const now = (0, utils_1.UnixNow)(); if (startTime.lt(now) || endTime.lte(startTime)) { throw new errors_1.TimeError("UTXOSet.buildAddDelegatorTx -- startTime must be in the future and endTime must come after startTime"); } const aad = new _1.AssetAmountDestination(toAddresses, toThreshold, fromSigner.from, fromSigner.signer, changeAddresses, changeThreshold); if (avaxAssetID.toString("hex") === feeAssetID.toString("hex")) { aad.addAssetAmount(avaxAssetID, stakeAmount, fee); } else { aad.addAssetAmount(avaxAssetID, stakeAmount, zero); if (this._feeCheck(fee, feeAssetID)) { aad.addAssetAmount(feeAssetID, zero, fee); } } const minSpendableErr = yield this.spender.getMinimumSpendable(aad, asOf, zero, "Stake"); if (typeof minSpendableErr === "undefined") { ins = aad.getInputs(); outs = aad.getChangeOutputs(); stakeOuts = aad.getOutputs(); owners = aad.getOutputOwners(); } else { throw minSpendableErr; } const rewardOutputOwners = new _1.SECPOwnerOutput(rewardAddresses, rewardLocktime, rewardThreshold); const baseTx = new _1.AddDelegatorTx(networkID, blockchainID, outs, ins, memo, nodeID, startTime, endTime, stakeAmount, stakeOuts, new _1.ParseableOutput(rewardOutputOwners)); baseTx.setOutputOwners(owners); return new _1.UnsignedTx(baseTx); }); /** * Class representing an unsigned [[AddValidatorTx]] transaction. * * @param networkID NetworkID, [[DefaultNetworkID]] * @param blockchainID BlockchainID, default undefined * @param avaxAssetID {@link https://github.com/feross/buffer|Buffer} of the asset ID for AVAX * @param toAddresses An array of addresses as {@link https://github.com/feross/buffer|Buffer} recieves the stake at the end of the staking period * @param fromSigner The addresses being used to send and verify the funds from the UTXOs {@link https://github.com/feross/buffer|Buffer} * @param changeAddresses An array of addresses as {@link https://github.com/feross/buffer|Buffer} who gets the change leftover from the staking payment * @param nodeID The node ID of the validator being added. * @param startTime The Unix time when the validator starts validating the Primary Network. * @param endTime The Unix time when the validator stops validating the Primary Network (and staked AVAX is returned). * @param stakeAmount A {@link https://github.com/indutny/bn.js/|BN} for the amount of stake to be delegated in nAVAX. * @param rewardLocktime The locktime field created in the resulting reward outputs * @param rewardThreshold The number of signatures required to spend the funds in the resultant reward UTXO * @param rewardAddresses The addresses the validator reward goes. * @param delegationFee A number for the percentage of reward to be given to the validator when someone delegates to them. Must be between 0 and 100. * @param minStake A {@link https://github.com/indutny/bn.js/|BN} representing the minimum stake required to validate on this network. * @param fee Optional. The amount of fees to burn in its smallest denomination, represented as {@link https://github.com/indutny/bn.js/|BN} * @param feeAssetID Optional. The assetID of the fees being burned. * @param memo Optional contains arbitrary bytes, up to 256 bytes * @param asOf Optional. The timestamp to verify the transaction against as a {@link https://github.com/indutny/bn.js/|BN} * @param toThreshold Optional. The number of signatures required to spend the funds in the stake UTXO * @param changeThreshold Optional. The number of signatures required to spend the funds in the change UTXO */ this.buildAddValidatorTx = (networkID = utils_1.DefaultNetworkID, blockchainID, toAddresses, fromSigner, changeAddresses, nodeID, startTime, endTime, stakeAmount, stakeAssetID, rewardLocktime, rewardThreshold, rewardAddresses, delegationFee, fee = zero, feeAssetID = undefined, memo = undefined, asOf = (0, utils_1.UnixNow)(), toThreshold = 1, changeThreshold = 1) => __awaiter(this, void 0, void 0, function* () { if (this.caminoEnabled) { throw new Error("Use buildCaminoAddValidatorTx"); } let ins = []; let outs = []; let stakeOuts = []; let owners = []; const now = (0, utils_1.UnixNow)(); if (startTime.lt(now) || endTime.lte(startTime)) { throw new errors_1.TimeError("UTXOSet.buildAddValidatorTx -- startTime must be in the future and endTime must come after startTime"); } if (delegationFee > 100 || delegationFee < 0) { throw new errors_1.TimeError("UTXOSet.buildAddValidatorTx -- startTime must be in the range of 0 to 100, inclusively"); } const aad = new _1.AssetAmountDestination(toAddresses, toThreshold, fromSigner.from, fromSigner.signer, changeAddresses, changeThreshold); if (stakeAssetID.toString("hex") === feeAssetID.toString("hex")) { aad.addAssetAmount(stakeAssetID, stakeAmount, fee); } else { aad.addAssetAmount(stakeAssetID, stakeAmount, zero); if (this._feeCheck(fee, feeAssetID)) { aad.addAssetAmount(feeAssetID, zero, fee); } } const minSpendableErr = yield this.spender.getMinimumSpendable(aad, asOf, zero, "Stake"); if (typeof minSpendableErr === "undefined") { ins = aad.getInputs(); outs = aad.getChangeOutputs(); stakeOuts = aad.getOutputs(); owners = aad.getOutputOwners(); } else { throw minSpendableErr; } const rewardOutputOwners = new _1.SECPOwnerOutput(rewardAddresses, rewardLocktime, rewardThreshold); const baseTx = new _1.AddValidatorTx(networkID, blockchainID, outs, ins, memo, nodeID, startTime, endTime, stakeAmount, stakeOuts, new _1.ParseableOutput(rewardOutputOwners), delegationFee); baseTx.setOutputOwners(owners); return new _1.UnsignedTx(baseTx); }); /** * Class representing an unsigned [[CreateSubnetTx]] transaction. * * @param networkID Networkid, [[DefaultNetworkID]] * @param blockchainID Blockchainid, default undefined * @param fromSigner The addresses being used to send and verify the funds from the UTXOs {@link https://github.com/feross/buffer|Buffer} * @param changeAddresses The addresses that can spend the change remaining from the spent UTXOs. * @param subnetOwnerAddresses An array of {@link https://github.com/feross/buffer|Buffer} for the addresses to add to a subnet * @param subnetOwnerThreshold The number of owners's signatures required to add a validator to the network * @param fee Optional. The amount of fees to burn in its smallest denomination, represented as {@link https://github.com/indutny/bn.js/|BN} * @param feeAssetID Optional. The assetID of the fees being burned * @param memo Optional contains arbitrary bytes, up to 256 bytes * @param asOf Optional. The timestamp to verify the transaction against as a {@link https://github.com/indutny/bn.js/|BN} * @param changeThreshold Optional. The number of signatures required to spend the funds in the resultant change UTXO * * @returns An unsigned transaction created from the passed in parameters. */ this.buildCreateSubnetTx = (networkID = utils_1.DefaultNetworkID, blockchainID, fromSigner, changeAddresses, subnetOwnerAddresses, subnetOwnerThreshold, fee = zero, feeAssetID = undefined, memo = undefined, asOf = zero, changeThreshold = 1) => __awaiter(this, void 0, void 0, function* () { let ins = []; let outs = []; let owners = []; if (this._feeCheck(fee, feeAssetID)) { const aad = new _1.AssetAmountDestination([], 0, fromSigner.from, fromSigner.signer, changeAddresses, changeThreshold); aad.addAssetAmount(feeAssetID, zero, fee); const minSpendableErr = yield this.spender.getMinimumSpendable(aad, asOf, zero, "Unlocked"); if (typeof minSpendableErr === "undefined") { ins = aad.getInputs(); outs = aad.getAllOutputs(); owners = aad.getOutputOwners(); } else { throw minSpendableErr; } } const locktime = new bn_js_1.default(0); const subnetOwners = new _1.SECPOwnerOutput(subnetOwnerAddresses, locktime, subnetOwnerThreshold); const baseTx = new _1.CreateSubnetTx(networkID, blockchainID, outs, ins, memo, subnetOwners); baseTx.setOutputOwners(owners); return new _1.UnsignedTx(baseTx); }); /** * Build an unsigned [[CreateChainTx]]. * * @param networkID Networkid, [[DefaultNetworkID]] * @param blockchainID Blockchainid, default undefined * @param fromSigner The addresses being used to send and verify the funds from the UTXOs {@link https://github.com/feross/buffer|Buffer} * @param changeAddresses The addresses that can spend the change remaining from the spent UTXOs. * @param subnetID Optional ID of the Subnet that validates this blockchain * @param chainName Optional A human readable name for the chain; need not be unique * @param vmID Optional ID of the VM running on the new chain * @param fxIDs Optional IDs of the feature extensions running on the new chain * @param genesisData Optional Byte representation of genesis state of the new chain * @param fee Optional. The amount of fees to burn in its smallest denomination, represented as {@link https://github.com/indutny/bn.js/|BN} * @param feeAssetID Optional. The assetID of the fees being burned * @param memo Optional contains arbitrary bytes, up to 256 bytes * @param asOf Optional. The timestamp to verify the transaction against as a {@link https://github.com/indutny/bn.js/|BN} * @param subnetAuth Optional. An Auth struct to sign for the Subnet. * @param changeThreshold Optional. The number of signatures required to spend the funds in the resultant change UTXO * * @returns An unsigned CreateChainTx created from the passed in parameters. */ this.buildCreateChainTx = (networkID = utils_1.DefaultNetworkID, blockchainID, fromSigner, changeAddresses, subnetID = undefined, chainName = undefined, vmID = undefined, fxIDs = undefined, genesisData = undefined, fee = zero, feeAssetID = undefined, memo = undefined, asOf = zero, subnetAuth = { addresses: [], threshold: 0, signer: [] }, changeThreshold = 1) => __awaiter(this, void 0, void 0, function* () { let ins = []; let outs = []; let owners = []; if (this._feeCheck(fee, feeAssetID)) { const aad = new _1.AssetAmountDestination([], 0, fromSigner.from, fromSigner.signer, changeAddresses, changeThreshold); aad.addAssetAmount(feeAssetID, zero, fee); const minSpendableErr = yield this.spender.getMinimumSpendable(aad, asOf, zero, "Unlocked"); if (typeof minSpendableErr === "undefined") { ins = aad.getInputs(); outs = aad.getAllOutputs(); owners = aad.getOutputOwners(); } else { throw minSpendableErr; } } const baseTx = new _1.CreateChainTx(networkID, blockchainID, outs, ins, memo, subnetID, chainName, vmID, fxIDs, genesisData); subnetAuth.signer.forEach((subnetAuthSigner) => { baseTx.addSignatureIdx(subnetAuthSigner[0], subnetAuthSigner[1]); }); // We need to fetch the AUTH for later msig verification // For now we use simply what we get in subnetAuth owners.push(new common_1.OutputOwners(subnetAuth.addresses, common_1.ZeroBN, subnetAuth.threshold)); baseTx.setOutputOwners(owners); return new _1.UnsignedTx(baseTx); }); /** * Helper function which creates an unsigned [[CaminoAddValidatorTx]]. For more granular control, you may create your own * [[UnsignedTx]] manually and import the [[CaminoAddValidatorTx]] class directly. * * @param networkID Networkid, [[DefaultNetworkID]] * @param blockchainID Blockchainid, default undefined * @param toAddresses An array of addresses as {@link https://github.com/feross/buffer|Buffer} who received the staked tokens at the end of the staking period * @param fromSigner The addresses being used to send and verify the funds from the UTXOs {@link https://github.com/feross/buffer|Buffer} * @param changeAddresses An array of addresses as {@link https://github.com/feross/buffer|Buffer} who gets the change leftover from the fee payment * @param nodeID The node ID of the validator being added. * @param nodeOwner The address and signature indices of the registered nodeId owner. * @param startTime The Unix time when the validator starts validating the Primary Network. * @param endTime The Unix time when the validator stops validating the Primary Network (and staked AVAX is returned). * @param stakeAmount The amount being delegated as a {@link https://github.com/indutny/bn.js/|BN} * @param rewardAddresses The addresses which will recieve the rewards from the delegated stake. * @param rewardLocktime Optional. The locktime field created in the resulting reward outputs * @param rewardThreshold Opional. The number of signatures required to spend the funds in the resultant reward UTXO. Default 1. * @param memo Optional contains arbitrary bytes, up to 256 bytes * @param asOf Optional. The timestamp to verify the transaction against as a {@link https://github.com/indutny/bn.js/|BN} * @param toThreshold Optional. The number of signatures required to spend the funds in the received UTXO * @param changeThreshold Optional. The number of signatures required to spend the funds in the resultant change UTXO * * @returns An unsigned transaction created from the passed in parameters. */ this.buildCaminoAddValidatorTx = (networkID = utils_1.DefaultNetworkID, blockchainID, fromSigner, change, nodeID, nodeOwner, startTime, endTime, stakeAmount, stakeAssetID, rewards, rewardLocktime = zero, rewardThreshold = 1, memo = undefined, asOf = zero, toThreshold = 1, changeThreshold = 1) => __awaiter(this, void 0, void 0, function* () { let ins = []; let outs = []; let owners = []; const now = (0, utils_1.UnixNow)(); if (startTime.lt(now) || endTime.lte(startTime)) { throw new errors_1.TimeError("buildCaminoAddValidatorTx -- startTime must be in the future and endTime must come after startTime"); } const aad = new _1.AssetAmountDestination([], toThreshold, fromSigner.from, fromSigner.signer, change, changeThreshold); aad.addAssetAmount(stakeAssetID, stakeAmount, new bn_js_1.default(0)); const minSpendableErr = yield this.spender.getMinimumSpendable(aad, asOf, zero, "Bond"); if (typeof minSpendableErr === "undefined") { ins = aad.getInputs(); outs = aad.getAllOutputs(); owners = aad.getOutputOwners(); } else { throw minSpendableErr; } const rewardOutputOwners = new _1.SECPOwnerOutput(rewards, rewardLocktime, rewardThreshold); const baseTx = new _1.CaminoAddValidatorTx(networkID, blockchainID, outs, ins, memo, nodeID, startTime, endTime, stakeAmount, new _1.ParseableOutput(rewardOutputOwners)); nodeOwner.auth.forEach((o) => { baseTx.addSignatureIdx(o[0], o[1]); }); owners.push(new common_1.OutputOwners([nodeOwner.address], common_1.ZeroBN, 1)); baseTx.setOutputOwners(owners); return new _1.UnsignedTx(baseTx); }); /** * Build an unsigned [[AddressStateTx]]. * * @param version Optional. Transaction version number, default 0. * @param networkID Networkid, [[DefaultNetworkID]] * @param blockchainID Blockchainid, default undefined * @param fromSigner The addresses being used to send and verify the funds from the UTXOs {@link https://github.com/feross/buffer|Buffer} * @param changeAddresses The addresses that can spend the change remaining from the spent UTXOs. * @param address The address to alter state. * @param state The state to set or remove on the given address * @param remove Optional. Flag if state should be applied or removed * @param fee Optional. The amount of fees to burn in its smallest denomination, represented as {@link https://github.com/indutny/bn.js/|BN} * @param feeAssetID Optional. The assetID of the fees being burned * @param memo Optional contains arbitrary bytes, up to 256 bytes * @param asOf Optional. The timestamp to verify the transaction against as a {@link https://github.com/indutny/bn.js/|BN} * @param changeThreshold Optional. The number of signatures required to spend the funds in the resultant change UTXO * * @returns An unsigned AddressStateTx created from the passed in parameters. */ this.buildAddressStateTx = (version = utils_1.DefaultTransactionVersionNumber, networkID = utils_1.DefaultNetworkID, blockchainID, fromSigner, changeAddresses, address, state, remove = false, fee = zero, feeAssetID = undefined, memo = undefined, asOf = zero, changeThreshold = 1, executorAddress = undefined, executorAuth = []) => __awaiter(this, void 0, void 0, function* () { let ins = []; let outs = []; let owners = []; if (this._feeCheck(fee, feeAssetID)) { const aad = new _1.AssetAmountDestination([], 0, fromSigner.from, fromSigner.signer, changeAddresses, changeThreshold); aad.addAssetAmount(feeAssetID, zero, fee); const minSpendableErr = yield this.spender.getMinimumSpendable(aad, asOf, zero, "Unlocked"); if (typeof minSpendableErr === "undefined") { ins = aad.getInputs(); outs = aad.getAllOutputs(); } else { throw minSpendableErr; } } const baseTx = new _1.AddressStateTx(version, networkID, blockchainID, outs, ins, memo, address, state, remove, executorAddress); if (executorAddress) { executorAuth.forEach((signer) => { baseTx.addSignatureIdx(signer[0], signer[1]); }); owners.push(new common_1.OutputOwners([executorAddress], common_1.ZeroBN, 1)); } baseTx.setOutputOwners(owners); return new _1.UnsignedTx(baseTx); }); /** * Build an unsigned [[RegisterNodeTx]]. * * @param networkID Networkid, [[DefaultNetworkID]] * @param blockchainID Blockchainid, default undefined * @param fromSigner The addresses being used to send and verify the funds from the UTXOs {@link https://github.com/feross/buffer|Buffer} * @param changeAddresses The addresses that can spend the change remaining from the spent UTXOs. * @param oldNodeID Optional. ID of the existing NodeID to replace or remove. * @param newNodeID Optional. ID of the newNodID to register address. * @param address The consortiumMemberAddress, single or multi-sig. * @param addressAuths An array of index and address to verify ownership of address. * @param fee Optional. The amount of fees to burn in its smallest denomination, represented as {@link https://github.com/indutny/bn.js/|BN} * @param feeAssetID Optional. The assetID of the fees being burned * @param memo Optional contains arbitrary bytes, up to 256 bytes * @param asOf Optional. The timestamp to verify the transaction against as a {@link https://github.com/indutny/bn.js/|BN} * @param changeThreshold Optional. The number of signatures required to spend the funds in the resultant change UTXO * * @returns An unsigned RegisterNodeTx created from the passed in parameters. */ this.buildRegisterNodeTx = (networkID = utils_1.DefaultNetworkID, blockchainID, fromSigner, changeAddresses, oldNodeID = undefined, newNodeID = undefined, address = undefined, addressAuths = [], fee = zero, feeAssetID = undefined, memo = undefined, asOf = zero, changeThreshold = 1) => __awaiter(this, void 0, void 0, function* () { let ins = []; let outs = []; let owners = []; if (this._feeCheck(fee, feeAssetID)) { const aad = new _1.AssetAmountDestination([], 0, fromSigner.from, fromSigner.signer, changeAddresses, changeThreshold); aad.addAssetAmount(feeAssetID, zero, fee); const minSpendableErr = yield this.spender.getMinimumSpendable(aad, asOf, zero, "Unlocked"); if (typeof minSpendableErr === "undefined") { ins = aad.getInputs(); outs = aad.getAllOutputs(); owners = aad.getOutputOwners(); } else { throw minSpendableErr; } } const baseTx = new _1.RegisterNodeTx(networkID, blockchainID, outs, ins, memo, oldNodeID, newNodeID, address); addressAuths.forEach((addressAuth) => { baseTx.addSignatureIdx(addressAuth[0], addressAuth[1]); }); owners.push(newNodeID && !oldNodeID ? new common_1.OutputOwners([newNodeID], common_1.ZeroBN, 1) : new common_1.OutputOwners()); owners.push(new common_1.OutputOwners([address], common_1.ZeroBN, 1)); baseTx.setOutputOwners(owners); return new _1.UnsignedTx(baseTx); }); /** * Build an unsigned [[DepositTx]]. * * @param version Optional. Transaction version number, default 0. * @param networkID Networkid, [[DefaultNetworkID]] * @param blockchainID Blockchainid, default undefined * @param fromSigner The addresses being used to send and verify the funds from the UTXOs {@link https://github.com/feross/buffer|Buffer} * @param changeAddresses The addresses that can spend the change remaining from the spent UTXOs. * @param depositOfferID ID of the deposit offer. * @param depositDuration Duration of the deposit * @param rewardsOwner Optional The owners of the reward. If omitted, all inputs must have the same owner * @param depositCreatorAddress Address that is authorized to create deposit with given offer. Could be empty, if offer owner is empty. * @param depositCreatorAuth Auth for deposit creator address * @param depositOfferOwnerSigs Signatures which recover to depositOfferOwner address(es) * @param depositOfferOwnerAuth Auth for deposit offer owner * @param fee Optional. The amount of fees to burn in its smallest denomination, represented as {@link https://github.com/indutny/bn.js/|BN} * @param feeAssetID Optional. The assetID of the fees being burned * @param memo Optional contains arbitrary bytes, up to 256 bytes * @param asOf Optional. The timestamp to verify the transaction against as a {@link https://github.com/indutny/bn.js/|BN} * @param changeThreshold Optional. The number of signatures required to spend the funds in the resultant change UTXO * * @returns An unsigned DepositTx created from the passed in parameters. */ this.buildDepositTx = (version = utils_1.DefaultTransactionVersionNumber, networkID = utils_1.DefaultNetworkID, blockchainID, fromSigner, changeAddresses, depositOfferID, depositDuration, rewardsOwner, depositCreatorAddress = undefined, depositCreatorAuth = [], depositOfferOwnerSigs = [], depositOfferOwnerAuth = [], fee = zero, feeAssetID = undefined, memo = undefined, asOf = zero, amountToLock, changeThreshold = 1, to = [], toThreshold = 0) => __awaiter(this, void 0, void 0, function* () { let ins = []; let outs = []; let owners = []; if (this._feeCheck(fee, feeAssetID)) { const aad = new _1.AssetAmountDestination(to, toThreshold, fromSigner.from, fromSigner.signer, changeAddresses, changeThreshold); aad.addAssetAmount(feeAssetID, amountToLock, fee); const minSpendableErr = yield this.spender.getMinimumSpendable(aad, asOf, zero, "Deposit"); if (typeof minSpendableErr === "undefined") { ins = aad.getInputs(); outs = aad.getAllOutputs(); owners = aad.getOutputOwners(); } else { throw minSpendableErr; } } const secpOwners = new _1.SECPOwnerOutput(rewardsOwner.getAddresses(), rewardsOwner === null || rewardsOwner === void 0 ? void 0 : rewardsOwner.getLocktime(), rewardsOwner.getThreshold()); const baseTx = new _1.DepositTx(version, networkID, blockchainID, outs, ins, memo, depositOfferID, depositDuration, new _1.ParseableOutput(secpOwners), depositCreatorAddress); if (version > 0) { baseTx.addDepositCreatorAuth(depositCreatorAuth); baseTx.addOwnerAuth(depositOfferOwnerAuth, depositOfferOwnerSigs); owners.push(new common_1.OutputOwners([depositCreatorAddress], common_1.ZeroBN, 1)); // Create pseudo addresses for Multisig, they have