|
| 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 | +}); |
0 commit comments