contracts/ens: more cid sanity checks

This commit is contained in:
Elad 2019-03-06 21:26:48 +07:00
parent 5852134849
commit 1032f140e9
2 changed files with 44 additions and 57 deletions

View file

@ -267,7 +267,7 @@ func (ens *ENS) SetContentHash(name string, hash []byte) (*types.Transaction, er
func decodeMultiCodec(b []byte) (common.Hash, error) { func decodeMultiCodec(b []byte) (common.Hash, error) {
// Create a cid from a marshaled string // Create a cid from a marshaled string
c, err := cid.Decode(string(b)) _, err := cid.Decode(string(b))
if err != nil { if err != nil {
return common.Hash{}, err return common.Hash{}, err
} }

View file

@ -29,6 +29,8 @@ 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" mh "github.com/multiformats/go-multihash"
) )
@ -125,36 +127,16 @@ func TestENS(t *testing.T) {
} }
func TestCIDSanity(t *testing.T) { func TestCIDSanity(t *testing.T) {
for _, v := range []struct { hashStr := "d1de9994b4d039f6548d191eb26786769f580809256b4685ef316805265ea162"
name string hash := common.HexToHash(hashStr) //this always yields a 32 byte long hash
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) cc, err := encodeCid(hash)
if err != nil { if err != nil {
if v.fail {
return
}
t.Fatal(err) t.Fatal(err)
} }
if cc.Prefix().MhLength != 32 { if cc.Prefix().MhLength != 32 {
t.Fatal("w00t") t.Fatal("w00t")
} }
fmt.Println(cc.Hash())
decoded, err := mh.Decode(cc.Hash()) decoded, err := mh.Decode(cc.Hash())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -169,17 +151,22 @@ func TestCIDSanity(t *testing.T) {
if decoded.Length != 32 { if decoded.Length != 32 {
t.Fatal("wrong length") t.Fatal("wrong length")
} }
fmt.Println("Created CID: ", cc)
}) bbbb, e := cc.StringOfBase(multibase.Base16)
if e != nil {
t.Fatal(e)
}
fmt.Println(bbbb)
//create the CID string artificially
hashStr = "f01551b20" + hashStr
fmt.Println(cc.Hash())
/*c, err := cid.Decode("zdvgqEMYmNeH5fKciougvQcfzMcNjF3Z1tPouJ8C7pc3pe63k") c, err := cid.Decode(hashStr) //"zdvgqEMYmNeH5fKciougvQcfzMcNjF3Z1tPouJ8C7pc3pe63k")
if err != nil { if err != nil {
t.Fatal("Error decoding CID") t.Fatalf("Error decoding CID: %v", err)
} }
fmt.Sprintf("Got CID: %v", c) fmt.Sprintf("Got CID: %v", c)
fmt.Println("Got CID:", c.Prefix()) fmt.Println("Got CID:", c.Prefix())
*/
}
} }