Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/core/utils/DtoMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,12 @@ export class DtoMapping {
public static assign<T>(object: T, attributes: any): T {
return Object.assign({ __proto__: Object.getPrototypeOf(object) }, object, attributes);
}

/**
* Map one enum type to another by value
* @param value enum value to be mapped
*/
public static mapEnum<E1, E2>(value: E1 | undefined): E2 {
return (value as unknown) as E2;
}
}
10 changes: 7 additions & 3 deletions src/infrastructure/BlockHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { Http } from './Http';
import { BlockSearchCriteria } from './searchCriteria/BlockSearchCriteria';
import { Page } from './Page';
import { Address } from '../model/account/Address';
import { DtoMapping } from '../core/utils/DtoMapping';

/**
* Blockchain http repository.
Expand Down Expand Up @@ -71,8 +72,8 @@ export class BlockHttp extends Http implements BlockRepository {
criteria.pageSize,
criteria.pageNumber,
criteria.offset,
criteria.order,
criteria.orderBy,
DtoMapping.mapEnum(criteria.order),
DtoMapping.mapEnum(criteria.orderBy),
),
(body) => super.toPage(body.pagination, body.data, this.toBlockInfo),
);
Expand Down Expand Up @@ -129,7 +130,10 @@ export class BlockHttp extends Http implements BlockRepository {
public getMerkleTransaction(height: UInt64, hash: string): Observable<MerkleProofInfo> {
return this.call(
this.blockRoutesApi.getMerkleTransaction(height.toString(), hash),
(body) => new MerkleProofInfo(body.merklePath!.map((payload) => new MerklePathItem(payload.position, payload.hash))),
(body) =>
new MerkleProofInfo(
body.merklePath!.map((payload) => new MerklePathItem(DtoMapping.mapEnum(payload.position), payload.hash)),
),
);
}
}
2 changes: 1 addition & 1 deletion src/infrastructure/Listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { TransactionStatusError } from '../model/transaction/TransactionStatusEr
import { IListener } from './IListener';
import { NamespaceRepository } from './NamespaceRepository';
import { CreateTransactionFromDTO } from './transaction/CreateTransactionFromDTO';
import { BlockInfoDTO } from 'symbol-openapi-typescript-node-client/dist/model/blockInfoDTO';
import { BlockInfoDTO } from 'symbol-openapi-typescript-node-client';
import { NewBlock } from '../model/blockchain/NewBlock';
import { PublicAccount } from '../model/account/PublicAccount';
import { UInt64 } from '../model/UInt64';
Expand Down
3 changes: 2 additions & 1 deletion src/infrastructure/MosaicHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { MosaicSearchCriteria } from './searchCriteria/MosaicSearchCriteria';
import { Page } from './Page';
import { MosaicRoutesApi, MosaicIds, MosaicInfoDTO } from 'symbol-openapi-typescript-node-client';
import { Address } from '../model/account/Address';
import { DtoMapping } from '../core/utils/DtoMapping';

/**
* Mosaic http repository.
Expand Down Expand Up @@ -92,7 +93,7 @@ export class MosaicHttp extends Http implements MosaicRepository {
criteria.pageSize,
criteria.pageNumber,
criteria.offset,
criteria.order,
DtoMapping.mapEnum(criteria.order),
),
(body) => super.toPage(body.pagination, body.data, this.toMosaicInfo, networkType),
),
Expand Down
2 changes: 1 addition & 1 deletion src/infrastructure/QueryParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { Order } from 'symbol-openapi-typescript-node-client/dist/model/order';
import { Order } from './searchCriteria/Order';

/**
* The query params structure describes pagination params for requests.
Expand Down
15 changes: 7 additions & 8 deletions src/infrastructure/ReceiptHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { UInt64 } from '../model/UInt64';
import { Http } from './Http';
import { CreateStatementFromDTO } from './receipt/CreateReceiptFromDTO';
import { ReceiptRepository } from './ReceiptRepository';
import { DtoMapping } from '../core/utils/DtoMapping';

/**
* Receipt http repository.
Expand All @@ -38,12 +39,6 @@ export class ReceiptHttp extends Http implements ReceiptRepository {
*/
private readonly receiptRoutesApi: ReceiptRoutesApi;

/**
* @internal
* network type for the mappings.
*/
private readonly networkTypeObservable: Observable<NetworkType>;

/**
* Constructor
* @param url
Expand All @@ -52,7 +47,6 @@ export class ReceiptHttp extends Http implements ReceiptRepository {
constructor(url: string, networkType?: NetworkType | Observable<NetworkType>) {
super(url);
this.receiptRoutesApi = new ReceiptRoutesApi(url);
this.networkTypeObservable = this.createNetworkTypeObservable(networkType);
this.receiptRoutesApi.useQuerystring = true;
}

Expand All @@ -68,7 +62,12 @@ export class ReceiptHttp extends Http implements ReceiptRepository {
*/
public getMerkleReceipts(height: UInt64, hash: string): Observable<MerkleProofInfo> {
return observableFrom(this.receiptRoutesApi.getMerkleReceipts(height.toString(), hash)).pipe(
map(({ body }) => new MerkleProofInfo(body.merklePath!.map((payload) => new MerklePathItem(payload.position, payload.hash)))),
map(
({ body }) =>
new MerkleProofInfo(
body.merklePath!.map((payload) => new MerklePathItem(DtoMapping.mapEnum(payload.position), payload.hash)),
),
),
catchError((error) => throwError(this.errorHandling(error))),
);
}
Expand Down
7 changes: 4 additions & 3 deletions src/infrastructure/TransactionHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { TransactionSearchCriteria } from './searchCriteria/TransactionSearchCri
import { Page } from './Page';
import { TransactionGroup } from './TransactionGroup';
import http = require('http');
import { DtoMapping } from '../core/utils/DtoMapping';

