Skip to content

Commit 6fffd51

Browse files
author
Grégory Saive
authored
Merge pull request #87 from rg911/task/g38_raise_error_adding_agg_tx_as_inner_tx
Added #38 Raise an error when adding an aggregate tx as an inner tranction
2 parents 6770480 + 077d4f1 commit 6fffd51

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

src/model/transaction/AggregateTransaction.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ export class AggregateTransaction extends Transaction {
133133
public signTransactionWithCosignatories(initiatorAccount: Account, cosignatories: Account[]) {
134134
const aggregateTransaction = this.buildTransaction();
135135
const signedTransactionRaw = aggregateTransaction.signTransactionWithCosigners(initiatorAccount, cosignatories);
136-
return new SignedTransaction(signedTransactionRaw.payload, signedTransactionRaw.hash, initiatorAccount.publicKey, this.type, this.networkType);
136+
return new SignedTransaction(signedTransactionRaw.payload, signedTransactionRaw.hash, initiatorAccount.publicKey,
137+
this.type, this.networkType);
137138
}
138139

139140
/**

src/model/transaction/Transaction.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { Deadline } from './Deadline';
2424
import { InnerTransaction } from './InnerTransaction';
2525
import { SignedTransaction } from './SignedTransaction';
2626
import { TransactionInfo } from './TransactionInfo';
27+
import { TransactionType } from './TransactionType';
2728

2829
/**
2930
* An abstract transaction class that serves as the base class of all NEM transactions.
@@ -112,6 +113,9 @@ export abstract class Transaction {
112113
* @returns InnerTransaction
113114
*/
114115
public toAggregate(signer: PublicAccount): InnerTransaction {
116+
if (this.type === TransactionType.AGGREGATE_BONDED || this.type === TransactionType.AGGREGATE_COMPLETE) {
117+
throw new Error('Inner transaction cannot be an aggregated transaction.');
118+
}
115119
return Object.assign({__proto__: Object.getPrototypeOf(this)}, this, {signer});
116120
}
117121

test/model/transaction/AggregateTransaction.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,4 +315,28 @@ describe('AggregateTransaction', () => {
315315

316316
expect(aggregateTransaction.type).to.be.equal(0x4241);
317317
});
318+
319+
it('should throw exception when adding an aggregated transaction as inner transaction', () => {
320+
const transferTransaction = TransferTransaction.create(
321+
Deadline.create(1, ChronoUnit.HOURS),
322+
Address.createFromRawAddress('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC'),
323+
[],
324+
PlainMessage.create('test-message'),
325+
NetworkType.MIJIN_TEST,
326+
);
327+
328+
const aggregateTransaction = AggregateTransaction.createComplete(
329+
Deadline.create(),
330+
[transferTransaction.toAggregate(account.publicAccount)],
331+
NetworkType.MIJIN_TEST,
332+
[]);
333+
334+
expect(() => {
335+
AggregateTransaction.createComplete(
336+
Deadline.create(),
337+
[aggregateTransaction.toAggregate(account.publicAccount)],
338+
NetworkType.MIJIN_TEST,
339+
[]);
340+
}).to.throw(Error, 'Inner transaction cannot be an aggregated transaction.');
341+
});
318342
});

test/model/transaction/Transaction.spec.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,24 @@
1616

1717
import { expect } from 'chai';
1818
import { VerifiableTransaction } from 'nem2-library';
19+
import { Account } from '../../../src/model/account/Account';
1920
import { NetworkType } from '../../../src/model/blockchain/NetworkType';
20-
import { Account } from '../../../src/model/model';
21+
import { AggregateTransaction } from '../../../src/model/transaction/AggregateTransaction';
2122
import { Deadline } from '../../../src/model/transaction/Deadline';
2223
import { SignedTransaction } from '../../../src/model/transaction/SignedTransaction';
2324
import { Transaction } from '../../../src/model/transaction/Transaction';
2425
import { TransactionInfo } from '../../../src/model/transaction/TransactionInfo';
2526
import { TransactionType } from '../../../src/model/transaction/TransactionType';
2627
import { UInt64 } from '../../../src/model/UInt64';
28+
import { TestingAccount } from '../../conf/conf.spec';
2729

2830
describe('Transaction', () => {
31+
let account: Account;
32+
33+
before(() => {
34+
account = TestingAccount;
35+
});
36+
2937
describe('isUnannounced', () => {
3038
it('should return true when there is no Transaction Info', () => {
3139
const transaction = new FakeTransaction(TransactionType.TRANSFER,
@@ -146,6 +154,29 @@ describe('Transaction', () => {
146154
expect(after).to.be.equal(true);
147155
});
148156
});
157+
158+
describe('toAggregate', () => {
159+
it('should throw exception when adding an aggregated transaction as inner transaction', () => {
160+
const transaction = new FakeTransaction(TransactionType.TRANSFER,
161+
NetworkType.MIJIN_TEST,
162+
1,
163+
Deadline.create(),
164+
UInt64.fromUint(0),
165+
undefined,
166+
undefined,
167+
);
168+
169+
const aggregateTransaction = AggregateTransaction.createComplete(
170+
Deadline.create(),
171+
[transaction.toAggregate(account.publicAccount)],
172+
NetworkType.MIJIN_TEST,
173+
[]);
174+
175+
expect(() => {
176+
aggregateTransaction.toAggregate(account.publicAccount);
177+
}).to.throw(Error, 'Inner transaction cannot be an aggregated transaction.');
178+
});
179+
});
149180
});
150181

151182
class FakeTransaction extends Transaction {

0 commit comments

Comments
 (0)