mirror of
https://github.com/anyproto/any-sync.git
synced 2025-06-11 10:18:08 +09:00
Update changebuilder to include CID creation
This commit is contained in:
parent
de1e6af58f
commit
3424f15a02
3 changed files with 34 additions and 4 deletions
|
@ -3,6 +3,7 @@ package acltree
|
||||||
import (
|
import (
|
||||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/account"
|
"github.com/anytypeio/go-anytype-infrastructure-experiments/account"
|
||||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/aclchanges/pb"
|
"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/anytypeio/go-anytype-infrastructure-experiments/util/keys"
|
||||||
"github.com/gogo/protobuf/proto"
|
"github.com/gogo/protobuf/proto"
|
||||||
)
|
)
|
||||||
|
@ -83,10 +84,6 @@ func (c *changeBuilder) Build() (*Change, []byte, error) {
|
||||||
Identity: c.acc.Identity,
|
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)
|
fullMarshalledChange, err := proto.Marshal(aclChange)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
|
@ -95,6 +92,12 @@ func (c *changeBuilder) Build() (*Change, []byte, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
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
|
ch.Sign = signature
|
||||||
|
|
||||||
return ch, fullMarshalledChange, nil
|
return ch, fullMarshalledChange, nil
|
||||||
|
|
5
go.mod
5
go.mod
|
@ -12,6 +12,11 @@ require (
|
||||||
github.com/stretchr/testify v1.7.0
|
github.com/stretchr/testify v1.7.0
|
||||||
github.com/textileio/go-threads v1.0.2-0.20210304072541-d0f91da84404
|
github.com/textileio/go-threads v1.0.2-0.20210304072541-d0f91da84404
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
|
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 (
|
require (
|
||||||
|
|
22
util/cid/cid.go
Normal file
22
util/cid/cid.go
Normal 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
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue