diff --git a/acltree/changebuilder.go b/acltree/changebuilder.go index 423bbca4..8e2334bc 100644 --- a/acltree/changebuilder.go +++ b/acltree/changebuilder.go @@ -3,6 +3,7 @@ package acltree import ( "github.com/anytypeio/go-anytype-infrastructure-experiments/account" "github.com/anytypeio/go-anytype-infrastructure-experiments/aclchanges/pb" + "github.com/anytypeio/go-anytype-infrastructure-experiments/util/cid" "github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys" "github.com/gogo/protobuf/proto" ) @@ -83,10 +84,6 @@ func (c *changeBuilder) Build() (*Change, []byte, error) { Identity: c.acc.Identity, } - // TODO: add CID creation logic based on content - ch := NewChange(c.id, aclChange) - ch.DecryptedDocumentChange = marshalled - fullMarshalledChange, err := proto.Marshal(aclChange) if err != nil { return nil, nil, err @@ -95,6 +92,12 @@ func (c *changeBuilder) Build() (*Change, []byte, error) { if err != nil { return nil, nil, err } + id, err := cid.NewCIDFromBytes(fullMarshalledChange) + if err != nil { + return nil, nil, err + } + ch := NewChange(id, aclChange) + ch.DecryptedDocumentChange = marshalled ch.Sign = signature return ch, fullMarshalledChange, nil diff --git a/go.mod b/go.mod index 0c846236..01f7d96e 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,11 @@ require ( github.com/stretchr/testify v1.7.0 github.com/textileio/go-threads v1.0.2-0.20210304072541-d0f91da84404 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c + github.com/multiformats/go-base32 v0.0.3 + github.com/multiformats/go-multiaddr v0.3.3 + github.com/multiformats/go-multiaddr-dns v0.3.1 + github.com/multiformats/go-multibase v0.0.3 + github.com/multiformats/go-multihash v0.0.15 ) require ( diff --git a/util/cid/cid.go b/util/cid/cid.go new file mode 100644 index 00000000..437d23f9 --- /dev/null +++ b/util/cid/cid.go @@ -0,0 +1,22 @@ +package cid + +import ( + "github.com/ipfs/go-cid" + mh "github.com/multiformats/go-multihash" +) + +func NewCIDFromBytes(data []byte) (string, error) { + hash, err := mh.Sum(data, mh.SHA2_256, -1) + if err != nil { + return "", err + } + return cid.NewCidV1(cid.DagCBOR, hash).String(), nil +} + +func VerifyCID(data []byte, id string) bool { + hash, err := mh.Sum(data, mh.SHA2_256, -1) + if err != nil { + return false + } + return cid.NewCidV1(cid.DagCBOR, hash).String() == id +}