Skip to content

Commit c1bf3ec

Browse files
committed
Moved listener to method
1 parent c006f3b commit c1bf3ec

File tree

3 files changed

+231
-17
lines changed

3 files changed

+231
-17
lines changed
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
/*
2+
* Copyright 2019 NEM
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { expect } from 'chai';
18+
import { Listener } from '../../src/infrastructure/Listener';
19+
import { TransactionHttp } from '../../src/infrastructure/TransactionHttp';
20+
import { Account } from '../../src/model/account/Account';
21+
import { Address } from '../../src/model/account/Address';
22+
import { NetworkType } from '../../src/model/blockchain/NetworkType';
23+
import { PlainMessage } from '../../src/model/message/PlainMessage';
24+
import { NetworkCurrencyMosaic } from '../../src/model/mosaic/NetworkCurrencyMosaic';
25+
import { AggregateTransaction } from '../../src/model/transaction/AggregateTransaction';
26+
import { Deadline } from '../../src/model/transaction/Deadline';
27+
import { MultisigAccountModificationTransaction } from '../../src/model/transaction/MultisigAccountModificationTransaction';
28+
import { TransferTransaction } from '../../src/model/transaction/TransferTransaction';
29+
import { TransactionService } from '../../src/service/TransactionService';
30+
31+
describe('TransactionService', () => {
32+
let account: Account;
33+
let account2: Account;
34+
let multisigAccount: Account;
35+
let cosignAccount1: Account;
36+
let cosignAccount2: Account;
37+
let cosignAccount3: Account;
38+
let url: string;
39+
let generationHash: string;
40+
let transactionHttp: TransactionHttp;
41+
let config;
42+
43+
before((done) => {
44+
const path = require('path');
45+
require('fs').readFile(path.resolve(__dirname, '../conf/network.conf'), (err, data) => {
46+
if (err) {
47+
throw err;
48+
}
49+
const json = JSON.parse(data);
50+
config = json;
51+
account = Account.createFromPrivateKey(json.testAccount.privateKey, NetworkType.MIJIN_TEST);
52+
account2 = Account.createFromPrivateKey(json.testAccount2.privateKey, NetworkType.MIJIN_TEST);
53+
multisigAccount = Account.createFromPrivateKey(json.multisigAccount.privateKey, NetworkType.MIJIN_TEST);
54+
cosignAccount1 = Account.createFromPrivateKey(json.cosignatoryAccount.privateKey, NetworkType.MIJIN_TEST);
55+
cosignAccount2 = Account.createFromPrivateKey(json.cosignatory2Account.privateKey, NetworkType.MIJIN_TEST);
56+
cosignAccount3 = Account.createFromPrivateKey(json.cosignatory3Account.privateKey, NetworkType.MIJIN_TEST);
57+
url = json.apiUrl;
58+
generationHash = json.generationHash;
59+
transactionHttp = new TransactionHttp(json.apiUrl);
60+
done();
61+
});
62+
});
63+
64+
/**
65+
* =========================
66+
* Setup test data
67+
* =========================
68+
*/
69+
describe('Setup test multisig account', () => {
70+
let listener: Listener;
71+
before (() => {
72+
listener = new Listener(config.apiUrl);
73+
return listener.open();
74+
});
75+
after(() => {
76+
return listener.close();
77+
});
78+
it('Announce MultisigAccountModificationTransaction', (done) => {
79+
const modifyMultisigAccountTransaction = MultisigAccountModificationTransaction.create(
80+
Deadline.create(),
81+
2,
82+
1,
83+
[
84+
cosignAccount1.publicAccount,
85+
cosignAccount2.publicAccount,
86+
cosignAccount3.publicAccount,
87+
],
88+
[],
89+
NetworkType.MIJIN_TEST,
90+
);
91+
92+
const aggregateTransaction = AggregateTransaction.createComplete(Deadline.create(),
93+
[modifyMultisigAccountTransaction.toAggregate(multisigAccount.publicAccount)],
94+
NetworkType.MIJIN_TEST,
95+
[]);
96+
const signedTransaction = aggregateTransaction
97+
.signTransactionWithCosignatories(multisigAccount, [cosignAccount1, cosignAccount2, cosignAccount3], generationHash);
98+
99+
listener.confirmed(multisigAccount.address).subscribe(() => {
100+
done();
101+
});
102+
listener.status(multisigAccount.address).subscribe((error) => {
103+
console.log('Error:', error);
104+
done();
105+
});
106+
transactionHttp.announce(signedTransaction);
107+
});
108+
});
109+
110+
/**
111+
* =========================
112+
* Test
113+
* =========================
114+
*/
115+
116+
describe('should announce transaction', () => {
117+
let listener: Listener;
118+
before (() => {
119+
listener = new Listener(config.apiUrl);
120+
return listener.open();
121+
});
122+
after(() => {
123+
return listener.close();
124+
});
125+
it('announce', (done) => {
126+
const transactionService = new TransactionService(url);
127+
const transferTransaction = TransferTransaction.create(
128+
Deadline.create(),
129+
account2.address,
130+
[
131+
NetworkCurrencyMosaic.createAbsolute(1),
132+
],
133+
PlainMessage.create('test-message'),
134+
NetworkType.MIJIN_TEST,
135+
);
136+
const signedTransaction = transferTransaction.signWith(account, generationHash);
137+
transactionService.announce(signedTransaction, listener).subscribe((tx: TransferTransaction) => {
138+
expect(tx.signer!.publicKey).to.be.equal(account.publicKey);
139+
expect((tx.recipientAddress as Address).equals(account2.address)).to.be.true;
140+
expect(tx.message.payload).to.be.equal('test-message');
141+
done();
142+
});
143+
});
144+
});
145+
146+
/**
147+
* =========================
148+
* House Keeping
149+
* =========================
150+
*/
151+
152+
describe('Restore test multisig Accounts', () => {
153+
let listener: Listener;
154+
before (() => {
155+
listener = new Listener(config.apiUrl);
156+
return listener.open();
157+
});
158+
after(() => {
159+
return listener.close();
160+
});
161+
it('Announce MultisigAccountModificationTransaction', (done) => {
162+
const removeCosigner1 = MultisigAccountModificationTransaction.create(
163+
Deadline.create(),
164+
-1,
165+
0,
166+
[],
167+
[ cosignAccount1.publicAccount,
168+
],
169+
NetworkType.MIJIN_TEST,
170+
);
171+
const removeCosigner2 = MultisigAccountModificationTransaction.create(
172+
Deadline.create(),
173+
0,
174+
0,
175+
[],
176+
[
177+
cosignAccount2.publicAccount,
178+
],
179+
NetworkType.MIJIN_TEST,
180+
);
181+
182+
const removeCosigner3 = MultisigAccountModificationTransaction.create(
183+
Deadline.create(),
184+
-1,
185+
-1,
186+
[],
187+
[
188+
cosignAccount3.publicAccount,
189+
],
190+
NetworkType.MIJIN_TEST,
191+
);
192+
193+
const aggregateTransaction = AggregateTransaction.createComplete(Deadline.create(),
194+
[removeCosigner1.toAggregate(multisigAccount.publicAccount),
195+
removeCosigner2.toAggregate(multisigAccount.publicAccount),
196+
removeCosigner3.toAggregate(multisigAccount.publicAccount)],
197+
NetworkType.MIJIN_TEST,
198+
[]);
199+
const signedTransaction = aggregateTransaction
200+
.signTransactionWithCosignatories(cosignAccount1, [cosignAccount2, cosignAccount3], generationHash);
201+
202+
listener.confirmed(cosignAccount1.address).subscribe(() => {
203+
done();
204+
});
205+
listener.status(cosignAccount1.address).subscribe((error) => {
206+
console.log('Error:', error);
207+
done();
208+
});
209+
transactionHttp.announce(signedTransaction);
210+
});
211+
});
212+
});

src/service/TransactionService.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ import { flatMap } from 'rxjs/operators';
2020
import { Listener } from '../infrastructure/Listener';
2121
import { ReceiptHttp } from '../infrastructure/ReceiptHttp';
2222
import { TransactionHttp } from '../infrastructure/TransactionHttp';
23-
import { Address } from '../model/account/Address';
24-
import { MosaicId } from '../model/mosaic/MosaicId';
25-
import { NamespaceId } from '../model/namespace/NamespaceId';
26-
import { ResolutionType } from '../model/receipt/ResolutionType';
27-
import { Statement } from '../model/receipt/Statement';
2823
import { AggregateTransaction } from '../model/transaction/AggregateTransaction';
2924
import { SignedTransaction } from '../model/transaction/SignedTransaction';
3025
import { Transaction } from '../model/transaction/Transaction';
@@ -37,19 +32,19 @@ export class TransactionService implements ITransactionService {
3732

3833
private readonly transactionHttp: TransactionHttp;
3934
private readonly receiptHttp: ReceiptHttp;
40-
private readonly listener: Listener;
4135
/**
4236
* Constructor
4337
* @param url Base catapult-rest url
4438
*/
4539
constructor(url: string) {
4640
this.transactionHttp = new TransactionHttp(url);
4741
this.receiptHttp = new ReceiptHttp(url);
48-
this.listener = new Listener(url);
4942
}
5043

5144
/**
45+
* Resolve unresolved mosaic / address from array of transactions
5246
* @param transationHashes List of transaction hashes.
47+
* @param listener Websocket listener
5348
* @returns Observable<Transaction[]>
5449
*/
5550
public resolveAliases(transationHashes: string[]): Observable<Transaction[]> {
@@ -61,36 +56,41 @@ export class TransactionService implements ITransactionService {
6156
}
6257

6358
/**
59+
* Announce transaction
6460
* @param signedTransaction Signed transaction to be announced.
61+
* @param listener Websocket listener
6562
* @returns {Observable<Transaction>}
6663
*/
67-
public announce(signedTransaction: SignedTransaction): Observable<Transaction> {
64+
public announce(signedTransaction: SignedTransaction, listener: Listener): Observable<Transaction> {
6865
return this.transactionHttp.announce(signedTransaction).pipe(
69-
flatMap(() => this.listener.confirmed(signedTransaction.getSignerAddress(), signedTransaction.hash)),
66+
flatMap(() => listener.confirmed(signedTransaction.getSignerAddress(), signedTransaction.hash)),
7067
);
7168
}
7269

7370
/**
7471
* Announce aggregate transaction
7572
* @param signedTransaction Signed aggregate bonded transaction.
73+
* @param listener Websocket listener
7674
* @returns {Observable<AggregateTransaction>}
7775
*/
78-
public announceAggregateBonded(signedTransaction: SignedTransaction): Observable<AggregateTransaction> {
76+
public announceAggregateBonded(signedTransaction: SignedTransaction, listener: Listener): Observable<AggregateTransaction> {
7977
return this.transactionHttp.announceAggregateBonded(signedTransaction).pipe(
80-
flatMap(() => this.listener.aggregateBondedAdded(signedTransaction.getSignerAddress(), signedTransaction.hash)),
78+
flatMap(() => listener.aggregateBondedAdded(signedTransaction.getSignerAddress(), signedTransaction.hash)),
8179
);
8280
}
8381

8482
/**
8583
* Announce aggregate bonded transaction with lock fund
8684
* @param signedHashLockTransaction Signed hash lock transaction.
8785
* @param signedAggregateTransaction Signed aggregate bonded transaction.
86+
* @param listener Websocket listener
8887
* @returns {Observable<AggregateTransaction>}
8988
*/
9089
public announceHashLockAggregateBonded(signedHashLockTransaction: SignedTransaction,
91-
signedAggregateTransaction: SignedTransaction): Observable<AggregateTransaction> {
92-
return this.announce(signedHashLockTransaction).pipe(
93-
flatMap(() => this.announceAggregateBonded(signedAggregateTransaction)),
90+
signedAggregateTransaction: SignedTransaction,
91+
listener: Listener): Observable<AggregateTransaction> {
92+
return this.announce(signedHashLockTransaction, listener).pipe(
93+
flatMap(() => this.announceAggregateBonded(signedAggregateTransaction, listener)),
9494
);
9595

9696
}

src/service/interfaces/ITransactionService.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import {Observable} from 'rxjs';
18+
import { Listener } from '../../infrastructure/Listener';
1819
import { AggregateTransaction } from '../../model/transaction/AggregateTransaction';
1920
import { SignedTransaction } from '../../model/transaction/SignedTransaction';
2021
import { Transaction } from '../../model/transaction/Transaction';
@@ -34,14 +35,14 @@ export interface ITransactionService {
3435
* @param signedTransaction Signed transaction to be announced.
3536
* @returns {Observable<Transaction>}
3637
*/
37-
announce(signedTransaction: SignedTransaction): Observable<Transaction>;
38+
announce(signedTransaction: SignedTransaction, listener: Listener): Observable<Transaction>;
3839

3940
/**
4041
* Announce aggregate transaction
4142
* @param signedTransaction Signed aggregate bonded transaction.
4243
* @returns {Observable<AggregateTransaction>}
4344
*/
44-
announceAggregateBonded(signedTransaction: SignedTransaction): Observable<AggregateTransaction>;
45+
announceAggregateBonded(signedTransaction: SignedTransaction, listener: Listener): Observable<AggregateTransaction>;
4546

4647
/**
4748
* Announce aggregate bonded transaction with lock fund
@@ -50,5 +51,6 @@ export interface ITransactionService {
5051
* @returns {Observable<AggregateTransaction>}
5152
*/
5253
announceHashLockAggregateBonded(signedHashLockTransaction: SignedTransaction,
53-
signedAggregateTransaction: SignedTransaction): Observable<AggregateTransaction>;
54+
signedAggregateTransaction: SignedTransaction,
55+
listener: Listener): Observable<AggregateTransaction>;
5456
}

0 commit comments

Comments
 (0)