Skip to content

Commit

Permalink
merkle: add option to group leaves
Browse files Browse the repository at this point in the history
  • Loading branch information
halseth committed Oct 6, 2023
1 parent 319bddb commit 1104178
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions cmd/merkle/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"crypto/sha256"
"encoding/hex"
"fmt"
Expand All @@ -10,7 +11,7 @@ import (
)

type config struct {
Leaves string `short:"l" long:"leaves" description:"space separated string of hex values to commit to (must be power of 2)"`
Leaves string `short:"l" long:"leaves" description:"space separated string of hex values to commit to (must be power of 2). To group more values in single leaf, use (val1,val2)"`
}

var cfg = config{}
Expand All @@ -37,12 +38,41 @@ func run() error {
continue
}

h, err := hex.DecodeString(l)
if err != nil {
return fmt.Errorf("unable to parse '%s': %w", l, err)
var bs []byte

// Grouped leaves
if strings.HasPrefix(l, "(") &&
strings.HasSuffix(l, ")") {

l = strings.TrimPrefix(l, "(")
l = strings.TrimSuffix(l, ")")

els := strings.Split(l, ",")

group := bytes.Buffer{}
for _, el := range els {
h, err := hex.DecodeString(el)
if err != nil {
return fmt.Errorf("unable to parse '%s': %w", l, err)
}

hash := sha256.Sum256(h)
group.Write(hash[:])
}

bs = group.Bytes()
} else if l == "<>" {
bs = []byte{}
} else {
var err error
bs, err = hex.DecodeString(l)
if err != nil {
return fmt.Errorf("unable to parse '%s': %w", l, err)
}

}

hash := sha256.Sum256(h)
hash := sha256.Sum256(bs)
level = append(level, hash)
}

Expand Down

0 comments on commit 1104178

Please sign in to comment.