multiformats/multihash

adding our own experimental multihash, (how to manage the deps ?)

iglake opened this issue · 3 comments

Hello all I am a newbie at go and the way deps are handle, I tried a simple test case to learn the "ropes" and still can't figure it out :

how to we add our own hash function ... the "code" and table edition, is straight forward, however the package/module import is not ... still very "hermetic" to me,
here is what I tried :

1) install golang :

ver=1.12.5
wget -c https://dl.google.com/go/go$ver.linux-amd64.tar.gz
echo "aea86e3c73495f205929cfebba0d63f1382c8ac59be081b6351681415f4063cf  go$ver.linux-amd64.tar.gz" | shasum -a 2
export GOROOT=$HOME/sdk/go$ver
mkdir -p $GOROOT
mv go$ver.linux-amd64.tar.gz $GOROOT
cd $GOROOT/..
rm go; ln -s go$ver go
tar xfv go/go$ver.linux-amd64.tar.gz --wildcards go/\*

2) setting the environment

export PATH=$HOME/.local/bin:$HOME/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin

export GOROOT=$HOME/sdk/go1.12.5
export GO111MODULE=on
export GO15VENDOREXPERIMENT=1

export GOPATH=$HOME/goQuestion
mkdir $GOPATH
export GOBIN=$GOPATH/bin
mkdir $GOBIN

export PATH=$GOROOT/bin:$(go env GOPATH)/bin:$PATH

3) getting the code

cd $GOPATH

mkdir -p src/github.com/testing
cd src/github.com/testing
git clone https://github.com/multiformats/go-multihash
cd go-multihash

hacking the code ...

# --------------------------------------------------------------------------------------------------------------
cat <<PATCH | ed sum.go
191a

  RegisterHashFunc(MYHASH, sumMYHASH)
.
81a
func sumMYHASH(data []byte, length int) ([]byte, error) {
  return data, nil

}

.
wq
PATCH
# --------------------------------------------------------------------------------------------------------------
cat <<PATCH | ed multihash.go
137a
  MYHASH:       28,
.
114a
  MYHASH:          "myhash",
.
91a
  "myhash":       MYHASH,
.
52a
  MYHASH     = 0x79

.
wq
PATCH
# --------------------------------------------------------------------------------------------------------------
go build
cd multihash
go build
go install
echo 0123456789abcdef | multihash -a myhash -e hex -l 136
cd ../..

building the rest of ipfs ...

git clone https://github.com/ipfs/go-ipfs
cd go-ipfs
cat <<PATCH | ed go.mod
2a
replace github.com/multiformats/go-multihash => ../go-multihash

.
wq
PATCH

go build
cd cmd/ipfs
go build
echo 0123456789abcdef | ./ipfs add -n --hash myhash
Error: unrecognized hash function: myhash

what am I doing wrong ?

I'm guessing this is due to paths. Even if you checkout go-ipfs in $GOPATH/src/github.com/testing, all the internal imports will point to github.com/ipfs. I'd try making these changes in-place to see if they work better.

Also, could you post a diff instead of an ed summary?

here are the diffs :

diff --git a/sum.go b/sum.go
index 249cb75..015e5dc 100644
--- a/sum.go
+++ b/sum.go
@@ -79,6 +79,11 @@ func sumBlake2b(data []byte, size int) ([]byte, error) {
  return hasher.Sum(nil)[:], nil
 }

+func sumMYHASH(data []byte, length int) ([]byte, error) {
+  return data, nil
+
+}
+
 func sumID(data []byte, length int) ([]byte, error) {
  if length >= 0 && length != len(data) {
    return nil, fmt.Errorf("the length of the identity hash (%d) must be equal to the length of the data (%d)",
@@ -190,6 +195,8 @@ func registerNonStdlibHashFuncs() {
  RegisterHashFunc(SHAKE_128, sumSHAKE128)
  RegisterHashFunc(SHAKE_256, sumSHAKE256)

+  RegisterHashFunc(MYHASH, sumMYHASH)
+
  // Blake family of hash functions
  // BLAKE2S
  for c := uint64(BLAKE2S_MIN); c <= BLAKE2S_MAX; c++ {

and

diff --git a/multihash.go b/multihash.go
index cbd6be7..e94025c 100644
--- a/multihash.go
+++ b/multihash.go
@@ -50,6 +50,8 @@ const (
 	KECCAK_384 = 0x1C
 	KECCAK_512 = 0x1D
 
+  MYHASH     = 0x79
+
   MYHASH     = 0x79
 
 	SHAKE_128 = 0x18
@@ -89,6 +91,7 @@ func init() {
 	}
 }
 
+  "myhash":       MYHASH,
 // Names maps the name of a hash to the code
 var Names = map[string]uint64{
   "myhash":       MYHASH,
@@ -112,6 +115,7 @@ var Names = map[string]uint64{
 	"x11":          X11,
 	"md5":          MD5,
 }
+  MYHASH:          "myhash",
 
 // Codes maps a hash code to it's name
 var Codes = map[uint64]string{
@@ -135,6 +139,7 @@ var Codes = map[uint64]string{
 	X11:          "x11",
 	MD5:          "md5",
 }
+  MYHASH:       28,
 
 // DefaultLengths maps a hash code to it's default length
 var DefaultLengths = map[uint64]int{

Those diffs won't produce valid go. Unfortunately, I believe you'll need to familiarize yourself with go (https://tour.golang.org) before proceeding.