resolver.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package resolver
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. "github.com/ethereum/go-ethereum/common"
  6. "github.com/ethereum/go-ethereum/crypto"
  7. xe "github.com/ethereum/go-ethereum/xeth"
  8. )
  9. /*
  10. Resolver implements the Ethereum DNS mapping
  11. HashReg : Key Hash (hash of domain name or contract code) -> Content Hash
  12. UrlHint : Content Hash -> Url Hint
  13. The resolver is meant to be called by the roundtripper transport implementation
  14. of a url scheme
  15. */
  16. // contract addresses will be hardcoded after they're created
  17. var URLHintContractAddress string = "0000000000000000000000000000000000000000000000000000000000001234"
  18. var HashRegContractAddress string = "0000000000000000000000000000000000000000000000000000000000005678"
  19. func CreateContracts(xeth *xe.XEth, addr string) {
  20. var err error
  21. URLHintContractAddress, err = xeth.Transact(addr, "", "100000000000", "1000000", "100000", ContractCodeURLhint)
  22. if err != nil {
  23. panic(err)
  24. }
  25. HashRegContractAddress, err = xeth.Transact(addr, "", "100000000000", "1000000", "100000", ContractCodeHashReg)
  26. if err != nil {
  27. panic(err)
  28. }
  29. URLHintContractAddress = URLHintContractAddress[2:]
  30. HashRegContractAddress = HashRegContractAddress[2:]
  31. }
  32. type Resolver struct {
  33. backend Backend
  34. urlHintContractAddress string
  35. hashRegContractAddress string
  36. }
  37. type Backend interface {
  38. StorageAt(string, string) string
  39. }
  40. func New(eth Backend, uhca, nrca string) *Resolver {
  41. return &Resolver{eth, uhca, nrca}
  42. }
  43. func (self *Resolver) KeyToContentHash(khash common.Hash) (chash common.Hash, err error) {
  44. // look up in hashReg
  45. key := storageAddress(1, khash[:])
  46. hash := self.backend.StorageAt("0x"+self.hashRegContractAddress, key)
  47. if hash == "0x0" || len(hash) < 3 {
  48. err = fmt.Errorf("GetHashReg: content hash not found")
  49. return
  50. }
  51. copy(chash[:], common.Hex2BytesFixed(hash[2:], 32))
  52. return
  53. }
  54. func (self *Resolver) ContentHashToUrl(chash common.Hash) (uri string, err error) {
  55. // look up in URL reg
  56. key := storageAddress(1, chash[:])
  57. hex := self.backend.StorageAt("0x"+self.urlHintContractAddress, key)
  58. uri = string(common.Hex2Bytes(hex[2:]))
  59. l := len(uri)
  60. for (l > 0) && (uri[l-1] == 0) {
  61. l--
  62. }
  63. uri = uri[:l]
  64. if l == 0 {
  65. err = fmt.Errorf("GetURLhint: URL hint not found")
  66. }
  67. return
  68. }
  69. func (self *Resolver) KeyToUrl(key common.Hash) (uri string, hash common.Hash, err error) {
  70. // look up in urlHint
  71. hash, err = self.KeyToContentHash(key)
  72. if err != nil {
  73. return
  74. }
  75. uri, err = self.ContentHashToUrl(hash)
  76. return
  77. }
  78. func storageAddress(varidx uint32, key []byte) string {
  79. data := make([]byte, 64)
  80. binary.BigEndian.PutUint32(data[60:64], varidx)
  81. copy(data[0:32], key[0:32])
  82. //fmt.Printf("%x %v\n", key, common.Bytes2Hex(crypto.Sha3(data)))
  83. return "0x" + common.Bytes2Hex(crypto.Sha3(data))
  84. }