From 5852134849fcb46ba4591777457b148ecd144ca2 Mon Sep 17 00:00:00 2001 From: Elad Date: Wed, 6 Mar 2019 20:13:44 +0700 Subject: [PATCH] contracts/ens: cid sanity tests --- contracts/ens/ens.go | 26 +++++++++++++--- contracts/ens/ens_test.go | 63 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 4 deletions(-) diff --git a/contracts/ens/ens.go b/contracts/ens/ens.go index 3930ae4fb0..2a761542d5 100644 --- a/contracts/ens/ens.go +++ b/contracts/ens/ens.go @@ -25,12 +25,15 @@ import ( "encoding/binary" "strings" + mh "github.com/multiformats/go-multihash" + "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/contracts/ens/contract" "github.com/ethereum/go-ethereum/contracts/ens/fallback_contract" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" + "github.com/ipfs/go-cid" ) var ( @@ -262,6 +265,14 @@ func (ens *ENS) SetContentHash(name string, hash []byte) (*types.Transaction, er } func decodeMultiCodec(b []byte) (common.Hash, error) { + + // Create a cid from a marshaled string + c, 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) @@ -274,9 +285,16 @@ func decodeMultiCodec(b []byte) (common.Hash, error) { } -func encodeMultiCodec(h common.Hash) []byte { - b := []byte{0xe4, 0x01, 0x01, 0x1b, 0x20} +// encodeCid encodes a swarm hash into an IPLD CID +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 = append(b, h.Bytes()...) // append actual hash bytes + multi, err := mh.Cast(b) + if err != nil { + return cid.Cid{}, err + } - b = append(b, h.Bytes()...) - return b + c := cid.NewCidV1(cid.Raw, multi) //todo: cid.Raw should be swarm manifest + + return c, nil } diff --git a/contracts/ens/ens_test.go b/contracts/ens/ens_test.go index 764a575cb7..4c7bfcda79 100644 --- a/contracts/ens/ens_test.go +++ b/contracts/ens/ens_test.go @@ -17,6 +17,8 @@ package ens import ( + "bytes" + "fmt" "math/big" "testing" @@ -27,6 +29,7 @@ import ( "github.com/ethereum/go-ethereum/contracts/ens/fallback_contract" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/crypto" + mh "github.com/multiformats/go-multihash" ) var ( @@ -120,3 +123,63 @@ func TestENS(t *testing.T) { } t.Fatal("todo: try to set old contract with new multicodec stuff and assert fail, set new contract with multicodec stuff, encode, decode and assert returns correct hash") } + +func TestCIDSanity(t *testing.T) { + for _, v := range []struct { + name string + hashStr string + fail bool + }{ + { + name: "hash OK, should not fail", + hashStr: "d1de9994b4d039f6548d191eb26786769f580809256b4685ef316805265ea162", + fail: false, + }, + { + name: "hash empty , should fail", + hashStr: "", + fail: true, + }, + } { + t.Run(v.name, func(t *testing.T) { + hash := common.HexToHash(v.hashStr) + cc, err := encodeCid(hash) + if err != nil { + if v.fail { + return + } + t.Fatal(err) + } + + if cc.Prefix().MhLength != 32 { + t.Fatal("w00t") + } + fmt.Println(cc.Hash()) + decoded, err := mh.Decode(cc.Hash()) + if err != nil { + t.Fatal(err) + } + if decoded.Length != 32 { + t.Fatal("invalid length") + } + if !bytes.Equal(decoded.Digest, hash[:]) { + t.Fatalf("hashes not equal") + } + + if decoded.Length != 32 { + t.Fatal("wrong length") + } + fmt.Println("Created CID: ", cc) + + }) + + /*c, err := cid.Decode("zdvgqEMYmNeH5fKciougvQcfzMcNjF3Z1tPouJ8C7pc3pe63k") + if err != nil { + t.Fatal("Error decoding CID") + } + + fmt.Sprintf("Got CID: %v", c) + fmt.Println("Got CID:", c.Prefix()) + */ + } +}