|
| 1 | +import { TransactionType, InvalidTransactionError } from '@bitgo/sdk-core'; |
| 2 | +import { BaseCoin as CoinConfig } from '@bitgo/statics'; |
| 3 | +import { Transaction as VetTransaction, Secp256k1 } from '@vechain/sdk-core'; |
| 4 | +import { Transaction } from './transaction'; |
| 5 | +import { VetTransactionData } from '../iface'; |
| 6 | +import EthereumAbi from 'ethereumjs-abi'; |
| 7 | +import utils from '../utils'; |
| 8 | +import BigNumber from 'bignumber.js'; |
| 9 | +import { addHexPrefix, BN } from 'ethereumjs-util'; |
| 10 | +import { ZERO_VALUE_AMOUNT } from '../constants'; |
| 11 | + |
| 12 | +export class ValidatorRegistrationTransaction extends Transaction { |
| 13 | + private _stakingContractAddress: string; |
| 14 | + private _validator: string; |
| 15 | + private _stakingPeriod: number; |
| 16 | + |
| 17 | + constructor(_coinConfig: Readonly<CoinConfig>) { |
| 18 | + super(_coinConfig); |
| 19 | + this._type = TransactionType.StakingLock; |
| 20 | + } |
| 21 | + |
| 22 | + get validator(): string { |
| 23 | + return this._validator; |
| 24 | + } |
| 25 | + |
| 26 | + set validator(address: string) { |
| 27 | + this._validator = address; |
| 28 | + } |
| 29 | + |
| 30 | + get stakingPeriod(): number { |
| 31 | + return this._stakingPeriod; |
| 32 | + } |
| 33 | + |
| 34 | + set stakingPeriod(period: number) { |
| 35 | + this._stakingPeriod = period; |
| 36 | + } |
| 37 | + |
| 38 | + get stakingContractAddress(): string { |
| 39 | + return this._stakingContractAddress; |
| 40 | + } |
| 41 | + |
| 42 | + set stakingContractAddress(address: string) { |
| 43 | + this._stakingContractAddress = address; |
| 44 | + } |
| 45 | + |
| 46 | + buildClauses(): void { |
| 47 | + if (!this.stakingContractAddress) { |
| 48 | + throw new Error('Staking contract address is not set'); |
| 49 | + } |
| 50 | + |
| 51 | + if (!this.validator) { |
| 52 | + throw new Error('Validator address is not set'); |
| 53 | + } |
| 54 | + |
| 55 | + if (!this.stakingPeriod) { |
| 56 | + throw new Error('Staking period is not set'); |
| 57 | + } |
| 58 | + |
| 59 | + utils.validateContractAddressForValidatorRegistration(this.stakingContractAddress, this._coinConfig); |
| 60 | + const addValidationData = this.getAddValidationClauseData(this.validator, this.stakingPeriod); |
| 61 | + this._transactionData = addValidationData; |
| 62 | + // Create the clause for delegation |
| 63 | + this._clauses = [ |
| 64 | + { |
| 65 | + to: this.stakingContractAddress, |
| 66 | + value: ZERO_VALUE_AMOUNT, |
| 67 | + data: addValidationData, |
| 68 | + }, |
| 69 | + ]; |
| 70 | + |
| 71 | + // Set recipients based on the clauses |
| 72 | + this._recipients = [ |
| 73 | + { |
| 74 | + address: this.stakingContractAddress, |
| 75 | + amount: ZERO_VALUE_AMOUNT, |
| 76 | + }, |
| 77 | + ]; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Encodes addValidation transaction data using ethereumjs-abi for addValidation method |
| 82 | + * @param {string} validator - address of the validator |
| 83 | + * @param {number} period - staking period, denoted in blocks, that the Validator commits to hard |
| 84 | +locking their VET into the built-in staker contract. Allowed values are 60480 (7 days), |
| 85 | +129600 (15 days) or 259200 (30 days) |
| 86 | + * @returns {string} - The encoded transaction data |
| 87 | + */ |
| 88 | + getAddValidationClauseData(validator: string, period: number): string { |
| 89 | + const methodName = 'addValidation'; |
| 90 | + const types = ['address', 'uint32']; |
| 91 | + const params = [validator, new BN(period)]; |
| 92 | + |
| 93 | + const method = EthereumAbi.methodID(methodName, types); |
| 94 | + const args = EthereumAbi.rawEncode(types, params); |
| 95 | + |
| 96 | + return addHexPrefix(Buffer.concat([method, args]).toString('hex')); |
| 97 | + } |
| 98 | + |
| 99 | + toJson(): VetTransactionData { |
| 100 | + const json: VetTransactionData = { |
| 101 | + id: this.id, |
| 102 | + chainTag: this.chainTag, |
| 103 | + blockRef: this.blockRef, |
| 104 | + expiration: this.expiration, |
| 105 | + gasPriceCoef: this.gasPriceCoef, |
| 106 | + gas: this.gas, |
| 107 | + dependsOn: this.dependsOn, |
| 108 | + nonce: this.nonce, |
| 109 | + data: this.transactionData, |
| 110 | + value: ZERO_VALUE_AMOUNT, |
| 111 | + sender: this.sender, |
| 112 | + to: this.stakingContractAddress, |
| 113 | + stakingContractAddress: this.stakingContractAddress, |
| 114 | + amountToStake: ZERO_VALUE_AMOUNT, |
| 115 | + validatorAddress: this.validator, |
| 116 | + stakingPeriod: this.stakingPeriod, |
| 117 | + }; |
| 118 | + |
| 119 | + return json; |
| 120 | + } |
| 121 | + |
| 122 | + fromDeserializedSignedTransaction(signedTx: VetTransaction): void { |
| 123 | + try { |
| 124 | + if (!signedTx || !signedTx.body) { |
| 125 | + throw new InvalidTransactionError('Invalid transaction: missing transaction body'); |
| 126 | + } |
| 127 | + |
| 128 | + // Store the raw transaction |
| 129 | + this.rawTransaction = signedTx; |
| 130 | + |
| 131 | + // Set transaction body properties |
| 132 | + const body = signedTx.body; |
| 133 | + this.chainTag = typeof body.chainTag === 'number' ? body.chainTag : 0; |
| 134 | + this.blockRef = body.blockRef || '0x0'; |
| 135 | + this.expiration = typeof body.expiration === 'number' ? body.expiration : 64; |
| 136 | + this.clauses = body.clauses || []; |
| 137 | + this.gasPriceCoef = typeof body.gasPriceCoef === 'number' ? body.gasPriceCoef : 128; |
| 138 | + this.gas = typeof body.gas === 'number' ? body.gas : Number(body.gas) || 0; |
| 139 | + this.dependsOn = body.dependsOn || null; |
| 140 | + this.nonce = String(body.nonce); |
| 141 | + |
| 142 | + // Set validator registration-specific properties |
| 143 | + if (body.clauses.length > 0) { |
| 144 | + // Get the addValidation clause |
| 145 | + const addValidationClause = body.clauses[0]; |
| 146 | + if (addValidationClause.to) { |
| 147 | + this.stakingContractAddress = addValidationClause.to; |
| 148 | + } |
| 149 | + |
| 150 | + // Extract validator and period from addValidation data |
| 151 | + if (addValidationClause.data) { |
| 152 | + this.transactionData = addValidationClause.data; |
| 153 | + const decoded = utils.decodeAddValidationData(addValidationClause.data); |
| 154 | + this.validator = decoded.validator; |
| 155 | + this.stakingPeriod = decoded.period; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + // Set recipients from clauses |
| 160 | + this.recipients = body.clauses.map((clause) => ({ |
| 161 | + address: (clause.to || '0x0').toString().toLowerCase(), |
| 162 | + amount: new BigNumber(clause.value || 0).toString(), |
| 163 | + })); |
| 164 | + this.loadInputsAndOutputs(); |
| 165 | + |
| 166 | + // Set sender address |
| 167 | + if (signedTx.signature && signedTx.origin) { |
| 168 | + this.sender = signedTx.origin.toString().toLowerCase(); |
| 169 | + } |
| 170 | + |
| 171 | + // Set signatures if present |
| 172 | + if (signedTx.signature) { |
| 173 | + // First signature is sender's signature |
| 174 | + this.senderSignature = Buffer.from(signedTx.signature.slice(0, Secp256k1.SIGNATURE_LENGTH)); |
| 175 | + |
| 176 | + // If there's additional signature data, it's the fee payer's signature |
| 177 | + if (signedTx.signature.length > Secp256k1.SIGNATURE_LENGTH) { |
| 178 | + this.feePayerSignature = Buffer.from(signedTx.signature.slice(Secp256k1.SIGNATURE_LENGTH)); |
| 179 | + } |
| 180 | + } |
| 181 | + } catch (e) { |
| 182 | + throw new InvalidTransactionError(`Failed to deserialize transaction: ${e.message}`); |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments