Skip to content

Commit

Permalink
fix: /accounts/{accountId}/convert encoding for ecdsa (#1280)
Browse files Browse the repository at this point in the history
* start the rework

* Update comment in tests
  • Loading branch information
TarikGul committed Jun 29, 2023
1 parent 45eee30 commit 86edf0b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
25 changes: 24 additions & 1 deletion src/services/accounts/AccountsConvertService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,29 @@ 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:
'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
Expand All @@ -158,7 +181,7 @@ describe('Convert accounts', () => {
const expectedResponse = {
ss58Prefix: '0',
network: 'polkadot',
address: '12ZviSbX1Pzmnw1mg4FUg6Qra2CW7Q3z9iqmcWKWUphp5qgq',
address: '1rsCBWhPgyDETNS9yxnANSnm3KAtkxm4mu9jjfMhDF6xaV8',
accountId:
'0x2607fd20388303bd409e551202ee47b753b4382feac914e9f7ab0d4f728c2bf7',
scheme: 'ecdsa',
Expand Down
20 changes: 17 additions & 3 deletions src/services/accounts/AccountsConvertService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 86edf0b

Please sign in to comment.