/** *** Copyright (c) 2016-2019, Jaguar0625, gimre, BloodyRookie, Tech Bureau, Corp. *** Copyright (c) 2020-present, Jaguar0625, gimre, BloodyRookie. *** All rights reserved. *** *** This file is part of Catapult. *** *** Catapult is free software: you can redistribute it and/or modify *** it under the terms of the GNU Lesser General Public License as published by *** the Free Software Foundation, either version 3 of the License, or *** (at your option) any later version. *** *** Catapult is distributed in the hope that it will be useful, *** but WITHOUT ANY WARRANTY; without even the implied warranty of *** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *** GNU Lesser General Public License for more details. *** *** You should have received a copy of the GNU Lesser General Public License *** along with Catapult. If not, see . **/ import { AmountDto } from './AmountDto'; import { BlockDurationDto } from './BlockDurationDto'; import { GeneratorUtils } from './GeneratorUtils'; import { Hash256Dto } from './Hash256Dto'; import { HashLockTransactionBodyBuilder } from './HashLockTransactionBodyBuilder'; import { NetworkTypeDto } from './NetworkTypeDto'; import { PublicKeyDto } from './PublicKeyDto'; import { Serializer } from './Serializer'; import { SignatureDto } from './SignatureDto'; import { TimestampDto } from './TimestampDto'; import { TransactionBuilder } from './TransactionBuilder'; import { TransactionTypeDto } from './TransactionTypeDto'; import { UnresolvedMosaicBuilder } from './UnresolvedMosaicBuilder'; /** * Lock a deposit needed to announce an AggregateBondedTransaction. An AggregateBondedTransaction consumes network resources as it is stored in every node's partial cache while it waits to be fully signed. To avoid spam attacks a HashLockTransaction must be announced and confirmed before an AggregateBondedTransaction can be announced. The HashLockTransaction locks a certain amount of funds (**10** XYM by default) until the aggregate is signed. Upon completion of the aggregate, the locked funds become available again to the account that signed the HashLockTransaction. If the lock expires before the aggregate is signed by all cosignatories (**48h by default), the locked funds become a reward collected by the block harvester at the height where the lock expires. \note It is not necessary to sign the aggregate and its HashLockTransaction with the same account. For example, if Bob wants to announce an aggregate and does not have enough funds to announce a HashLockTransaction, he can ask Alice to announce the lock transaction for him by sharing the signed AggregateTransaction hash. **/ export class HashLockTransactionBuilder extends TransactionBuilder implements Serializer { /** Hash lock transaction body. **/ readonly hashLockTransactionBody: HashLockTransactionBodyBuilder; /** * Constructor. * * @param signature Entity's signature generated by the signing account.. * @param signerPublicKey Public key of the signer of the entity.. * @param version Version of this structure.. * @param network Network on which this entity was created.. * @param type Transaction type. * @param fee Transaction fee. * @param deadline Transaction deadline. * @param mosaic Locked mosaic.. * @param duration Number of blocks for which a lock should be valid. The default maximum is 48h (See the `maxHashLockDuration` network property).. * @param hash Hash of the AggregateBondedTransaction to be confirmed before unlocking the mosaics.. */ public constructor( signature: SignatureDto, signerPublicKey: PublicKeyDto, version: number, network: NetworkTypeDto, type: TransactionTypeDto, fee: AmountDto, deadline: TimestampDto, mosaic: UnresolvedMosaicBuilder, duration: BlockDurationDto, hash: Hash256Dto, ) { super(signature, signerPublicKey, version, network, type, fee, deadline); this.hashLockTransactionBody = new HashLockTransactionBodyBuilder(mosaic, duration, hash); } /** * Load from binary array - Creates an object from payload. * * @param payload - Byte payload to use to serialize the object. */ public static loadFromBinary(payload: Uint8Array): HashLockTransactionBuilder { const byteArray = Array.from(payload); const superObject = TransactionBuilder.loadFromBinary(payload); byteArray.splice(0, superObject.getSize()); const hashLockTransactionBody: HashLockTransactionBodyBuilder = HashLockTransactionBodyBuilder.loadFromBinary( Uint8Array.from(byteArray), ); byteArray.splice(0, hashLockTransactionBody.getSize()); return new HashLockTransactionBuilder( superObject.signature, superObject.signerPublicKey, superObject.version, superObject.network, superObject.type, superObject.fee, superObject.deadline, hashLockTransactionBody.mosaic, hashLockTransactionBody.duration, hashLockTransactionBody.hash, ); } /** * Creates an instance of HashLockTransactionBuilder. * * @param signature Entity's signature generated by the signing account.. * @param signerPublicKey Public key of the signer of the entity.. * @param version Version of this structure.. * @param network Network on which this entity was created.. * @param type Transaction type. * @param fee Transaction fee. * @param deadline Transaction deadline. * @param mosaic Locked mosaic.. * @param duration Number of blocks for which a lock should be valid. The default maximum is 48h (See the `maxHashLockDuration` network property).. * @param hash Hash of the AggregateBondedTransaction to be confirmed before unlocking the mosaics.. * @return Instance of HashLockTransactionBuilder. */ public static createHashLockTransactionBuilder( signature: SignatureDto, signerPublicKey: PublicKeyDto, version: number, network: NetworkTypeDto, type: TransactionTypeDto, fee: AmountDto, deadline: TimestampDto, mosaic: UnresolvedMosaicBuilder, duration: BlockDurationDto, hash: Hash256Dto, ): HashLockTransactionBuilder { return new HashLockTransactionBuilder(signature, signerPublicKey, version, network, type, fee, deadline, mosaic, duration, hash); } /** * Gets Locked mosaic.. * * @return Locked mosaic.. */ public getMosaic(): UnresolvedMosaicBuilder { return this.hashLockTransactionBody.getMosaic(); } /** * Gets Number of blocks for which a lock should be valid. The default maximum is 48h (See the `maxHashLockDuration` network property).. * * @return Number of blocks for which a lock should be valid. The default maximum is 48h (See the `maxHashLockDuration` network property).. */ public getDuration(): BlockDurationDto { return this.hashLockTransactionBody.getDuration(); } /** * Gets Hash of the AggregateBondedTransaction to be confirmed before unlocking the mosaics.. * * @return Hash of the AggregateBondedTransaction to be confirmed before unlocking the mosaics.. */ public getHash(): Hash256Dto { return this.hashLockTransactionBody.getHash(); } /** * Gets the size of the object. * * @return Size in bytes. */ public getSize(): number { let size = super.getSize(); size += this.hashLockTransactionBody.getSize(); // hashLockTransactionBody return size; } /** * Gets the body builder of the object. * * @return Body builder. */ public getBody(): HashLockTransactionBodyBuilder { return this.hashLockTransactionBody; } /** * Serializes an object to bytes. * * @return Serialized bytes. */ public serialize(): Uint8Array { let newArray = Uint8Array.from([]); const superBytes = super.serialize(); newArray = GeneratorUtils.concatTypedArrays(newArray, superBytes); const hashLockTransactionBodyBytes = this.hashLockTransactionBody.serialize(); newArray = GeneratorUtils.concatTypedArrays(newArray, hashLockTransactionBodyBytes); return newArray; } }