cryptocoinjs / base-x

Encode/decode any base
MIT License
312 stars 75 forks source link

Inconsistent length of encoded strings #71

Closed mitar closed 3 years ago

mitar commented 4 years ago

So I would like to encode 128 bits consistently to 22 character in base58, but this is now always true. For example:

> const BASE58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
> const bs58 = require('base-x')(BASE58);
> bs58.decode('WDRoFqNxhwGYcnzDzasyY')
<Buffer 04 14 3a 63 2b 25 10 00 3f d0 46 31 cf e3 5a cf>
> 'WDRoFqNxhwGYcnzDzasyY'.length
21
> bs58.decode('PvctNaG977YY2NskPa4My6')
<Buffer b9 a5 95 ef f2 a0 27 8f db 85 fb 82 3e e9 a9 8d>
> 'PvctNaG977YY2NskPa4My6'.length
22

They are both 128 bits. I would like that this is always encoded to 22 characters. Is this possible somehow?

junderw commented 3 years ago

Nope.

The protocol is not fixed-width.

mitar commented 3 years ago

Do you know of any base58 protocol which is?

junderw commented 3 years ago

No. Use traditional base64 for fixed length.

This library does not support that though.

buffer.toString('base64') is fixed length.

mitar commented 3 years ago

I would like to use BASE58 characters though for encoding.

junderw commented 3 years ago

Then use base58 and account for variable length.