-
Notifications
You must be signed in to change notification settings - Fork 57
Alias resolution for accountHttp #540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6e5c4a0
Fixed #499
rg911 4323b31
Fixed e2e tests
rg911 174f5a3
Fixed lints
rg911 9fd93bf
Fixed years
rg911 08e19c2
another year fix
rg911 3f49ef1
Merge branch 'master' into task/g499_resolve_alias
rg911 6d3c079
Chaned to use address array and reduce rest calls
rg911 d8dda68
handle empty array
rg911 65cb1db
Merge branch 'master' into task/g499_resolve_alias
rg911 73e9929
Merge branch 'master' into task/g499_resolve_alias
rg911 12e9293
Fixed typos
rg911 e4d518a
more typo fix
rg911 fa02e96
Fixed function name
rg911 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| /* | ||
| * Copyright 2020 NEM | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { expect } from 'chai'; | ||
| import { Account } from '../../src/model/account/Account'; | ||
| import { NetworkType } from '../../src/model/network/NetworkType'; | ||
| import { Deadline } from '../../src/model/transaction/Deadline'; | ||
| import { UInt64 } from '../../src/model/UInt64'; | ||
| import { IntegrationTestHelper } from '../infrastructure/IntegrationTestHelper'; | ||
| import { AccountService } from '../../src/service/AccountService'; | ||
| import { NamespaceRegistrationTransaction } from '../../src/model/transaction/NamespaceRegistrationTransaction'; | ||
| import { NamespaceId } from '../../src/model/namespace/NamespaceId'; | ||
|
|
||
| describe('AccountService', () => { | ||
| const helper = new IntegrationTestHelper(); | ||
| let generationHash: string; | ||
| let account: Account; | ||
| let networkType: NetworkType; | ||
| let accountService: AccountService; | ||
| let namespaceId: NamespaceId; | ||
| const name = 'root-test-namespace-' + Math.floor(Math.random() * 10000); | ||
|
|
||
| before(() => { | ||
| return helper.start().then(() => { | ||
| account = helper.account; | ||
| generationHash = helper.generationHash; | ||
| networkType = helper.networkType; | ||
| accountService = new AccountService(helper.repositoryFactory); | ||
| }); | ||
| }); | ||
| before(() => { | ||
| return helper.listener.open(); | ||
| }); | ||
|
|
||
| after(() => { | ||
| helper.listener.close(); | ||
| }); | ||
|
|
||
| /** | ||
| * ========================= | ||
| * Setup test data | ||
| * ========================= | ||
| */ | ||
| describe('Create a namespace', () => { | ||
| it('Announce NamespaceRegistrationTransaction', () => { | ||
| const registerNamespaceTransaction = NamespaceRegistrationTransaction.createRootNamespace( | ||
| Deadline.create(), | ||
| name, | ||
| UInt64.fromUint(300000), | ||
| networkType, | ||
| helper.maxFee, | ||
| ); | ||
| namespaceId = new NamespaceId(name); | ||
| const signedTransaction = registerNamespaceTransaction.signWith(account, generationHash); | ||
| return helper.announce(signedTransaction); | ||
| }); | ||
| }); | ||
|
|
||
| /** | ||
| * ========================= | ||
| * Test | ||
| * ========================= | ||
| */ | ||
| describe('call accountInfoWithResolvedMosaic', () => { | ||
| it('accountInfoWithResolvedMosaic', async () => { | ||
| const info = await accountService.accountInfoWithResolvedMosaic([account.address]).toPromise(); | ||
| expect(info).to.not.be.undefined; | ||
| expect(info[0].resolvedMosaics).to.not.be.undefined; | ||
| expect(info[0].resolvedMosaics?.length).to.be.greaterThan(0); | ||
| }); | ||
| }); | ||
|
|
||
| describe('call accountNamespacesWithName', () => { | ||
| it('accountNamespacesWithName', async () => { | ||
| const info = await accountService.accountNamespacesWithName([account.address]).toPromise(); | ||
| expect(info).to.not.be.undefined; | ||
| expect(info.find((i) => i.id.equals(namespaceId))).to.not.be.undefined; | ||
| expect(info.find((i) => i.id.equals(namespaceId))?.namespaceName).to.be.equal(name); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /* | ||
| * Copyright 2020 NEM | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { AccountInfo } from './AccountInfo'; | ||
| import { ResolvedMosaic } from '../mosaic/ResolvedMosaic'; | ||
|
|
||
| /** | ||
| * Account info with resolved mosaic | ||
| */ | ||
| export type AccountInfoResolvedMosaic = AccountInfo & { resolvedMosaics?: ResolvedMosaic[] }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /* | ||
| * Copyright 2020 NEM | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { NamespaceName } from '../namespace/NamespaceName'; | ||
| import { Mosaic } from './Mosaic'; | ||
|
|
||
| /** | ||
| * Resolved mosaic model with namespace name | ||
| */ | ||
| export type ResolvedMosaic = Mosaic & { namespaceName?: NamespaceName }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* | ||
| * Copyright 2020 NEM | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { NamespaceInfo } from './NamespaceInfo'; | ||
|
|
||
| /** | ||
| * Resolved mosaic model with namespace name | ||
| */ | ||
| export type NamespaceInfoWithName = NamespaceInfo & { namespaceName?: string }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| /* | ||
| * Copyright 2020 NEM | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { Observable, of } from 'rxjs'; | ||
| import { map, withLatestFrom } from 'rxjs/operators'; | ||
| import { RepositoryFactory } from '../infrastructure/RepositoryFactory'; | ||
| import { AccountRepository } from '../infrastructure/AccountRepository'; | ||
| import { NamespaceRepository } from '../infrastructure/NamespaceRepository'; | ||
| import { Address } from '../model/account/Address'; | ||
| import { mergeMap } from 'rxjs/operators'; | ||
| import { DtoMapping } from '../core/utils/DtoMapping'; | ||
| import { IAccountService } from './interfaces/IAccountService'; | ||
| import { NamespaceInfoWithName } from '../model/namespace/NamespaceInfoWithName'; | ||
| import { ResolvedMosaic } from '../model/mosaic/ResolvedMosaic'; | ||
| import { Mosaic } from '../model/mosaic/Mosaic'; | ||
| import { MosaicId } from '../model/mosaic/MosaicId'; | ||
| import { NamespaceId } from '../model/namespace/NamespaceId'; | ||
| import { AccountInfoResolvedMosaic } from '../model/account/AccountInfoResolvedMosaic'; | ||
| import { AccountInfo } from '../model/account/AccountInfo'; | ||
| import { NamespaceName } from '../model/namespace/NamespaceName'; | ||
| /** | ||
| * Account Service | ||
| */ | ||
| export class AccountService implements IAccountService { | ||
| private readonly accountRepository: AccountRepository; | ||
|
|
||
| private readonly namespaceRepository: NamespaceRepository; | ||
|
|
||
| /** | ||
| * Constructor | ||
| * @param repositoryFactory | ||
| */ | ||
| constructor(public readonly repositoryFactory: RepositoryFactory) { | ||
| this.accountRepository = repositoryFactory.createAccountRepository(); | ||
| this.namespaceRepository = repositoryFactory.createNamespaceRepository(); | ||
| } | ||
|
|
||
| /** | ||
| * Get account info with resolved mosaic | ||
| * @param addresses Array of addresses | ||
| */ | ||
| public accountInfoWithResolvedMosaic(addresses: Address[]): Observable<AccountInfoResolvedMosaic[]> { | ||
| const accountInfoObservable = this.accountRepository.getAccountsInfo(addresses); | ||
| const distinctNames = accountInfoObservable.pipe( | ||
| mergeMap((info) => { | ||
| const namespaceIds = this.getDistinctNamespaceIdFromAccountInfors(info); | ||
| if (namespaceIds.length) { | ||
| return this.namespaceRepository.getNamespacesName(namespaceIds); | ||
| } | ||
| return of([]); | ||
| }), | ||
| ); | ||
|
|
||
| return accountInfoObservable.pipe( | ||
| withLatestFrom(distinctNames), | ||
| map(([infos, names]) => { | ||
| return infos.map((info) => { | ||
| const resolved = this.resolveMosaics(info.mosaics, names); | ||
| return DtoMapping.assign(info, { resolvedMosaics: resolved }); | ||
| }); | ||
| }), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Get namespace info for account with namespace name | ||
| * @param addresses Array of addresses | ||
| * @returns {Observable<NamespaceInfoWithName[]>} | ||
| */ | ||
| public accountNamespacesWithName(addresses: Address[]): Observable<NamespaceInfoWithName[]> { | ||
| return this.namespaceRepository.getNamespacesFromAccounts(addresses).pipe( | ||
| mergeMap((infos) => { | ||
| const namespaceIds = infos.map((i) => i.id); | ||
| return this.namespaceRepository.getNamespacesName(namespaceIds).pipe( | ||
| map((resolved) => { | ||
| return infos.map((info) => { | ||
| const name = resolved.find((r) => r.namespaceId.equals(info.id)); | ||
| return DtoMapping.assign(info, { namespaceName: name?.name }); | ||
| }); | ||
| }), | ||
| ); | ||
| }), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Resolve mosaics provides namespace names | ||
| * @param mosaics unresolved mosaics | ||
| * @return {ResolvedMosaic[]} | ||
| */ | ||
| private resolveMosaics(mosaics: Mosaic[], names: NamespaceName[]): ResolvedMosaic[] { | ||
| return mosaics.map((mosaic) => { | ||
| if (mosaic.id instanceof MosaicId) { | ||
| return mosaic as ResolvedMosaic; | ||
| } else { | ||
| const name = names.find((f) => f.namespaceId.equals(mosaic.id)); | ||
| if (name) { | ||
| return DtoMapping.assign(mosaic, { namespaceName: name }); | ||
| } else { | ||
| return mosaic as ResolvedMosaic; | ||
| } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Get distince list of namespacesIds from list of accountInfos | ||
rg911 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * @param accountInfo List of account info | ||
| * @returns {NamespaceId[]} | ||
| */ | ||
| private getDistinctNamespaceIdFromAccountInfors(accountInfo: AccountInfo[]): NamespaceId[] { | ||
rg911 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
dgarcia360 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
rg911 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const namespaceIds: NamespaceId[] = []; | ||
| accountInfo.forEach((info) => { | ||
| info.mosaics.forEach((mosaic) => { | ||
| if (mosaic.id instanceof NamespaceId) { | ||
| if (!namespaceIds.find((n) => n.equals(mosaic.id))) { | ||
| namespaceIds.push(mosaic.id); | ||
| } | ||
| } | ||
| }); | ||
| }); | ||
| return namespaceIds; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| * Copyright 2020 NEM | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { Observable } from 'rxjs'; | ||
| import { Address } from '../../model/account/Address'; | ||
| import { AccountInfoResolvedMosaic } from '../../model/account/AccountInfoResolvedMosaic'; | ||
| import { NamespaceInfoWithName } from '../../model/namespace/NamespaceInfoWithName'; | ||
|
|
||
| /** | ||
| * Block Service Interface | ||
| */ | ||
| export interface IAccountService { | ||
| /** | ||
| * Get account info with resolved mosaic | ||
| * @param addresses Array of addresses | ||
| */ | ||
| accountInfoWithResolvedMosaic(addresses: Address[]): Observable<AccountInfoResolvedMosaic[]>; | ||
|
|
||
| /** | ||
| * Get namespace info for account with namespace name | ||
| * @param addresses Array of addresses | ||
| * @returns {Observable<NamespaceInfoWithName[]>} | ||
| */ | ||
| accountNamespacesWithName(addresses: Address[]): Observable<NamespaceInfoWithName[]>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.