Is it possible to use the web3.js libary to do base58 encoding and decoding?

It seems that web3.js includes the bignumber library, but I'm unsure as to whether this can handle the base58 encoding used, for example, by ipfs.

I'm hoping for a function that looks a bit like this (which fails): E.g. (based on this)

web3.utils.toBN('QmXGTaGWTT1uUtfSb2sBAvArMEVLK4rQEcQg5bv7wwdzwU', 58) //web3 v1.x


web3.toBigNumber("QmXGTaGWTT1uUtfSb2sBAvArMEVLK4rQEcQg5bv7wwdzwU",58) //web3 v0.x

which returns a bignumber integer from the base58 number. Is it possible to do base58 encoding and decoding using the web3.js library?


Web3.js doesn't provide a base58 encoder/decoder because it's only concerned with interfacing to ethereum, so there's no direct use of this feature. You'll have to use a module such as bs58.

Here's an example:

const bs58 = require('bs58')
const BN = require('bn.js')

const hash = 'QmXGTaGWTT1uUtfSb2sBAvArMEVLK4rQEcQg5bv7wwdzwU'
const hex = bs58.decode(hash).toString('hex')

console.log(hex) // 122084a644bfcb8639e1b1a1fc72fd0ad1826b91f7a9baa06ad409ac3c02b31f981b

const n = new BN(hex, 16)
console.log(n.toString(10)) // 537335293128262426148241029128274019001757729355677528305490323656269309818148891

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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