// HashToHash(key) resolves contenthash for key (a hash) using HashReg // resolution is costless non-transactional // implemented as direct retrieval from db func (self *Registrar) HashToHash(khash common.Hash) (chash common.Hash, err error) { if zero.MatchString(HashRegAddr) { return common.Hash{}, fmt.Errorf("HashReg address is not set") } // look up in hashReg at := HashRegAddr[2:] key := storageAddress(storageMapping(storageIdx2Addr(1), khash[:])) hash := self.backend.StorageAt(at, key) if hash == "0x0" || len(hash) < 3 || (hash == common.Hash{}.Hex()) { err = fmt.Errorf("HashToHash: content hash not found for '%v'", khash.Hex()) return } copy(chash[:], common.Hex2BytesFixed(hash[2:], 32)) return }
// SetUrlToHash(from, hash, url) registers a url to a content hash so that the content can be fetched // address is used as sender for the transaction and will be the owner of a new // registry entry on first time use // FIXME: silently doing nothing if sender is not the owner // note that with content addressed storage, this step is no longer necessary func (self *Registrar) SetUrlToHash(address common.Address, hash common.Hash, url string) (txh string, err error) { if zero.MatchString(UrlHintAddr) { return "", fmt.Errorf("UrlHint address is not set") } hashHex := common.Bytes2Hex(hash[:]) var urlHex string urlb := []byte(url) var cnt byte n := len(urlb) for n > 0 { if n > 32 { n = 32 } urlHex = common.Bytes2Hex(urlb[:n]) urlb = urlb[n:] n = len(urlb) bcnt := make([]byte, 32) bcnt[31] = cnt data := registerUrlAbi + hashHex + common.Bytes2Hex(bcnt) + common.Bytes2Hex(common.Hex2BytesFixed(urlHex, 32)) txh, err = self.backend.Transact( address.Hex(), UrlHintAddr, "", "", "", "", data, ) if err != nil { return } cnt++ } return }