Skip to content

Commit 7f19820

Browse files
committed
Manually added aggregate transaction builder
1 parent 51fc076 commit 7f19820

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// tslint:disable: jsdoc-format
2+
/**
3+
*** Copyright (c) 2016-present,
4+
*** Jaguar0625, gimre, BloodyRookie, Tech Bureau, Corp. All rights reserved.
5+
***
6+
*** This file is part of Catapult.
7+
***
8+
*** Catapult is free software: you can redistribute it and/or modify
9+
*** it under the terms of the GNU Lesser General Public License as published by
10+
*** the Free Software Foundation, either version 3 of the License, or
11+
*** (at your option) any later version.
12+
***
13+
*** Catapult is distributed in the hope that it will be useful,
14+
*** but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
*** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
*** GNU Lesser General Public License for more details.
17+
***
18+
*** You should have received a copy of the GNU Lesser General Public License
19+
*** along with Catapult. If not, see <http://www.gnu.org/licenses/>.
20+
**/
21+
22+
import { AddressAliasTransactionBodyBuilder } from './AddressAliasTransactionBodyBuilder';
23+
import { AddressDto } from './AddressDto';
24+
import { AliasActionDto } from './AliasActionDto';
25+
import { AmountDto } from './AmountDto';
26+
import { EntityTypeDto } from './EntityTypeDto';
27+
import { GeneratorUtils } from './GeneratorUtils';
28+
import { KeyDto } from './KeyDto';
29+
import { NamespaceIdDto } from './NamespaceIdDto';
30+
import { SignatureDto } from './SignatureDto';
31+
import { TimestampDto } from './TimestampDto';
32+
import { TransactionBuilder } from './TransactionBuilder';
33+
34+
/** binary layout for an aggregate transaction. */
35+
export class AggregateTransactionBuilder extends TransactionBuilder {
36+
/** embedded transactions. */
37+
transactions: Uint8Array;
38+
/** cosignatures. */
39+
cosignatures: Uint8Array;
40+
41+
/**
42+
* Constructor.
43+
*
44+
* @param signature entity signature.
45+
* @param signer entity signer's public key.
46+
* @param version entity version.
47+
* @param type entity type.
48+
* @param fee transaction fee.
49+
* @param deadline transaction deadline.
50+
* @param transactions embedded transactions.
51+
* @param cosignatures cosignatures.
52+
*/
53+
public constructor(
54+
signature: SignatureDto,
55+
signer: KeyDto,
56+
version: number,
57+
type: EntityTypeDto,
58+
fee: AmountDto,
59+
deadline: TimestampDto,
60+
transactions: Uint8Array,
61+
cosignatures: Uint8Array) {
62+
super(signature, signer, version, type, fee, deadline);
63+
this.transactions = transactions;
64+
this.cosignatures = cosignatures;
65+
}
66+
67+
/**
68+
* loadFromBinary - Create an instance of AggregateTransactionBuilder from a stream.
69+
*
70+
* @param Uint8Array Byte to use to serialize the object.
71+
* @return An instance of AggregateTransactionBuilder.
72+
*/
73+
public static loadFromBinary(payload: Uint8Array): AggregateTransactionBuilder {
74+
const byteArray = Array.from(payload);
75+
const superObject = TransactionBuilder.loadFromBinary(Uint8Array.from(byteArray));
76+
byteArray.splice(0, superObject.getSize());
77+
const payloadSize = GeneratorUtils.bufferToUint(GeneratorUtils.getBytes(Uint8Array.from(byteArray), 4));
78+
byteArray.splice(0, 4);
79+
const transactions = GeneratorUtils.getBytes(Uint8Array.from(byteArray), payloadSize);
80+
byteArray.splice(0, transactions.length);
81+
const cosignatures = Uint8Array.from(byteArray);
82+
// tslint:disable-next-line: max-line-length
83+
return new AggregateTransactionBuilder(superObject.signature, superObject.signer, superObject.version,
84+
superObject.type, superObject.fee, superObject.deadline,
85+
transactions, cosignatures);
86+
}
87+
88+
/**
89+
* Get embedded transactions.
90+
*
91+
* @return embedded transactions.
92+
*/
93+
public getTransactions(): Uint8Array {
94+
return this.transactions;
95+
}
96+
97+
/**
98+
* Get cosignatures.
99+
*
100+
* @return cosignatures.
101+
*/
102+
public getCosignatures(): Uint8Array {
103+
return this.cosignatures;
104+
}
105+
106+
/**
107+
* Get the size of the object.
108+
*
109+
* @return Size in bytes.
110+
*/
111+
public getSize(): number {
112+
let size: number = super.getSize();
113+
size += 4; // payloadSize
114+
size += this.transactions.length;
115+
size += this.cosignatures.length;
116+
return size;
117+
}
118+
119+
/**
120+
* Serialize the object to bytes.
121+
*
122+
* @return Serialized bytes.
123+
*/
124+
public serialize(): Uint8Array {
125+
let newArray = Uint8Array.from([]);
126+
const superBytes = super.serialize();
127+
newArray = GeneratorUtils.concatTypedArrays(newArray, superBytes);
128+
const payloadSizeBytes = GeneratorUtils.uintToBuffer(this.transactions.length, 4);
129+
newArray = GeneratorUtils.concatTypedArrays(newArray, payloadSizeBytes);
130+
const transactionBytes = this.transactions;
131+
newArray = GeneratorUtils.concatTypedArrays(newArray, transactionBytes);
132+
const cosignaturesBytes = this.transactions;
133+
newArray = GeneratorUtils.concatTypedArrays(newArray, cosignaturesBytes);
134+
return newArray;
135+
}
136+
}

0 commit comments

Comments
 (0)