|
| 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 { assert } from 'chai'; |
| 18 | +import { Listener } from '../../src/infrastructure/Listener'; |
| 19 | +import { NamespaceHttp } from '../../src/infrastructure/NamespaceHttp'; |
| 20 | +import { TransactionHttp } from '../../src/infrastructure/TransactionHttp'; |
| 21 | +import { Account } from '../../src/model/account/Account'; |
| 22 | +import { NetworkType } from '../../src/model/blockchain/NetworkType'; |
| 23 | +import { PlainMessage } from '../../src/model/message/PlainMessage'; |
| 24 | +import { Mosaic, MosaicSupplyChangeAction, MosaicSupplyChangeTransaction, Address } from '../../src/model/model'; |
| 25 | +import { MosaicFlags } from '../../src/model/mosaic/MosaicFlags'; |
| 26 | +import { MosaicId } from '../../src/model/mosaic/MosaicId'; |
| 27 | +import { MosaicNonce } from '../../src/model/mosaic/MosaicNonce'; |
| 28 | +import { NetworkCurrencyMosaic } from '../../src/model/mosaic/NetworkCurrencyMosaic'; |
| 29 | +import { AliasAction } from '../../src/model/namespace/AliasAction'; |
| 30 | +import { NamespaceId } from '../../src/model/namespace/NamespaceId'; |
| 31 | +import { AddressAliasTransaction } from '../../src/model/transaction/AddressAliasTransaction'; |
| 32 | +import { Deadline } from '../../src/model/transaction/Deadline'; |
| 33 | +import { MosaicAliasTransaction } from '../../src/model/transaction/MosaicAliasTransaction'; |
| 34 | +import { MosaicDefinitionTransaction } from '../../src/model/transaction/MosaicDefinitionTransaction'; |
| 35 | +import { NamespaceRegistrationTransaction } from '../../src/model/transaction/NamespaceRegistrationTransaction'; |
| 36 | +import { TransferTransaction } from '../../src/model/transaction/TransferTransaction'; |
| 37 | +import { UInt64 } from '../../src/model/UInt64'; |
| 38 | +import { TransactionService } from '../../src/service/TransactionService'; |
| 39 | +import { expect } from 'chai'; |
| 40 | + |
| 41 | +describe('TransactionService', () => { |
| 42 | + let account: Account; |
| 43 | + let account2: Account; |
| 44 | + let url: string; |
| 45 | + let generationHash: string; |
| 46 | + let namespaceHttp: NamespaceHttp; |
| 47 | + let addressAlias: NamespaceId; |
| 48 | + let mosaicAlias: NamespaceId; |
| 49 | + let mosaicId: MosaicId; |
| 50 | + let transactionHttp: TransactionHttp; |
| 51 | + let config; |
| 52 | + let transactionHashes: string[]; |
| 53 | + |
| 54 | + before((done) => { |
| 55 | + const path = require('path'); |
| 56 | + require('fs').readFile(path.resolve(__dirname, '../conf/network.conf'), (err, data) => { |
| 57 | + if (err) { |
| 58 | + throw err; |
| 59 | + } |
| 60 | + const json = JSON.parse(data); |
| 61 | + config = json; |
| 62 | + account = Account.createFromPrivateKey(json.testAccount.privateKey, NetworkType.MIJIN_TEST); |
| 63 | + account2 = Account.createFromPrivateKey(json.testAccount2.privateKey, NetworkType.MIJIN_TEST); |
| 64 | + url = json.apiUrl; |
| 65 | + generationHash = json.generationHash; |
| 66 | + namespaceHttp = new NamespaceHttp(json.apiUrl); |
| 67 | + transactionHttp = new TransactionHttp(json.apiUrl); |
| 68 | + transactionHashes = []; |
| 69 | + done(); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + /** |
| 74 | + * ========================= |
| 75 | + * Setup test data |
| 76 | + * ========================= |
| 77 | + */ |
| 78 | + describe('Create address alias NamespaceId', () => { |
| 79 | + let listener: Listener; |
| 80 | + before (() => { |
| 81 | + listener = new Listener(config.apiUrl); |
| 82 | + return listener.open(); |
| 83 | + }); |
| 84 | + after(() => { |
| 85 | + return listener.close(); |
| 86 | + }); |
| 87 | + it('Announce NamespaceRegistrationTransaction', (done) => { |
| 88 | + const namespaceName = 'root-test-namespace-' + Math.floor(Math.random() * 10000); |
| 89 | + const registerNamespaceTransaction = NamespaceRegistrationTransaction.createRootNamespace( |
| 90 | + Deadline.create(), |
| 91 | + namespaceName, |
| 92 | + UInt64.fromUint(9), |
| 93 | + NetworkType.MIJIN_TEST, |
| 94 | + ); |
| 95 | + addressAlias = new NamespaceId(namespaceName); |
| 96 | + const signedTransaction = registerNamespaceTransaction.signWith(account, generationHash); |
| 97 | + |
| 98 | + listener.confirmed(account.address).subscribe(() => { |
| 99 | + done(); |
| 100 | + }); |
| 101 | + listener.status(account.address).subscribe((error) => { |
| 102 | + console.log('Error:', error); |
| 103 | + assert(false); |
| 104 | + done(); |
| 105 | + }); |
| 106 | + transactionHttp.announce(signedTransaction); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + describe('Create mosaic alias NamespaceId', () => { |
| 111 | + let listener: Listener; |
| 112 | + before (() => { |
| 113 | + listener = new Listener(config.apiUrl); |
| 114 | + return listener.open(); |
| 115 | + }); |
| 116 | + after(() => { |
| 117 | + return listener.close(); |
| 118 | + }); |
| 119 | + it('Announce NamespaceRegistrationTransaction', (done) => { |
| 120 | + const namespaceName = 'root-test-namespace-' + Math.floor(Math.random() * 10000); |
| 121 | + const registerNamespaceTransaction = NamespaceRegistrationTransaction.createRootNamespace( |
| 122 | + Deadline.create(), |
| 123 | + namespaceName, |
| 124 | + UInt64.fromUint(9), |
| 125 | + NetworkType.MIJIN_TEST, |
| 126 | + ); |
| 127 | + mosaicAlias = new NamespaceId(namespaceName); |
| 128 | + const signedTransaction = registerNamespaceTransaction.signWith(account, generationHash); |
| 129 | + |
| 130 | + listener.confirmed(account.address).subscribe(() => { |
| 131 | + done(); |
| 132 | + }); |
| 133 | + listener.status(account.address).subscribe((error) => { |
| 134 | + console.log('Error:', error); |
| 135 | + assert(false); |
| 136 | + done(); |
| 137 | + }); |
| 138 | + transactionHttp.announce(signedTransaction); |
| 139 | + }); |
| 140 | + }); |
| 141 | + |
| 142 | + describe('Setup test AddressAlias', () => { |
| 143 | + let listener: Listener; |
| 144 | + before (() => { |
| 145 | + listener = new Listener(config.apiUrl); |
| 146 | + return listener.open(); |
| 147 | + }); |
| 148 | + after(() => { |
| 149 | + return listener.close(); |
| 150 | + }); |
| 151 | + |
| 152 | + it('Announce addressAliasTransaction', (done) => { |
| 153 | + const addressAliasTransaction = AddressAliasTransaction.create( |
| 154 | + Deadline.create(), |
| 155 | + AliasAction.Link, |
| 156 | + addressAlias, |
| 157 | + account.address, |
| 158 | + NetworkType.MIJIN_TEST, |
| 159 | + ); |
| 160 | + const signedTransaction = addressAliasTransaction.signWith(account, generationHash); |
| 161 | + |
| 162 | + listener.confirmed(account.address).subscribe(() => { |
| 163 | + done(); |
| 164 | + }); |
| 165 | + listener.status(account.address).subscribe((error) => { |
| 166 | + console.log('Error:', error); |
| 167 | + assert(false); |
| 168 | + done(); |
| 169 | + }); |
| 170 | + transactionHttp.announce(signedTransaction); |
| 171 | + }); |
| 172 | + }); |
| 173 | + |
| 174 | + describe('Setup test MosaicId', () => { |
| 175 | + let listener: Listener; |
| 176 | + before (() => { |
| 177 | + listener = new Listener(config.apiUrl); |
| 178 | + return listener.open(); |
| 179 | + }); |
| 180 | + after(() => { |
| 181 | + return listener.close(); |
| 182 | + }); |
| 183 | + it('Announce MosaicDefinitionTransaction', (done) => { |
| 184 | + const nonce = MosaicNonce.createRandom(); |
| 185 | + mosaicId = MosaicId.createFromNonce(nonce, account.publicAccount); |
| 186 | + const mosaicDefinitionTransaction = MosaicDefinitionTransaction.create( |
| 187 | + Deadline.create(), |
| 188 | + nonce, |
| 189 | + mosaicId, |
| 190 | + MosaicFlags.create(true, true, false), |
| 191 | + 3, |
| 192 | + UInt64.fromUint(0), |
| 193 | + NetworkType.MIJIN_TEST, |
| 194 | + ); |
| 195 | + const signedTransaction = mosaicDefinitionTransaction.signWith(account, generationHash); |
| 196 | + |
| 197 | + listener.confirmed(account.address).subscribe(() => { |
| 198 | + done(); |
| 199 | + }); |
| 200 | + transactionHttp.announce(signedTransaction); |
| 201 | + }); |
| 202 | + }); |
| 203 | + |
| 204 | + describe('MosaicSupplyChangeTransaction', () => { |
| 205 | + let listener: Listener; |
| 206 | + before (() => { |
| 207 | + listener = new Listener(config.apiUrl); |
| 208 | + return listener.open(); |
| 209 | + }); |
| 210 | + after(() => { |
| 211 | + return listener.close(); |
| 212 | + }); |
| 213 | + it('standalone', (done) => { |
| 214 | + const mosaicSupplyChangeTransaction = MosaicSupplyChangeTransaction.create( |
| 215 | + Deadline.create(), |
| 216 | + mosaicId, |
| 217 | + MosaicSupplyChangeAction.Increase, |
| 218 | + UInt64.fromUint(10), |
| 219 | + NetworkType.MIJIN_TEST, |
| 220 | + ); |
| 221 | + const signedTransaction = mosaicSupplyChangeTransaction.signWith(account, generationHash); |
| 222 | + listener.confirmed(account.address).subscribe(() => { |
| 223 | + done(); |
| 224 | + }); |
| 225 | + listener.status(account.address).subscribe((error) => { |
| 226 | + console.log('Error:', error); |
| 227 | + assert(false); |
| 228 | + done(); |
| 229 | + }); |
| 230 | + transactionHttp.announce(signedTransaction); |
| 231 | + }); |
| 232 | + }); |
| 233 | + |
| 234 | + describe('Setup MosaicAlias', () => { |
| 235 | + let listener: Listener; |
| 236 | + before (() => { |
| 237 | + listener = new Listener(config.apiUrl); |
| 238 | + return listener.open(); |
| 239 | + }); |
| 240 | + after(() => { |
| 241 | + return listener.close(); |
| 242 | + }); |
| 243 | + |
| 244 | + it('Announce MosaicAliasTransaction', (done) => { |
| 245 | + const mosaicAliasTransaction = MosaicAliasTransaction.create( |
| 246 | + Deadline.create(), |
| 247 | + AliasAction.Link, |
| 248 | + mosaicAlias, |
| 249 | + mosaicId, |
| 250 | + NetworkType.MIJIN_TEST, |
| 251 | + ); |
| 252 | + const signedTransaction = mosaicAliasTransaction.signWith(account, generationHash); |
| 253 | + |
| 254 | + listener.confirmed(account.address).subscribe(() => { |
| 255 | + done(); |
| 256 | + }); |
| 257 | + listener.status(account.address).subscribe((error) => { |
| 258 | + console.log('Error:', error); |
| 259 | + assert(false); |
| 260 | + done(); |
| 261 | + }); |
| 262 | + transactionHttp.announce(signedTransaction); |
| 263 | + }); |
| 264 | + }); |
| 265 | + describe('Create Transfer with alias', () => { |
| 266 | + let listener: Listener; |
| 267 | + before (() => { |
| 268 | + listener = new Listener(config.apiUrl); |
| 269 | + return listener.open(); |
| 270 | + }); |
| 271 | + after(() => { |
| 272 | + return listener.close(); |
| 273 | + }); |
| 274 | + |
| 275 | + it('Announce TransferTransaction', (done) => { |
| 276 | + const transferTransaction = TransferTransaction.create( |
| 277 | + Deadline.create(), |
| 278 | + addressAlias, |
| 279 | + [ |
| 280 | + // NetworkCurrencyMosaic.createAbsolute(1), //Seems get banned on rest if passing multiple mosaic alias in |
| 281 | + new Mosaic(mosaicAlias, UInt64.fromUint(1)), |
| 282 | + ], |
| 283 | + PlainMessage.create('test-message'), |
| 284 | + NetworkType.MIJIN_TEST, |
| 285 | + ); |
| 286 | + const signedTransaction = transferTransaction.signWith(account, generationHash); |
| 287 | + |
| 288 | + transactionHashes.push(signedTransaction.hash); |
| 289 | + |
| 290 | + listener.confirmed(account.address).subscribe(() => { |
| 291 | + done(); |
| 292 | + }); |
| 293 | + listener.status(account.address).subscribe((error) => { |
| 294 | + console.log('Error:', error); |
| 295 | + assert(false); |
| 296 | + done(); |
| 297 | + }); |
| 298 | + transactionHttp.announce(signedTransaction); |
| 299 | + }); |
| 300 | + }); |
| 301 | + describe('should return resolved transaction', () => { |
| 302 | + it('call transaction service', (done) => { |
| 303 | + const transactionService = new TransactionService(url); |
| 304 | + return transactionService.resolveAliases(transactionHashes).subscribe((transactions) => { |
| 305 | + transactions.map((tx: TransferTransaction) => { |
| 306 | + expect((tx.recipientAddress as Address).plain()).to.be.equal(account.address.plain()); |
| 307 | + expect(tx.mosaics.find((m) => m.id.toHex() === mosaicId.toHex())).not.to.equal(undefined); |
| 308 | + }); |
| 309 | + done(); |
| 310 | + }); |
| 311 | + }); |
| 312 | + }); |
| 313 | +}); |
0 commit comments