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