|
| 1 | +import assert from 'assert'; |
| 2 | +import { Secp256k1, Transaction as VetTransaction } from '@vechain/sdk-core'; |
| 3 | + |
| 4 | +import { BaseCoin as CoinConfig } from '@bitgo/statics'; |
| 5 | +import { InvalidTransactionError, TransactionType } from '@bitgo/sdk-core'; |
| 6 | +import { Transaction } from './transaction'; |
| 7 | +import utils from '../utils'; |
| 8 | + |
| 9 | +import { VetTransactionData } from '../iface'; |
| 10 | + |
| 11 | +export class TokenTransaction extends Transaction { |
| 12 | + private _tokenAddress: string; |
| 13 | + |
| 14 | + constructor(_coinConfig: Readonly<CoinConfig>) { |
| 15 | + super(_coinConfig); |
| 16 | + this._type = TransactionType.Send; |
| 17 | + } |
| 18 | + |
| 19 | + get tokenAddress(): string { |
| 20 | + return this._tokenAddress; |
| 21 | + } |
| 22 | + |
| 23 | + set tokenAddress(address: string) { |
| 24 | + this._tokenAddress = address; |
| 25 | + } |
| 26 | + |
| 27 | + buildClauses(): void { |
| 28 | + if (!this.tokenAddress) { |
| 29 | + throw new Error('Token address is not set'); |
| 30 | + } |
| 31 | + this.clauses = this.recipients.map((recipient) => { |
| 32 | + const data = utils.getTransferTokenData(recipient.address, String(recipient.amount)); |
| 33 | + return { |
| 34 | + to: this.tokenAddress, |
| 35 | + value: '0x0', |
| 36 | + data, |
| 37 | + }; |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + toJson(): VetTransactionData { |
| 42 | + const json: VetTransactionData = { |
| 43 | + id: this.id, |
| 44 | + chainTag: this.chainTag, |
| 45 | + blockRef: this.blockRef, |
| 46 | + expiration: this.expiration, |
| 47 | + recipients: this.recipients, |
| 48 | + gasPriceCoef: this.gasPriceCoef, |
| 49 | + gas: this.gas, |
| 50 | + dependsOn: this.dependsOn, |
| 51 | + nonce: this.nonce, |
| 52 | + sender: this.sender, |
| 53 | + feePayer: this.feePayerAddress, |
| 54 | + tokenAddress: this.tokenAddress, |
| 55 | + }; |
| 56 | + |
| 57 | + return json; |
| 58 | + } |
| 59 | + |
| 60 | + fromDeserializedSignedTransaction(signedTx: VetTransaction): void { |
| 61 | + try { |
| 62 | + if (!signedTx || !signedTx.body) { |
| 63 | + throw new InvalidTransactionError('Invalid transaction: missing transaction body'); |
| 64 | + } |
| 65 | + |
| 66 | + // Store the raw transaction |
| 67 | + this.rawTransaction = signedTx; |
| 68 | + |
| 69 | + // Set transaction body properties |
| 70 | + const body = signedTx.body; |
| 71 | + this.chainTag = body.chainTag; |
| 72 | + this.blockRef = body.blockRef; |
| 73 | + this.expiration = body.expiration; |
| 74 | + this.clauses = body.clauses; |
| 75 | + this.gasPriceCoef = typeof body.gasPriceCoef === 'number' ? body.gasPriceCoef : 128; |
| 76 | + this.gas = Number(body.gas); |
| 77 | + this.dependsOn = body.dependsOn; |
| 78 | + this.nonce = String(body.nonce); |
| 79 | + // Set recipients from clauses |
| 80 | + assert(body.clauses[0].to, 'token address not found in the clauses'); |
| 81 | + this.tokenAddress = body.clauses[0].to; |
| 82 | + this.recipients = body.clauses.map((clause) => utils.decodeTransferTokenData(clause.data)); |
| 83 | + this.loadInputsAndOutputs(); |
| 84 | + |
| 85 | + // Set sender address |
| 86 | + if (signedTx.signature && signedTx.origin) { |
| 87 | + this.sender = signedTx.origin.toString().toLowerCase(); |
| 88 | + } |
| 89 | + |
| 90 | + // Set signatures if present |
| 91 | + if (signedTx.signature) { |
| 92 | + // First signature is sender's signature |
| 93 | + this.senderSignature = Buffer.from(signedTx.signature.slice(0, Secp256k1.SIGNATURE_LENGTH)); |
| 94 | + |
| 95 | + // If there's additional signature data, it's the fee payer's signature |
| 96 | + if (signedTx.signature.length > Secp256k1.SIGNATURE_LENGTH) { |
| 97 | + this.feePayerSignature = Buffer.from(signedTx.signature.slice(Secp256k1.SIGNATURE_LENGTH)); |
| 98 | + } |
| 99 | + } |
| 100 | + } catch (e) { |
| 101 | + throw new InvalidTransactionError(`Failed to deserialize transaction: ${e.message}`); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments