3

I need to convert a number (ex. 1, 2, 32, 43, etc) into a base58 string in order to use it in getProgramAccounts memcmp filters. I'm able to do this with strings, but I've been unable to find a function that works properly with numbers.

3 Answers 3

4

As per the documentation for bs58:

encode(input) input must be a Uint8Array, Buffer, or an Array. It returns a string

const bs58_string = bs58.encode(Uint8Array.from([2]))

1
  • 1
    Thanks! This worked out. It turns out I also had a small mistake on my offset that was messing things up
    – web3man
    Commented Aug 5, 2022 at 8:38
0

install npm package "bs58" (npm install bs58), Documentation link https://www.npmjs.com/package/bs58

try snippet

const bs58 = require('bs58');
const number = '503503503958';
const bytes = bs58.decode(number);
console.log(Buffer.from(bytes).toString('hex'));

if you just need to convert the value to bs58 (or verify) than you can use this website https://www.browserling.com/tools/base58-encode

1
  • I get a Invalid param: DecodeError(InvalidCharacter { character: '0', index: 0 }) when I try this? This is a u32 number in the struct. What string should I expect for example, for the number 2
    – web3man
    Commented Aug 5, 2022 at 7:03
0

This will actually depend on the representation of the number, not only its value

Value u8 u16le u32le u64le
0 1 11 1111 11111111
1 2 5R 2UzHM Ahg1opVcGX
2 3 9q 3xyZh LQM2cdzDY3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.