|
| 1 | +import { createHash } from 'crypto'; |
| 2 | +import { TransactionType } from '@bitgo/sdk-core'; |
| 3 | +import { BaseCoin as CoinConfig } from '@bitgo/statics'; |
| 4 | + |
| 5 | +import { Transaction } from './transaction'; |
| 6 | +import { TransactionReceipt, ResourceManagementContract } from './iface'; |
| 7 | +import { UNDELEGATION_TYPE_URL } from './constants'; |
| 8 | +import { decodeTransaction, getByteArrayFromHexAddress, TRANSACTION_DEFAULT_EXPIRATION } from './utils'; |
| 9 | +import { protocol } from '../../resources/protobuf/tron'; |
| 10 | +import { ResourceManagementTxBuilder } from './resourceManagementTxBuilder'; |
| 11 | + |
| 12 | +import ContractType = protocol.Transaction.Contract.ContractType; |
| 13 | + |
| 14 | +export class UndelegateResourceTxBuilder extends ResourceManagementTxBuilder { |
| 15 | + constructor(_coinConfig: Readonly<CoinConfig>) { |
| 16 | + super(_coinConfig); |
| 17 | + } |
| 18 | + |
| 19 | + /** @inheritdoc */ |
| 20 | + protected get transactionType(): TransactionType { |
| 21 | + return TransactionType.UnDelegateResource; |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Initialize the transaction builder fields using the transaction data |
| 26 | + * |
| 27 | + * @param {TransactionReceipt | string} rawTransaction the transaction data in a string or JSON format |
| 28 | + * @returns {DelegateResourceTxBuilder} the builder with the transaction data set |
| 29 | + */ |
| 30 | + initBuilder(rawTransaction: TransactionReceipt | string): this { |
| 31 | + this.validateRawTransaction(rawTransaction); |
| 32 | + const tx = this.fromImplementation(rawTransaction); |
| 33 | + this.transaction = tx; |
| 34 | + this._signingKeys = []; |
| 35 | + const rawData = tx.toJson().raw_data; |
| 36 | + this._refBlockBytes = rawData.ref_block_bytes; |
| 37 | + this._refBlockHash = rawData.ref_block_hash; |
| 38 | + this._expiration = rawData.expiration; |
| 39 | + this._timestamp = rawData.timestamp; |
| 40 | + this.transaction.setTransactionType(TransactionType.UnDelegateResource); |
| 41 | + const contractCall = rawData.contract[0] as ResourceManagementContract; |
| 42 | + this.initResourceManagementContractCall(contractCall); |
| 43 | + return this; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Helper method to create the undelegate resource transaction |
| 48 | + */ |
| 49 | + protected createResourceManagementTransaction(): void { |
| 50 | + const rawDataHex = this.getResourceManagementTxRawDataHex(); |
| 51 | + const rawData = decodeTransaction(rawDataHex); |
| 52 | + const contract = rawData.contract[0] as ResourceManagementContract; |
| 53 | + const contractParameter = contract.parameter; |
| 54 | + contractParameter.value.owner_address = this._ownerAddress.toLocaleLowerCase(); |
| 55 | + contractParameter.value.balance = Number(this._balance); |
| 56 | + contractParameter.value.receiver_address = this._receiverAddress.toLocaleLowerCase(); |
| 57 | + contractParameter.value.resource = this._resource; |
| 58 | + contractParameter.type_url = UNDELEGATION_TYPE_URL; |
| 59 | + contract.type = 'UnDelegateResourceContract'; |
| 60 | + const hexBuffer = Buffer.from(rawDataHex, 'hex'); |
| 61 | + const id = createHash('sha256').update(hexBuffer).digest('hex'); |
| 62 | + const txReceipt: TransactionReceipt = { |
| 63 | + raw_data: rawData, |
| 64 | + raw_data_hex: rawDataHex, |
| 65 | + txID: id, |
| 66 | + signature: this.transaction.signature, |
| 67 | + }; |
| 68 | + this.transaction = new Transaction(this._coinConfig, txReceipt); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Helper method to get the undelegate resource transaction raw data hex |
| 73 | + * |
| 74 | + * @returns {string} the undelegate resource transaction raw data hex |
| 75 | + */ |
| 76 | + protected getResourceManagementTxRawDataHex(): string { |
| 77 | + const rawContract = { |
| 78 | + ownerAddress: getByteArrayFromHexAddress(this._ownerAddress), |
| 79 | + receiverAddress: getByteArrayFromHexAddress(this._receiverAddress), |
| 80 | + balance: this._balance, |
| 81 | + resource: this._resource, |
| 82 | + }; |
| 83 | + const undelegateResourceContract = protocol.UnDelegateResourceContract.fromObject(rawContract); |
| 84 | + const undelegateResourceContractBytes = |
| 85 | + protocol.UnDelegateResourceContract.encode(undelegateResourceContract).finish(); |
| 86 | + const txContract = { |
| 87 | + type: ContractType.UnDelegateResourceContract, |
| 88 | + parameter: { |
| 89 | + value: undelegateResourceContractBytes, |
| 90 | + type_url: UNDELEGATION_TYPE_URL, |
| 91 | + }, |
| 92 | + }; |
| 93 | + const raw = { |
| 94 | + refBlockBytes: Buffer.from(this._refBlockBytes, 'hex'), |
| 95 | + refBlockHash: Buffer.from(this._refBlockHash, 'hex'), |
| 96 | + expiration: this._expiration || Date.now() + TRANSACTION_DEFAULT_EXPIRATION, |
| 97 | + timestamp: this._timestamp || Date.now(), |
| 98 | + contract: [txContract], |
| 99 | + }; |
| 100 | + const rawTx = protocol.Transaction.raw.create(raw); |
| 101 | + return Buffer.from(protocol.Transaction.raw.encode(rawTx).finish()).toString('hex'); |
| 102 | + } |
| 103 | +} |
0 commit comments