/
AggregateTransaction.ts
405 lines (379 loc) · 16.6 KB
/
AggregateTransaction.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
* Copyright 2018 NEM
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
AggregateBondedTransactionBuilder,
AggregateCompleteTransactionBuilder,
AmountDto,
CosignatureBuilder,
EmbeddedTransactionBuilder,
Hash256Dto,
PublicKeyDto,
SignatureDto,
TimestampDto,
TransactionBuilder,
} from 'catbuffer-typescript';
import { KeyPair, MerkleHashBuilder, SHA3Hasher } from '../../core/crypto';
import { Convert } from '../../core/format';
import { DtoMapping } from '../../core/utils';
import { CreateTransactionFromPayload } from '../../infrastructure/transaction';
import { Account, PublicAccount, UnresolvedAddress } from '../account';
import { NetworkType } from '../network';
import { Statement } from '../receipt';
import { UInt64 } from '../UInt64';
import { AggregateTransactionCosignature } from './AggregateTransactionCosignature';
import { CosignatureSignedTransaction } from './CosignatureSignedTransaction';
import { Deadline } from './Deadline';
import { InnerTransaction } from './InnerTransaction';
import { SignedTransaction } from './SignedTransaction';
import { Transaction } from './Transaction';
import { TransactionInfo } from './TransactionInfo';
import { TransactionType } from './TransactionType';
import { TransactionVersion } from './TransactionVersion';
/**
* Aggregate innerTransactions contain multiple innerTransactions that can be initiated by different accounts.
*/
export class AggregateTransaction extends Transaction {
/**
* @param networkType
* @param type
* @param version
* @param deadline
* @param maxFee
* @param innerTransactions
* @param cosignatures
* @param signature
* @param signer
* @param transactionInfo
*/
constructor(
networkType: NetworkType,
type: number,
version: number,
deadline: Deadline,
maxFee: UInt64,
/**
* The array of innerTransactions included in the aggregate transaction.
*/
public readonly innerTransactions: InnerTransaction[],
/**
* The array of transaction cosigners signatures.
*/
public readonly cosignatures: AggregateTransactionCosignature[],
signature?: string,
signer?: PublicAccount,
transactionInfo?: TransactionInfo,
) {
super(type, networkType, version, deadline, maxFee, signature, signer, transactionInfo);
}
/**
* Create an aggregate complete transaction object
* @param deadline - The deadline to include the transaction.
* @param innerTransactions - The array of inner innerTransactions.
* @param networkType - The network type.
* @param cosignatures
* @param maxFee - (Optional) Max fee defined by the sender
* @param signature - (Optional) Transaction signature
* @param signer - (Optional) Signer public account
* @returns {AggregateTransaction}
*/
public static createComplete(
deadline: Deadline,
innerTransactions: InnerTransaction[],
networkType: NetworkType,
cosignatures: AggregateTransactionCosignature[],
maxFee: UInt64 = new UInt64([0, 0]),
signature?: string,
signer?: PublicAccount,
): AggregateTransaction {
return new AggregateTransaction(
networkType,
TransactionType.AGGREGATE_COMPLETE,
TransactionVersion.AGGREGATE_COMPLETE,
deadline,
maxFee,
innerTransactions,
cosignatures,
signature,
signer,
);
}
/**
* Create an aggregate bonded transaction object
* @param {Deadline} deadline
* @param {InnerTransaction[]} innerTransactions
* @param {NetworkType} networkType
* @param {AggregateTransactionCosignature[]} cosignatures
* @param {UInt64} maxFee - (Optional) Max fee defined by the sender
* @param {string} signature - (Optional) Transaction signature
* @param {PublicAccount} signer - (Optional) Signer public account
* @return {AggregateTransaction}
*/
public static createBonded(
deadline: Deadline,
innerTransactions: InnerTransaction[],
networkType: NetworkType,
cosignatures: AggregateTransactionCosignature[] = [],
maxFee: UInt64 = new UInt64([0, 0]),
signature?: string,
signer?: PublicAccount,
): AggregateTransaction {
return new AggregateTransaction(
networkType,
TransactionType.AGGREGATE_BONDED,
TransactionVersion.AGGREGATE_BONDED,
deadline,
maxFee,
innerTransactions,
cosignatures,
signature,
signer,
);
}
/**
* Create a transaction object from payload
* @param {string} payload Binary payload
* @returns {AggregateTransaction}
*/
public static createFromPayload(payload: string): AggregateTransaction {
/**
* Get transaction type from the payload hex
* As buffer uses separate builder class for Complete and bonded
*/
const builder = AggregateCompleteTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload));
const type = (builder.type as number) as TransactionType;
const innerTransactions = builder.getTransactions();
const networkType = builder.getNetwork().valueOf();
const signerPublicKey = Convert.uint8ToHex(builder.getSignerPublicKey().publicKey);
const signature = Transaction.getSignatureFromPayload(payload, false);
const consignatures = builder.getCosignatures().map((cosig) => {
return new AggregateTransactionCosignature(
Convert.uint8ToHex(cosig.signature.signature),
PublicAccount.createFromPublicKey(Convert.uint8ToHex(cosig.signerPublicKey.publicKey), networkType),
new UInt64(cosig.version),
);
});
return type === TransactionType.AGGREGATE_COMPLETE
? AggregateTransaction.createComplete(
Deadline.createFromDTO(builder.deadline.timestamp),
innerTransactions.map((transactionRaw) => {
return CreateTransactionFromPayload(Convert.uint8ToHex(transactionRaw.serialize()), true) as InnerTransaction;
}),
networkType,
consignatures,
new UInt64(builder.fee.amount),
signature,
signerPublicKey.match(`^[0]+$`) ? undefined : PublicAccount.createFromPublicKey(signerPublicKey, networkType),
)
: AggregateTransaction.createBonded(
Deadline.createFromDTO(builder.deadline.timestamp),
innerTransactions.map((transactionRaw) => {
return CreateTransactionFromPayload(Convert.uint8ToHex(transactionRaw.serialize()), true) as InnerTransaction;
}),
networkType,
consignatures,
new UInt64(builder.fee.amount),
signature,
signerPublicKey.match(`^[0]+$`) ? undefined : PublicAccount.createFromPublicKey(signerPublicKey, networkType),
);
}
/**
* @description add inner transactions to current list
* @param {InnerTransaction[]} transaction
* @returns {AggregateTransaction}
* @memberof AggregateTransaction
*/
public addTransactions(transactions: InnerTransaction[]): AggregateTransaction {
const innerTransactions = this.innerTransactions.concat(transactions);
return DtoMapping.assign(this, { innerTransactions });
}
/**
* @description add cosignatures to current list
* @param {AggregateTransactionCosignature[]} transaction
* @returns {AggregateTransaction}
* @memberof AggregateTransaction
*/
public addCosignatures(cosigs: AggregateTransactionCosignature[]): AggregateTransaction {
const cosignatures = this.cosignatures.concat(cosigs);
return DtoMapping.assign(this, { cosignatures });
}
/**
* @internal
* Sign transaction with cosignatories creating a new SignedTransaction
* @param initiatorAccount - Initiator account
* @param cosignatories - The array of accounts that will cosign the transaction
* @param generationHash - Network generation hash hex
* @returns {SignedTransaction}
*/
public signTransactionWithCosignatories(
initiatorAccount: Account,
cosignatories: Account[],
generationHash: string,
): SignedTransaction {
const signedTransaction = this.signWith(initiatorAccount, generationHash);
const transactionHashBytes = Convert.hexToUint8(signedTransaction.hash);
let signedPayload = signedTransaction.payload;
cosignatories.forEach((cosigner) => {
const keyPairEncoded = KeyPair.createKeyPairFromPrivateKeyString(cosigner.privateKey);
const signature = KeyPair.sign(keyPairEncoded, transactionHashBytes);
signedPayload += UInt64.fromUint(0).toHex() + cosigner.publicKey + Convert.uint8ToHex(signature);
});
// Calculate new size
const size = `00000000${(signedPayload.length / 2).toString(16)}`;
const formatedSize = size.substr(size.length - 8, size.length);
const littleEndianSize =
formatedSize.substr(6, 2) + formatedSize.substr(4, 2) + formatedSize.substr(2, 2) + formatedSize.substr(0, 2);
signedPayload = littleEndianSize + signedPayload.substr(8, signedPayload.length - 8);
return new SignedTransaction(signedPayload, signedTransaction.hash, initiatorAccount.publicKey, this.type, this.networkType);
}
/**
* @internal
* Sign transaction with cosignatories collected from cosigned transactions and creating a new SignedTransaction
* For off chain Aggregated Complete Transaction co-signing.
* @param initiatorAccount - Initiator account
* @param {CosignatureSignedTransaction[]} cosignatureSignedTransactions - Array of cosigned transaction
* @param generationHash - Network generation hash hex
* @return {SignedTransaction}
*/
public signTransactionGivenSignatures(
initiatorAccount: Account,
cosignatureSignedTransactions: CosignatureSignedTransaction[],
generationHash: string,
): SignedTransaction {
const signedTransaction = this.signWith(initiatorAccount, generationHash);
let signedPayload = signedTransaction.payload;
cosignatureSignedTransactions.forEach((cosignedTransaction) => {
signedPayload += cosignedTransaction.version.toHex() + cosignedTransaction.signerPublicKey + cosignedTransaction.signature;
});
// Calculate new size
const size = `00000000${(signedPayload.length / 2).toString(16)}`;
const formatedSize = size.substr(size.length - 8, size.length);
const littleEndianSize =
formatedSize.substr(6, 2) + formatedSize.substr(4, 2) + formatedSize.substr(2, 2) + formatedSize.substr(0, 2);
signedPayload = littleEndianSize + signedPayload.substr(8, signedPayload.length - 8);
return new SignedTransaction(signedPayload, signedTransaction.hash, initiatorAccount.publicKey, this.type, this.networkType);
}
/**
* Check if account has signed transaction
* @param publicAccount - Signer public account
* @returns {boolean}
*/
public signedByAccount(publicAccount: PublicAccount): boolean {
return (
this.cosignatures.find((cosignature) => cosignature.signer.equals(publicAccount)) !== undefined ||
(this.signer !== undefined && this.signer.equals(publicAccount))
);
}
/**
* @internal
* @returns {TransactionBuilder}
*/
protected createBuilder(): TransactionBuilder {
const transactions = this.innerTransactions.map((transaction) => (transaction as Transaction).toEmbeddedTransaction());
const cosignatures = this.cosignatures.map((cosignature) => {
const signerBytes = Convert.hexToUint8(cosignature.signer.publicKey);
const signatureBytes = Convert.hexToUint8(cosignature.signature);
return new CosignatureBuilder(cosignature.version.toDTO(), new PublicKeyDto(signerBytes), new SignatureDto(signatureBytes));
});
const builder =
this.type === TransactionType.AGGREGATE_COMPLETE ? AggregateCompleteTransactionBuilder : AggregateBondedTransactionBuilder;
return new builder(
this.getSignatureAsBuilder(),
this.getSignerAsBuilder(),
this.versionToDTO(),
this.networkType.valueOf(),
this.type.valueOf(),
new AmountDto(this.maxFee.toDTO()),
new TimestampDto(this.deadline.toDTO()),
new Hash256Dto(this.calculateInnerTransactionHash()),
transactions,
cosignatures,
);
}
/**
* @internal
* @returns {EmbeddedTransactionBuilder}
*/
public toEmbeddedTransaction(): EmbeddedTransactionBuilder {
throw new Error('Method not implemented');
}
/**
* @internal
* Generate inner transaction root hash (merkle tree)
* @returns {Uint8Array}
*/
private calculateInnerTransactionHash(): Uint8Array {
// Note: Transaction hashing *always* uses SHA3
const hasher = SHA3Hasher.createHasher(32);
const builder = new MerkleHashBuilder(32);
this.innerTransactions.forEach((transaction) => {
const entityHash: Uint8Array = new Uint8Array(32);
// for each embedded transaction hash their body
hasher.reset();
const byte = transaction.toEmbeddedTransaction().serialize();
hasher.update(byte);
hasher.finalize(entityHash);
// update merkle tree (add transaction hash)
builder.update(entityHash);
});
// calculate root hash with all transactions
return builder.getRootHash();
}
/**
* @internal
* @returns {AggregateTransaction}
*/
public resolveAliases(statement: Statement): AggregateTransaction {
const transactionInfo = this.checkTransactionHeightAndIndex();
return DtoMapping.assign(this, {
innerTransactions: this.innerTransactions
.map((tx) => tx.resolveAliases(statement, transactionInfo.index))
.sort((a, b) => a.transactionInfo!.index - b.transactionInfo!.index),
});
}
/**
* Set transaction maxFee using fee multiplier for **ONLY AGGREGATE TRANSACTIONS**
* @param feeMultiplier The fee multiplier
* @param requiredCosignatures Required number of cosignatures
* @returns {AggregateTransaction}
*/
public setMaxFeeForAggregate(feeMultiplier: number, requiredCosignatures: number): AggregateTransaction {
if (this.type !== TransactionType.AGGREGATE_BONDED && this.type !== TransactionType.AGGREGATE_COMPLETE) {
throw new Error('setMaxFeeForAggregate can only be used for aggregate transactions.');
}
// Check if current cosignature count is greater than requiredCosignatures.
const calculatedCosignatures = requiredCosignatures > this.cosignatures.length ? requiredCosignatures : this.cosignatures.length;
// version + public key + signature
const sizePerCosignature = 8 + 32 + 64;
// Remove current cosignature length and use the calculated one.
const calculatedSize = this.size - this.cosignatures.length * sizePerCosignature + calculatedCosignatures * sizePerCosignature;
return DtoMapping.assign(this, {
maxFee: UInt64.fromUint(calculatedSize * feeMultiplier),
});
}
/**
* @internal
* Check a given address should be notified in websocket channels
* @param address address to be notified
* @returns {boolean}
*/
public shouldNotifyAccount(address: UnresolvedAddress): boolean {
return (
super.isSigned(address) ||
this.cosignatures.find((_) => _.signer.address.equals(address)) !== undefined ||
this.innerTransactions.find((innerTransaction: InnerTransaction) => innerTransaction.shouldNotifyAccount(address)) !== undefined
);
}
}