From 4e3969b3368abe1a9ee7c5458751e814f04cce62 Mon Sep 17 00:00:00 2001 From: tarikgul Date: Tue, 2 May 2023 22:50:48 -0400 Subject: [PATCH 1/2] start the rework --- .../accounts/AccountsConvertService.spec.ts | 23 ++++++++++++++++++- .../accounts/AccountsConvertService.ts | 20 +++++++++++++--- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/services/accounts/AccountsConvertService.spec.ts b/src/services/accounts/AccountsConvertService.spec.ts index e9a3e6cee..dff3e5600 100644 --- a/src/services/accounts/AccountsConvertService.spec.ts +++ b/src/services/accounts/AccountsConvertService.spec.ts @@ -150,6 +150,27 @@ describe('Convert accounts', () => { ).toStrictEqual(expectedResponse); }); + it('Should convert a valid Kusama publicKey when `publicKey` equals `true`', () => { + const expectedResponse = { + accountId: + '0x96074594cccf1cd185fa8a72ceaeefd86648f8d45514f3ce33c31bdd07e4655d', + address: 'Fy2rsYCoowQBtuFXqLE65ehAY9T6KWcGiNCQAyPDCkfpm4s', + network: 'kusama', + publicKey: true, + scheme: 'sr25519', + ss58Prefix: '2', + }; + + const kusamaPublicKey = + '0x96074594cccf1cd185fa8a72ceaeefd86648f8d45514f3ce33c31bdd07e4655d'; + + expect( + sanitizeNumbers( + validateService.accountConvert(kusamaPublicKey, 'sr25519', 2, true) + ) + ).toStrictEqual(expectedResponse); + }); + // We try to convert a Polkadot AccountId to an SS58 Address by setting the publicKey=true // which is not correct and that is why in the response we have an invalid address. // If we would like to convert it correctly and have the expected SS58 address @@ -158,7 +179,7 @@ describe('Convert accounts', () => { const expectedResponse = { ss58Prefix: '0', network: 'polkadot', - address: '12ZviSbX1Pzmnw1mg4FUg6Qra2CW7Q3z9iqmcWKWUphp5qgq', + address: '1rsCBWhPgyDETNS9yxnANSnm3KAtkxm4mu9jjfMhDF6xaV8', accountId: '0x2607fd20388303bd409e551202ee47b753b4382feac914e9f7ab0d4f728c2bf7', scheme: 'ecdsa', diff --git a/src/services/accounts/AccountsConvertService.ts b/src/services/accounts/AccountsConvertService.ts index ec6fcbf47..44d50b565 100644 --- a/src/services/accounts/AccountsConvertService.ts +++ b/src/services/accounts/AccountsConvertService.ts @@ -16,13 +16,25 @@ import { Keyring } from '@polkadot/api'; import { isHex } from '@polkadot/util'; -import { allNetworks } from '@polkadot/util-crypto'; -import { blake2AsHex } from '@polkadot/util-crypto'; +import { hexToU8a } from '@polkadot/util'; +import { allNetworks, blake2AsHex } from '@polkadot/util-crypto'; import { BadRequest } from 'http-errors'; import { IAccountConvert } from '../../types/responses/AccountConvert'; import { AbstractService } from '../AbstractService'; +/** + * Copyright 2023 via polkadot-js/common + * + * The slightly modified below logic is copyrighted from polkadot-js/common . The exact path to the code can be seen here: + * https://github.com/polkadot-js/common/blob/e5cb0ba2b4a6b5817626cc964b4f66334f2410e4/packages/keyring/src/pair/index.ts#L44-L49 + */ +const TYPE_ADDRESS = { + ecdsa: (p: string) => (hexToU8a(p).length > 32 ? blake2AsHex(p) : p), + ed25519: (p: string) => p, + sr25519: (p: string) => p, +}; + export class AccountsConvertService extends AbstractService { /** * Takes a given AccountId or Public Key (hex) and converts it to an SS58 address. @@ -59,7 +71,9 @@ export class AccountsConvertService extends AbstractService { ); } - const accountId2Encode = publicKey ? blake2AsHex(accountId) : accountId; + const accountId2Encode = publicKey + ? TYPE_ADDRESS[scheme](accountId) + : accountId; const keyring = new Keyring({ type: scheme, ss58Format: ss58Prefix }); const address = keyring.encodeAddress(accountId2Encode, ss58Prefix); From b975a52fd07d5cead29d2497902e1bb1775bd939 Mon Sep 17 00:00:00 2001 From: tarikgul Date: Thu, 29 Jun 2023 05:07:58 -0400 Subject: [PATCH 2/2] Update comment in tests --- src/services/accounts/AccountsConvertService.spec.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/services/accounts/AccountsConvertService.spec.ts b/src/services/accounts/AccountsConvertService.spec.ts index dff3e5600..a09a48594 100644 --- a/src/services/accounts/AccountsConvertService.spec.ts +++ b/src/services/accounts/AccountsConvertService.spec.ts @@ -150,6 +150,8 @@ describe('Convert accounts', () => { ).toStrictEqual(expectedResponse); }); + // This ensures the behaviour of the endpoint correctly converts a kusama publicKey given + // the following input. See PR: https://github.com/paritytech/substrate-api-sidecar/pull/1280 it('Should convert a valid Kusama publicKey when `publicKey` equals `true`', () => { const expectedResponse = { accountId: