Skip to content

Commit 5b9b4ad

Browse files
committed
Manually added aggregate transaction builder
1 parent 0d30788 commit 5b9b4ad

File tree

3 files changed

+335
-0
lines changed

3 files changed

+335
-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+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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 { GeneratorUtils } from './GeneratorUtils';
23+
import { KeyDto } from './KeyDto';
24+
import { SignatureDto } from './SignatureDto';
25+
26+
/** a cosignature. */
27+
export class CosignatureBuilder {
28+
/** cosigner public key. */
29+
signer: KeyDto;
30+
/** cosigner signature. */
31+
signature: SignatureDto;
32+
33+
/**
34+
* Constructor.
35+
*
36+
* @param signer cosigner public key.
37+
* @param signature cosigner signature.
38+
*/
39+
public constructor(signer: KeyDto, signature: SignatureDto) {
40+
this.signer = signer;
41+
this.signature = signature;
42+
}
43+
44+
/**
45+
* loadFromBinary - Create an instance of CosignatureBuilder from a stream.
46+
*
47+
* @param payload Byte to use to serialize the object.
48+
* @return An instance of CosignatureBuilder.
49+
*/
50+
public static loadFromBinary(payload: Uint8Array): CosignatureBuilder {
51+
const byteArray = Array.from(payload);
52+
const signer = KeyDto.loadFromBinary(Uint8Array.from(byteArray));
53+
byteArray.splice(0, signer.getSize());
54+
const signature = SignatureDto.loadFromBinary(Uint8Array.from(byteArray));
55+
byteArray.splice(0, signature.getSize());
56+
return new CosignatureBuilder(signer, signature);
57+
}
58+
59+
/**
60+
* Get cosigner public key.
61+
*
62+
* @return cosigner public key.
63+
*/
64+
public getSigner(): KeyDto {
65+
return this.signer;
66+
}
67+
68+
/**
69+
* Get cosigner signature.
70+
*
71+
* @return cosigner signature.
72+
*/
73+
public getSignature(): SignatureDto {
74+
return this.signature;
75+
}
76+
77+
/**
78+
* Get the size of the object.
79+
*
80+
* @return Size in bytes.
81+
*/
82+
public getSize(): number {
83+
let size = 0;
84+
size += this.signer.getSize();
85+
size += this.signature.getSize();
86+
return size;
87+
}
88+
89+
/**
90+
* Serialize the object to bytes.
91+
*
92+
* @return Serialized bytes.
93+
*/
94+
public serialize(): Uint8Array {
95+
let newArray = Uint8Array.from([]);
96+
const signerBytes = this.signer.serialize();
97+
newArray = GeneratorUtils.concatTypedArrays(newArray, signerBytes);
98+
const signatureBytes = this.signature.serialize();
99+
newArray = GeneratorUtils.concatTypedArrays(newArray, signatureBytes);
100+
return newArray;
101+
}
102+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 { GeneratorUtils } from './GeneratorUtils';
23+
import { KeyDto } from './KeyDto';
24+
import { SignatureDto } from './SignatureDto';
25+
import { CosignatureBuilder } from './CosignatureBuilder';
26+
import { Hash256Dto } from './Hash256Dto';
27+
28+
/** a detached cosignature. */
29+
export class DetachedCosignatureBuilder extends CosignatureBuilder {
30+
/** hash of the corresponding parent. */
31+
parentHash: Hash256Dto;
32+
33+
/**
34+
* Constructor.
35+
*
36+
* @param signer cosigner public key.
37+
* @param signature cosigner signature.
38+
* @param parentHash hash of the corresponding parent.
39+
*/
40+
public constructor(signer: KeyDto, signature: SignatureDto, parentHash: Hash256Dto) {
41+
super(signer, signature);
42+
this.parentHash = parentHash;
43+
}
44+
45+
/**
46+
* loadFromBinary - Create an instance of DetachedCosignatureBuilder from a stream.
47+
*
48+
* @param payload Byte to use to serialize the object.
49+
* @return An instance of DetachedCosignatureBuilder.
50+
*/
51+
public static loadFromBinary(payload: Uint8Array): DetachedCosignatureBuilder {
52+
const byteArray = Array.from(payload);
53+
const signer = KeyDto.loadFromBinary(Uint8Array.from(byteArray));
54+
byteArray.splice(0, signer.getSize());
55+
const signature = SignatureDto.loadFromBinary(Uint8Array.from(byteArray));
56+
byteArray.splice(0, signature.getSize());
57+
const parentHash = Hash256Dto.loadFromBinary(Uint8Array.from(byteArray));
58+
byteArray.splice(0, parentHash.getSize());
59+
return new DetachedCosignatureBuilder(signer, signature, parentHash);
60+
}
61+
62+
/**
63+
* Get hash of the corresponding parent.
64+
*
65+
* @return hash of the corresponding parent.
66+
*/
67+
public getParentHash(): Hash256Dto {
68+
return this.parentHash;
69+
}
70+
71+
/**
72+
* Get the size of the object.
73+
*
74+
* @return Size in bytes.
75+
*/
76+
public getSize(): number {
77+
let size = super.getSize();
78+
size += this.parentHash.getSize();
79+
return size;
80+
}
81+
82+
/**
83+
* Serialize the object to bytes.
84+
*
85+
* @return Serialized bytes.
86+
*/
87+
public serialize(): Uint8Array {
88+
let newArray = Uint8Array.from([]);
89+
const signerBytes = this.signer.serialize();
90+
newArray = GeneratorUtils.concatTypedArrays(newArray, signerBytes);
91+
const signatureBytes = this.signature.serialize();
92+
newArray = GeneratorUtils.concatTypedArrays(newArray, signatureBytes);
93+
const parentHashBytes = this.parentHash.serialize();
94+
newArray = GeneratorUtils.concatTypedArrays(newArray, parentHashBytes);
95+
return newArray;
96+
}
97+
}

0 commit comments

Comments
 (0)