-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add /accounts/:accountId/convert endpoint (#1007)
* 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
Showing
15 changed files
with
453 additions
and
1 deletion.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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 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 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 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,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) | ||
); | ||
}; | ||
} |
This file contains 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 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.