|
| 1 | +import { TransactionType, InvalidTransactionError } from '@bitgo/sdk-core'; |
| 2 | +import { getCreateForwarderParamsAndTypes, calculateForwarderV1Address } from '@bitgo/abstract-eth'; |
| 3 | +import { BaseCoin as CoinConfig } from '@bitgo/statics'; |
| 4 | +import * as ethUtil from 'ethereumjs-util'; |
| 5 | +import EthereumAbi from 'ethereumjs-abi'; |
| 6 | +import { Transaction as VetTransaction, Secp256k1 } from '@vechain/sdk-core'; |
| 7 | +import { Transaction } from './transaction'; |
| 8 | +import { VetTransactionData } from '../iface'; |
| 9 | + |
| 10 | +export class AddressInitializationTransaction extends Transaction { |
| 11 | + private _baseAddress: string; |
| 12 | + private _feeAddress: string; |
| 13 | + private _salt: string; |
| 14 | + private _initCode: string; |
| 15 | + private _transactionData: string; |
| 16 | + private _deployedAddress: string; |
| 17 | + |
| 18 | + constructor(_coinConfig: Readonly<CoinConfig>) { |
| 19 | + super(_coinConfig); |
| 20 | + this._type = TransactionType.AddressInitialization; |
| 21 | + } |
| 22 | + |
| 23 | + get baseAddress(): string { |
| 24 | + return this._baseAddress; |
| 25 | + } |
| 26 | + |
| 27 | + set baseAddress(address: string) { |
| 28 | + this._baseAddress = address; |
| 29 | + } |
| 30 | + |
| 31 | + get feeAddress(): string { |
| 32 | + return this._feeAddress; |
| 33 | + } |
| 34 | + |
| 35 | + set feeAddress(address: string) { |
| 36 | + this._feeAddress = address; |
| 37 | + } |
| 38 | + |
| 39 | + get salt(): string { |
| 40 | + return this._salt; |
| 41 | + } |
| 42 | + |
| 43 | + set salt(salt: string) { |
| 44 | + this._salt = salt; |
| 45 | + } |
| 46 | + |
| 47 | + get initCode(): string { |
| 48 | + return this._initCode; |
| 49 | + } |
| 50 | + |
| 51 | + set initCode(initCode: string) { |
| 52 | + this._initCode = initCode; |
| 53 | + } |
| 54 | + |
| 55 | + get transactionData(): string { |
| 56 | + return this._transactionData; |
| 57 | + } |
| 58 | + |
| 59 | + set transactionData(transactionData: string) { |
| 60 | + this._transactionData = transactionData; |
| 61 | + } |
| 62 | + |
| 63 | + get deployedAddress(): string { |
| 64 | + return this._deployedAddress; |
| 65 | + } |
| 66 | + |
| 67 | + set deployedAddress(address: string) { |
| 68 | + this._deployedAddress = address; |
| 69 | + } |
| 70 | + |
| 71 | + /** @inheritdoc */ |
| 72 | + async build(): Promise<void> { |
| 73 | + super.build(); |
| 74 | + |
| 75 | + if (this._salt && this._initCode) { |
| 76 | + const saltBuffer = ethUtil.setLengthLeft(ethUtil.toBuffer(this._salt), 32); |
| 77 | + |
| 78 | + const { createForwarderParams, createForwarderTypes } = getCreateForwarderParamsAndTypes( |
| 79 | + this._baseAddress, |
| 80 | + saltBuffer, |
| 81 | + this._feeAddress |
| 82 | + ); |
| 83 | + |
| 84 | + // Hash the wallet base address and fee address if present with the given salt, so the address directly relies on the base address and fee address |
| 85 | + const calculationSalt = ethUtil.bufferToHex( |
| 86 | + EthereumAbi.soliditySHA3(createForwarderTypes, createForwarderParams) |
| 87 | + ); |
| 88 | + this.deployedAddress = calculateForwarderV1Address(this._contract, calculationSalt, this._initCode); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** @inheritdoc */ |
| 93 | + buildClauses(): void { |
| 94 | + this._clauses = [ |
| 95 | + { |
| 96 | + to: this._contract, |
| 97 | + value: '0x0', |
| 98 | + data: this._transactionData, |
| 99 | + }, |
| 100 | + ]; |
| 101 | + } |
| 102 | + |
| 103 | + /** @inheritdoc */ |
| 104 | + toJson(): VetTransactionData { |
| 105 | + const json: VetTransactionData = { |
| 106 | + id: this.id, |
| 107 | + chainTag: this.chainTag, |
| 108 | + blockRef: this.blockRef, |
| 109 | + expiration: this.expiration, |
| 110 | + gasPriceCoef: this.gasPriceCoef, |
| 111 | + gas: this.gas, |
| 112 | + dependsOn: this.dependsOn, |
| 113 | + nonce: this.nonce, |
| 114 | + data: this.transactionData, |
| 115 | + value: '0', |
| 116 | + sender: this.sender, |
| 117 | + to: this.contract, |
| 118 | + deployedAddress: this.deployedAddress, |
| 119 | + }; |
| 120 | + return json; |
| 121 | + } |
| 122 | + |
| 123 | + /** @inheritdoc */ |
| 124 | + fromDeserializedSignedTransaction(signedTx: VetTransaction): void { |
| 125 | + try { |
| 126 | + if (!signedTx || !signedTx.body) { |
| 127 | + throw new InvalidTransactionError('Invalid transaction: missing transaction body'); |
| 128 | + } |
| 129 | + |
| 130 | + // Store the raw transaction |
| 131 | + this.rawTransaction = signedTx; |
| 132 | + |
| 133 | + // Set transaction body properties |
| 134 | + const body = signedTx.body; |
| 135 | + this.chainTag = typeof body.chainTag === 'number' ? body.chainTag : 0; |
| 136 | + this.blockRef = body.blockRef || '0x0'; |
| 137 | + this.expiration = typeof body.expiration === 'number' ? body.expiration : 64; |
| 138 | + this.clauses = body.clauses || []; |
| 139 | + this.gasPriceCoef = typeof body.gasPriceCoef === 'number' ? body.gasPriceCoef : 128; |
| 140 | + this.gas = typeof body.gas === 'number' ? body.gas : Number(body.gas) || 0; |
| 141 | + this.dependsOn = body.dependsOn || null; |
| 142 | + this.nonce = typeof body.nonce === 'number' ? body.nonce : Number(body.nonce) || 0; |
| 143 | + // Set recipients from clauses |
| 144 | + this.contract = body.clauses[0]?.to || '0x0'; |
| 145 | + this.transactionData = body.clauses[0]?.data || '0x0'; |
| 146 | + |
| 147 | + // Set sender address |
| 148 | + if (signedTx.origin) { |
| 149 | + this.sender = signedTx.origin.toString().toLowerCase(); |
| 150 | + } |
| 151 | + |
| 152 | + // Set signatures if present |
| 153 | + if (signedTx.signature) { |
| 154 | + // First signature is sender's signature |
| 155 | + this.senderSignature = Buffer.from(signedTx.signature.slice(0, Secp256k1.SIGNATURE_LENGTH)); |
| 156 | + |
| 157 | + // If there's additional signature data, it's the fee payer's signature |
| 158 | + if (signedTx.signature.length > Secp256k1.SIGNATURE_LENGTH) { |
| 159 | + this.feePayerSignature = Buffer.from(signedTx.signature.slice(Secp256k1.SIGNATURE_LENGTH)); |
| 160 | + } |
| 161 | + } |
| 162 | + } catch (e) { |
| 163 | + throw new InvalidTransactionError(`Failed to deserialize transaction: ${e.message}`); |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments