mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
constracts/ens: address PR comments
This commit is contained in:
parent
1f9594eb8e
commit
f8e76bc085
2 changed files with 37 additions and 36 deletions
|
|
@ -27,13 +27,13 @@ import (
|
||||||
const (
|
const (
|
||||||
cidv1 = 0x1
|
cidv1 = 0x1
|
||||||
|
|
||||||
ns_ipfs = 0xe3
|
nsIpfs = 0xe3
|
||||||
ns_swarm = 0xe4
|
nsSwarm = 0xe4
|
||||||
|
|
||||||
swarm_typecode = 0xfa //swarm manifest
|
swarmTypecode = 0xfa //swarm manifest, see https://github.com/multiformats/multicodec/blob/master/table.csv
|
||||||
swarm_hashtype = 0xd6 // BMT
|
swarmHashtype = 0xd6 // BMT, see https://github.com/multiformats/multicodec/blob/master/table.csv
|
||||||
|
|
||||||
hash_length = 32
|
hashLength = 32
|
||||||
)
|
)
|
||||||
|
|
||||||
// deocodeEIP1577ContentHash decodes a chain-stored content hash from an ENS record according to EIP-1577
|
// deocodeEIP1577ContentHash decodes a chain-stored content hash from an ENS record according to EIP-1577
|
||||||
|
|
@ -41,7 +41,7 @@ const (
|
||||||
// Note: only CIDv1 is supported
|
// Note: only CIDv1 is supported
|
||||||
func decodeEIP1577ContentHash(buf []byte) (storageNs, contentType, hashType, hashLength uint64, hash []byte, err error) {
|
func decodeEIP1577ContentHash(buf []byte) (storageNs, contentType, hashType, hashLength uint64, hash []byte, err error) {
|
||||||
if len(buf) < 10 {
|
if len(buf) < 10 {
|
||||||
return 0, 0, 0, 0, nil, fmt.Errorf("buffer too short")
|
return 0, 0, 0, 0, nil, errors.New("buffer too short")
|
||||||
}
|
}
|
||||||
|
|
||||||
storageNs, n := binary.Uvarint(buf)
|
storageNs, n := binary.Uvarint(buf)
|
||||||
|
|
@ -76,19 +76,19 @@ func extractContentHash(buf []byte) (common.Hash, error) {
|
||||||
return common.Hash{}, err
|
return common.Hash{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if storageNs != ns_swarm {
|
if storageNs != nsSwarm {
|
||||||
return common.Hash{}, errors.New("unknown storage system")
|
return common.Hash{}, errors.New("unknown storage system")
|
||||||
}
|
}
|
||||||
|
|
||||||
if contentType != swarm_typecode {
|
if contentType != swarmTypecode {
|
||||||
return common.Hash{}, errors.New("unknown content type")
|
return common.Hash{}, errors.New("unknown content type")
|
||||||
}
|
}
|
||||||
|
|
||||||
if hashType != swarm_hashtype {
|
if hashType != swarmHashtype {
|
||||||
return common.Hash{}, errors.New("unknown multihash type")
|
return common.Hash{}, errors.New("unknown multihash type")
|
||||||
}
|
}
|
||||||
|
|
||||||
if hashLength != hash_length {
|
if hashLength != hashLength {
|
||||||
return common.Hash{}, errors.New("odd hash length, swarm expects 32 bytes")
|
return common.Hash{}, errors.New("odd hash length, swarm expects 32 bytes")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -102,11 +102,11 @@ func extractContentHash(buf []byte) (common.Hash, error) {
|
||||||
func encodeSwarmHash(hash common.Hash) ([]byte, error) {
|
func encodeSwarmHash(hash common.Hash) ([]byte, error) {
|
||||||
var cidBytes []byte
|
var cidBytes []byte
|
||||||
var headerBytes = []byte{
|
var headerBytes = []byte{
|
||||||
ns_swarm, //swarm namespace
|
nsSwarm, //swarm namespace
|
||||||
cidv1, // CIDv1
|
cidv1, // CIDv1
|
||||||
swarm_typecode, // swarm hash
|
swarmTypecode, // swarm hash
|
||||||
swarm_hashtype, // swarm bmt hash
|
swarmHashtype, // swarm bmt hash
|
||||||
hash_length, //hash length. 32 bytes
|
hashLength, //hash length. 32 bytes
|
||||||
}
|
}
|
||||||
|
|
||||||
varintbuf := make([]byte, binary.MaxVarintLen64)
|
varintbuf := make([]byte, binary.MaxVarintLen64)
|
||||||
|
|
|
||||||
|
|
@ -26,16 +26,16 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const ()
|
||||||
eipSpecHash = "e3010170122029f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f"
|
|
||||||
eipHash = "29f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f"
|
|
||||||
|
|
||||||
dag_pb = 0x70
|
|
||||||
sha2_256 = 0x12
|
|
||||||
)
|
|
||||||
|
|
||||||
// Tests for the decoding of the example ENS
|
// Tests for the decoding of the example ENS
|
||||||
func TestEIPSpecCidDecode(t *testing.T) {
|
func TestEIPSpecCidDecode(t *testing.T) {
|
||||||
|
const (
|
||||||
|
eipSpecHash = "e3010170122029f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f"
|
||||||
|
eipHash = "29f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f"
|
||||||
|
dagPb = 0x70
|
||||||
|
sha2256 = 0x12
|
||||||
|
)
|
||||||
b, err := hex.DecodeString(eipSpecHash)
|
b, err := hex.DecodeString(eipSpecHash)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|
@ -46,24 +46,24 @@ func TestEIPSpecCidDecode(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
storageNs, contentType, hashType, hashLength, hashBytes, err := decodeEIP1577ContentHash(b)
|
storageNs, contentType, hashType, hashLength, decodedHashBytes, err := decodeEIP1577ContentHash(b)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if storageNs != ns_ipfs {
|
if storageNs != nsIpfs {
|
||||||
t.Fatal("wrong ns")
|
t.Fatal("wrong ns")
|
||||||
}
|
}
|
||||||
if contentType != dag_pb {
|
if contentType != dagPb {
|
||||||
t.Fatal("should be swarm typecode")
|
t.Fatal("should be ipfs typecode")
|
||||||
}
|
}
|
||||||
if hashType != sha2_256 {
|
if hashType != sha2256 {
|
||||||
t.Fatal("should be sha2-256")
|
t.Fatal("should be sha2-256")
|
||||||
}
|
}
|
||||||
if hashLength != 32 {
|
if hashLength != 32 {
|
||||||
t.Fatal("should be 32")
|
t.Fatal("should be 32")
|
||||||
}
|
}
|
||||||
if !bytes.Equal(hashBytes, hashBytes) {
|
if !bytes.Equal(hashBytes, decodedHashBytes) {
|
||||||
t.Fatal("should be equal")
|
t.Fatal("should be equal")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -74,30 +74,32 @@ func TestManualCidDecode(t *testing.T) {
|
||||||
for _, v := range []struct {
|
for _, v := range []struct {
|
||||||
name string
|
name string
|
||||||
headerBytes []byte
|
headerBytes []byte
|
||||||
fails bool
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "values correct, should not fail",
|
name: "values correct, should not fail",
|
||||||
headerBytes: []byte{0xe4, 0x01, 0xfa, 0xd6, 0x20},
|
headerBytes: []byte{0xe4, 0x01, 0xfa, 0xd6, 0x20},
|
||||||
fails: false,
|
wantErr: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "cid version wrong, should fail",
|
name: "cid version wrong, should fail",
|
||||||
headerBytes: []byte{0xe4, 0x00, 0xfa, 0xd6, 0x20},
|
headerBytes: []byte{0xe4, 0x00, 0xfa, 0xd6, 0x20},
|
||||||
fails: true,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "hash length wrong, should fail",
|
name: "hash length wrong, should fail",
|
||||||
headerBytes: []byte{0xe4, 0x01, 0xfa, 0xd6, 0x1f},
|
headerBytes: []byte{0xe4, 0x01, 0xfa, 0xd6, 0x1f},
|
||||||
fails: true,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "values correct for ipfs, should fail",
|
name: "values correct for ipfs, should fail",
|
||||||
headerBytes: []byte{0xe3, 0x01, 0x70, 0x12, 0x20},
|
headerBytes: []byte{0xe3, 0x01, 0x70, 0x12, 0x20},
|
||||||
fails: true,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Run(v.name, func(t *testing.T) {
|
t.Run(v.name, func(t *testing.T) {
|
||||||
|
const eipHash = "29f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f"
|
||||||
|
|
||||||
var bb []byte
|
var bb []byte
|
||||||
buf := make([]byte, binary.MaxVarintLen64)
|
buf := make([]byte, binary.MaxVarintLen64)
|
||||||
for _, vv := range v.headerBytes {
|
for _, vv := range v.headerBytes {
|
||||||
|
|
@ -110,7 +112,7 @@ func TestManualCidDecode(t *testing.T) {
|
||||||
str := hex.EncodeToString(bb)
|
str := hex.EncodeToString(bb)
|
||||||
fmt.Println(str)
|
fmt.Println(str)
|
||||||
decodedHash, e := extractContentHash(bb)
|
decodedHash, e := extractContentHash(bb)
|
||||||
switch v.fails {
|
switch v.wantErr {
|
||||||
case true:
|
case true:
|
||||||
if e == nil {
|
if e == nil {
|
||||||
t.Fatal("the decode should fail")
|
t.Fatal("the decode should fail")
|
||||||
|
|
@ -122,15 +124,14 @@ func TestManualCidDecode(t *testing.T) {
|
||||||
if !bytes.Equal(decodedHash[:], h[:]) {
|
if !bytes.Equal(decodedHash[:], h[:]) {
|
||||||
t.Fatal("hashes not equal")
|
t.Fatal("hashes not equal")
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestManuelCidEncode(t *testing.T) {
|
func TestManuelCidEncode(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
|
||||||
|
const eipHash = "29f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f"
|
||||||
cidBytes, err := encodeSwarmHash(common.HexToHash(eipHash))
|
cidBytes, err := encodeSwarmHash(common.HexToHash(eipHash))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue