Skip to content

Commit 835ba0b

Browse files
committed
Moved listener to method
1 parent 2997e4f commit 835ba0b

File tree

3 files changed

+230
-12
lines changed

3 files changed

+230
-12
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: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,19 @@ export class TransactionService implements ITransactionService {
4242

4343
private readonly transactionHttp: TransactionHttp;
4444
private readonly receiptHttp: ReceiptHttp;
45-
private readonly listener: Listener;
4645
/**
4746
* Constructor
4847
* @param url Base catapult-rest url
4948
*/
5049
constructor(url: string) {
5150
this.transactionHttp = new TransactionHttp(url);
5251
this.receiptHttp = new ReceiptHttp(url);
53-
this.listener = new Listener(url);
5452
}
5553

5654
/**
55+
* Resolve unresolved mosaic / address from array of transactions
5756
* @param transationHashes List of transaction hashes.
57+
* @param listener Websocket listener
5858
* @returns Observable<Transaction[]>
5959
*/
6060
public resolveAliases(transationHashes: string[]): Observable<Transaction[]> {
@@ -150,35 +150,39 @@ export class TransactionService implements ITransactionService {
150150

151151
/**
152152
* @param signedTransaction Signed transaction to be announced.
153+
* @param listener Websocket listener
153154
* @returns {Observable<Transaction>}
154155
*/
155-
public announce(signedTransaction: SignedTransaction): Observable<Transaction> {
156+
public announce(signedTransaction: SignedTransaction, listener: Listener): Observable<Transaction> {
156157
return this.transactionHttp.announce(signedTransaction).pipe(
157-
flatMap(() => this.listener.confirmed(signedTransaction.getSignerAddress(), signedTransaction.hash)),
158+
flatMap(() => listener.confirmed(signedTransaction.getSignerAddress(), signedTransaction.hash)),
158159
);
159160
}
160161

161162
/**
162163
* Announce aggregate transaction
163164
* @param signedTransaction Signed aggregate bonded transaction.
165+
* @param listener Websocket listener
164166
* @returns {Observable<AggregateTransaction>}
165167
*/
166-
public announceAggregateBonded(signedTransaction: SignedTransaction): Observable<AggregateTransaction> {
168+
public announceAggregateBonded(signedTransaction: SignedTransaction, listener: Listener): Observable<AggregateTransaction> {
167169
return this.transactionHttp.announceAggregateBonded(signedTransaction).pipe(
168-
flatMap(() => this.listener.aggregateBondedAdded(signedTransaction.getSignerAddress(), signedTransaction.hash)),
170+
flatMap(() => listener.aggregateBondedAdded(signedTransaction.getSignerAddress(), signedTransaction.hash)),
169171
);
170172
}
171173

172174
/**
173175
* Announce aggregate bonded transaction with lock fund
174176
* @param signedHashLockTransaction Signed hash lock transaction.
175177
* @param signedAggregateTransaction Signed aggregate bonded transaction.
178+
* @param listener Websocket listener
176179
* @returns {Observable<AggregateTransaction>}
177180
*/
178181
public announceHashLockAggregateBonded(signedHashLockTransaction: SignedTransaction,
179-
signedAggregateTransaction: SignedTransaction): Observable<AggregateTransaction> {
180-
return this.announce(signedHashLockTransaction).pipe(
181-
flatMap(() => this.announceAggregateBonded(signedAggregateTransaction)),
182+
signedAggregateTransaction: SignedTransaction,
183+
listener: Listener): Observable<AggregateTransaction> {
184+
return this.announce(signedHashLockTransaction, listener).pipe(
185+
flatMap(() => this.announceAggregateBonded(signedAggregateTransaction, listener)),
182186
);
183187

184188
}

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)