Skip to content

Commit

Permalink
pkg/utils: add RawReportContext and HashReport to support non-evm rel…
Browse files Browse the repository at this point in the history
…ays (#60)
  • Loading branch information
jmank88 committed Jun 23, 2022
1 parent 3ac96f3 commit 79bdba0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.12.0
github.com/riferrei/srclient v0.4.1-0.20211229125508-8edc580da179
github.com/satori/go.uuid v1.2.0
github.com/smartcontractkit/libocr v0.0.0-20211210213233-5443fb9db7f7
github.com/stretchr/testify v1.7.0
go.uber.org/atomic v1.7.0
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,7 @@ github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb
github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E=
github.com/santhosh-tekuri/jsonschema/v5 v5.0.0 h1:TToq11gyfNlrMFZiYujSekIsPd9AmsA2Bj/iv+s4JHE=
github.com/santhosh-tekuri/jsonschema/v5 v5.0.0/go.mod h1:FKdcjfQW6rpZSnxxUvEA5H/cDPdvJ/SZJQLWWXWGrZ0=
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/segmentio/kafka-go v0.1.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfPOCvTvk+EJo=
Expand Down
32 changes: 32 additions & 0 deletions pkg/utils/report.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package utils

import (
"crypto/sha256"
"encoding/binary"

"github.com/smartcontractkit/libocr/offchainreporting2/types"
)

// RawReportContext is a copy of evmutil.RawReportContext to avoid importing go-ethereum.
// github.com/smartcontractkit/libocr/offchainreporting2/chains/evmutil#RawReportContext
func RawReportContext(repctx types.ReportContext) [3][32]byte {
rawRepctx := [3][32]byte{}
copy(rawRepctx[0][:], repctx.ConfigDigest[:])
binary.BigEndian.PutUint32(rawRepctx[1][32-5:32-1], repctx.Epoch)
rawRepctx[1][31] = repctx.Round
rawRepctx[2] = repctx.ExtraHash
return rawRepctx
}

// HashReport returns a report digest using SHA256 hash.
func HashReport(ctx types.ReportContext, r types.Report) ([]byte, error) {
rawCtx := RawReportContext(ctx)
buf := sha256.New()
for _, v := range [][]byte{r[:], rawCtx[0][:], rawCtx[1][:], rawCtx[2][:]} {
if _, err := buf.Write(v); err != nil {
return []byte{}, err
}
}

return buf.Sum(nil), nil
}

0 comments on commit 79bdba0

Please sign in to comment.