diff --git a/e2e/service/MosaicRestrictionTransactionService.spec.ts b/e2e/service/MosaicRestrictionTransactionService.spec.ts index facbe26d12..73a54cad3c 100644 --- a/e2e/service/MosaicRestrictionTransactionService.spec.ts +++ b/e2e/service/MosaicRestrictionTransactionService.spec.ts @@ -1,17 +1,23 @@ import { expect } from 'chai'; import { KeyGenerator } from '../../src/core/format/KeyGenerator'; +import { NamespaceRepository } from '../../src/infrastructure/NamespaceRepository'; import { RestrictionMosaicRepository } from '../../src/infrastructure/RestrictionMosaicRepository'; import { Account } from '../../src/model/account/Account'; import { NetworkType } from '../../src/model/blockchain/NetworkType'; import { MosaicFlags } from '../../src/model/mosaic/MosaicFlags'; import { MosaicId } from '../../src/model/mosaic/MosaicId'; import { MosaicNonce } from '../../src/model/mosaic/MosaicNonce'; +import { AliasAction } from '../../src/model/namespace/AliasAction'; +import { NamespaceId } from '../../src/model/namespace/NamespaceId'; import { MosaicRestrictionType } from '../../src/model/restriction/MosaicRestrictionType'; +import { AddressAliasTransaction } from '../../src/model/transaction/AddressAliasTransaction'; import { AggregateTransaction } from '../../src/model/transaction/AggregateTransaction'; import { Deadline } from '../../src/model/transaction/Deadline'; import { MosaicAddressRestrictionTransaction } from '../../src/model/transaction/MosaicAddressRestrictionTransaction'; +import { MosaicAliasTransaction } from '../../src/model/transaction/MosaicAliasTransaction'; import { MosaicDefinitionTransaction } from '../../src/model/transaction/MosaicDefinitionTransaction'; import { MosaicGlobalRestrictionTransaction } from '../../src/model/transaction/MosaicGlobalRestrictionTransaction'; +import { NamespaceRegistrationTransaction } from '../../src/model/transaction/NamespaceRegistrationTransaction'; import { TransactionType } from '../../src/model/transaction/TransactionType'; import { UInt64 } from '../../src/model/UInt64'; import { MosaicRestrictionTransactionService } from '../../src/service/MosaicRestrictionTransactionService'; @@ -22,10 +28,13 @@ describe('MosaicRestrictionTransactionService', () => { const key = KeyGenerator.generateUInt64Key('TestKey'); let account: Account; let restrictionRepository: RestrictionMosaicRepository; + let namespaceRepository: NamespaceRepository; let mosaicId: MosaicId; let generationHash: string; const helper = new IntegrationTestHelper(); let networkType: NetworkType; + let namespaceIdAddress: NamespaceId; + let namespaceIdMosaic: NamespaceId; before(() => { return helper.start().then(() => { @@ -33,6 +42,7 @@ describe('MosaicRestrictionTransactionService', () => { generationHash = helper.generationHash; networkType = helper.networkType; restrictionRepository = helper.repositoryFactory.createRestrictionMosaicRepository(); + namespaceRepository = helper.repositoryFactory.createNamespaceRepository(); }); }); @@ -64,6 +74,7 @@ describe('MosaicRestrictionTransactionService', () => { UInt64.fromUint(1000), networkType, helper.maxFee, ); + console.log(mosaicId.toHex()); const signedTransaction = mosaicDefinitionTransaction.signWith(account, generationHash); return helper.announce(signedTransaction); }); @@ -99,6 +110,7 @@ describe('MosaicRestrictionTransactionService', () => { account.address, UInt64.fromUint(2), networkType, + undefined, helper.maxFee, ); const aggregateTransaction = AggregateTransaction.createComplete(Deadline.create(), @@ -111,14 +123,80 @@ describe('MosaicRestrictionTransactionService', () => { }); }); + describe('NamespaceRegistrationTransaction', () => { + + it('standalone', () => { + const namespaceName = 'root-test-namespace-' + Math.floor(Math.random() * 10000); + const registerNamespaceTransaction = NamespaceRegistrationTransaction.createRootNamespace( + Deadline.create(), + namespaceName, + UInt64.fromUint(50), + networkType, helper.maxFee, + ); + namespaceIdMosaic = new NamespaceId(namespaceName); + const signedTransaction = registerNamespaceTransaction.signWith(account, generationHash); + + return helper.announce(signedTransaction); + }); + }); + + describe('NamespaceRegistrationTransaction', () => { + + it('standalone', () => { + const namespaceName = 'root-test-namespace-' + Math.floor(Math.random() * 10000); + const registerNamespaceTransaction = NamespaceRegistrationTransaction.createRootNamespace( + Deadline.create(), + namespaceName, + UInt64.fromUint(50), + networkType, helper.maxFee, + ); + namespaceIdAddress = new NamespaceId(namespaceName); + const signedTransaction = registerNamespaceTransaction.signWith(account, generationHash); + + return helper.announce(signedTransaction); + }); + }); + + describe('AddressAliasTransaction', () => { + + it('standalone', () => { + const addressAliasTransaction = AddressAliasTransaction.create( + Deadline.create(), + AliasAction.Link, + namespaceIdAddress, + account.address, + networkType, helper.maxFee, + ); + const signedTransaction = addressAliasTransaction.signWith(account, generationHash); + + return helper.announce(signedTransaction); + }); + }); + + describe('MosaicAliasTransaction', () => { + + it('standalone', () => { + const mosaicAliasTransaction = MosaicAliasTransaction.create( + Deadline.create(), + AliasAction.Link, + namespaceIdMosaic, + mosaicId, + networkType, helper.maxFee, + ); + const signedTransaction = mosaicAliasTransaction.signWith(account, generationHash); + + return helper.announce(signedTransaction); + }); + }); + /** * ========================= * Test * ========================= */ - describe('Test new services', () => { + describe('Test new services - MosaicGlobalRestriction', () => { it('should create MosaicGlobalRestrictionTransaction', (done) => { - const service = new MosaicRestrictionTransactionService(restrictionRepository); + const service = new MosaicRestrictionTransactionService(restrictionRepository, namespaceRepository); return service.createMosaicGlobalRestrictionTransaction( deadline, @@ -137,9 +215,33 @@ describe('MosaicRestrictionTransactionService', () => { done(); }); }); - it('should create MosaicAddressRestrictionTransaction', (done) => { - const service = new MosaicRestrictionTransactionService(restrictionRepository); + }); + + describe('Test new services - MosaicGlobalRestriction', () => { + it('should create MosaicGlobalRestrictionTransaction using alias', (done) => { + const service = new MosaicRestrictionTransactionService(restrictionRepository, namespaceRepository); + return service.createMosaicGlobalRestrictionTransaction( + deadline, + networkType, + namespaceIdMosaic, + key, + '2', + MosaicRestrictionType.GE, undefined, helper.maxFee, + ).subscribe((transaction: MosaicGlobalRestrictionTransaction) => { + expect(transaction.type).to.be.equal(TransactionType.MOSAIC_GLOBAL_RESTRICTION); + expect(transaction.previousRestrictionValue.toString()).to.be.equal('1'); + expect(transaction.previousRestrictionType).to.be.equal(MosaicRestrictionType.GE); + expect(transaction.newRestrictionValue.toString()).to.be.equal('2'); + expect(transaction.newRestrictionType).to.be.equal(MosaicRestrictionType.GE); + expect(transaction.restrictionKey.toHex()).to.be.equal(key.toHex()); + done(); + }); + }); + }); + describe('Test new services - MosaicAddressRestriction', () => { + it('should create MosaicAddressRestrictionTransaction', (done) => { + const service = new MosaicRestrictionTransactionService(restrictionRepository, namespaceRepository); return service.createMosaicAddressRestrictionTransaction( deadline, networkType, @@ -159,10 +261,33 @@ describe('MosaicRestrictionTransactionService', () => { }); }); + describe('Test new services - MosaicAddressRestriction', () => { + it('should create MosaicAddressRestrictionTransaction with address alias', (done) => { + const service = new MosaicRestrictionTransactionService(restrictionRepository, namespaceRepository); + + return service.createMosaicAddressRestrictionTransaction( + deadline, + networkType, + mosaicId, + key, + namespaceIdAddress, + '4', + helper.maxFee, + ).subscribe((transaction: MosaicAddressRestrictionTransaction) => { + expect(transaction.type).to.be.equal(TransactionType.MOSAIC_ADDRESS_RESTRICTION); + expect(transaction.previousRestrictionValue.toString()).to.be.equal('3'); + expect(transaction.newRestrictionValue.toString()).to.be.equal('4'); + expect(transaction.targetAddressToString()).to.be.equal(account.address.plain()); + expect(transaction.restrictionKey.toHex()).to.be.equal(key.toHex()); + done(); + }); + }); + }); + describe('Announce MosaicGlobalRestriction through service', () => { it('should create MosaicGlobalRestriction and announce', (done) => { - const service = new MosaicRestrictionTransactionService(restrictionRepository); + const service = new MosaicRestrictionTransactionService(restrictionRepository, namespaceRepository); service.createMosaicGlobalRestrictionTransaction( deadline, networkType, @@ -188,7 +313,7 @@ describe('MosaicRestrictionTransactionService', () => { describe('Announce MosaicAddressRestriction through service', () => { it('should create MosaicAddressRestriction and announce', (done) => { - const service = new MosaicRestrictionTransactionService(restrictionRepository); + const service = new MosaicRestrictionTransactionService(restrictionRepository, namespaceRepository); return service.createMosaicAddressRestrictionTransaction( deadline, diff --git a/src/model/transaction/MosaicAddressRestrictionTransaction.ts b/src/model/transaction/MosaicAddressRestrictionTransaction.ts index ca0fa027a8..ee80a02ebb 100644 --- a/src/model/transaction/MosaicAddressRestrictionTransaction.ts +++ b/src/model/transaction/MosaicAddressRestrictionTransaction.ts @@ -23,7 +23,7 @@ import { SignatureDto, TimestampDto, UnresolvedAddressDto, - UnresolvedMosaicIdDto + UnresolvedMosaicIdDto, } from 'catbuffer'; import { Convert } from '../../core/format'; import { DtoMapping } from '../../core/utils/DtoMapping'; diff --git a/src/service/MosaicRestrictionTransactionService.ts b/src/service/MosaicRestrictionTransactionService.ts index 028f1aad6f..bbc2b3bfc0 100644 --- a/src/service/MosaicRestrictionTransactionService.ts +++ b/src/service/MosaicRestrictionTransactionService.ts @@ -15,7 +15,10 @@ */ import { Observable, of } from 'rxjs'; -import { catchError, map, switchMap } from 'rxjs/operators'; +import { combineLatest } from 'rxjs'; +import { catchError, map } from 'rxjs/operators'; +import { mergeMap } from 'rxjs/operators'; +import { NamespaceRepository } from '../infrastructure/NamespaceRepository'; import { RestrictionMosaicRepository } from '../infrastructure/RestrictionMosaicRepository'; import { Address } from '../model/account/Address'; import { NetworkType } from '../model/blockchain/NetworkType'; @@ -41,15 +44,17 @@ export class MosaicRestrictionTransactionService { /** * Constructor * @param restrictionMosaicRepository + * @param namespaceRepository */ - constructor(private readonly restrictionMosaicRepository: RestrictionMosaicRepository) { + constructor(private readonly restrictionMosaicRepository: RestrictionMosaicRepository, + private readonly namespaceRepository: NamespaceRepository) { } /** * Create a MosaicGlobalRestrictionTransaction object without previous restriction data * @param deadline - Deadline * @param networkType - Network identifier - * @param mosaicId - MosaicId + * @param mosaicId - Unresolved mosaicId * @param restrictionKey - Restriction key * @param restrictionValue - New restriction value * @param restrictionType - New restriction type @@ -58,76 +63,78 @@ export class MosaicRestrictionTransactionService { */ public createMosaicGlobalRestrictionTransaction(deadline: Deadline, networkType: NetworkType, - mosaicId: MosaicId, + mosaicId: MosaicId | NamespaceId, restrictionKey: UInt64, restrictionValue: string, restrictionType: MosaicRestrictionType, referenceMosaicId: MosaicId | NamespaceId = new MosaicId(UInt64.fromUint(0).toDTO()), maxFee: UInt64 = new UInt64([0, 0])): Observable { this.validateInput(restrictionValue); - return this.getGlobalRestrictionEntry(mosaicId, restrictionKey).pipe( - map((restrictionEntry: MosaicGlobalRestrictionItem | undefined) => { - const currentValue = restrictionEntry ? UInt64.fromNumericString(restrictionEntry.restrictionValue) : - this.defaultMosaicGlobalRestrictionValue; - const currentType = restrictionEntry ? restrictionEntry.restrictionType : MosaicRestrictionType.NONE; - - return MosaicGlobalRestrictionTransaction.create( - deadline, - mosaicId, - restrictionKey, - currentValue, - currentType, - UInt64.fromNumericString(restrictionValue), - restrictionType, - networkType, - referenceMosaicId, - maxFee, - ); - }), - catchError((err) => { - throw Error(err); - })); + return this.getResolvedMosaicId(mosaicId).pipe( + mergeMap((resolvedMosaicId) => this.getGlobalRestrictionEntry(resolvedMosaicId, restrictionKey).pipe( + map((restrictionEntry: MosaicGlobalRestrictionItem | undefined) => { + const currentValue = restrictionEntry ? UInt64.fromNumericString(restrictionEntry.restrictionValue) : + this.defaultMosaicGlobalRestrictionValue; + const currentType = restrictionEntry ? restrictionEntry.restrictionType : MosaicRestrictionType.NONE; + return MosaicGlobalRestrictionTransaction.create( + deadline, + resolvedMosaicId, + restrictionKey, + currentValue, + currentType, + UInt64.fromNumericString(restrictionValue), + restrictionType, + networkType, + referenceMosaicId, + maxFee, + ); + }), + ))); } /** * Create a MosaicAddressRestrictionTransaction object without previous restriction data * @param deadline - Deadline * @param networkType - Network identifier - * @param mosaicId - MosaicId + * @param mosaicId - Unresolved mosaicId * @param restrictionKey - Restriction key - * @param targetAddress - Target address + * @param targetAddress - Unresolved target address * @param restrictionValue - New restriction value * @param maxFee - Max fee */ public createMosaicAddressRestrictionTransaction(deadline: Deadline, networkType: NetworkType, - mosaicId: MosaicId, + mosaicId: MosaicId | NamespaceId, restrictionKey: UInt64, - targetAddress: Address, + targetAddress: Address | NamespaceId, restrictionValue: string, maxFee: UInt64 = new UInt64([0, 0])): Observable { this.validateInput(restrictionValue); - return this.getGlobalRestrictionEntry(mosaicId, restrictionKey).pipe( - switchMap((restrictionEntry: MosaicGlobalRestrictionItem | undefined) => { - if (!restrictionEntry) { - throw Error('Global restriction is not valid for RestrictionKey: ' + restrictionKey); - } - return this.getAddressRestrictionEntry(mosaicId, restrictionKey, targetAddress).pipe( - map((optionalValue) => { - const currentValue = optionalValue ? - UInt64.fromNumericString(optionalValue) : this.defaultMosaicAddressRestrictionValue; - return MosaicAddressRestrictionTransaction.create( - deadline, - mosaicId, - restrictionKey, - targetAddress, - UInt64.fromNumericString(restrictionValue), - networkType, - currentValue, - maxFee, - ); - })); - }), + const combinedUnresolved = combineLatest(this.getResolvedMosaicId(mosaicId), this.getResolvedAddress(targetAddress)); + return combinedUnresolved.pipe( + mergeMap(([resolvedMosaicId, resolvedAddress]) => this.getGlobalRestrictionEntry(resolvedMosaicId, restrictionKey).pipe( + mergeMap((restrictionEntry: MosaicGlobalRestrictionItem | undefined) => { + if (!restrictionEntry) { + throw new Error('Global restriction is not valid for RestrictionKey: ' + restrictionKey); + } + return this.getAddressRestrictionEntry(resolvedMosaicId, restrictionKey, resolvedAddress).pipe( + map((optionalValue) => { + const currentValue = optionalValue ? + UInt64.fromNumericString(optionalValue) : this.defaultMosaicAddressRestrictionValue; + return MosaicAddressRestrictionTransaction.create( + deadline, + mosaicId, + restrictionKey, + targetAddress, + UInt64.fromNumericString(restrictionValue), + networkType, + currentValue, + maxFee, + ); + }), + ); + }), + )), ); } @@ -141,14 +148,14 @@ export class MosaicRestrictionTransactionService { private getAddressRestrictionEntry(mosaicId: MosaicId, restrictionKey: UInt64, targetAddress: Address): Observable { return this.restrictionMosaicRepository.getMosaicAddressRestriction(mosaicId, targetAddress).pipe( map((mosaicRestriction) => { - return mosaicRestriction.restrictions.get(restrictionKey.toHex()); + return mosaicRestriction.restrictions.get(restrictionKey.toString()); }), catchError((err: Error) => { const error = JSON.parse(err.message); if (error && error.statusCode && error.statusCode === 404) { return of(undefined); } - throw Error(err.message); + throw new Error(err.message); })); } @@ -161,14 +168,15 @@ export class MosaicRestrictionTransactionService { private getGlobalRestrictionEntry(mosaicId: MosaicId, restrictionKey: UInt64): Observable { return this.restrictionMosaicRepository.getMosaicGlobalRestriction(mosaicId).pipe( map((mosaicRestriction: MosaicGlobalRestriction) => { - return mosaicRestriction.restrictions.get(restrictionKey.toHex()); + console.log(mosaicRestriction.restrictions); + return mosaicRestriction.restrictions.get(restrictionKey.toString()); }), catchError((err: Error) => { const error = JSON.parse(err.message); if (error && error.statusCode && error.statusCode === 404) { return of(undefined); } - throw Error(err.message); + throw new Error(err.message); })); } @@ -178,7 +186,55 @@ export class MosaicRestrictionTransactionService { */ private validateInput(value: string) { if (!UInt64.isLongNumericString(value)) { - throw Error(`RestrictionValue: ${value} is not a valid numeric string.`); + throw new Error(`RestrictionValue: ${value} is not a valid numeric string.`); + } + } + + /** + * @internal + * Get resolved mosaicId from namespace repository + * @param unresolvedMosaicId unresolved mosaicId + * @returns {MosaicId} + */ + private getResolvedMosaicId(unresolvedMosaicId: MosaicId | NamespaceId): Observable { + if (unresolvedMosaicId instanceof MosaicId) { + return of(unresolvedMosaicId); + } + + return this.namespaceRepository.getLinkedMosaicId(unresolvedMosaicId).pipe( + map((mosaicId) => { + if (!mosaicId) { + throw new Error(`Invalid unresolvedMosaicId: ${unresolvedMosaicId.toHex()}`); + } + return mosaicId; + }), + catchError((err) => { + throw new Error(err); + }), + ); + } + + /** + * @internal + * Get resolved address from namespace repository + * @param unresolvedAddress unresolved address + * @returns {Address} + */ + private getResolvedAddress(unresolvedAddress: Address | NamespaceId): Observable
{ + if (unresolvedAddress instanceof Address) { + return of(unresolvedAddress); } + + return this.namespaceRepository.getLinkedAddress(unresolvedAddress).pipe( + map((address) => { + if (!address) { + throw new Error(`Invalid unresolvedAddress: ${unresolvedAddress.toHex()}`); + } + return address; + }), + catchError((err) => { + throw new Error(err); + }), + ); } } diff --git a/test/service/MosaicRestrictionTransactionservice.spec.ts b/test/service/MosaicRestrictionTransactionservice.spec.ts index 11883af3bc..cfa0f9ab74 100644 --- a/test/service/MosaicRestrictionTransactionservice.spec.ts +++ b/test/service/MosaicRestrictionTransactionservice.spec.ts @@ -18,10 +18,12 @@ import { expect } from 'chai'; import { of as observableOf } from 'rxjs'; import { deepEqual, instance, mock, when } from 'ts-mockito'; import { KeyGenerator } from '../../src/core/format/KeyGenerator'; +import { NamespaceRepository } from '../../src/infrastructure/NamespaceRepository'; import { RestrictionMosaicRepository } from '../../src/infrastructure/RestrictionMosaicRepository'; import { Account } from '../../src/model/account/Account'; import { NetworkType } from '../../src/model/blockchain/NetworkType'; import { MosaicId } from '../../src/model/mosaic/MosaicId'; +import { NamespaceId } from '../../src/model/namespace/NamespaceId'; import { MosaicAddressRestriction } from '../../src/model/restriction/MosaicAddressRestriction'; import { MosaicGlobalRestriction } from '../../src/model/restriction/MosaicGlobalRestriction'; import { MosaicGlobalRestrictionItem } from '../../src/model/restriction/MosaicGlobalRestrictionItem'; @@ -38,6 +40,8 @@ import { TestingAccount } from '../conf/conf.spec'; describe('MosaicRestrictionTransactionService', () => { let account: Account; let mosaicId: MosaicId; + let unresolvedMosaicId: NamespaceId; + let unresolvedAddress: NamespaceId; let referenceMosaicId: MosaicId; let mosaicRestrictionTransactionService: MosaicRestrictionTransactionService; const key = KeyGenerator.generateUInt64Key('TestKey'); @@ -50,21 +54,33 @@ describe('MosaicRestrictionTransactionService', () => { before(() => { account = TestingAccount; mosaicId = new MosaicId('85BBEA6CC462B244'); + unresolvedMosaicId = NamespaceId.createFromEncoded('F8495AEE892FA108'); + unresolvedAddress = NamespaceId.createFromEncoded('AB114281960BF1CC'); mosaicIdWrongKey = new MosaicId('85BBEA6CC462B288'); referenceMosaicId = new MosaicId('1AB129B545561E6A'); const mockRestrictionRepository = mock(); + const mockNamespaceRepository = mock(); when(mockRestrictionRepository .getMosaicGlobalRestriction(deepEqual(mosaicId))) .thenReturn(observableOf(mockGlobalRestriction())); when(mockRestrictionRepository .getMosaicGlobalRestriction(deepEqual(mosaicIdWrongKey))) - .thenThrow(new Error()); + .thenThrow(new Error('Wrong mosaicId')); when(mockRestrictionRepository .getMosaicAddressRestriction(deepEqual(mosaicId), deepEqual(account.address))) .thenReturn(observableOf(mockAddressRestriction())); + when(mockNamespaceRepository.getLinkedMosaicId(deepEqual(unresolvedMosaicId))) + .thenReturn(observableOf(mosaicId)); + when(mockNamespaceRepository.getLinkedMosaicId(deepEqual(unresolvedAddress))) + .thenThrow(new Error('invalid namespaceId')); + when(mockNamespaceRepository.getLinkedAddress(deepEqual(unresolvedAddress))) + .thenReturn(observableOf(account.address)); + when(mockNamespaceRepository.getLinkedAddress(deepEqual(unresolvedMosaicId))) + .thenThrow(new Error('invalid namespaceId')); const restrictionRepository = instance(mockRestrictionRepository); - mosaicRestrictionTransactionService = new MosaicRestrictionTransactionService(restrictionRepository); + const namespaceRepository = instance(mockNamespaceRepository); + mosaicRestrictionTransactionService = new MosaicRestrictionTransactionService(restrictionRepository, namespaceRepository); }); it('should create MosaicGlobalRestriction Transaction', (done) => { @@ -77,7 +93,7 @@ describe('MosaicRestrictionTransactionService', () => { MosaicRestrictionType.LE) .subscribe((transaction: MosaicGlobalRestrictionTransaction) => { expect(transaction.type).to.be.equal(TransactionType.MOSAIC_GLOBAL_RESTRICTION); - expect(transaction.restrictionKey.toHex()).to.be.equal(key.toHex()); + expect(transaction.restrictionKey.toString()).to.be.equal(key.toString()); expect(transaction.previousRestrictionType).to.be.equal(globalRestrictionType); expect(transaction.previousRestrictionValue.toString()).to.be.equal(globalRestrictionValue); expect(transaction.referenceMosaicId.toHex()).to.be.equal(new MosaicId(UInt64.fromUint(0).toDTO()).toHex()); @@ -114,13 +130,72 @@ describe('MosaicRestrictionTransactionService', () => { '2000') .subscribe((transaction: MosaicAddressRestrictionTransaction) => { expect(transaction.type).to.be.equal(TransactionType.MOSAIC_ADDRESS_RESTRICTION); - expect(transaction.restrictionKey.toHex()).to.be.equal(key.toHex()); + expect(transaction.restrictionKey.toString()).to.be.equal(key.toString()); expect(transaction.targetAddressToString()).to.be.equal(account.address.plain()); expect(transaction.previousRestrictionValue.toString()).to.be.equal(addressRestrictionValue); done(); }); }); + it('should create MosaicGlobalRestriction Transaction with unresolvedMosaicId', (done) => { + mosaicRestrictionTransactionService.createMosaicGlobalRestrictionTransaction( + Deadline.create(), + NetworkType.MIJIN_TEST, + unresolvedMosaicId, + key, + '2000', + MosaicRestrictionType.LE) + .subscribe((transaction: MosaicGlobalRestrictionTransaction) => { + expect(transaction.type).to.be.equal(TransactionType.MOSAIC_GLOBAL_RESTRICTION); + expect(transaction.restrictionKey.toHex()).to.be.equal(key.toHex()); + expect(transaction.previousRestrictionType).to.be.equal(globalRestrictionType); + expect(transaction.previousRestrictionValue.toString()).to.be.equal(globalRestrictionValue); + expect(transaction.referenceMosaicId.toHex()).to.be.equal(new MosaicId(UInt64.fromUint(0).toDTO()).toHex()); + done(); + }); + }); + + it('should create MosaicAddressRestriction Transaction with unresolvedAddress', (done) => { + mosaicRestrictionTransactionService.createMosaicAddressRestrictionTransaction( + Deadline.create(), + NetworkType.MIJIN_TEST, + unresolvedMosaicId, + key, + unresolvedAddress, + '2000') + .subscribe((transaction: MosaicAddressRestrictionTransaction) => { + expect(transaction.type).to.be.equal(TransactionType.MOSAIC_ADDRESS_RESTRICTION); + expect(transaction.restrictionKey.toString()).to.be.equal(key.toString()); + expect(transaction.targetAddressToString()).to.be.equal(unresolvedAddress.toHex()); + expect(transaction.previousRestrictionValue.toString()).to.be.equal(addressRestrictionValue); + done(); + }); + }); + + it('should throw error with invalid unresolvedMosaicId', () => { + expect(() => { + mosaicRestrictionTransactionService.createMosaicGlobalRestrictionTransaction( + Deadline.create(), + NetworkType.MIJIN_TEST, + unresolvedAddress, + key, + '2000', + MosaicRestrictionType.LE); + }).to.throw(); + }); + + it('should throw error with invalid unresolvedAddress', () => { + expect(() => { + mosaicRestrictionTransactionService.createMosaicAddressRestrictionTransaction( + Deadline.create(), + NetworkType.MIJIN_TEST, + mosaicId, + key, + unresolvedMosaicId, + '2000'); + }).to.throw(); + }); + it('should throw error with invalid value / key', () => { expect(() => { mosaicRestrictionTransactionService.createMosaicGlobalRestrictionTransaction( @@ -143,16 +218,16 @@ describe('MosaicRestrictionTransactionService', () => { }).to.throw(Error, 'RestrictionValue: wrong value is not a valid numeric string.'); }); - it('should throw error with invalid global restriction key - MosaicAddressRestriction', () => { - expect(() => { - mosaicRestrictionTransactionService.createMosaicAddressRestrictionTransaction( - Deadline.create(), - NetworkType.MIJIN_TEST, - mosaicIdWrongKey, - invalidKey, - account.address, - '2000'); - }).to.throw(); + it('should throw error with invalid address restriction key - MosaicAddressRestriction', () => { + mosaicRestrictionTransactionService.createMosaicAddressRestrictionTransaction( + Deadline.create(), + NetworkType.MIJIN_TEST, + mosaicIdWrongKey, + invalidKey, + account.address, + '2000').subscribe((t) => {}, (err) => { + expect(err).not.to.be.undefined; + }); }); function mockGlobalRestriction(): MosaicGlobalRestriction { @@ -161,7 +236,7 @@ describe('MosaicRestrictionTransactionService', () => { MosaicRestrictionEntryType.GLOBAL, mosaicId, new Map() - .set(key.toHex(), + .set(key.toString(), new MosaicGlobalRestrictionItem( referenceMosaicId, globalRestrictionValue, @@ -178,7 +253,7 @@ describe('MosaicRestrictionTransactionService', () => { mosaicId, account.address, new Map() - .set(key.toHex(), + .set(key.toString(), addressRestrictionValue, ), );