2017-08-18 04:50:15 +03:00
|
|
|
package macaroons
|
|
|
|
|
|
|
|
import (
|
2020-07-23 19:26:59 +03:00
|
|
|
"bytes"
|
2019-09-29 02:10:57 +03:00
|
|
|
"context"
|
2017-08-18 04:50:15 +03:00
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-11-10 05:12:11 +03:00
|
|
|
"sync"
|
2017-08-18 04:50:15 +03:00
|
|
|
|
2020-01-10 05:45:26 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb/kvdb"
|
2018-02-01 03:04:56 +03:00
|
|
|
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcwallet/snacl"
|
2020-10-06 18:23:30 +03:00
|
|
|
"github.com/btcsuite/btcwallet/walletdb"
|
2017-08-18 04:50:15 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// RootKeyLen is the length of a root key.
|
|
|
|
RootKeyLen = 32
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// rootKeyBucketName is the name of the root key store bucket.
|
|
|
|
rootKeyBucketName = []byte("macrootkeys")
|
|
|
|
|
2020-07-23 19:26:59 +03:00
|
|
|
// DefaultRootKeyID is the ID of the default root key. The first is
|
2017-08-18 04:50:15 +03:00
|
|
|
// just 0, to emulate the memory storage that comes with bakery.
|
2020-07-23 19:26:59 +03:00
|
|
|
DefaultRootKeyID = []byte("0")
|
2017-08-18 04:50:15 +03:00
|
|
|
|
2020-10-06 18:23:30 +03:00
|
|
|
// encryptionKeyID is the name of the database key that stores the
|
2018-02-01 03:04:56 +03:00
|
|
|
// encryption key, encrypted with a salted + hashed password. The
|
|
|
|
// format is 32 bytes of salt, and the rest is encrypted key.
|
2020-10-06 18:23:30 +03:00
|
|
|
encryptionKeyID = []byte("enckey")
|
2018-02-01 03:04:56 +03:00
|
|
|
|
|
|
|
// ErrAlreadyUnlocked specifies that the store has already been
|
|
|
|
// unlocked.
|
|
|
|
ErrAlreadyUnlocked = fmt.Errorf("macaroon store already unlocked")
|
|
|
|
|
|
|
|
// ErrStoreLocked specifies that the store needs to be unlocked with
|
|
|
|
// a password.
|
|
|
|
ErrStoreLocked = fmt.Errorf("macaroon store is locked")
|
|
|
|
|
|
|
|
// ErrPasswordRequired specifies that a nil password has been passed.
|
|
|
|
ErrPasswordRequired = fmt.Errorf("a non-nil password is required")
|
2020-07-23 19:26:59 +03:00
|
|
|
|
|
|
|
// ErrKeyValueForbidden is used when the root key ID uses encryptedKeyID as
|
|
|
|
// its value.
|
|
|
|
ErrKeyValueForbidden = fmt.Errorf("root key ID value is not allowed")
|
2020-10-06 18:23:30 +03:00
|
|
|
|
|
|
|
// ErrRootKeyBucketNotFound specifies that there is no macaroon root key
|
|
|
|
// bucket yet which can/should only happen if the store has been
|
|
|
|
// corrupted or was initialized incorrectly.
|
|
|
|
ErrRootKeyBucketNotFound = fmt.Errorf("root key bucket not found")
|
|
|
|
|
|
|
|
// ErrEncKeyNotFound specifies that there was no encryption key found
|
|
|
|
// even if one was expected to be generated.
|
|
|
|
ErrEncKeyNotFound = fmt.Errorf("macaroon encryption key not found")
|
2017-08-18 04:50:15 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// RootKeyStorage implements the bakery.RootKeyStorage interface.
|
|
|
|
type RootKeyStorage struct {
|
2020-01-10 05:45:26 +03:00
|
|
|
kvdb.Backend
|
2018-02-01 03:04:56 +03:00
|
|
|
|
2019-11-10 05:12:11 +03:00
|
|
|
encKeyMtx sync.RWMutex
|
|
|
|
encKey *snacl.SecretKey
|
2017-08-18 04:50:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRootKeyStorage creates a RootKeyStorage instance.
|
|
|
|
// TODO(aakselrod): Add support for encryption of data with passphrase.
|
2020-01-10 05:45:26 +03:00
|
|
|
func NewRootKeyStorage(db kvdb.Backend) (*RootKeyStorage, error) {
|
2017-08-18 04:50:15 +03:00
|
|
|
// If the store's bucket doesn't exist, create it.
|
2020-01-10 05:45:26 +03:00
|
|
|
err := kvdb.Update(db, func(tx kvdb.RwTx) error {
|
|
|
|
_, err := tx.CreateTopLevelBucket(rootKeyBucketName)
|
2017-08-18 04:50:15 +03:00
|
|
|
return err
|
2020-10-26 16:06:32 +03:00
|
|
|
}, func() {})
|
2017-08-18 04:50:15 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-08-22 09:18:19 +03:00
|
|
|
|
2017-08-18 04:50:15 +03:00
|
|
|
// Return the DB wrapped in a RootKeyStorage object.
|
2020-01-10 05:45:26 +03:00
|
|
|
return &RootKeyStorage{Backend: db, encKey: nil}, nil
|
2018-02-01 03:04:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateUnlock sets an encryption key if one is not already set, otherwise it
|
|
|
|
// checks if the password is correct for the stored encryption key.
|
|
|
|
func (r *RootKeyStorage) CreateUnlock(password *[]byte) error {
|
2019-11-10 05:12:11 +03:00
|
|
|
r.encKeyMtx.Lock()
|
|
|
|
defer r.encKeyMtx.Unlock()
|
|
|
|
|
2018-02-01 03:04:56 +03:00
|
|
|
// Check if we've already unlocked the store; return an error if so.
|
|
|
|
if r.encKey != nil {
|
|
|
|
return ErrAlreadyUnlocked
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if a nil password has been passed; return an error if so.
|
|
|
|
if password == nil {
|
|
|
|
return ErrPasswordRequired
|
|
|
|
}
|
|
|
|
|
2020-01-10 05:45:26 +03:00
|
|
|
return kvdb.Update(r, func(tx kvdb.RwTx) error {
|
|
|
|
bucket := tx.ReadWriteBucket(rootKeyBucketName)
|
2020-10-06 18:23:30 +03:00
|
|
|
if bucket == nil {
|
|
|
|
return ErrRootKeyBucketNotFound
|
|
|
|
}
|
|
|
|
dbKey := bucket.Get(encryptionKeyID)
|
2018-02-01 03:04:56 +03:00
|
|
|
if len(dbKey) > 0 {
|
|
|
|
// We've already stored a key, so try to unlock with
|
|
|
|
// the password.
|
|
|
|
encKey := &snacl.SecretKey{}
|
|
|
|
err := encKey.Unmarshal(dbKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = encKey.DeriveKey(password)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
r.encKey = encKey
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// We haven't yet stored a key, so create a new one.
|
2020-01-17 16:23:42 +03:00
|
|
|
encKey, err := snacl.NewSecretKey(
|
|
|
|
password, scryptN, scryptR, scryptP,
|
|
|
|
)
|
2018-02-01 03:04:56 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:23:30 +03:00
|
|
|
err = bucket.Put(encryptionKeyID, encKey.Marshal())
|
2018-02-01 03:04:56 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
r.encKey = encKey
|
|
|
|
return nil
|
2020-10-26 16:06:32 +03:00
|
|
|
}, func() {})
|
2017-08-18 04:50:15 +03:00
|
|
|
}
|
|
|
|
|
2020-10-06 18:23:30 +03:00
|
|
|
// ChangePassword decrypts the macaroon root key with the old password and then
|
|
|
|
// encrypts it again with the new password.
|
|
|
|
func (r *RootKeyStorage) ChangePassword(oldPw, newPw []byte) error {
|
|
|
|
// We need the store to already be unlocked. With this we can make sure
|
|
|
|
// that there already is a key in the DB.
|
|
|
|
if r.encKey == nil {
|
|
|
|
return ErrStoreLocked
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if a nil password has been passed; return an error if so.
|
|
|
|
if oldPw == nil || newPw == nil {
|
|
|
|
return ErrPasswordRequired
|
|
|
|
}
|
|
|
|
|
|
|
|
return kvdb.Update(r, func(tx kvdb.RwTx) error {
|
|
|
|
bucket := tx.ReadWriteBucket(rootKeyBucketName)
|
|
|
|
if bucket == nil {
|
|
|
|
return ErrRootKeyBucketNotFound
|
|
|
|
}
|
|
|
|
encKeyDb := bucket.Get(encryptionKeyID)
|
|
|
|
rootKeyDb := bucket.Get(DefaultRootKeyID)
|
|
|
|
|
|
|
|
// Both the encryption key and the root key must be present
|
|
|
|
// otherwise we are in the wrong state to change the password.
|
|
|
|
if len(encKeyDb) == 0 || len(rootKeyDb) == 0 {
|
|
|
|
return ErrEncKeyNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal parameters for old encryption key and derive the
|
|
|
|
// old key with them.
|
|
|
|
encKeyOld := &snacl.SecretKey{}
|
|
|
|
err := encKeyOld.Unmarshal(encKeyDb)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = encKeyOld.DeriveKey(&oldPw)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new encryption key from the new password.
|
|
|
|
encKeyNew, err := snacl.NewSecretKey(
|
|
|
|
&newPw, scryptN, scryptR, scryptP,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now try to decrypt the root key with the old encryption key,
|
|
|
|
// encrypt it with the new one and then store it in the DB.
|
|
|
|
decryptedKey, err := encKeyOld.Decrypt(rootKeyDb)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rootKey := make([]byte, len(decryptedKey))
|
|
|
|
copy(rootKey, decryptedKey)
|
|
|
|
encryptedKey, err := encKeyNew.Encrypt(rootKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = bucket.Put(DefaultRootKeyID, encryptedKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, store the new encryption key parameters in the DB
|
|
|
|
// as well.
|
|
|
|
err = bucket.Put(encryptionKeyID, encKeyNew.Marshal())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
r.encKey = encKeyNew
|
|
|
|
return nil
|
|
|
|
}, func() {})
|
|
|
|
}
|
|
|
|
|
2017-08-18 04:50:15 +03:00
|
|
|
// Get implements the Get method for the bakery.RootKeyStorage interface.
|
2018-01-16 19:18:41 +03:00
|
|
|
func (r *RootKeyStorage) Get(_ context.Context, id []byte) ([]byte, error) {
|
2019-11-10 05:12:11 +03:00
|
|
|
r.encKeyMtx.RLock()
|
|
|
|
defer r.encKeyMtx.RUnlock()
|
|
|
|
|
2018-02-01 03:04:56 +03:00
|
|
|
if r.encKey == nil {
|
|
|
|
return nil, ErrStoreLocked
|
|
|
|
}
|
2017-08-18 04:50:15 +03:00
|
|
|
var rootKey []byte
|
2020-05-07 01:45:50 +03:00
|
|
|
err := kvdb.View(r, func(tx kvdb.RTx) error {
|
2020-10-06 18:23:30 +03:00
|
|
|
bucket := tx.ReadBucket(rootKeyBucketName)
|
|
|
|
if bucket == nil {
|
|
|
|
return ErrRootKeyBucketNotFound
|
|
|
|
}
|
|
|
|
dbKey := bucket.Get(id)
|
2017-08-23 21:30:09 +03:00
|
|
|
if len(dbKey) == 0 {
|
2017-08-18 04:50:15 +03:00
|
|
|
return fmt.Errorf("root key with id %s doesn't exist",
|
2018-01-16 19:18:41 +03:00
|
|
|
string(id))
|
2017-08-18 04:50:15 +03:00
|
|
|
}
|
2017-08-23 21:30:09 +03:00
|
|
|
|
2018-02-01 03:04:56 +03:00
|
|
|
decKey, err := r.encKey.Decrypt(dbKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rootKey = make([]byte, len(decKey))
|
|
|
|
copy(rootKey[:], decKey)
|
2017-08-18 04:50:15 +03:00
|
|
|
return nil
|
2020-10-20 17:18:40 +03:00
|
|
|
}, func() {
|
|
|
|
rootKey = nil
|
2017-08-18 04:50:15 +03:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-08-22 09:18:19 +03:00
|
|
|
|
2017-08-18 04:50:15 +03:00
|
|
|
return rootKey, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RootKey implements the RootKey method for the bakery.RootKeyStorage
|
|
|
|
// interface.
|
2020-07-23 19:26:59 +03:00
|
|
|
func (r *RootKeyStorage) RootKey(ctx context.Context) ([]byte, []byte, error) {
|
2019-11-10 05:12:11 +03:00
|
|
|
r.encKeyMtx.RLock()
|
|
|
|
defer r.encKeyMtx.RUnlock()
|
|
|
|
|
2018-02-01 03:04:56 +03:00
|
|
|
if r.encKey == nil {
|
|
|
|
return nil, nil, ErrStoreLocked
|
|
|
|
}
|
2017-08-18 04:50:15 +03:00
|
|
|
var rootKey []byte
|
2020-07-23 19:26:59 +03:00
|
|
|
|
|
|
|
// Read the root key ID from the context. If no key is specified in the
|
|
|
|
// context, an error will be returned.
|
|
|
|
id, err := RootKeyIDFromContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:23:30 +03:00
|
|
|
if bytes.Equal(id, encryptionKeyID) {
|
2020-07-23 19:26:59 +03:00
|
|
|
return nil, nil, ErrKeyValueForbidden
|
|
|
|
}
|
|
|
|
|
|
|
|
err = kvdb.Update(r, func(tx kvdb.RwTx) error {
|
2020-10-06 18:23:30 +03:00
|
|
|
bucket := tx.ReadWriteBucket(rootKeyBucketName)
|
|
|
|
if bucket == nil {
|
|
|
|
return ErrRootKeyBucketNotFound
|
|
|
|
}
|
|
|
|
dbKey := bucket.Get(id)
|
2018-02-01 03:04:56 +03:00
|
|
|
|
|
|
|
// If there's a root key stored in the bucket, decrypt it and
|
|
|
|
// return it.
|
|
|
|
if len(dbKey) != 0 {
|
|
|
|
decKey, err := r.encKey.Decrypt(dbKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rootKey = make([]byte, len(decKey))
|
|
|
|
copy(rootKey[:], decKey[:])
|
2017-08-18 04:50:15 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:23:30 +03:00
|
|
|
// Otherwise, create a new root key, encrypt it,
|
2018-02-01 03:04:56 +03:00
|
|
|
// and store it in the bucket.
|
2020-10-06 18:23:30 +03:00
|
|
|
newKey, err := generateAndStoreNewRootKey(bucket, id, r.encKey)
|
|
|
|
rootKey = newKey
|
|
|
|
return err
|
2020-10-26 16:06:32 +03:00
|
|
|
}, func() {
|
|
|
|
rootKey = nil
|
2017-08-18 04:50:15 +03:00
|
|
|
})
|
|
|
|
if err != nil {
|
2018-01-16 19:18:41 +03:00
|
|
|
return nil, nil, err
|
2017-08-18 04:50:15 +03:00
|
|
|
}
|
2017-08-22 09:18:19 +03:00
|
|
|
|
2017-08-18 04:50:15 +03:00
|
|
|
return rootKey, id, nil
|
|
|
|
}
|
2018-02-01 03:04:56 +03:00
|
|
|
|
2020-10-06 18:23:30 +03:00
|
|
|
// GenerateNewRootKey generates a new macaroon root key, replacing the previous
|
|
|
|
// root key if it existed.
|
|
|
|
func (r *RootKeyStorage) GenerateNewRootKey() error {
|
|
|
|
// We need the store to already be unlocked. With this we can make sure
|
|
|
|
// that there already is a key in the DB that can be replaced.
|
|
|
|
if r.encKey == nil {
|
|
|
|
return ErrStoreLocked
|
|
|
|
}
|
|
|
|
return kvdb.Update(r, func(tx kvdb.RwTx) error {
|
|
|
|
bucket := tx.ReadWriteBucket(rootKeyBucketName)
|
|
|
|
if bucket == nil {
|
|
|
|
return ErrRootKeyBucketNotFound
|
|
|
|
}
|
|
|
|
_, err := generateAndStoreNewRootKey(
|
|
|
|
bucket, DefaultRootKeyID, r.encKey,
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}, func() {})
|
|
|
|
}
|
|
|
|
|
2018-02-01 03:04:56 +03:00
|
|
|
// Close closes the underlying database and zeroes the encryption key stored
|
|
|
|
// in memory.
|
|
|
|
func (r *RootKeyStorage) Close() error {
|
2019-11-10 05:12:11 +03:00
|
|
|
r.encKeyMtx.Lock()
|
|
|
|
defer r.encKeyMtx.Unlock()
|
|
|
|
|
2018-03-18 02:34:19 +03:00
|
|
|
if r.encKey != nil {
|
|
|
|
r.encKey.Zero()
|
2020-10-06 18:23:30 +03:00
|
|
|
r.encKey = nil
|
2018-03-18 02:34:19 +03:00
|
|
|
}
|
2020-01-10 05:45:26 +03:00
|
|
|
return r.Backend.Close()
|
2018-02-01 03:04:56 +03:00
|
|
|
}
|
2020-07-23 19:26:59 +03:00
|
|
|
|
2020-10-06 18:23:30 +03:00
|
|
|
// generateAndStoreNewRootKey creates a new random RootKeyLen-byte root key,
|
|
|
|
// encrypts it with the given encryption key and stores it in the bucket.
|
|
|
|
// Any previously set key will be overwritten.
|
|
|
|
func generateAndStoreNewRootKey(bucket walletdb.ReadWriteBucket, id []byte,
|
|
|
|
key *snacl.SecretKey) ([]byte, error) {
|
|
|
|
|
|
|
|
rootKey := make([]byte, RootKeyLen)
|
|
|
|
if _, err := io.ReadFull(rand.Reader, rootKey); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
encryptedKey, err := key.Encrypt(rootKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return rootKey, bucket.Put(id, encryptedKey)
|
|
|
|
}
|
|
|
|
|
2020-07-23 19:26:59 +03:00
|
|
|
// ListMacaroonIDs returns all the root key ID values except the value of
|
|
|
|
// encryptedKeyID.
|
|
|
|
func (r *RootKeyStorage) ListMacaroonIDs(_ context.Context) ([][]byte, error) {
|
|
|
|
r.encKeyMtx.RLock()
|
|
|
|
defer r.encKeyMtx.RUnlock()
|
|
|
|
|
|
|
|
// Check it's unlocked.
|
|
|
|
if r.encKey == nil {
|
|
|
|
return nil, ErrStoreLocked
|
|
|
|
}
|
|
|
|
|
|
|
|
var rootKeySlice [][]byte
|
|
|
|
|
|
|
|
// Read all the items in the bucket and append the keys, which are the
|
|
|
|
// root key IDs we want.
|
|
|
|
err := kvdb.View(r, func(tx kvdb.RTx) error {
|
|
|
|
|
|
|
|
// appendRootKey is a function closure that appends root key ID
|
|
|
|
// to rootKeySlice.
|
|
|
|
appendRootKey := func(k, _ []byte) error {
|
|
|
|
// Only append when the key value is not encryptedKeyID.
|
2020-10-06 18:23:30 +03:00
|
|
|
if !bytes.Equal(k, encryptionKeyID) {
|
2020-07-23 19:26:59 +03:00
|
|
|
rootKeySlice = append(rootKeySlice, k)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.ReadBucket(rootKeyBucketName).ForEach(appendRootKey)
|
2020-10-20 17:18:40 +03:00
|
|
|
}, func() {
|
|
|
|
rootKeySlice = nil
|
2020-07-23 19:26:59 +03:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return rootKeySlice, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteMacaroonID removes one specific root key ID. If the root key ID is
|
|
|
|
// found and deleted, it will be returned.
|
|
|
|
func (r *RootKeyStorage) DeleteMacaroonID(
|
|
|
|
_ context.Context, rootKeyID []byte) ([]byte, error) {
|
|
|
|
|
|
|
|
r.encKeyMtx.RLock()
|
|
|
|
defer r.encKeyMtx.RUnlock()
|
|
|
|
|
|
|
|
// Check it's unlocked.
|
|
|
|
if r.encKey == nil {
|
|
|
|
return nil, ErrStoreLocked
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the rootKeyID is not empty.
|
|
|
|
if len(rootKeyID) == 0 {
|
|
|
|
return nil, ErrMissingRootKeyID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deleting encryptedKeyID or DefaultRootKeyID is not allowed.
|
2020-10-06 18:23:30 +03:00
|
|
|
if bytes.Equal(rootKeyID, encryptionKeyID) ||
|
2020-07-23 19:26:59 +03:00
|
|
|
bytes.Equal(rootKeyID, DefaultRootKeyID) {
|
|
|
|
|
|
|
|
return nil, ErrDeletionForbidden
|
|
|
|
}
|
|
|
|
|
|
|
|
var rootKeyIDDeleted []byte
|
|
|
|
err := kvdb.Update(r, func(tx kvdb.RwTx) error {
|
|
|
|
bucket := tx.ReadWriteBucket(rootKeyBucketName)
|
|
|
|
|
|
|
|
// Check the key can be found. If not, return nil.
|
|
|
|
if bucket.Get(rootKeyID) == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Once the key is found, we do the deletion.
|
|
|
|
if err := bucket.Delete(rootKeyID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rootKeyIDDeleted = rootKeyID
|
|
|
|
|
|
|
|
return nil
|
2020-10-26 16:06:32 +03:00
|
|
|
}, func() {
|
|
|
|
rootKeyIDDeleted = nil
|
2020-07-23 19:26:59 +03:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return rootKeyIDDeleted, nil
|
|
|
|
}
|