mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
contracts/ens: cid sanity tests
This commit is contained in:
parent
29d9b6b294
commit
5852134849
2 changed files with 85 additions and 4 deletions
|
|
@ -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}
|
||||
|
||||
b = append(b, h.Bytes()...)
|
||||
return b
|
||||
// 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
|
||||
}
|
||||
|
||||
c := cid.NewCidV1(cid.Raw, multi) //todo: cid.Raw should be swarm manifest
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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())
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue