Skip to content
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

Implement support for compressed public keys #41

Closed
jakubgs opened this issue Jul 1, 2020 · 5 comments · Fixed by #42
Closed

Implement support for compressed public keys #41

jakubgs opened this issue Jul 1, 2020 · 5 comments · Fixed by #42
Assignees

Comments

@jakubgs
Copy link
Member

jakubgs commented Jul 1, 2020

Based off of status-im/status-go#1937 the handler needs to be able to accept compressed public key.

@Samyoul which compressed formats do we need to support? Just multiformat base58 encoded?

@jakubgs jakubgs self-assigned this Jul 1, 2020
@Samyoul
Copy link
Member

Samyoul commented Jul 1, 2020

Thanks for creating this issue.

The specs for this are here https://github.com/status-im/specs/blob/master/docs/stable/2-account.md#public-key-serialization


We actually support all multibase encoding formats. See https://github.com/multiformats/multibase

This may be useful https://github.com/multiformats/js-multibase


Public key types:

  • secp256k1 pks
  • bls12-381 g1 pks
  • bls12-381 g2 pks

But I've been told that we'd only use secp256k1 pks.

@jakubgs
Copy link
Member Author

jakubgs commented Jul 2, 2020

Looks like the secret sauce are multibase and secp256k1 JS libaries:

> s = 'zQ3shPyZJnxZK4Bwyx9QsaksNKDYTPmpwPvGSjMYVHoXHeEgB'
'zQ3shPyZJnxZK4Bwyx9QsaksNKDYTPmpwPvGSjMYVHoXHeEgB'
> b = multibase.decode(s)
<Buffer e7 01 02 26 1c 55 67 5e 55 ff 25 ed b5 0b 34 5c fb 3a 3f 35 f6 07 12 d2 51 cb aa ab 97 bd 50 05 4c 6e bc>
> secp256k1.publicKeyConvert(b.slice(2), compressed=false)
Uint8Array(65) [
    4,  38,  28,  85, 103,  94,  85, 255,  37, 237, 181,
   11,  52,  92, 251,  58,  63,  53, 246,   7,  18, 210,
   81, 203, 170, 171, 151, 189,  80,   5,  76, 110, 188,
   60, 212, 226,  34,   0, 198, 141, 175, 116, 147, 225,
  248, 218, 106,  25,  10, 104, 166, 113, 226, 211, 151,
  120,   9,  97,  36,  36, 199, 195, 136, 139, 198
]

@jakubgs
Copy link
Member Author

jakubgs commented Jul 2, 2020

And the other way around:

> k = "0x041c678bdeb6940df3e436cee76612ae6177f46e54e541fc024b572fafd9be89ba43612d6e26bba8f3961b5d379efe1fb031a718ea24936a83550086f4ba2c5c94"
'0x041c678bdeb6940df3e436cee76612ae6177f46e54e541fc024b572fafd9be89ba43612d6e26bba8f3961b5d379efe1fb031a718ea24936a83550086f4ba2c5c94'
> f = "f" + k.slice(2)
'f041c678bdeb6940df3e436cee76612ae6177f46e54e541fc024b572fafd9be89ba43612d6e26bba8f3961b5d379efe1fb031a718ea24936a83550086f4ba2c5c94'
> b = multibase.decode(f)
<Buffer 04 1c 67 8b de b6 94 0d f3 e4 36 ce e7 66 12 ae 61 77 f4 6e 54 e5 41 fc 02 4b 57 2f af d9 be 89 ba 43 61 2d 6e 26 bb a8 f3 96 1b 5d 37 9e fe 1f b0 31 ... 15 more bytes>
> c = secp256k1.publicKeyConvert(b)
Uint8Array(33) [
    2,  28, 103, 139, 222, 182, 148,  13,
  243, 228,  54, 206, 231, 102,  18, 174,
   97, 119, 244, 110,  84, 229,  65, 252,
    2,  75,  87,  47, 175, 217, 190, 137,
  186
]
> secp256k1MultifactorID = Buffer.from([231, 1])
> d = Buffer.concat([secp256k1MultifactorID, c])
<Buffer e7 01 02 1c 67 8b de b6 94 0d f3 e4 36 ce e7 66 12 ae 61 77 f4 6e 54 e5 41 fc 02 4b 57 2f af d9 be 89 ba>
> multibase.encode('base58btc', d).toString()
'zQ3shPKfkQPeJdWMrXMtSfCFHhDR9gck99GsYt4Er3yvHxT2Z'

@jakubgs
Copy link
Member Author

jakubgs commented Jul 2, 2020

This post was a good resource: status-im/status-go#1937 (comment)

@Samyoul
Copy link
Member

Samyoul commented Jul 7, 2020

I'm please you found it helpful, I always try share the information I learn to make the learning curve of anyone else to be less steep than mine 😆

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants