-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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.
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great Job, LGTM 👍 Just need to resolve the merge conflict
091861f
to
7273f77
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New commits look great! Lets get this merged in soon.
!( | ||
cryptoScheme === 'ed25519' || | ||
cryptoScheme === 'sr25519' || | ||
cryptoScheme === 'ecdsa' | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm, nice work!
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
SS58
address. 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
validate
endpoint.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/validate
endpoint after this PR was merged.Query Parameters
This endpoint can take the following 3 query parameters :
scheme
prefix
publicKey
Possible values
The possible values for these parameters are the following :
scheme
can take one of those 3 values{sr25519, ed25519, ecdsa}
prefix
can be one of the prefices found herepublicKey
is a boolean value and can betrue
orfalse
Default values
The default values for these query params are the following :
scheme
=sr25519
prefix
=42
publicKey
=true
Examples
So, for example if the query parameters that are given are the following :
prefix=2
scheme=ed25519
publicKey=false
then that means that the endpoint will give as an output :
ed25519
algorithm (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)sr25519
scheme (default value) andSpecial Case / Use of query param
publicKey
In most of the cases we can output a valid SS58 address based only on the
scheme
andprefix
since Public Key (hex) is identical to the Account ID (so specifying the query parampublicKey
does 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
publicKey
to be specified astrue
orfalse
.This case is when we have
scheme=ecdsa
.When we have
ecdsa
as scheme then Public key is different than the Account ID so we first need to hash the public key withblake2
and 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
accountId
toaddress
) that happens in theaccountConvert
method is correctpublic
(because currently is not used)