1
0
Fork 0
mirror of https://github.com/anyproto/any-sync.git synced 2025-06-12 02:30:41 +09:00

Update changebuilder to include CID creation

This commit is contained in:
mcrakhman 2022-07-11 23:56:13 +02:00 committed by Mikhail Iudin
parent de1e6af58f
commit 3424f15a02
No known key found for this signature in database
GPG key ID: FAAAA8BAABDFF1C0
3 changed files with 34 additions and 4 deletions

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
go.mod
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
util/cid/cid.go Normal file
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
}