Skip to content

Commit

Permalink
Merge pull request #4 from datatrails/dev/robin/8859-idtimestamp-comm…
Browse files Browse the repository at this point in the history
…itted

Support hashing with idtimestamp committed
  • Loading branch information
robinbryce authored Jan 2, 2024
2 parents b4db355 + c60c50b commit 46e2be8
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 45 deletions.
64 changes: 64 additions & 0 deletions simplehash/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package simplehash

import (
"encoding/binary"

"google.golang.org/protobuf/types/known/timestamppb"
)

// These options are not part of the event schema. The can be used to adjust how
// the schema is applied to produce a hash for different purposes.

type HashOptions struct {
accumulateHash bool
publicFromPermissioned bool
asConfirmed bool
prefix []byte
committed *timestamppb.Timestamp
idcommitted []byte
}

type HashOption func(*HashOptions)

// WithIDCommitted includes the snowflakeid unique commitment timestamp in the hash
// idcommitted is never (legitimately) zero
func WithIDCommitted(idcommitted uint64) HashOption {
return func(o *HashOptions) {
o.idcommitted = make([]byte, 8)
binary.BigEndian.PutUint64(o.idcommitted, idcommitted)
}
}

// WithPrefix pre-pends the provided bytes to the hash. This option can be used
// multiple times and the successive bytes are appended to the prefix. This is
// typically used to provide hash domain seperation where second pre-image
// collisions are a concerne.
func WithPrefix(b []byte) HashOption {
return func(o *HashOptions) {
o.prefix = append(o.prefix, b...)
}
}

func WithTimestampCommitted(committed *timestamppb.Timestamp) HashOption {
return func(o *HashOptions) {
o.committed = committed
}
}

func WithAccumulate() HashOption {
return func(o *HashOptions) {
o.accumulateHash = true
}
}

func WithPublicFromPermissioned() HashOption {
return func(o *HashOptions) {
o.publicFromPermissioned = true
}
}

func WithAsConfirmed() HashOption {
return func(o *HashOptions) {
o.asConfirmed = true
}
}
56 changes: 11 additions & 45 deletions simplehash/schemav2.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
v2assets "github.com/datatrails/go-datatrails-common-api-gen/assets/v2/assets"
"github.com/datatrails/go-datatrails-common-api-gen/marshalers/simpleoneof"
"github.com/zeebo/bencode"
"google.golang.org/protobuf/types/known/timestamppb"
)

var (
Expand All @@ -32,50 +31,6 @@ func NewHasherV2() HasherV2 {
return h
}

type HashOptions struct {
accumulateHash bool
publicFromPermissioned bool
asConfirmed bool
prefix []byte
committed *timestamppb.Timestamp
}

type HashOption func(*HashOptions)

// WithPrefix pre-pends the provided bytes to the hash. This option can be used
// multiple times and the successive bytes are appended to the prefix. This is
// typically used to provide hash domain seperation where second pre-image
// collisions are a concerne.
func WithPrefix(b []byte) HashOption {
return func(o *HashOptions) {
o.prefix = append(o.prefix, b...)
}
}

func WithTimestampCommitted(committed *timestamppb.Timestamp) HashOption {
return func(o *HashOptions) {
o.committed = committed
}
}

func WithAccumulate() HashOption {
return func(o *HashOptions) {
o.accumulateHash = true
}
}

func WithPublicFromPermissioned() HashOption {
return func(o *HashOptions) {
o.publicFromPermissioned = true
}
}

func WithAsConfirmed() HashOption {
return func(o *HashOptions) {
o.asConfirmed = true
}
}

// Reset resets the hasher state
// This is only useful in combination with WithAccumulate
func (h *HasherV2) Reset() { h.hasher.Reset() }
Expand Down Expand Up @@ -142,6 +97,11 @@ func (h *HasherV2) HashEvent(event *v2assets.EventResponse, opts ...HashOption)
return err
}

// If the idcommitted is provided, add it to the hash first
if o.idcommitted != nil {
h.hasher.Write(o.idcommitted)
}

return V2HashEvent(h.hasher, v2Event)
}

Expand Down Expand Up @@ -187,6 +147,12 @@ func (h *HasherV2) HashEventJSON(event []byte, opts ...HashOption) error {
// correct one.
v2Event.ConfirmationStatus = v2assets.ConfirmationStatus_name[int32(v2assets.ConfirmationStatus_CONFIRMED)]
}

// If the idcommitted is provided, add it to the hash first
if o.idcommitted != nil {
h.hasher.Write(o.idcommitted)
}

return V2HashEvent(h.hasher, v2Event)
}

Expand Down

0 comments on commit 46e2be8

Please sign in to comment.