2015-03-31 00:57:17 +00:00
|
|
|
package resolver
|
|
|
|
|
|
|
|
import (
|
2015-03-31 15:02:55 +00:00
|
|
|
"encoding/binary"
|
2015-03-31 00:57:17 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2015-04-03 15:37:59 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2015-03-31 15:02:55 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2015-03-31 00:57:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
Resolver implements the Ethereum DNS mapping
|
|
|
|
NameReg : Domain Name (or Code hash of Contract) -> Content Hash
|
|
|
|
UrlHint : Content Hash -> Url Hint
|
2015-03-31 15:02:55 +00:00
|
|
|
|
|
|
|
The resolver is meant to be called by the roundtripper transport implementation
|
|
|
|
of a url scheme
|
2015-03-31 00:57:17 +00:00
|
|
|
*/
|
|
|
|
const (
|
2015-04-03 15:37:59 +00:00
|
|
|
URLHintContractAddress = core.ContractAddrURLhint
|
|
|
|
NameRegContractAddress = core.ContractAddrHashReg
|
2015-03-31 00:57:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Resolver struct {
|
2015-04-01 11:29:16 +00:00
|
|
|
backend Backend
|
2015-03-31 15:02:55 +00:00
|
|
|
urlHintContractAddress string
|
|
|
|
nameRegContractAddress string
|
|
|
|
}
|
|
|
|
|
2015-04-01 11:29:16 +00:00
|
|
|
type Backend interface {
|
|
|
|
StorageAt(string, string) string
|
2015-03-31 00:57:17 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 11:29:16 +00:00
|
|
|
func New(eth Backend, uhca, nrca string) *Resolver {
|
|
|
|
return &Resolver{eth, uhca, nrca}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Resolver) NameToContentHash(name string) (chash common.Hash, err error) {
|
2015-03-31 00:57:17 +00:00
|
|
|
// look up in nameReg
|
2015-04-03 15:37:59 +00:00
|
|
|
key := storageAddress(1, common.Hex2BytesFixed(name, 32))
|
2015-04-06 06:01:36 +00:00
|
|
|
hash := self.backend.StorageAt("0x"+self.nameRegContractAddress, key)
|
2015-04-01 11:29:16 +00:00
|
|
|
copy(chash[:], common.Hex2Bytes(hash))
|
2015-03-31 00:57:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-01 11:29:16 +00:00
|
|
|
func (self *Resolver) ContentHashToUrl(chash common.Hash) (uri string, err error) {
|
2015-03-31 00:57:17 +00:00
|
|
|
// look up in nameReg
|
2015-04-03 15:37:59 +00:00
|
|
|
key := storageAddress(2, chash[:])
|
2015-04-06 06:01:36 +00:00
|
|
|
hex := self.backend.StorageAt("0x"+self.urlHintContractAddress, key)
|
|
|
|
uri = string(common.Hex2Bytes(hex[2:]))
|
2015-03-31 15:02:55 +00:00
|
|
|
l := len(uri)
|
|
|
|
for (l > 0) && (uri[l-1] == 0) {
|
|
|
|
l--
|
|
|
|
}
|
|
|
|
uri = uri[:l]
|
2015-04-01 11:29:16 +00:00
|
|
|
|
2015-03-31 15:02:55 +00:00
|
|
|
if l == 0 {
|
|
|
|
err = fmt.Errorf("GetURLhint: URL hint not found")
|
|
|
|
}
|
|
|
|
return
|
2015-03-31 00:57:17 +00:00
|
|
|
}
|
|
|
|
|
2015-03-31 15:02:55 +00:00
|
|
|
func (self *Resolver) NameToUrl(name string) (uri string, hash common.Hash, err error) {
|
2015-03-31 00:57:17 +00:00
|
|
|
// look up in urlHint
|
2015-03-31 15:02:55 +00:00
|
|
|
hash, err = self.NameToContentHash(name)
|
2015-03-31 00:57:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-03-31 15:02:55 +00:00
|
|
|
uri, err = self.ContentHashToUrl(hash)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func storageAddress(varidx uint32, key []byte) string {
|
|
|
|
data := make([]byte, 64)
|
2015-04-06 06:01:36 +00:00
|
|
|
binary.BigEndian.PutUint32(data[60:64], varidx)
|
|
|
|
copy(data[0:32], key[0:32])
|
|
|
|
return "0x" + common.Bytes2Hex(crypto.Sha3(data))
|
2015-03-31 00:57:17 +00:00
|
|
|
}
|