Add individual key derivation for trees

This commit is contained in:
mcrakhman 2023-03-27 13:01:35 +02:00 committed by Mikhail Iudin
parent 72a64afbec
commit 07f658a96f
No known key found for this signature in database
GPG Key ID: FAAAA8BAABDFF1C0
4 changed files with 24 additions and 2 deletions

View File

@ -45,7 +45,11 @@ func (h *historyTree) rebuildFromStorage(beforeId string, include bool) (err err
if len(ot.keys) != len(state.UserReadKeys()) { if len(ot.keys) != len(state.UserReadKeys()) {
for key, value := range state.UserReadKeys() { for key, value := range state.UserReadKeys() {
ot.keys[key] = value treeKey, err := deriveTreeKey(value, h.id)
if err != nil {
return err
}
ot.keys[key] = treeKey
} }
} }
return return

View File

@ -640,7 +640,11 @@ func (ot *objectTree) validateTree(newChanges []*Change) error {
// just not to take lock many times, updating the key map from aclList // just not to take lock many times, updating the key map from aclList
if len(ot.keys) != len(state.UserReadKeys()) { if len(ot.keys) != len(state.UserReadKeys()) {
for key, value := range state.UserReadKeys() { for key, value := range state.UserReadKeys() {
ot.keys[key] = value treeKey, err := deriveTreeKey(value, ot.id)
if err != nil {
return err
}
ot.keys[key] = treeKey
} }
} }
if len(newChanges) == 0 { if len(newChanges) == 0 {

View File

@ -1,5 +1,10 @@
package objecttree package objecttree
import (
"fmt"
"github.com/anytypeio/any-sync/util/crypto"
)
func commonSnapshotForTwoPaths(ourPath []string, theirPath []string) (string, error) { func commonSnapshotForTwoPaths(ourPath []string, theirPath []string) (string, error) {
var i int var i int
var j int var j int
@ -27,3 +32,11 @@ OuterLoop:
} }
return ourPath[i+1], nil return ourPath[i+1], nil
} }
func deriveTreeKey(key crypto.SymKey, cid string) (crypto.SymKey, error) {
raw, err := key.Raw()
if err != nil {
return nil, err
}
return crypto.DeriveSymmetricKey(raw, fmt.Sprintf(crypto.AnysyncTreePath, cid))
}

View File

@ -6,6 +6,7 @@ import (
const ( const (
AnytypeAccountPath = "m/SLIP-0021/anytype/account" AnytypeAccountPath = "m/SLIP-0021/anytype/account"
AnysyncTreePath = "m/SLIP-0021/anysync/tree/%s"
AnytypeAccountPrefix = "m/44'/607'" AnytypeAccountPrefix = "m/44'/607'"
) )