contracts/ens: removed multiformats dependencies

This commit is contained in:
Elad 2019-03-08 16:54:41 +07:00
parent 2168ea0c66
commit 3a14b32ab6
2 changed files with 34 additions and 44 deletions

View file

@ -27,7 +27,6 @@ import (
"fmt" "fmt"
"strings" "strings"
mh "github.com/multiformats/go-multihash"
"github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -35,7 +34,6 @@ import (
"github.com/ethereum/go-ethereum/contracts/ens/fallback_contract" "github.com/ethereum/go-ethereum/contracts/ens/fallback_contract"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ipfs/go-cid"
) )
var ( var (
@ -266,27 +264,6 @@ func (ens *ENS) SetContentHash(name string, hash []byte) (*types.Transaction, er
return resolver.Contract.SetContenthash(&opts, node, hash) return resolver.Contract.SetContenthash(&opts, node, hash)
} }
func decodeMultiCodec(b []byte) (common.Hash, error) {
// Create a cid from a marshaled string
_, err := cid.Decode(string(b))
if err != nil {
return common.Hash{}, err
}
return common.Hash{}, nil
/* from the EIP documentation
storage system: Swarm (0xe4)
CID version: 1 (0x01)
content type: swarm-manifest (0x??)
hash function: keccak-256 (0x1B)
hash length: 32 bytes (0x20)
hash: 29f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f
*/
//<protoCode uvarint><cid-version><multicodec-content-type><multihash-content-address>
}
func manualDecode(buf []byte) (common.Hash, error) { func manualDecode(buf []byte) (common.Hash, error) {
if len(buf) < 2 { if len(buf) < 2 {
return common.Hash{}, errors.New("buffer too short") return common.Hash{}, errors.New("buffer too short")
@ -332,7 +309,7 @@ func manualDecode(buf []byte) (common.Hash, error) {
} }
// encodeCid encodes a swarm hash into an IPLD CID // encodeCid encodes a swarm hash into an IPLD CID
func encodeCid(h common.Hash) (cid.Cid, error) { /*func encodeCid(h common.Hash) (cid.Cid, error) {
b := []byte{0x1b, 0x20} //0x1b = keccak256 (should be changed to bmt), 0x20 = 32 bytes hash length b := []byte{0x1b, 0x20} //0x1b = keccak256 (should be changed to bmt), 0x20 = 32 bytes hash length
b = append(b, h.Bytes()...) // append actual hash bytes b = append(b, h.Bytes()...) // append actual hash bytes
multi, err := mh.Cast(b) multi, err := mh.Cast(b)
@ -343,4 +320,4 @@ func encodeCid(h common.Hash) (cid.Cid, error) {
c := cid.NewCidV1(cid.Raw, multi) //todo: cid.Raw should be swarm manifest c := cid.NewCidV1(cid.Raw, multi) //todo: cid.Raw should be swarm manifest
return c, nil return c, nil
} }*/

View file

@ -31,9 +31,6 @@ import (
"github.com/ethereum/go-ethereum/contracts/ens/fallback_contract" "github.com/ethereum/go-ethereum/contracts/ens/fallback_contract"
"github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ipfs/go-cid"
"github.com/multiformats/go-multibase"
mh "github.com/multiformats/go-multihash"
) )
var ( var (
@ -130,24 +127,39 @@ func TestENS(t *testing.T) {
func TestManuelCidDecode(t *testing.T) { func TestManuelCidDecode(t *testing.T) {
// call cid encode method with hash. expect byte slice returned, compare according to spec // call cid encode method with hash. expect byte slice returned, compare according to spec
bb := []byte{} bb := []byte{}
buf := make([]byte, binary.MaxVarintLen64)
for _, v := range []byte{0xe4, 0x01, 0x99, 0x1b, 0x20} { for _, v := range []struct {
n := binary.PutUvarint(buf, uint64(v)) name string
bb = append(bb, buf[:n]...) headerBytes []byte
} fails bool
h := common.HexToHash("29f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f") }{
bb = append(bb, h[:]...) {
str := hex.EncodeToString(bb) name: "w00t",
fmt.Println(str) headerBytes: []byte{0xe4, 0x01, 0x99, 0x1b, 0x20},
decodedHash, e := manualDecode(bb) fails: false,
if e != nil { },
t.Fatal(e) } {
buf := make([]byte, binary.MaxVarintLen64)
for _, vv := range v.headerBytes {
n := binary.PutUvarint(buf, uint64(vv))
bb = append(bb, buf[:n]...)
}
h := common.HexToHash("29f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f")
bb = append(bb, h[:]...)
str := hex.EncodeToString(bb)
fmt.Println(str)
decodedHash, e := manualDecode(bb)
if e != nil {
t.Fatal(e)
}
if !bytes.Equal(decodedHash[:], h[:]) {
t.Fatal("hashes not equal")
}
} }
if !bytes.Equal(decodedHash[:], h[:]) {
t.Fatal("hashes not equal")
}
/* from the EIP documentation /* from the EIP documentation
storage system: Swarm (0xe4) storage system: Swarm (0xe4)
CID version: 1 (0x01) CID version: 1 (0x01)
@ -173,6 +185,7 @@ func TestManuelCidEncode(t *testing.T) {
} }
/*
func TestCIDSanity(t *testing.T) { func TestCIDSanity(t *testing.T) {
hashStr := "d1de9994b4d039f6548d191eb26786769f580809256b4685ef316805265ea162" hashStr := "d1de9994b4d039f6548d191eb26786769f580809256b4685ef316805265ea162"
hash := common.HexToHash(hashStr) //this always yields a 32 byte long hash hash := common.HexToHash(hashStr) //this always yields a 32 byte long hash
@ -216,4 +229,4 @@ func TestCIDSanity(t *testing.T) {
fmt.Sprintf("Got CID: %v", c) fmt.Sprintf("Got CID: %v", c)
fmt.Println("Got CID:", c.Prefix()) fmt.Println("Got CID:", c.Prefix())
} }*/