contracts/ens: cid sanity tests

This commit is contained in:
Elad 2019-03-06 20:13:44 +07:00
parent 29d9b6b294
commit 5852134849
2 changed files with 85 additions and 4 deletions

View file

@ -25,12 +25,15 @@ import (
"encoding/binary" "encoding/binary"
"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"
"github.com/ethereum/go-ethereum/contracts/ens/contract" "github.com/ethereum/go-ethereum/contracts/ens/contract"
"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 (
@ -262,6 +265,14 @@ 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
c, err := cid.Decode(string(b))
if err != nil {
return common.Hash{}, err
}
return common.Hash{}, nil
/* from the EIP documentation /* from the EIP documentation
storage system: Swarm (0xe4) storage system: Swarm (0xe4)
CID version: 1 (0x01) CID version: 1 (0x01)
@ -274,9 +285,16 @@ func decodeMultiCodec(b []byte) (common.Hash, error) {
} }
func encodeMultiCodec(h common.Hash) []byte { // encodeCid encodes a swarm hash into an IPLD CID
b := []byte{0xe4, 0x01, 0x01, 0x1b, 0x20} 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()...) c := cid.NewCidV1(cid.Raw, multi) //todo: cid.Raw should be swarm manifest
return b
return c, nil
} }

View file

@ -17,6 +17,8 @@
package ens package ens
import ( import (
"bytes"
"fmt"
"math/big" "math/big"
"testing" "testing"
@ -27,6 +29,7 @@ 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"
mh "github.com/multiformats/go-multihash"
) )
var ( 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") 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())
*/
}
}