import * as bs58 from "bs58" ; window [ "bs58" ] = bs58 ; typedef Multihash property { string } digest property { number } hashFunction property { number } size param { string } multihash returns { Multihash } export function getBytes32FromMultiash ( multihash ) { const decoded = bs58 . decode ( multihash ) ; return { digest : `0x${ decoded . slice ( 2 ) . toString ( "hex" ) }` , hashFunction : decoded [ 0 ] , size : decoded [ 1 ] } ; } param { Multihash } multihash returns { ( string | null ) } export function getMultihashFromBytes32 ( multihash ) { const { digest , hashFunction , size } = multihash ; if ( size === 0 ) return null ; const hashBytes = Buffer . from ( digest . slice ( 2 ) , "hex" ) ; const multihashBytes = new ( hashBytes . constructor as any ) ( 2 + hashBytes . length ) ; multihashBytes [ 0 ] = hashFunction ; multihashBytes [ 1 ] = size ; multihashBytes . set ( hashBytes , 2 ) ; return bs58 . encode ( multihashBytes ) ; } param { array } response returns { Multihash } export function parseContractResponse ( response ) { const [ digest , hashFunction , size ] = response ; return { digest , hashFunction : hashFunction . toNumber ( ) , size : size . toNumber ( ) } ; } param { array } response returns { string } export function getMultihashFromContractResponse ( response ) { return getMultihashFromBytes32 ( parseContractResponse ( response ) ) ; }