/**
* Transaction http repository.
Expand Down Expand Up @@ -237,7 +238,7 @@ export class TransactionHttp extends Http implements TransactionRepository {
criteria.pageSize,
criteria.pageNumber,
criteria.offset,
criteria.order,
DtoMapping.mapEnum(criteria.order),
);
case TransactionGroup.Unconfirmed:
return this.transactionRoutesApi.searchUnconfirmedTransactions(
Expand All @@ -250,7 +251,7 @@ export class TransactionHttp extends Http implements TransactionRepository {
criteria.pageSize,
criteria.pageNumber,
criteria.offset,
criteria.order,
DtoMapping.mapEnum(criteria.order),
);
case TransactionGroup.Partial:
return this.transactionRoutesApi.searchPartialTransactions(
Expand All @@ -263,7 +264,7 @@ export class TransactionHttp extends Http implements TransactionRepository {
criteria.pageSize,
criteria.pageNumber,
criteria.offset,
criteria.order,
DtoMapping.mapEnum(criteria.order),
);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/infrastructure/infrastructure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@ export * from './paginationStreamer/TransactionPaginationStreamer';
export * from './TransactionStatusHttp';
export * from './TransactionStatusRepository';
export * from './TransactionGroup';
export * from './searchCriteria/BlockOrderBy';
export * from './searchCriteria/Order';
19 changes: 19 additions & 0 deletions src/infrastructure/searchCriteria/BlockOrderBy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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.
*/
export enum BlockOrderBy {
Id = 'id',
Height = 'height',
}
4 changes: 2 additions & 2 deletions src/infrastructure/searchCriteria/BlockSearchCriteria.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import { SearchCriteria } from './SearchCriteria';
import { BlockOrderByEnum } from 'symbol-openapi-typescript-node-client/dist/model/blockOrderByEnum';
import { BlockOrderBy } from './BlockOrderBy';

/**
* Defines the params used to search blocks. With this criteria, you can sort and filter
Expand All @@ -35,5 +35,5 @@ export interface BlockSearchCriteria extends SearchCriteria {
/**
* Order by block id or height. (optional)
*/
orderBy?: BlockOrderByEnum;
orderBy?: BlockOrderBy;
}
22 changes: 22 additions & 0 deletions src/infrastructure/searchCriteria/Order.ts
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.
*/
/**
* Indicates how to sort the results: * ``asc`` - ascending * ``desc`` - descending
*/
export enum Order {
Asc = 'asc',
Desc = 'desc',
}
2 changes: 1 addition & 1 deletion src/infrastructure/searchCriteria/SearchCriteria.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { Order } from 'symbol-openapi-typescript-node-client/dist/model/order';
import { Order } from './Order';

export interface SearchCriteria {
/**
Expand Down
6 changes: 3 additions & 3 deletions src/model/blockchain/MerklePathItem.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { PositionEnum } from 'symbol-openapi-typescript-node-client/dist/model/positionEnum';

/*
* Copyright 2019 NEM
*
Expand All @@ -16,6 +14,8 @@ import { PositionEnum } from 'symbol-openapi-typescript-node-client/dist/model/p
* limitations under the License.
*/

import { MerklePosition } from './MerklePosition';

/**
* The block merkle path item
*/
Expand All @@ -28,7 +28,7 @@ export class MerklePathItem {
/**
* The position
*/
public readonly position?: PositionEnum,
public readonly position?: MerklePosition,
/**
* The hash
*/
Expand Down
19 changes: 19 additions & 0 deletions src/model/blockchain/MerklePosition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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.
*/
export enum MerklePosition {
Left = 'left',
Right = 'right',
}
1 change: 1 addition & 0 deletions src/model/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export * from './blockchain/BlockInfo';
export * from './blockchain/MerklePathItem';
export * from './blockchain/MerkleProofInfo';
export * from './blockchain/NewBlock';
export * from './blockchain/MerklePosition';

// Diagnostic
export * from './node/ServerInfo';
Expand Down
4 changes: 2 additions & 2 deletions src/service/BlockService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
import { sha3_256 } from 'js-sha3';
import { combineLatest, Observable, of } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
import { PositionEnum } from 'symbol-openapi-typescript-node-client/dist/model/positionEnum';
import { BlockRepository } from '../infrastructure/BlockRepository';
import { ReceiptRepository } from '../infrastructure/ReceiptRepository';
import { RepositoryFactory } from '../infrastructure/RepositoryFactory';
import { MerklePathItem } from '../model/blockchain/MerklePathItem';
import { UInt64 } from '../model/UInt64';
import { IBlockService } from './interfaces/IBlockService';
import { MerklePosition } from '../model/blockchain/MerklePosition';

/**
* Block Service
Expand Down Expand Up @@ -82,7 +82,7 @@ export class BlockService implements IBlockService {
const rootToCompare = merklePathItem.reduce((proofHash, pathItem) => {
const hasher = sha3_256.create();
// Left
if (pathItem.position !== undefined && pathItem.position === PositionEnum.Left) {
if (pathItem.position !== undefined && pathItem.position === MerklePosition.Left) {
return hasher.update(Buffer.from(pathItem.hash + proofHash, 'hex')).hex();
} else {
// Right
Expand Down
Loading