Skip to content

Commit

Permalink
feat: add /accounts/:accountId/convert endpoint (#1007)
Browse files Browse the repository at this point in the history
* Added the `convert` endpoint
- Controller, service, response type

* Removed getPair

* SS58 Address from Public key (hex)
- Using the query param `publicKey` to output the SS58 address if the input/parameter that is given by the user is a Public Key (hex) and not an accountID.

* Changes in validation code & Adding tests
- Added specific types that the `RequestHandler` can accept.
- Cleaned the code that validates the query params (and sets default values) in the Controller.
- Added tests with different valid or invalid endpoints to check.

* Updated the Docs
- Updated the docs with the `convert` endpoint functionality &  path & query params.

* Changes in the docs based on Tarik's feedback
- Keeping the "AccountId" written with the same format.
- Other minor changes in formatting.

* Changes in code & formatting based on Tarik's feedback

* Minor change in the docstrings

* refactor controller to use validateBoolean

Co-authored-by: tarikgul <tarik@parity.io>
  • Loading branch information
Imod7 and TarikGul committed Sep 21, 2022
1 parent bc79dc5 commit e2d6fae
Show file tree
Hide file tree
Showing 15 changed files with 453 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/dist/app.bundle.js

Large diffs are not rendered by default.

62 changes: 62 additions & 0 deletions docs/src/openapi-v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,68 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/AccountValidation'
/accounts/{accountId}/convert:
get:
tags:
- accounts
summary: Convert a given AccountId to an SS58 address.
description: Returns the SS58 prefix, the network address format, the SS58 address, and the AccountId
that was given as input parameter, the scheme that was used and if it is a public key or not (boolean).
operationId: accountConvert
parameters:
- name: accountId
in: path
description: AccountId or Public Key (hex).
required: true
schema:
format: AccountId or Hex
type: string
- name: scheme
in: query
description: The cryptographic scheme to be used in order to convert the AccountId to
an SS58 address. It can take one of three values [sr25519, ed25519, ecdsa].
The default scheme that is used is `sr25519` (if it is not set in the query parameter).
required: false
schema:
type: string
format: string
default: 'sr25519'
- name: prefix
in: query
description: The address prefix which can be one of the values found in the SS58-registry.
required: false
schema:
type: string
format: number
default: 42
- name: publicKey
in: query
description: Defines if the given value in the path parameter is a Public Key (hex)
or not (hence AccountId).
required: false
schema:
type: string
format: boolean
default: true
responses:
"200":
description: successfully converted the AccountId and retrieved the address info.
content:
application/json:
schema:
$ref: '#/components/schemas/AccountConvert'
"400":
description: Invalid AccountId
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
"404":
description: AccountId not found
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
/blocks:
get:
tags:
Expand Down
1 change: 1 addition & 0 deletions src/chains-config/kusamaControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { initLRUCache, QueryFeeDetailsCache } from './cache';
export const kusamaControllers: ControllerConfig = {
controllers: [
'AccountsBalanceInfo',
'AccountsConvert',
'AccountsStakingInfo',
'AccountsStakingPayouts',
'AccountsValidate',
Expand Down
1 change: 1 addition & 0 deletions src/chains-config/polkadotControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { initLRUCache, QueryFeeDetailsCache } from './cache';
export const polkadotControllers: ControllerConfig = {
controllers: [
'AccountsBalanceInfo',
'AccountsConvert',
'AccountsStakingInfo',
'AccountsStakingPayouts',
'AccountsValidate',
Expand Down
1 change: 1 addition & 0 deletions src/chains-config/westendControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { initLRUCache, QueryFeeDetailsCache } from './cache';
export const westendControllers: ControllerConfig = {
controllers: [
'AccountsBalanceInfo',
'AccountsConvert',
'AccountsStakingInfo',
'AccountsStakingPayouts',
'AccountsValidate',
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/AbstractController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { AnyJson } from 'src/types/polkadot-js';
import {
IAddressNumberParams,
IAddressParam,
IConvertQueryParams,
INumberParam,
IParaIdParam,
IRangeQueryParam,
Expand All @@ -37,6 +38,7 @@ import { verifyNonZeroUInt, verifyUInt } from '../util/integers/verifyInt';

type SidecarRequestHandler =
| RequestHandler<unknown, unknown, unknown, IRangeQueryParam>
| RequestHandler<IAddressParam, unknown, unknown, IConvertQueryParams>
| RequestHandler<IAddressParam>
| RequestHandler<IAddressNumberParams>
| RequestHandler<INumberParam>
Expand Down
71 changes: 71 additions & 0 deletions src/controllers/accounts/AccountsConvertController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2017-2022 Parity Technologies (UK) Ltd.
// This file is part of Substrate API Sidecar.
//
// Substrate API Sidecar is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import { ApiPromise } from '@polkadot/api';
import { RequestHandler } from 'express';
import { BadRequest } from 'http-errors';

import { validateBoolean } from '../../middleware';
import { AccountsConvertService } from '../../services/accounts';
import { IAddressParam, IConvertQueryParams } from '../../types/requests';
import AbstractController from '../AbstractController';

export default class AccountsConvertController extends AbstractController<AccountsConvertService> {
constructor(api: ApiPromise) {
super(api, '/accounts/:address/convert', new AccountsConvertService(api));
this.initRoutes();
}

protected initRoutes(): void {
this.router.use(this.path, validateBoolean(['publicKey']));
this.safeMountAsyncGetHandlers([['', this.accountConvert]]);
}

private accountConvert: RequestHandler<
IAddressParam,
unknown,
unknown,
IConvertQueryParams
> = ({ params: { address }, query: { scheme, prefix, publicKey } }, res) => {
// Validation of the `scheme` query param
const cryptoScheme = scheme ? scheme : 'sr25519';
if (
!(
cryptoScheme === 'ed25519' ||
cryptoScheme === 'sr25519' ||
cryptoScheme === 'ecdsa'
)
) {
throw new BadRequest(
'The `scheme` query parameter provided can be one of the following three values : [ed25519, sr25519, ecdsa]'
);
}

// Validation of the `prefix` query param
const networkPrefix = prefix ? prefix : '42';
const ss58Prefix = this.parseNumberOrThrow(
networkPrefix,
'The `prefix` query parameter provided is not a number.'
);

const pubKey = publicKey === 'true';

AccountsConvertController.sanitizedSend(
res,
this.service.accountConvert(address, cryptoScheme, ss58Prefix, pubKey)
);
};
}
1 change: 1 addition & 0 deletions src/controllers/accounts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

export { default as AccountsAssets } from './AccountsAssetsController';
export { default as AccountsBalanceInfo } from './AccountsBalanceInfoController';
export { default as AccountsConvert } from './AccountsConvertController';
export { default as AccountsStakingInfo } from './AccountsStakingInfoController';
export { default as AccountsStakingPayouts } from './AccountsStakingPayoutsController';
export { default as AccountsValidate } from './AccountsValidateController';
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import {
AccountsAssets,
AccountsBalanceInfo,
AccountsConvert,
AccountsStakingInfo,
AccountsStakingPayouts,
AccountsValidate,
Expand Down Expand Up @@ -47,6 +48,7 @@ export const controllers = {
BlocksTrace,
AccountsAssets,
AccountsBalanceInfo,
AccountsConvert,
AccountsStakingInfo,
AccountsValidate,
AccountsVestingInfo,
Expand Down
Loading

0 comments on commit e2d6fae

Please sign in to comment.