admin管理员组

文章数量:1022982

I am using IPFS-multihash method to store IPFS hash in smart contracts. For that I need to decode base58 format:

QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4

to binary(as hex):

1220b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9

But I am unable to use bs58 module in browser (I tried using Browserify). Can you tell me the javascript implementation for decoding and encoding in base58 so that I can implement the above method without using node_modules? Or can you tell how exactly can I use Browserify to use a node module step-by-step?

After running the first mand 'bs58' folder is created in 'node_modules' folder.....there is no lib folder. Can you guide me with the exact mands I must use?

npm install --save bs58

npm install -g browserify

browserify < lib/bs58.js > lib/bs85.bundle.js

My file structure is somewhat like this:

-node_modules
-src
  |___index.html
  |___js
      |____app.js

I am using IPFS-multihash method to store IPFS hash in smart contracts. For that I need to decode base58 format:

QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4

to binary(as hex):

1220b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9

But I am unable to use bs58 module in browser (I tried using Browserify). Can you tell me the javascript implementation for decoding and encoding in base58 so that I can implement the above method without using node_modules? Or can you tell how exactly can I use Browserify to use a node module step-by-step?

After running the first mand 'bs58' folder is created in 'node_modules' folder.....there is no lib folder. Can you guide me with the exact mands I must use?

npm install --save bs58

npm install -g browserify

browserify < lib/bs58.js > lib/bs85.bundle.js

My file structure is somewhat like this:

-node_modules
-src
  |___index.html
  |___js
      |____app.js
Share Improve this question asked May 10, 2019 at 8:34 ak07_ak07_ 1052 silver badges9 bronze badges 2
  • Use this for instance? gist.github./diafygi/90a3e80ca1c2793220e5 – user5734311 Commented May 10, 2019 at 8:36
  • Thanks @ChrisG. It worked! Just had to convert it into Hex String. – ak07_ Commented May 10, 2019 at 8:56
Add a ment  | 

2 Answers 2

Reset to default 1

For me, it worked by using Base58 implementation and converting the resulting result into hex.

var MAP = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
var decoded = toHexString(from_b58(ipfsHash,MAP)).toUpperCase();

Thanks to Chris G

Depending on your use case, handling Base58 alone may not be enough.

Consider using official cids library to future-proof your contracts.

IPFS content identifiers are called CIDs (docs). Current default is CIDv0 (equal to a raw multihash in Base58btc), but CIDv1 are an opt-in upgrade already used in the wild (it allows encoding to arbitrary bases). IPFS it will switch to CIDv1 in Base32 as the new default in the future (but everyone will be free to use other base if they wish to do so):

<cidv0> ::= <multihash-content-address>
<cidv1> ::= <multibase-prefix><cid-version><multicodec-content-type><multihash-content-address>

To convert full IPFS CID to hex with cids library, you would do something like:

const cidHex = new CID('bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy').buffer.toString('hex').toUpperCase()

or if you only care about raw multihash, you can extract it from CID via .multihash:

const mhHex = new CID('bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy').multihash.toString('hex').toUpperCase()

The cids library should work fine with browserify and other bundlers. There is a prebuilt version for the browser as well.

Hope this helps :)

I am using IPFS-multihash method to store IPFS hash in smart contracts. For that I need to decode base58 format:

QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4

to binary(as hex):

1220b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9

But I am unable to use bs58 module in browser (I tried using Browserify). Can you tell me the javascript implementation for decoding and encoding in base58 so that I can implement the above method without using node_modules? Or can you tell how exactly can I use Browserify to use a node module step-by-step?

After running the first mand 'bs58' folder is created in 'node_modules' folder.....there is no lib folder. Can you guide me with the exact mands I must use?

npm install --save bs58

npm install -g browserify

browserify < lib/bs58.js > lib/bs85.bundle.js

My file structure is somewhat like this:

-node_modules
-src
  |___index.html
  |___js
      |____app.js

I am using IPFS-multihash method to store IPFS hash in smart contracts. For that I need to decode base58 format:

QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4

to binary(as hex):

1220b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9

But I am unable to use bs58 module in browser (I tried using Browserify). Can you tell me the javascript implementation for decoding and encoding in base58 so that I can implement the above method without using node_modules? Or can you tell how exactly can I use Browserify to use a node module step-by-step?

After running the first mand 'bs58' folder is created in 'node_modules' folder.....there is no lib folder. Can you guide me with the exact mands I must use?

npm install --save bs58

npm install -g browserify

browserify < lib/bs58.js > lib/bs85.bundle.js

My file structure is somewhat like this:

-node_modules
-src
  |___index.html
  |___js
      |____app.js
Share Improve this question asked May 10, 2019 at 8:34 ak07_ak07_ 1052 silver badges9 bronze badges 2
  • Use this for instance? gist.github./diafygi/90a3e80ca1c2793220e5 – user5734311 Commented May 10, 2019 at 8:36
  • Thanks @ChrisG. It worked! Just had to convert it into Hex String. – ak07_ Commented May 10, 2019 at 8:56
Add a ment  | 

2 Answers 2

Reset to default 1

For me, it worked by using Base58 implementation and converting the resulting result into hex.

var MAP = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
var decoded = toHexString(from_b58(ipfsHash,MAP)).toUpperCase();

Thanks to Chris G

Depending on your use case, handling Base58 alone may not be enough.

Consider using official cids library to future-proof your contracts.

IPFS content identifiers are called CIDs (docs). Current default is CIDv0 (equal to a raw multihash in Base58btc), but CIDv1 are an opt-in upgrade already used in the wild (it allows encoding to arbitrary bases). IPFS it will switch to CIDv1 in Base32 as the new default in the future (but everyone will be free to use other base if they wish to do so):

<cidv0> ::= <multihash-content-address>
<cidv1> ::= <multibase-prefix><cid-version><multicodec-content-type><multihash-content-address>

To convert full IPFS CID to hex with cids library, you would do something like:

const cidHex = new CID('bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy').buffer.toString('hex').toUpperCase()

or if you only care about raw multihash, you can extract it from CID via .multihash:

const mhHex = new CID('bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy').multihash.toString('hex').toUpperCase()

The cids library should work fine with browserify and other bundlers. There is a prebuilt version for the browser as well.

Hope this helps :)

本文标签: ipfsBase58 javascript implementationStack Overflow