|
| 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, expect} from 'chai'; |
| 18 | +import { Convert } from '../../src/core/format/Convert'; |
| 19 | +import { Listener, TransactionHttp } from '../../src/infrastructure/infrastructure'; |
| 20 | +import { MetadataHttp } from '../../src/infrastructure/MetadataHttp'; |
| 21 | +import { Account } from '../../src/model/account/Account'; |
| 22 | +import {Address} from '../../src/model/account/Address'; |
| 23 | +import {PublicAccount} from '../../src/model/account/PublicAccount'; |
| 24 | +import {NetworkType} from '../../src/model/blockchain/NetworkType'; |
| 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 { NamespaceId } from '../../src/model/namespace/NamespaceId'; |
| 29 | +import { AccountMetadataTransaction } from '../../src/model/transaction/AccountMetadataTransaction'; |
| 30 | +import { AggregateTransaction } from '../../src/model/transaction/AggregateTransaction'; |
| 31 | +import { Deadline } from '../../src/model/transaction/Deadline'; |
| 32 | +import { MosaicDefinitionTransaction } from '../../src/model/transaction/MosaicDefinitionTransaction'; |
| 33 | +import { MosaicMetadataTransaction } from '../../src/model/transaction/MosaicMetadataTransaction'; |
| 34 | +import { NamespaceMetadataTransaction } from '../../src/model/transaction/NamespaceMetadataTransaction'; |
| 35 | +import { NamespaceRegistrationTransaction } from '../../src/model/transaction/NamespaceRegistrationTransaction'; |
| 36 | +import { UInt64 } from '../../src/model/UInt64'; |
| 37 | + |
| 38 | +describe('MetadataHttp', () => { |
| 39 | + let account: Account; |
| 40 | + let account2: Account; |
| 41 | + let account3: Account; |
| 42 | + let multisigAccount: Account; |
| 43 | + let cosignAccount1: Account; |
| 44 | + let cosignAccount2: Account; |
| 45 | + let cosignAccount3: Account; |
| 46 | + let accountAddress: Address; |
| 47 | + let accountPublicKey: string; |
| 48 | + let publicAccount: PublicAccount; |
| 49 | + let metadataHttp: MetadataHttp; |
| 50 | + let transactionHttp: TransactionHttp; |
| 51 | + let mosaicId: MosaicId; |
| 52 | + let namespaceId: NamespaceId; |
| 53 | + let generationHash: string; |
| 54 | + let config; |
| 55 | + |
| 56 | + before((done) => { |
| 57 | + const path = require('path'); |
| 58 | + require('fs').readFile(path.resolve(__dirname, '../conf/network.conf'), (err, data) => { |
| 59 | + if (err) { |
| 60 | + throw err; |
| 61 | + } |
| 62 | + const json = JSON.parse(data); |
| 63 | + config = json; |
| 64 | + account = Account.createFromPrivateKey(json.testAccount.privateKey, NetworkType.MIJIN_TEST); |
| 65 | + account2 = Account.createFromPrivateKey(json.testAccount2.privateKey, NetworkType.MIJIN_TEST); |
| 66 | + account3 = Account.createFromPrivateKey(json.testAccount3.privateKey, NetworkType.MIJIN_TEST); |
| 67 | + multisigAccount = Account.createFromPrivateKey(json.multisigAccount.privateKey, NetworkType.MIJIN_TEST); |
| 68 | + cosignAccount1 = Account.createFromPrivateKey(json.cosignatoryAccount.privateKey, NetworkType.MIJIN_TEST); |
| 69 | + cosignAccount2 = Account.createFromPrivateKey(json.cosignatory2Account.privateKey, NetworkType.MIJIN_TEST); |
| 70 | + cosignAccount3 = Account.createFromPrivateKey(json.cosignatory3Account.privateKey, NetworkType.MIJIN_TEST); |
| 71 | + accountAddress = Address.createFromRawAddress(json.testAccount.address); |
| 72 | + accountPublicKey = json.testAccount.publicKey; |
| 73 | + publicAccount = PublicAccount.createFromPublicKey(json.testAccount.publicKey, NetworkType.MIJIN_TEST); |
| 74 | + generationHash = json.generationHash; |
| 75 | + metadataHttp = new MetadataHttp(json.apiUrl); |
| 76 | + transactionHttp = new TransactionHttp(json.apiUrl); |
| 77 | + done(); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + /** |
| 82 | + * ========================= |
| 83 | + * Setup test data |
| 84 | + * ========================= |
| 85 | + */ |
| 86 | + |
| 87 | + describe('MosaicDefinitionTransaction', () => { |
| 88 | + let listener: Listener; |
| 89 | + before (() => { |
| 90 | + listener = new Listener(config.apiUrl); |
| 91 | + return listener.open(); |
| 92 | + }); |
| 93 | + after(() => { |
| 94 | + return listener.close(); |
| 95 | + }); |
| 96 | + it('standalone', (done) => { |
| 97 | + const nonce = MosaicNonce.createRandom(); |
| 98 | + mosaicId = MosaicId.createFromNonce(nonce, account.publicAccount); |
| 99 | + const mosaicDefinitionTransaction = MosaicDefinitionTransaction.create( |
| 100 | + Deadline.create(), |
| 101 | + nonce, |
| 102 | + mosaicId, |
| 103 | + MosaicFlags.create( true, true, true), |
| 104 | + 3, |
| 105 | + UInt64.fromUint(1000), |
| 106 | + NetworkType.MIJIN_TEST, |
| 107 | + ); |
| 108 | + const signedTransaction = mosaicDefinitionTransaction.signWith(account, generationHash); |
| 109 | + listener.confirmed(account.address).subscribe(() => { |
| 110 | + done(); |
| 111 | + }); |
| 112 | + listener.status(account.address).subscribe((error) => { |
| 113 | + console.log('Error:', error); |
| 114 | + assert(false); |
| 115 | + done(); |
| 116 | + }); |
| 117 | + transactionHttp.announce(signedTransaction); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + describe('Setup test NamespaceId', () => { |
| 122 | + let listener: Listener; |
| 123 | + before (() => { |
| 124 | + listener = new Listener(config.apiUrl); |
| 125 | + return listener.open(); |
| 126 | + }); |
| 127 | + after(() => { |
| 128 | + return listener.close(); |
| 129 | + }); |
| 130 | + it('Announce NamespaceRegistrationTransaction', (done) => { |
| 131 | + const namespaceName = 'root-test-namespace-' + Math.floor(Math.random() * 10000); |
| 132 | + const registerNamespaceTransaction = NamespaceRegistrationTransaction.createRootNamespace( |
| 133 | + Deadline.create(), |
| 134 | + namespaceName, |
| 135 | + UInt64.fromUint(9), |
| 136 | + NetworkType.MIJIN_TEST, |
| 137 | + ); |
| 138 | + namespaceId = new NamespaceId(namespaceName); |
| 139 | + const signedTransaction = registerNamespaceTransaction.signWith(account, generationHash); |
| 140 | + listener.confirmed(account.address).subscribe(() => { |
| 141 | + done(); |
| 142 | + }); |
| 143 | + listener.status(account.address).subscribe((error) => { |
| 144 | + console.log('Error:', error); |
| 145 | + assert(false); |
| 146 | + done(); |
| 147 | + }); |
| 148 | + transactionHttp.announce(signedTransaction); |
| 149 | + }); |
| 150 | + }); |
| 151 | + |
| 152 | + describe('AccountMetadataTransaction', () => { |
| 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('aggregate', (done) => { |
| 162 | + const accountMetadataTransaction = AccountMetadataTransaction.create( |
| 163 | + Deadline.create(), |
| 164 | + account.publicKey, |
| 165 | + UInt64.fromUint(5), |
| 166 | + 10, |
| 167 | + Convert.uint8ToUtf8(new Uint8Array(10)), |
| 168 | + NetworkType.MIJIN_TEST, |
| 169 | + ); |
| 170 | + |
| 171 | + const aggregateTransaction = AggregateTransaction.createComplete(Deadline.create(), |
| 172 | + [accountMetadataTransaction.toAggregate(account.publicAccount)], |
| 173 | + NetworkType.MIJIN_TEST, |
| 174 | + [], |
| 175 | + ); |
| 176 | + const signedTransaction = aggregateTransaction.signWith(account, generationHash); |
| 177 | + listener.confirmed(account.address).subscribe(() => { |
| 178 | + done(); |
| 179 | + }); |
| 180 | + listener.status(account.address).subscribe((error) => { |
| 181 | + console.log('Error:', error); |
| 182 | + assert(false); |
| 183 | + done(); |
| 184 | + }); |
| 185 | + transactionHttp.announce(signedTransaction); |
| 186 | + }); |
| 187 | + }); |
| 188 | + |
| 189 | + describe('MosaicMetadataTransaction', () => { |
| 190 | + let listener: Listener; |
| 191 | + before (() => { |
| 192 | + listener = new Listener(config.apiUrl); |
| 193 | + return listener.open(); |
| 194 | + }); |
| 195 | + after(() => { |
| 196 | + return listener.close(); |
| 197 | + }); |
| 198 | + it('aggregate', (done) => { |
| 199 | + const mosaicMetadataTransaction = MosaicMetadataTransaction.create( |
| 200 | + Deadline.create(), |
| 201 | + account.publicKey, |
| 202 | + UInt64.fromUint(5), |
| 203 | + mosaicId, |
| 204 | + 10, |
| 205 | + Convert.uint8ToUtf8(new Uint8Array(10)), |
| 206 | + NetworkType.MIJIN_TEST, |
| 207 | + ); |
| 208 | + |
| 209 | + const aggregateTransaction = AggregateTransaction.createComplete(Deadline.create(), |
| 210 | + [mosaicMetadataTransaction.toAggregate(account.publicAccount)], |
| 211 | + NetworkType.MIJIN_TEST, |
| 212 | + [], |
| 213 | + ); |
| 214 | + const signedTransaction = aggregateTransaction.signWith(account, generationHash); |
| 215 | + listener.confirmed(account.address).subscribe(() => { |
| 216 | + done(); |
| 217 | + }); |
| 218 | + listener.status(account.address).subscribe((error) => { |
| 219 | + console.log('Error:', error); |
| 220 | + assert(false); |
| 221 | + done(); |
| 222 | + }); |
| 223 | + transactionHttp.announce(signedTransaction); |
| 224 | + }); |
| 225 | + }); |
| 226 | + |
| 227 | + describe('NamespaceMetadataTransaction', () => { |
| 228 | + let listener: Listener; |
| 229 | + before (() => { |
| 230 | + listener = new Listener(config.apiUrl); |
| 231 | + return listener.open(); |
| 232 | + }); |
| 233 | + after(() => { |
| 234 | + return listener.close(); |
| 235 | + }); |
| 236 | + it('aggregate', (done) => { |
| 237 | + const namespaceMetadataTransaction = NamespaceMetadataTransaction.create( |
| 238 | + Deadline.create(), |
| 239 | + account.publicKey, |
| 240 | + UInt64.fromUint(5), |
| 241 | + namespaceId, |
| 242 | + 10, |
| 243 | + Convert.uint8ToUtf8(new Uint8Array(10)), |
| 244 | + NetworkType.MIJIN_TEST, |
| 245 | + ); |
| 246 | + |
| 247 | + const aggregateTransaction = AggregateTransaction.createComplete(Deadline.create(), |
| 248 | + [namespaceMetadataTransaction.toAggregate(account.publicAccount)], |
| 249 | + NetworkType.MIJIN_TEST, |
| 250 | + [], |
| 251 | + ); |
| 252 | + const signedTransaction = aggregateTransaction.signWith(account, generationHash); |
| 253 | + listener.confirmed(account.address).subscribe(() => { |
| 254 | + done(); |
| 255 | + }); |
| 256 | + listener.status(account.address).subscribe((error) => { |
| 257 | + console.log('Error:', error); |
| 258 | + assert(false); |
| 259 | + done(); |
| 260 | + }); |
| 261 | + transactionHttp.announce(signedTransaction); |
| 262 | + }); |
| 263 | + }); |
| 264 | + |
| 265 | + /** |
| 266 | + * ========================= |
| 267 | + * Tests |
| 268 | + * ========================= |
| 269 | + */ |
| 270 | + |
| 271 | + describe('getAccountMetadata', () => { |
| 272 | + it('should return metadata given a NEM Address', (done) => { |
| 273 | + metadataHttp.getAccountMetadata(accountAddress) |
| 274 | + .subscribe((metadata) => { |
| 275 | + expect(metadata.length).to.be.greaterThan(0); |
| 276 | + done(); |
| 277 | + }); |
| 278 | + }); |
| 279 | + }); |
| 280 | + |
| 281 | + describe('getAccountMetadataByKey', () => { |
| 282 | + it('should return metadata given a NEM Address and metadata key', (done) => { |
| 283 | + metadataHttp.getAccountMetadataByKey(accountAddress, UInt64.fromUint(5).toHex()) |
| 284 | + .subscribe((metadata) => { |
| 285 | + expect(metadata.length).to.be.greaterThan(0); |
| 286 | + done(); |
| 287 | + }); |
| 288 | + }); |
| 289 | + }); |
| 290 | + |
| 291 | + describe('getAccountMetadataByKeyAndSender', () => { |
| 292 | + it('should return metadata given a NEM Address and metadata key and sender public key', (done) => { |
| 293 | + metadataHttp.getAccountMetadataByKeyAndSender(accountAddress, UInt64.fromUint(5).toHex(), account.publicKey) |
| 294 | + .subscribe((metadata) => { |
| 295 | + expect(metadata.metadataEntry.senderPublicKey).to.be.equal(account.publicKey); |
| 296 | + done(); |
| 297 | + }); |
| 298 | + }); |
| 299 | + }); |
| 300 | + |
| 301 | + describe('getMosaicMetadata', () => { |
| 302 | + it('should return metadata given a mosaicId', (done) => { |
| 303 | + metadataHttp.getMosaicMetadata(mosaicId) |
| 304 | + .subscribe((metadata) => { |
| 305 | + expect(metadata.length).to.be.greaterThan(0); |
| 306 | + done(); |
| 307 | + }); |
| 308 | + }); |
| 309 | + }); |
| 310 | + |
| 311 | + describe('getMosaicMetadataByKey', () => { |
| 312 | + it('should return metadata given a mosaicId and metadata key', (done) => { |
| 313 | + metadataHttp.getMosaicMetadataByKey(mosaicId, UInt64.fromUint(5).toHex()) |
| 314 | + .subscribe((metadata) => { |
| 315 | + expect(metadata.length).to.be.greaterThan(0); |
| 316 | + done(); |
| 317 | + }); |
| 318 | + }); |
| 319 | + }); |
| 320 | + |
| 321 | + describe('getMosaicMetadataByKeyAndSender', () => { |
| 322 | + it('should return metadata given a mosaicId and metadata key and sender public key', (done) => { |
| 323 | + metadataHttp.getMosaicMetadataByKeyAndSender(mosaicId, UInt64.fromUint(5).toHex(), account.publicKey) |
| 324 | + .subscribe((metadata) => { |
| 325 | + expect(metadata.metadataEntry.senderPublicKey).to.be.equal(account.publicKey); |
| 326 | + done(); |
| 327 | + }); |
| 328 | + }); |
| 329 | + }); |
| 330 | + |
| 331 | + describe('getNamespaceMetadata', () => { |
| 332 | + it('should return metadata given a namespaceId', (done) => { |
| 333 | + metadataHttp.getNamespaceMetadata(namespaceId) |
| 334 | + .subscribe((metadata) => { |
| 335 | + expect(metadata.length).to.be.greaterThan(0); |
| 336 | + done(); |
| 337 | + }); |
| 338 | + }); |
| 339 | + }); |
| 340 | + |
| 341 | + describe('getNamespaceMetadataByKey', () => { |
| 342 | + it('should return metadata given a namespaceId and metadata key', (done) => { |
| 343 | + metadataHttp.getNamespaceMetadataByKey(namespaceId, UInt64.fromUint(5).toHex()) |
| 344 | + .subscribe((metadata) => { |
| 345 | + expect(metadata.length).to.be.greaterThan(0); |
| 346 | + done(); |
| 347 | + }); |
| 348 | + }); |
| 349 | + }); |
| 350 | + |
| 351 | + describe('getNamespaceMetadataByKeyAndSender', () => { |
| 352 | + it('should return metadata given a namespaceId and metadata key and sender public key', (done) => { |
| 353 | + metadataHttp.getNamespaceMetadataByKeyAndSender(namespaceId, UInt64.fromUint(5).toHex(), account.publicKey) |
| 354 | + .subscribe((metadata) => { |
| 355 | + expect(metadata.metadataEntry.senderPublicKey).to.be.equal(account.publicKey); |
| 356 | + done(); |
| 357 | + }); |
| 358 | + }); |
| 359 | + }); |
| 360 | +}); |
0 commit comments