2015-03-30 19:57:17 -05:00
|
|
|
package resolver
|
|
|
|
|
|
|
|
import (
|
2015-03-31 10:02:55 -05:00
|
|
|
"encoding/binary"
|
2015-03-30 19:57:17 -05:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2015-03-31 10:02:55 -05:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2015-04-14 08:20:52 -05:00
|
|
|
xe "github.com/ethereum/go-ethereum/xeth"
|
2015-03-30 19:57:17 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
Resolver implements the Ethereum DNS mapping
|
2015-04-07 04:50:17 -05:00
|
|
|
HashReg : Key Hash (hash of domain name or contract code) -> Content Hash
|
2015-03-30 19:57:17 -05:00
|
|
|
UrlHint : Content Hash -> Url Hint
|
2015-03-31 10:02:55 -05:00
|
|
|
|
|
|
|
The resolver is meant to be called by the roundtripper transport implementation
|
|
|
|
of a url scheme
|
2015-03-30 19:57:17 -05:00
|
|
|
*/
|
2015-04-14 08:20:52 -05:00
|
|
|
|
|
|
|
// contract addresses will be hardcoded after they're created
|
|
|
|
var URLHintContractAddress string = "0000000000000000000000000000000000000000000000000000000000001234"
|
|
|
|
var HashRegContractAddress string = "0000000000000000000000000000000000000000000000000000000000005678"
|
|
|
|
|
|
|
|
func CreateContracts(xeth *xe.XEth, addr string) {
|
|
|
|
var err error
|
|
|
|
URLHintContractAddress, err = xeth.Transact(addr, "", "100000000000", "1000000", "100000", ContractCodeURLhint)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
HashRegContractAddress, err = xeth.Transact(addr, "", "100000000000", "1000000", "100000", ContractCodeHashReg)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
URLHintContractAddress = URLHintContractAddress[2:]
|
|
|
|
HashRegContractAddress = HashRegContractAddress[2:]
|
|
|
|
}
|
2015-03-30 19:57:17 -05:00
|
|
|
|
|
|
|
type Resolver struct {
|
2015-04-01 06:29:16 -05:00
|
|
|
backend Backend
|
2015-03-31 10:02:55 -05:00
|
|
|
urlHintContractAddress string
|
2015-04-07 04:50:17 -05:00
|
|
|
hashRegContractAddress string
|
2015-03-31 10:02:55 -05:00
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:16 -05:00
|
|
|
type Backend interface {
|
|
|
|
StorageAt(string, string) string
|
2015-03-30 19:57:17 -05:00
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:16 -05:00
|
|
|
func New(eth Backend, uhca, nrca string) *Resolver {
|
|
|
|
return &Resolver{eth, uhca, nrca}
|
|
|
|
}
|
|
|
|
|
2015-04-07 04:50:17 -05:00
|
|
|
func (self *Resolver) KeyToContentHash(khash common.Hash) (chash common.Hash, err error) {
|
|
|
|
// look up in hashReg
|
|
|
|
key := storageAddress(1, khash[:])
|
|
|
|
hash := self.backend.StorageAt("0x"+self.hashRegContractAddress, key)
|
|
|
|
|
|
|
|
if hash == "0x0" || len(hash) < 3 {
|
|
|
|
err = fmt.Errorf("GetHashReg: content hash not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
copy(chash[:], common.Hex2BytesFixed(hash[2:], 32))
|
2015-03-30 19:57:17 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:16 -05:00
|
|
|
func (self *Resolver) ContentHashToUrl(chash common.Hash) (uri string, err error) {
|
2015-04-07 04:50:17 -05:00
|
|
|
// look up in URL reg
|
|
|
|
key := storageAddress(1, chash[:])
|
2015-04-06 01:01:36 -05:00
|
|
|
hex := self.backend.StorageAt("0x"+self.urlHintContractAddress, key)
|
|
|
|
uri = string(common.Hex2Bytes(hex[2:]))
|
2015-03-31 10:02:55 -05:00
|
|
|
l := len(uri)
|
|
|
|
for (l > 0) && (uri[l-1] == 0) {
|
|
|
|
l--
|
|
|
|
}
|
|
|
|
uri = uri[:l]
|
2015-04-01 06:29:16 -05:00
|
|
|
|
2015-03-31 10:02:55 -05:00
|
|
|
if l == 0 {
|
|
|
|
err = fmt.Errorf("GetURLhint: URL hint not found")
|
|
|
|
}
|
|
|
|
return
|
2015-03-30 19:57:17 -05:00
|
|
|
}
|
|
|
|
|
2015-04-07 04:50:17 -05:00
|
|
|
func (self *Resolver) KeyToUrl(key common.Hash) (uri string, hash common.Hash, err error) {
|
2015-03-30 19:57:17 -05:00
|
|
|
// look up in urlHint
|
2015-04-07 04:50:17 -05:00
|
|
|
hash, err = self.KeyToContentHash(key)
|
2015-03-30 19:57:17 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-03-31 10:02:55 -05:00
|
|
|
uri, err = self.ContentHashToUrl(hash)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func storageAddress(varidx uint32, key []byte) string {
|
|
|
|
data := make([]byte, 64)
|
2015-04-06 01:01:36 -05:00
|
|
|
binary.BigEndian.PutUint32(data[60:64], varidx)
|
|
|
|
copy(data[0:32], key[0:32])
|
2015-04-08 06:22:31 -05:00
|
|
|
//fmt.Printf("%x %v\n", key, common.Bytes2Hex(crypto.Sha3(data)))
|
2015-04-06 01:01:36 -05:00
|
|
|
return "0x" + common.Bytes2Hex(crypto.Sha3(data))
|
2015-03-30 19:57:17 -05:00
|
|
|
}
|