e4go icon indicating copy to clipboard operation
e4go copied to clipboard

Spurious use of hex.Encode for hashmaps

Open diagprov opened this issue 5 years ago • 5 comments

Currently we're using hex encoding in order to use hashes as keys in a hashmap, e.g.

https://github.com/teserakt-io/e4go/blob/d0f4bc63ee4ea018adf229e9f4c3d8febecc97c3/client.go#L427

However, this isn't actually necessary, as the following code example shows:

package main

import (
    "fmt"
    "golang.org/x/crypto/sha3"
)

func main() {

    keymap := make(map[string]string)

    data := "some data"
    topic := "some topic"
    topichash := sha3.Sum256([]byte(topic))
    keymap[string(topichash[:])] = data
    fmt.Println(keymap)
}

Here the hash is directly encoded as a string type, albeit not one you can print easily. In this, no conversion between formats is required.

We probably want to encode these keys for json output, but during normal operation I'm not sure we need it. What do you think @odeke-em ? (I would key a hashmap in C by passing the raw bytes through the hash function for the hashmap).

diagprov avatar Jan 21 '20 13:01 diagprov