obsidiansystems / ledger-app-nervos

MIT License
10 stars 10 forks source link

public key in xpubkey #158

Closed katat closed 4 years ago

katat commented 4 years ago

Hi,

I am trying to test out the extended public key from the ledger. The following is result from the device:

xpubkey {
  public_key: '044380e9e0f9e40f0b70bc37eae54d52629c827718fac84831cc399f88b6150a8c0292bbdabbaeed68b1790b659557ced6d21407b994eec72acbbe173045288444',
  chain_code: 'ac3f24519d363cde772f29986c798d3a06fdfa9ebe661d28593519b7a0ad0346'
}

The public_key got a length of 130 characters. This doesn't seem to align with the assumption in Neuron, which follows the format in https://learnmeabitcoin.com/technical/extended-keys. The public key is assumed to be 33 bytes, while 32 bytes for chain code. When importing a xpubkey, Neuron basically parses the serialized string by simply splitting the first 66 characters as public key and the rest 64 characters for chain code.

So I am wondering if the public_key generated from the ledger is actually correct?

katat commented 4 years ago

I guess this is relating to https://learnmeabitcoin.com/technical/public-key If the current public_key is a uncompressed public key, can we generate the compressed public key instead?

jonored commented 4 years ago

We do provide an uncompressed public key, and it's much easier to go from uncompressed to compressed than the other way around: given an uncompressed public key in hex,

compressed = ("13579bdf".includes(uncompressed[129])?"03":"02") + uncompressed.slice(2,66)

that is to say, the prefix is 0x02 | last_byte & 0x1 and the rest is just the first 32 bytes of key data. Uncompressed is also tagged with 0x04 instead of 0x02 or 0x03.