From f8e7a577de7f358de1be1f9ec98260494bc04259 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Thu, 17 Sep 2020 13:08:04 +0300 Subject: [PATCH] [#149] sdk/container: Add function to calculate identifier Signed-off-by: Leonard Lyubich --- pkg/container/container.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pkg/container/container.go b/pkg/container/container.go index 07765ca8..a14f2577 100644 --- a/pkg/container/container.go +++ b/pkg/container/container.go @@ -1,6 +1,8 @@ package container import ( + "crypto/sha256" + "github.com/nspcc-dev/neofs-api-go/v2/container" ) @@ -53,3 +55,17 @@ func NewContainerFromV2(c *container.Container) *Container { return cnr } + +// CalculateID calculates container identifier +// based on its structure. +func CalculateID(c *Container) *ID { + data, err := c.ToV2().StableMarshal(nil) + if err != nil { + panic(err) + } + + id := NewID() + id.SetSHA256(sha256.Sum256(data)) + + return id +}