Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env node
- let multihashes = require('multihashes')
- let address = 'QmbWGdzNEed8JEgmfKTysjNAxNG6UEJpv3sBCVctBXAEsJ'
- let raw_address = multihashes.fromB58String(address)
- let hash_info = multihashes.decode(raw_address)
- let digest = hash_info.digest
- if (hash_info.name !== 'sha2-256') {
- console.error("ERR: IPFS address isn't a SHA256")
- process.exit(1)
- }
- // After verifying if the algorithm is SHA256, "digest" can be passed
- // to the contract as a bytes32 field (probably encoded as a hexstring
- // because of Web3).
- let hexDigest = '0x' + digest.toString('hex')
- console.log(hexDigest)
- // Having a bytes32 encoded (by Web3) as a hex string, we can rebuild
- // the IPFS address using the following procedure.
- if (!hexDigest.startsWith('0x')) {
- // This is just a sanity test, Web3 should give us a hex string
- // beginning with "0x...".
- console.error("ERR: Invalid hex string received")
- process.exit(1)
- }
- let str_digest = Buffer.from(hexDigest.substr(2), 'hex')
- let multi_digest = multihashes.encode(str_digest, 'sha2-256')
- let ipfs_address = multihashes.toB58String(multi_digest)
- console.log(ipfs_address)
- console.log(ipfs_address === address)
Add Comment
Please, Sign In to add comment