Skip to content

Commit

Permalink
fixup event helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
jgough committed May 30, 2024
1 parent d109c5c commit 8e28942
Showing 1 changed file with 6 additions and 67 deletions.
73 changes: 6 additions & 67 deletions logverification/eventhelpers.go
Original file line number Diff line number Diff line change
@@ -1,70 +1,19 @@
package logverification

import (
"crypto/sha256"
"encoding/binary"
"encoding/json"
"errors"
"sort"

"github.com/datatrails/go-datatrails-common-api-gen/assets/v2/assets"
"github.com/datatrails/go-datatrails-merklelog/massifs"
"github.com/datatrails/go-datatrails-simplehash/simplehash"
"google.golang.org/protobuf/encoding/protojson"
)

// EventDetails contains key information for verifying inclusion of merkle log events
type EventDetails struct {
eventID string
tenantID string
eventHash []byte
merkleLog *assets.MerkleLogEntry
massifIndex uint64
}

// HashEvent creates a hash of the supplied event in the canonical V3 format.
func HashEvent(idBytes []byte, err error, v3event simplehash.V3Event) ([]byte, error) {
hasher := sha256.New()

domainSeparator := []byte{byte(LeafTypePlain)}
hasher.Write(domainSeparator)

hasher.Write(idBytes)

err = simplehash.V3HashEvent(hasher, v3event)
if err != nil {
return nil, err
}

hash := hasher.Sum(nil)

return hash, nil
}

// getIdTimestamp retrieves the ID Timestamp from a wrapped event
func getIdTimestamp(timestamp string) ([]byte, error) {

// Note that we store the idtimestamp in the event data with the epoch
// prefixed to it. So that no matter when the event data is examined, that
// information is available. We do NOT include the epoch in the hash because
// we will never alow a single log to span multiple epochs. We will just
// start a new log. And we preserve the unified history provability by
// puting a commitment to the final state of the previous log as the first
// entry in the new log.

id, epoch, err := massifs.SplitIDTimestampHex(timestamp)
if err != nil {
return nil, err
}
if epoch < 1 {
return nil, errors.New("epoch is before datatrails existed")
}
if epoch > 1 {
return nil, errors.New("epoch is after Jan 2038")
}
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, id)
return b, nil
eventID string
tenantID string
eventHash []byte
merkleLog *assets.MerkleLogEntry
}

// ParseEventList takes a json list of events returned by the datatrails events API
Expand Down Expand Up @@ -108,18 +57,8 @@ func ParseEventList(eventsJson []byte) ([]EventDetails, error) {
return nil, err
}

// Hash the events we want to verify using the v3 schema
v3event, err := simplehash.V3FromEventJSON(eventJson)
if err != nil {
return nil, err
}

idBytes, err := getIdTimestamp(merkleLog.Commit.Idtimestamp)
if err != nil {
return nil, err
}

eventHash, err := HashEvent(idBytes, err, v3event)
hasher := LogVersion0Hasher{}
eventHash, err := hasher.HashEvent(eventJson)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 8e28942

Please sign in to comment.