Update changebuilder to include CID creation

This commit is contained in:
mcrakhman committed 2022-07-11 23:56:13 +02:00
1 parent 9242f78b88
commit 1fb82a6d7f
3 files changed
+34 -4

No files matched your search

+7 -4
View File
@@ -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
+5
View File
@@ -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 (
+22
View File
@@ -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
}