2015-03-31 03:57:17 +03:00
|
|
|
package resolver
|
|
|
|
|
|
|
|
import (
|
2015-03-31 18:02:55 +03:00
|
|
|
"encoding/binary"
|
2015-03-31 03:57:17 +03:00
|
|
|
"fmt"
|
2015-03-31 18:02:55 +03:00
|
|
|
// "net/url"
|
2015-03-31 03:57:17 +03:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2015-03-31 18:02:55 +03:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2015-03-31 03:57:17 +03:00
|
|
|
"github.com/ethereum/go-ethereum/xeth"
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
Resolver implements the Ethereum DNS mapping
|
|
|
|
NameReg : Domain Name (or Code hash of Contract) -> Content Hash
|
|
|
|
UrlHint : Content Hash -> Url Hint
|
2015-03-31 18:02:55 +03:00
|
|
|
|
|
|
|
The resolver is meant to be called by the roundtripper transport implementation
|
|
|
|
of a url scheme
|
2015-03-31 03:57:17 +03:00
|
|
|
*/
|
|
|
|
const (
|
|
|
|
urlHintContractAddress = "urlhint"
|
|
|
|
nameRegContractAddress = "nameReg"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Resolver struct {
|
2015-03-31 18:02:55 +03:00
|
|
|
xeth *xeth.XEth
|
|
|
|
urlHintContractAddress string
|
|
|
|
nameRegContractAddress string
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(_xeth *xeth.XEth, uhca, nrca string) *Resolver {
|
|
|
|
return &Resolver{_xeth, uhca, nrca}
|
2015-03-31 03:57:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Resolver) NameToContentHash(name string) (hash common.Hash, err error) {
|
|
|
|
// look up in nameReg
|
2015-03-31 18:02:55 +03:00
|
|
|
hashbytes := self.xeth.StorageAt(self.nameRegContractAddress, storageAddress(0, common.Hex2Bytes(name)))
|
|
|
|
copy(hash[:], hashbytes[:32])
|
2015-03-31 03:57:17 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-31 18:02:55 +03:00
|
|
|
func (self *Resolver) ContentHashToUrl(hash common.Hash) (uri string, err error) {
|
2015-03-31 03:57:17 +03:00
|
|
|
// look up in nameReg
|
2015-03-31 18:02:55 +03:00
|
|
|
|
|
|
|
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[:])
|
2015-03-31 03:57:17 +03:00
|
|
|
// mime type?
|
2015-03-31 18:02:55 +03:00
|
|
|
return
|
2015-03-31 03:57:17 +03:00
|
|
|
}
|
|
|
|
|
2015-03-31 18:02:55 +03:00
|
|
|
func (self *Resolver) NameToUrl(name string) (uri string, hash common.Hash, err error) {
|
2015-03-31 03:57:17 +03:00
|
|
|
// look up in urlHint
|
2015-03-31 18:02:55 +03:00
|
|
|
hash, err = self.NameToContentHash(name)
|
2015-03-31 03:57:17 +03:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-03-31 18:02:55 +03:00
|
|
|
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))
|
2015-03-31 03:57:17 +03:00
|
|
|
}
|