-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from datatrails/dev/robin/8859-simplehash-schem…
…a-v3 Dev/robin/8859 simplehash schema v3
- Loading branch information
Showing
12 changed files
with
388 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
name: ci | ||
on: | ||
push: | ||
release: | ||
types: [published] | ||
|
||
jobs: | ||
test: | ||
name: Code quality and unit tests | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Setup go-task | ||
uses: arduino/setup-task@v1 | ||
with: | ||
version: 3.x | ||
repo-token: ${{ secrets.GITHUB_TOKEN }} | ||
- name: Setup Go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: '1.21.x' | ||
token: ${{ secrets.GITHUB_TOKEN }} | ||
cache-dependency-path: | | ||
datatrails-common-api/go.sum | ||
- name: Install Go quality tools | ||
run: | | ||
go install golang.org/x/tools/cmd/goimports@latest | ||
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b "${PWD}/bin" v1.55.2 | ||
echo "${PWD}/bin" >> $GITHUB_PATH | ||
- name: Code Quality | ||
run: | | ||
# Note: it is by design that we don't use the builder | ||
task codeqa | ||
- name: Test | ||
run: | | ||
# Note: it is by design that we don't use the builder | ||
task test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package simplehash | ||
|
||
import ( | ||
"crypto/sha256" | ||
"hash" | ||
|
||
v2assets "github.com/datatrails/go-datatrails-common-api-gen/assets/v2/assets" | ||
"github.com/datatrails/go-datatrails-common-api-gen/marshalers/simpleoneof" | ||
) | ||
|
||
type Hasher struct { | ||
hasher hash.Hash | ||
marshaler *simpleoneof.Marshaler | ||
} | ||
|
||
func NewHasher() Hasher { | ||
h := Hasher{ | ||
hasher: sha256.New(), | ||
marshaler: NewEventMarshaler(), | ||
} | ||
return h | ||
} | ||
|
||
func (h *Hasher) Sum(b []byte) []byte { return h.hasher.Sum(b) } | ||
|
||
// Reset resets the hasher state | ||
// This is only useful in combination with WithAccumulate | ||
func (h *Hasher) Reset() { h.hasher.Reset() } | ||
|
||
// NewEventMarshaler creates a flat marshaler to transform events to api format. | ||
// | ||
// otherwise attributes look like this: {"foo":{"str_val": "bar"}} instead of {"foo": "bar"} | ||
// this mimics the public list events api response, so minimises changes to the | ||
// public api response, to reproduce the anchor | ||
func NewEventMarshaler() *simpleoneof.Marshaler { | ||
return v2assets.NewFlatMarshalerForEvents() | ||
} | ||
|
||
func (h *Hasher) applyEventOptions(o HashOptions, event *v2assets.EventResponse) { | ||
if o.publicFromPermissioned { | ||
PublicFromPermissionedEvent(event) | ||
} | ||
|
||
// force the commited time in the hash. only useful to the service that is | ||
// actually doing the committing. public consumers only ever see confirmed | ||
// events with the timestamp already in place. | ||
if o.committed != nil { | ||
event.TimestampCommitted = o.committed | ||
} | ||
} | ||
|
||
func (h *Hasher) applyHashingOptions(o HashOptions) { | ||
|
||
// By default, one hash at at time with a reset. | ||
if !o.accumulateHash { | ||
h.hasher.Reset() | ||
} | ||
|
||
// If the prefix is provided it must be first. | ||
if len(o.prefix) != 0 { | ||
h.hasher.Write(o.prefix) | ||
} | ||
|
||
// If the idcommitted is provided, add it to the hash immediately before the | ||
// event data. | ||
if o.idcommitted != nil { | ||
h.hasher.Write(o.idcommitted) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package simplehash | ||
|
||
import v2assets "github.com/datatrails/go-datatrails-common-api-gen/assets/v2/assets" | ||
|
||
// PublicFromPermissionedEvent translates the permissioned event and asset identities to | ||
// their public counter parts. | ||
func PublicFromPermissionedEvent(event *v2assets.EventResponse) { | ||
event.Identity = v2assets.PublicIdentityFromPermissioned(event.Identity) | ||
event.AssetIdentity = v2assets.PublicIdentityFromPermissioned(event.AssetIdentity) | ||
} |
Oops, something went wrong.