Guest User

Untitled

a guest
Jan 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. #!/usr/bin/env node
  2. let multihashes = require('multihashes')
  3.  
  4. let address = 'QmbWGdzNEed8JEgmfKTysjNAxNG6UEJpv3sBCVctBXAEsJ'
  5.  
  6. let raw_address = multihashes.fromB58String(address)
  7. let hash_info = multihashes.decode(raw_address)
  8. let digest = hash_info.digest
  9. if (hash_info.name !== 'sha2-256') {
  10. console.error("ERR: IPFS address isn't a SHA256")
  11. process.exit(1)
  12. }
  13.  
  14. // After verifying if the algorithm is SHA256, "digest" can be passed
  15. // to the contract as a bytes32 field (probably encoded as a hexstring
  16. // because of Web3).
  17. let hexDigest = '0x' + digest.toString('hex')
  18. console.log(hexDigest)
  19.  
  20.  
  21.  
  22. // Having a bytes32 encoded (by Web3) as a hex string, we can rebuild
  23. // the IPFS address using the following procedure.
  24. if (!hexDigest.startsWith('0x')) {
  25. // This is just a sanity test, Web3 should give us a hex string
  26. // beginning with "0x...".
  27. console.error("ERR: Invalid hex string received")
  28. process.exit(1)
  29. }
  30.  
  31.  
  32. let str_digest = Buffer.from(hexDigest.substr(2), 'hex')
  33. let multi_digest = multihashes.encode(str_digest, 'sha2-256')
  34. let ipfs_address = multihashes.toB58String(multi_digest)
  35. console.log(ipfs_address)
  36. console.log(ipfs_address === address)
Add Comment
Please, Sign In to add comment