diff options
author | zelig <viktor.tron@gmail.com> | 2015-03-31 23:02:55 +0800 |
---|---|---|
committer | zelig <viktor.tron@gmail.com> | 2015-04-20 03:57:48 +0800 |
commit | 3a540425a3d7fc29233b5cd3e7b79b4866f35d57 (patch) | |
tree | d1e101d7dcf79d362d087d431f5c4cefafbd30fd /common/resolver/resolver.go | |
parent | 5b0ea1044ab7c14e11489bc6c612bb7f2e5a023e (diff) | |
download | go-tangerine-3a540425a3d7fc29233b5cd3e7b79b4866f35d57.tar.gz go-tangerine-3a540425a3d7fc29233b5cd3e7b79b4866f35d57.tar.zst go-tangerine-3a540425a3d7fc29233b5cd3e7b79b4866f35d57.zip |
reorg:
- statereg methods move to natspec/resolver/docserver
- fix failing test on invalid js input
Diffstat (limited to 'common/resolver/resolver.go')
-rw-r--r-- | common/resolver/resolver.go | 49 |
1 files changed, 40 insertions, 9 deletions
diff --git a/common/resolver/resolver.go b/common/resolver/resolver.go index aa54a689b..f855aad73 100644 --- a/common/resolver/resolver.go +++ b/common/resolver/resolver.go @@ -1,10 +1,12 @@ package resolver import ( + "encoding/binary" "fmt" - "net/url" + // "net/url" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/xeth" ) @@ -12,6 +14,9 @@ import ( Resolver implements the Ethereum DNS mapping NameReg : Domain Name (or Code hash of Contract) -> Content Hash UrlHint : Content Hash -> Url Hint + +The resolver is meant to be called by the roundtripper transport implementation +of a url scheme */ const ( urlHintContractAddress = "urlhint" @@ -19,27 +24,53 @@ const ( ) type Resolver struct { - xeth *xeth.XEth + xeth *xeth.XEth + urlHintContractAddress string + nameRegContractAddress string +} + +func New(_xeth *xeth.XEth, uhca, nrca string) *Resolver { + return &Resolver{_xeth, uhca, nrca} } func (self *Resolver) NameToContentHash(name string) (hash common.Hash, err error) { // look up in nameReg - copy(hash[:], []byte(name)[:32]) + hashbytes := self.xeth.StorageAt(self.nameRegContractAddress, storageAddress(0, common.Hex2Bytes(name))) + copy(hash[:], hashbytes[:32]) return } -func (self *Resolver) ContentHashToUrl(hash common.Hash) (uri *url.URL, err error) { +func (self *Resolver) ContentHashToUrl(hash common.Hash) (uri string, err error) { // look up in nameReg - rawurl := fmt.Sprintf("bzz://%x/my/path/mycontract.sud", hash[:]) + + urlHex := self.xeth.StorageAt(self.urlHintContractAddress, storageAddress(0, hash.Bytes())) + uri = string(common.Hex2Bytes(urlHex)) + l := len(uri) + for (l > 0) && (uri[l-1] == 0) { + l-- + } + uri = uri[:l] + if l == 0 { + err = fmt.Errorf("GetURLhint: URL hint not found") + } + // rawurl := fmt.Sprintf("bzz://%x/my/path/mycontract.s ud", hash[:]) // mime type? - return url.Parse(rawurl) + return } -func (self *Resolver) NameToUrl(name string) (uri *url.URL, err error) { +func (self *Resolver) NameToUrl(name string) (uri string, hash common.Hash, err error) { // look up in urlHint - hash, err := self.NameToContentHash(name) + hash, err = self.NameToContentHash(name) if err != nil { return } - return self.ContentHashToUrl(hash) + uri, err = self.ContentHashToUrl(hash) + return +} + +func storageAddress(varidx uint32, key []byte) string { + data := make([]byte, 64) + binary.BigEndian.PutUint32(data[28:32], varidx) + copy(data[32:64], key[0:32]) + return common.Bytes2Hex(crypto.Sha3(data)) } |