/** *** 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 { LockHashAlgorithmDto } from './LockHashAlgorithmDto'; import { NetworkTypeDto } from './NetworkTypeDto'; import { PublicKeyDto } from './PublicKeyDto'; import { SecretLockTransactionBodyBuilder } from './SecretLockTransactionBodyBuilder'; import { Serializer } from './Serializer'; import { SignatureDto } from './SignatureDto'; import { TimestampDto } from './TimestampDto'; import { TransactionBuilder } from './TransactionBuilder'; import { TransactionTypeDto } from './TransactionTypeDto'; import { UnresolvedAddressDto } from './UnresolvedAddressDto'; import { UnresolvedMosaicBuilder } from './UnresolvedMosaicBuilder'; /** * Start a token swap between different chains. Use a SecretLockTransaction to transfer mosaics between two accounts. The mosaics sent remain locked until a valid SecretProofTransaction unlocks them. The default expiration date is **365 days** after announcement (See the `maxSecretLockDuration` network property). If the lock expires before a valid SecretProofTransaction is announced the locked amount goes back to the initiator of the SecretLockTransaction. **/ export class SecretLockTransactionBuilder extends TransactionBuilder implements Serializer { /** Secret lock transaction body. **/ readonly secretLockTransactionBody: SecretLockTransactionBodyBuilder; /** * 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 recipientAddress Address that receives the funds once successfully unlocked by a SecretProofTransaction.. * @param secret Hashed proof.. * @param mosaic Locked mosaics.. * @param duration Number of blocks to wait for the SecretProofTransaction.. * @param hashAlgorithm Algorithm used to hash the proof.. */ public constructor( signature: SignatureDto, signerPublicKey: PublicKeyDto, version: number, network: NetworkTypeDto, type: TransactionTypeDto, fee: AmountDto, deadline: TimestampDto, recipientAddress: UnresolvedAddressDto, secret: Hash256Dto, mosaic: UnresolvedMosaicBuilder, duration: BlockDurationDto, hashAlgorithm: LockHashAlgorithmDto, ) { super(signature, signerPublicKey, version, network, type, fee, deadline); this.secretLockTransactionBody = new SecretLockTransactionBodyBuilder(recipientAddress, secret, mosaic, duration, hashAlgorithm); } /** * Load from binary array - Creates an object from payload. * * @param payload - Byte payload to use to serialize the object. */ public static loadFromBinary(payload: Uint8Array): SecretLockTransactionBuilder { const byteArray = Array.from(payload); const superObject = TransactionBuilder.loadFromBinary(payload); byteArray.splice(0, superObject.getSize()); const secretLockTransactionBody: SecretLockTransactionBodyBuilder = SecretLockTransactionBodyBuilder.loadFromBinary( Uint8Array.from(byteArray), ); byteArray.splice(0, secretLockTransactionBody.getSize()); return new SecretLockTransactionBuilder( superObject.signature, superObject.signerPublicKey, superObject.version, superObject.network, superObject.type, superObject.fee, superObject.deadline, secretLockTransactionBody.recipientAddress, secretLockTransactionBody.secret, secretLockTransactionBody.mosaic, secretLockTransactionBody.duration, secretLockTransactionBody.hashAlgorithm, ); } /** * Creates an instance of SecretLockTransactionBuilder. * * @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 recipientAddress Address that receives the funds once successfully unlocked by a SecretProofTransaction.. * @param secret Hashed proof.. * @param mosaic Locked mosaics.. * @param duration Number of blocks to wait for the SecretProofTransaction.. * @param hashAlgorithm Algorithm used to hash the proof.. * @return Instance of SecretLockTransactionBuilder. */ public static createSecretLockTransactionBuilder( signature: SignatureDto, signerPublicKey: PublicKeyDto, version: number, network: NetworkTypeDto, type: TransactionTypeDto, fee: AmountDto, deadline: TimestampDto, recipientAddress: UnresolvedAddressDto, secret: Hash256Dto, mosaic: UnresolvedMosaicBuilder, duration: BlockDurationDto, hashAlgorithm: LockHashAlgorithmDto, ): SecretLockTransactionBuilder { return new SecretLockTransactionBuilder( signature, signerPublicKey, version, network, type, fee, deadline, recipientAddress, secret, mosaic, duration, hashAlgorithm, ); } /** * Gets Address that receives the funds once successfully unlocked by a SecretProofTransaction.. * * @return Address that receives the funds once successfully unlocked by a SecretProofTransaction.. */ public getRecipientAddress(): UnresolvedAddressDto { return this.secretLockTransactionBody.getRecipientAddress(); } /** * Gets Hashed proof.. * * @return Hashed proof.. */ public getSecret(): Hash256Dto { return this.secretLockTransactionBody.getSecret(); } /** * Gets Locked mosaics.. * * @return Locked mosaics.. */ public getMosaic(): UnresolvedMosaicBuilder { return this.secretLockTransactionBody.getMosaic(); } /** * Gets Number of blocks to wait for the SecretProofTransaction.. * * @return Number of blocks to wait for the SecretProofTransaction.. */ public getDuration(): BlockDurationDto { return this.secretLockTransactionBody.getDuration(); } /** * Gets Algorithm used to hash the proof.. * * @return Algorithm used to hash the proof.. */ public getHashAlgorithm(): LockHashAlgorithmDto { return this.secretLockTransactionBody.getHashAlgorithm(); } /** * Gets the size of the object. * * @return Size in bytes. */ public getSize(): number { let size = super.getSize(); size += this.secretLockTransactionBody.getSize(); // secretLockTransactionBody return size; } /** * Gets the body builder of the object. * * @return Body builder. */ public getBody(): SecretLockTransactionBodyBuilder { return this.secretLockTransactionBody; } /** * 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 secretLockTransactionBodyBytes = this.secretLockTransactionBody.serialize(); newArray = GeneratorUtils.concatTypedArrays(newArray, secretLockTransactionBodyBytes); return newArray; } }