|
| 1 | +import { assert, expect } from 'chai'; |
| 2 | +import { Convert } from '../../src/core/format/Convert'; |
| 3 | +import { Listener } from '../../src/infrastructure/Listener'; |
| 4 | +import { MetadataHttp } from '../../src/infrastructure/MetadataHttp'; |
| 5 | +import { TransactionHttp } from '../../src/infrastructure/TransactionHttp'; |
| 6 | +import { Account } from '../../src/model/account/Account'; |
| 7 | +import { NetworkType } from '../../src/model/blockchain/NetworkType'; |
| 8 | +import { MetadataType } from '../../src/model/metadata/MetadataType'; |
| 9 | +import { MosaicFlags } from '../../src/model/mosaic/MosaicFlags'; |
| 10 | +import { MosaicId } from '../../src/model/mosaic/MosaicId'; |
| 11 | +import { MosaicNonce } from '../../src/model/mosaic/MosaicNonce'; |
| 12 | +import { NamespaceId } from '../../src/model/namespace/NamespaceId'; |
| 13 | +import { AccountMetadataTransaction } from '../../src/model/transaction/AccountMetadataTransaction'; |
| 14 | +import { AggregateTransaction } from '../../src/model/transaction/AggregateTransaction'; |
| 15 | +import { Deadline } from '../../src/model/transaction/Deadline'; |
| 16 | +import { MosaicDefinitionTransaction } from '../../src/model/transaction/MosaicDefinitionTransaction'; |
| 17 | +import { MosaicMetadataTransaction } from '../../src/model/transaction/MosaicMetadataTransaction'; |
| 18 | +import { NamespaceMetadataTransaction } from '../../src/model/transaction/NamespaceMetadataTransaction'; |
| 19 | +import { NamespaceRegistrationTransaction } from '../../src/model/transaction/NamespaceRegistrationTransaction'; |
| 20 | +import { TransactionType } from '../../src/model/transaction/TransactionType'; |
| 21 | +import { UInt64 } from '../../src/model/UInt64'; |
| 22 | +import { MetadataTransactionService } from '../../src/service/MetadataTransactionService'; |
| 23 | + |
| 24 | +describe('MetadataTransactionService', () => { |
| 25 | + const deadline = Deadline.create(); |
| 26 | + const key = UInt64.fromUint(123); |
| 27 | + const newValue = 'new test value'; |
| 28 | + let targetAccount: Account; |
| 29 | + let metadataHttp: MetadataHttp; |
| 30 | + let transactionHttp: TransactionHttp; |
| 31 | + let mosaicId: MosaicId; |
| 32 | + let namespaceId: NamespaceId; |
| 33 | + let config; |
| 34 | + let generationHash: string; |
| 35 | + |
| 36 | + before((done) => { |
| 37 | + const path = require('path'); |
| 38 | + require('fs').readFile(path.resolve(__dirname, '../conf/network.conf'), (err, data) => { |
| 39 | + if (err) { |
| 40 | + throw err; |
| 41 | + } |
| 42 | + const json = JSON.parse(data); |
| 43 | + config = json; |
| 44 | + targetAccount = Account.createFromPrivateKey(json.testAccount.privateKey, NetworkType.MIJIN_TEST); |
| 45 | + generationHash = json.generationHash; |
| 46 | + metadataHttp = new MetadataHttp(json.apiUrl); |
| 47 | + transactionHttp = new TransactionHttp(json.apiUrl); |
| 48 | + done(); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + /** |
| 53 | + * ========================= |
| 54 | + * Setup test data |
| 55 | + * ========================= |
| 56 | + */ |
| 57 | + |
| 58 | + describe('MosaicDefinitionTransaction', () => { |
| 59 | + let listener: Listener; |
| 60 | + before (() => { |
| 61 | + listener = new Listener(config.apiUrl); |
| 62 | + return listener.open(); |
| 63 | + }); |
| 64 | + after(() => { |
| 65 | + return listener.close(); |
| 66 | + }); |
| 67 | + it('standalone', (done) => { |
| 68 | + const nonce = MosaicNonce.createRandom(); |
| 69 | + mosaicId = MosaicId.createFromNonce(nonce, targetAccount.publicAccount); |
| 70 | + const mosaicDefinitionTransaction = MosaicDefinitionTransaction.create( |
| 71 | + Deadline.create(), |
| 72 | + nonce, |
| 73 | + mosaicId, |
| 74 | + MosaicFlags.create( true, true, true), |
| 75 | + 3, |
| 76 | + UInt64.fromUint(1000), |
| 77 | + NetworkType.MIJIN_TEST, |
| 78 | + ); |
| 79 | + const signedTransaction = mosaicDefinitionTransaction.signWith(targetAccount, generationHash); |
| 80 | + listener.confirmed(targetAccount.address).subscribe(() => { |
| 81 | + done(); |
| 82 | + }); |
| 83 | + listener.status(targetAccount.address).subscribe((error) => { |
| 84 | + console.log('Error:', error); |
| 85 | + done(); |
| 86 | + }); |
| 87 | + transactionHttp.announce(signedTransaction); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('Setup test NamespaceId', () => { |
| 92 | + let listener: Listener; |
| 93 | + before (() => { |
| 94 | + listener = new Listener(config.apiUrl); |
| 95 | + return listener.open(); |
| 96 | + }); |
| 97 | + after(() => { |
| 98 | + return listener.close(); |
| 99 | + }); |
| 100 | + it('Announce NamespaceRegistrationTransaction', (done) => { |
| 101 | + const namespaceName = 'root-test-namespace-' + Math.floor(Math.random() * 10000); |
| 102 | + const registerNamespaceTransaction = NamespaceRegistrationTransaction.createRootNamespace( |
| 103 | + Deadline.create(), |
| 104 | + namespaceName, |
| 105 | + UInt64.fromUint(9), |
| 106 | + NetworkType.MIJIN_TEST, |
| 107 | + ); |
| 108 | + namespaceId = new NamespaceId(namespaceName); |
| 109 | + const signedTransaction = registerNamespaceTransaction.signWith(targetAccount, generationHash); |
| 110 | + listener.confirmed(targetAccount.address).subscribe(() => { |
| 111 | + done(); |
| 112 | + }); |
| 113 | + listener.status(targetAccount.address).subscribe((error) => { |
| 114 | + console.log('Error:', error); |
| 115 | + done(); |
| 116 | + }); |
| 117 | + transactionHttp.announce(signedTransaction); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + describe('MosaicMetadataTransaction', () => { |
| 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('aggregate', (done) => { |
| 131 | + const mosaicMetadataTransaction = MosaicMetadataTransaction.create( |
| 132 | + Deadline.create(), |
| 133 | + targetAccount.publicKey, |
| 134 | + key, |
| 135 | + mosaicId, |
| 136 | + newValue.length, |
| 137 | + newValue, |
| 138 | + NetworkType.MIJIN_TEST, |
| 139 | + ); |
| 140 | + |
| 141 | + const aggregateTransaction = AggregateTransaction.createComplete(Deadline.create(), |
| 142 | + [mosaicMetadataTransaction.toAggregate(targetAccount.publicAccount)], |
| 143 | + NetworkType.MIJIN_TEST, |
| 144 | + [], |
| 145 | + ); |
| 146 | + const signedTransaction = aggregateTransaction.signWith(targetAccount, generationHash); |
| 147 | + listener.confirmed(targetAccount.address).subscribe(() => { |
| 148 | + done(); |
| 149 | + }); |
| 150 | + listener.status(targetAccount.address).subscribe((error) => { |
| 151 | + console.log('Error:', error); |
| 152 | + done(); |
| 153 | + }); |
| 154 | + transactionHttp.announce(signedTransaction); |
| 155 | + }); |
| 156 | + }); |
| 157 | + |
| 158 | + describe('NamespaceMetadataTransaction', () => { |
| 159 | + let listener: Listener; |
| 160 | + before (() => { |
| 161 | + listener = new Listener(config.apiUrl); |
| 162 | + return listener.open(); |
| 163 | + }); |
| 164 | + after(() => { |
| 165 | + return listener.close(); |
| 166 | + }); |
| 167 | + it('aggregate', (done) => { |
| 168 | + const namespaceMetadataTransaction = NamespaceMetadataTransaction.create( |
| 169 | + Deadline.create(), |
| 170 | + targetAccount.publicKey, |
| 171 | + key, |
| 172 | + namespaceId, |
| 173 | + newValue.length, |
| 174 | + newValue, |
| 175 | + NetworkType.MIJIN_TEST, |
| 176 | + ); |
| 177 | + |
| 178 | + const aggregateTransaction = AggregateTransaction.createComplete(Deadline.create(), |
| 179 | + [namespaceMetadataTransaction.toAggregate(targetAccount.publicAccount)], |
| 180 | + NetworkType.MIJIN_TEST, |
| 181 | + [], |
| 182 | + ); |
| 183 | + const signedTransaction = aggregateTransaction.signWith(targetAccount, generationHash); |
| 184 | + listener.confirmed(targetAccount.address).subscribe(() => { |
| 185 | + done(); |
| 186 | + }); |
| 187 | + listener.status(targetAccount.address).subscribe((error) => { |
| 188 | + console.log('Error:', error); |
| 189 | + done(); |
| 190 | + }); |
| 191 | + transactionHttp.announce(signedTransaction); |
| 192 | + }); |
| 193 | + }); |
| 194 | + |
| 195 | + /** |
| 196 | + * ========================= |
| 197 | + * Test |
| 198 | + * ========================= |
| 199 | + */ |
| 200 | + describe('Test new services', () => { |
| 201 | + it('should create AccountMetadataTransaction - no current metadata', (done) => { |
| 202 | + const metaDataService = new MetadataTransactionService(metadataHttp); |
| 203 | + |
| 204 | + return metaDataService.createMetadataTransaction( |
| 205 | + deadline, |
| 206 | + NetworkType.MIJIN_TEST, |
| 207 | + MetadataType.Account, |
| 208 | + targetAccount.publicAccount, |
| 209 | + key.toHex(), |
| 210 | + newValue, |
| 211 | + targetAccount.publicAccount, |
| 212 | + ).subscribe((transaction: AccountMetadataTransaction) => { |
| 213 | + expect(transaction.type).to.be.equal(TransactionType.ACCOUNT_METADATA_TRANSACTION); |
| 214 | + expect(transaction.scopedMetadataKey.toHex()).to.be.equal(key.toHex()); |
| 215 | + expect(transaction.value).to.be.equal(newValue); |
| 216 | + expect(transaction.targetPublicKey).to.be.equal(targetAccount.publicKey); |
| 217 | + done(); |
| 218 | + }); |
| 219 | + }); |
| 220 | + it('should create MosaicMetadataTransaction', (done) => { |
| 221 | + const metaDataService = new MetadataTransactionService(metadataHttp); |
| 222 | + |
| 223 | + return metaDataService.createMetadataTransaction( |
| 224 | + deadline, |
| 225 | + NetworkType.MIJIN_TEST, |
| 226 | + MetadataType.Mosaic, |
| 227 | + targetAccount.publicAccount, |
| 228 | + key.toHex(), |
| 229 | + newValue + 'delta', |
| 230 | + targetAccount.publicAccount, |
| 231 | + mosaicId, |
| 232 | + ).subscribe((transaction: MosaicMetadataTransaction) => { |
| 233 | + expect(transaction.type).to.be.equal(TransactionType.MOSAIC_METADATA_TRANSACTION); |
| 234 | + expect(transaction.scopedMetadataKey.toHex()).to.be.equal(key.toHex()); |
| 235 | + expect(transaction.valueSizeDelta).to.be.equal(5); |
| 236 | + expect(transaction.value).to.be.equal(newValue + 'delta'); |
| 237 | + expect(transaction.targetPublicKey).to.be.equal(targetAccount.publicKey); |
| 238 | + expect(transaction.targetMosaicId.toHex()).to.be.equal(mosaicId.toHex()); |
| 239 | + done(); |
| 240 | + }); |
| 241 | + }); |
| 242 | + it('should create NamespaceMetadataTransaction', (done) => { |
| 243 | + const metaDataService = new MetadataTransactionService(metadataHttp); |
| 244 | + |
| 245 | + return metaDataService.createMetadataTransaction( |
| 246 | + deadline, |
| 247 | + NetworkType.MIJIN_TEST, |
| 248 | + MetadataType.Namespace, |
| 249 | + targetAccount.publicAccount, |
| 250 | + key.toHex(), |
| 251 | + newValue + 'delta', |
| 252 | + targetAccount.publicAccount, |
| 253 | + namespaceId, |
| 254 | + ).subscribe((transaction: NamespaceMetadataTransaction) => { |
| 255 | + expect(transaction.type).to.be.equal(TransactionType.NAMESPACE_METADATA_TRANSACTION); |
| 256 | + expect(transaction.scopedMetadataKey.toHex()).to.be.equal(key.toHex()); |
| 257 | + expect(transaction.valueSizeDelta).to.be.equal(5); |
| 258 | + expect(transaction.value).to.be.equal(newValue + 'delta'); |
| 259 | + expect(transaction.targetPublicKey).to.be.equal(targetAccount.publicKey); |
| 260 | + expect(transaction.targetNamespaceId.toHex()).to.be.equal(namespaceId.toHex()); |
| 261 | + done(); |
| 262 | + }); |
| 263 | + }); |
| 264 | + }); |
| 265 | + |
| 266 | + describe('Announce transaction through service', () => { |
| 267 | + let listener: Listener; |
| 268 | + before (() => { |
| 269 | + listener = new Listener(config.apiUrl); |
| 270 | + return listener.open(); |
| 271 | + }); |
| 272 | + after(() => { |
| 273 | + return listener.close(); |
| 274 | + }); |
| 275 | + it('should create MosaicMetadataTransaction and announce', (done) => { |
| 276 | + const metaDataService = new MetadataTransactionService(metadataHttp); |
| 277 | + |
| 278 | + return metaDataService.createMetadataTransaction( |
| 279 | + deadline, |
| 280 | + NetworkType.MIJIN_TEST, |
| 281 | + MetadataType.Mosaic, |
| 282 | + targetAccount.publicAccount, |
| 283 | + key.toHex(), |
| 284 | + newValue + 'delta', |
| 285 | + targetAccount.publicAccount, |
| 286 | + mosaicId, |
| 287 | + ).subscribe((transaction: MosaicMetadataTransaction) => { |
| 288 | + const aggregateTransaction = AggregateTransaction.createComplete(Deadline.create(), |
| 289 | + [transaction.toAggregate(targetAccount.publicAccount)], |
| 290 | + NetworkType.MIJIN_TEST, |
| 291 | + [], |
| 292 | + ); |
| 293 | + const signedTransaction = aggregateTransaction.signWith(targetAccount, generationHash); |
| 294 | + listener.confirmed(targetAccount.address).subscribe(() => { |
| 295 | + done(); |
| 296 | + }); |
| 297 | + listener.status(targetAccount.address).subscribe((error) => { |
| 298 | + console.log('Error:', error); |
| 299 | + assert(false); |
| 300 | + done(); |
| 301 | + }); |
| 302 | + transactionHttp.announce(signedTransaction); |
| 303 | + }); |
| 304 | + }); |
| 305 | + }); |
| 306 | +}); |
0 commit comments