/** *** 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 { GeneratorUtils } from './GeneratorUtils'; import { NetworkTypeDto } from './NetworkTypeDto'; import { PublicKeyDto } from './PublicKeyDto'; import { Serializer } from './Serializer'; import { SignatureDto } from './SignatureDto'; import { TimestampDto } from './TimestampDto'; import { TransactionTypeDto } from './TransactionTypeDto'; /** * Binary layout for a transaction **/ export class TransactionBuilder implements Serializer { /** Entity's signature generated by the signing account.. **/ readonly signature: SignatureDto; /** Public key of the signer of the entity.. **/ readonly signerPublicKey: PublicKeyDto; /** Version of this structure.. **/ readonly version: number; /** Network on which this entity was created.. **/ readonly network: NetworkTypeDto; /** Transaction type. **/ readonly type: TransactionTypeDto; /** Transaction fee. **/ readonly fee: AmountDto; /** Transaction deadline. **/ readonly deadline: TimestampDto; /** * 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. */ public constructor( signature: SignatureDto, signerPublicKey: PublicKeyDto, version: number, network: NetworkTypeDto, type: TransactionTypeDto, fee: AmountDto, deadline: TimestampDto, ) { GeneratorUtils.notNull(signature, 'signature is null or undefined'); GeneratorUtils.notNull(signerPublicKey, 'signerPublicKey is null or undefined'); GeneratorUtils.notNull(version, 'version is null or undefined'); GeneratorUtils.notNull(network, 'network is null or undefined'); GeneratorUtils.notNull(type, 'type is null or undefined'); GeneratorUtils.notNull(fee, 'fee is null or undefined'); GeneratorUtils.notNull(deadline, 'deadline is null or undefined'); this.signature = signature; this.signerPublicKey = signerPublicKey; this.version = version; this.network = network; this.type = type; this.fee = fee; this.deadline = deadline; } /** * Load from binary array - Creates an object from payload. * * @param payload - Byte payload to use to serialize the object. */ public static loadFromBinary(payload: Uint8Array): TransactionBuilder { const byteArray = Array.from(payload); const size: number = GeneratorUtils.bufferToUint32(Uint8Array.from(byteArray)); byteArray.splice(0, 4); GeneratorUtils.bufferToUint32(Uint8Array.from(byteArray)); byteArray.splice(0, 4); const signature: SignatureDto = SignatureDto.loadFromBinary(Uint8Array.from(byteArray)); byteArray.splice(0, signature.getSize()); const signerPublicKey: PublicKeyDto = PublicKeyDto.loadFromBinary(Uint8Array.from(byteArray)); byteArray.splice(0, signerPublicKey.getSize()); GeneratorUtils.bufferToUint32(Uint8Array.from(byteArray)); byteArray.splice(0, 4); const version: number = GeneratorUtils.bufferToUint8(Uint8Array.from(byteArray)); byteArray.splice(0, 1); const network: NetworkTypeDto = GeneratorUtils.bufferToUint8(Uint8Array.from(byteArray)); byteArray.splice(0, 1); const type: TransactionTypeDto = GeneratorUtils.bufferToUint16(Uint8Array.from(byteArray)); byteArray.splice(0, 2); const fee: AmountDto = AmountDto.loadFromBinary(Uint8Array.from(byteArray)); byteArray.splice(0, fee.getSize()); const deadline: TimestampDto = TimestampDto.loadFromBinary(Uint8Array.from(byteArray)); byteArray.splice(0, deadline.getSize()); return new TransactionBuilder(signature, signerPublicKey, version, network, type, fee, deadline); } /** * Creates an instance of TransactionBuilder. * * @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. * @return Instance of TransactionBuilder. */ public static createTransactionBuilder( signature: SignatureDto, signerPublicKey: PublicKeyDto, version: number, network: NetworkTypeDto, type: TransactionTypeDto, fee: AmountDto, deadline: TimestampDto, ): TransactionBuilder { return new TransactionBuilder(signature, signerPublicKey, version, network, type, fee, deadline); } /** * Gets Entity's signature generated by the signing account.. * * @return Entity's signature generated by the signing account.. */ public getSignature(): SignatureDto { return this.signature; } /** * Gets Public key of the signer of the entity.. * * @return Public key of the signer of the entity.. */ public getSignerPublicKey(): PublicKeyDto { return this.signerPublicKey; } /** * Gets Version of this structure.. * * @return Version of this structure.. */ public getVersion(): number { return this.version; } /** * Gets Network on which this entity was created.. * * @return Network on which this entity was created.. */ public getNetwork(): NetworkTypeDto { return this.network; } /** * Gets transaction type. * * @return Transaction type. */ public getType(): TransactionTypeDto { return this.type; } /** * Gets transaction fee. * * @return Transaction fee. */ public getFee(): AmountDto { return this.fee; } /** * Gets transaction deadline. * * @return Transaction deadline. */ public getDeadline(): TimestampDto { return this.deadline; } /** * Gets the size of the object. * * @return Size in bytes. */ public getSize(): number { let size = 0; size += 4; // size size += 4; // verifiableEntityHeaderReserved1 size += this.signature.getSize(); // signature size += this.signerPublicKey.getSize(); // signerPublicKey size += 4; // entityBodyReserved1 size += 1; // version size += 1; // network size += 2; // type size += this.fee.getSize(); // fee size += this.deadline.getSize(); // deadline return size; } /** * Gets the body builder of the object. * * @return Body builder. */ public getBody(): undefined | Serializer { return undefined; } /** * Serializes an object to bytes. * * @return Serialized bytes. */ public serialize(): Uint8Array { let newArray = Uint8Array.from([]); const sizeBytes = GeneratorUtils.uint32ToBuffer(this.getSize()); newArray = GeneratorUtils.concatTypedArrays(newArray, sizeBytes); const verifiableEntityHeaderReserved1Bytes = GeneratorUtils.uint32ToBuffer(0); newArray = GeneratorUtils.concatTypedArrays(newArray, verifiableEntityHeaderReserved1Bytes); const signatureBytes = this.signature.serialize(); newArray = GeneratorUtils.concatTypedArrays(newArray, signatureBytes); const signerPublicKeyBytes = this.signerPublicKey.serialize(); newArray = GeneratorUtils.concatTypedArrays(newArray, signerPublicKeyBytes); const entityBodyReserved1Bytes = GeneratorUtils.uint32ToBuffer(0); newArray = GeneratorUtils.concatTypedArrays(newArray, entityBodyReserved1Bytes); const versionBytes = GeneratorUtils.uint8ToBuffer(this.getVersion()); newArray = GeneratorUtils.concatTypedArrays(newArray, versionBytes); const networkBytes = GeneratorUtils.uint8ToBuffer(this.network); newArray = GeneratorUtils.concatTypedArrays(newArray, networkBytes); const typeBytes = GeneratorUtils.uint16ToBuffer(this.type); newArray = GeneratorUtils.concatTypedArrays(newArray, typeBytes); const feeBytes = this.fee.serialize(); newArray = GeneratorUtils.concatTypedArrays(newArray, feeBytes); const deadlineBytes = this.deadline.serialize(); newArray = GeneratorUtils.concatTypedArrays(newArray, deadlineBytes); return newArray; } }