This commit is contained in:
Elad 2019-03-07 20:27:13 +07:00
parent 36822f805e
commit e8f983ec85
2 changed files with 93 additions and 0 deletions

View file

@ -23,6 +23,8 @@ package ens
import (
"encoding/binary"
"errors"
"fmt"
"strings"
mh "github.com/multiformats/go-multihash"
@ -285,6 +287,50 @@ func decodeMultiCodec(b []byte) (common.Hash, error) {
}
func manualDecode(buf []byte) (common.Hash, error) {
if len(buf) < 2 {
return common.Hash{}, errors.New("buffer too short")
}
storageSys, n := binary.Uvarint(buf)
if storageSys != 0xe3 && storageSys != 0xe4 {
return common.Hash{}, errors.New("unknown storage system")
}
buf = buf[n:]
vers, n := binary.Uvarint(buf)
if vers != 1 {
return common.Hash{}, fmt.Errorf("expected 1 as the cid version number, got: %d", vers)
}
buf = buf[n:]
ctype, n := binary.Uvarint(buf)
if ctype != 0x99 {
return common.Hash{}, errors.New("unknown content type")
}
buf = buf[n:]
hashType, n := binary.Uvarint(buf)
if hashType != 0x1b {
return common.Hash{}, errors.New("unknown multihash type")
}
buf = buf[n:]
hashLen, n := binary.Uvarint(buf)
if hashLen != 32 {
return common.Hash{}, errors.New("odd hash length, swarm expects 32 bytes")
}
buf = buf[n:]
if len(buf) != int(hashLen) {
return common.Hash{}, errors.New("hash length mismatch")
}
return common.BytesToHash(buf), nil
}
// 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

View file

@ -18,6 +18,8 @@ package ens
import (
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
"math/big"
"testing"
@ -125,6 +127,51 @@ 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 TestManuelCidDecode(t *testing.T) {
// call cid encode method with hash. expect byte slice returned, compare according to spec
bb := []byte{}
buf := make([]byte, binary.MaxVarintLen64)
for _, v := range []byte{0xe4, 0x01, 0x99, 0x1b, 0x20} {
n := binary.PutUvarint(buf, uint64(v))
bb = append(bb, buf[:n]...)
}
h := common.HexToHash("29f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f")
bb = append(bb, h[:]...)
str := hex.EncodeToString(bb)
fmt.Println(str)
decodedHash, e := manualDecode(bb)
if e != nil {
t.Fatal(e)
}
if !bytes.Equal(decodedHash[:], h[:]) {
t.Fatal("hashes not equal")
}
/* from the EIP documentation
storage system: Swarm (0xe4)
CID version: 1 (0x01)
content type: swarm-manifest (0x??)
hash function: keccak-256 (0x1B)
hash length: 32 bytes (0x20)
hash: 29f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f
*/
}
func TestManuelCidEncode(t *testing.T) {
// call cid encode method with hash. expect byte slice returned, compare according to spec
/* from the EIP documentation
storage system: Swarm (0xe4)
CID version: 1 (0x01)
content type: swarm-manifest (0x??)
hash function: keccak-256 (0x1B)
hash length: 32 bytes (0x20)
hash: 29f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f
*/
}
func TestCIDSanity(t *testing.T) {
hashStr := "d1de9994b4d039f6548d191eb26786769f580809256b4685ef316805265ea162"