Repository navigation
feat: add /accounts/:accountId/convert endpoint - #1007
Conversation
- Controller, service, response type
|
Dumb question probably (I read the comments this came from and didn't quite understand); what does the |
- 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.
@jsdw Thank you so much for your question and it is a quite logical question to have since when I created this PR I was actually not even using the query param |
- 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 with the `convert` endpoint functionality & path & query params.
|
I added the tests + docs in this PR.
|
|
TarikGul
left a comment
There was a problem hiding this comment.
Overall very solid job, just some nits, and a few questions above.
- Keeping the "AccountId" written with the same format. - Other minor changes in formatting.
TarikGul
left a comment
There was a problem hiding this comment.
Great Job, LGTM 👍 Just need to resolve the merge conflict
091861f to
7273f77
Compare
TarikGul
left a comment
There was a problem hiding this comment.
New commits look great! Lets get this merged in soon.
| !( | ||
| cryptoScheme === 'ed25519' || | ||
| cryptoScheme === 'sr25519' || | ||
| cryptoScheme === 'ecdsa' | ||
| ) |
There was a problem hiding this comment.
Totally happy with this!
When there are lots of conditions you can also use an approach like if (!['ed25519', 'sr25519', 'ecdsa'].includes(cryptoScheme)) { ... }, but your way is more efficient :)
| let network = null; | ||
| for (const networkParams of allNetworks) { | ||
| if (networkParams['prefix'] === ss58Prefix) { | ||
| network = networkParams['network']; | ||
| break; | ||
| } | ||
| } |
There was a problem hiding this comment.
Assuming allNetworks is an array, you can also do:
let network = allNetworks.find(n => n['prefix'] === ss58Prefix);
if (!network) {
throw new BadRequest(...)
}Not a big deal though :)
Description
This endpoint will receive as an input an Account ID or a Public key (hex) that is given by the user as the path parameter and convert it into an
SS58address. The conversion will depend on the values of the 3 query parameters that are mentioned below.This implementation was motivated by this comment while making changes in the
validateendpoint.From SS58 Address to Account ID also ?
Does this endpoint also implements the reverse conversion, meaning from SS58 Address to Account ID ? No because this functionality is already covered by the
/accounts/:address/validateendpoint after this PR was merged.Query Parameters
This endpoint can take the following 3 query parameters :
schemeprefixpublicKeyPossible values
The possible values for these parameters are the following :
schemecan take one of those 3 values{sr25519, ed25519, ecdsa}prefixcan be one of the prefices found herepublicKeyis a boolean value and can betrueorfalseDefault values
The default values for these query params are the following :
scheme=sr25519prefix=42publicKey=trueExamples
So, for example if the query parameters that are given are the following :
prefix=2scheme=ed25519publicKey=falsethen that means that the endpoint will give as an output :
ed25519algorithm (default value)Account ID(and not a Public Key (hex))If no query parameters are specified then the endpoint will take their default values (as defined above) and will output :
prefix= 42 which corresponds to substrate)sr25519scheme (default value) andSpecial Case / Use of query param
publicKeyIn most of the cases we can output a valid SS58 address based only on the
schemeandprefixsince Public Key (hex) is identical to the Account ID (so specifying the query parampublicKeydoes not make any difference).However, there is a specific case for which we also need to know if the path parameter that is given is :
hence we need the query parameter
publicKeyto be specified astrueorfalse.This case is when we have
scheme=ecdsa.When we have
ecdsaas scheme then Public key is different than the Account ID so we first need to hash the public key withblake2and then get the SS58 address from that.*** This is also explained in this substrate PR (subkey: display SS58 encoding of public key)
Sample Response
Sample request :
/accounts/0x002b5bab9258f5aff3117fce47854ef4c29c0eeae3faf855a2d6a9b67861c598/validate?prefix=0
Sample response :
chains-config
It was also added in
chains-config-> Controllers of :Todos
accountIdtoaddress) that happens in theaccountConvertmethod is correctpublic(because currently is not used)