js-multiformats icon indicating copy to clipboard operation
js-multiformats copied to clipboard

js-multibase is deprecated, says to use js-multiformats, but no docs on how to use it?

Open treeder opened this issue 1 year ago • 3 comments

I just want to decode, but it's not clear how to do it:

const decodedBytes = multibase.decode(bytes)

???

treeder avatar Jul 20 '23 17:07 treeder

I've added an example to https://github.com/multiformats/js-multiformats/issues/47#issuecomment-1649873600.

import { bases } from 'multiformats/basics'

// Single known multibase.
console.log(bases.base32.decode('bozwxq'))

// Combine multiple multibases.
const base32or64 = bases.base32.decoder.or(bases.base64url.decoder)
console.log(base32or64.decode('bozwxq'))
console.log(base32or64.decode('udm14'))

vmx avatar Jul 25 '23 13:07 vmx

Thanks @vmx . Does that mean we have to know what bases are used in advance? Seems like it kind of defeats the whole point of this...

treeder avatar Jul 25 '23 14:07 treeder

Seems like it kind of defeats the whole point of this...

It's always been some sort of fixed list (also in multibase). The difference is that you now decide which ones you care about. The answer may be "all of them". If that's the case you can also create a lookup table as mentioned in that comment: https://github.com/multiformats/js-multiformats/issues/121#issuecomment-937495215 and implemented e.g. at https://github.com/MatrixAI/js-id/commit/17cf2eb0137a46cd47cfbd70e14b731619086d39.

The idea is that usually an application won't encounter all possible encodings, but you know which ones to expect.

vmx avatar Jul 25 '23 15:07 vmx