github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/chat/storage/basebox.go (about)

     1  package storage
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/keybase/client/go/chat/globals"
     7  	"github.com/keybase/client/go/encrypteddb"
     8  	"github.com/keybase/client/go/libkb"
     9  	"github.com/keybase/client/go/protocol/keybase1"
    10  	"golang.org/x/net/context"
    11  )
    12  
    13  type baseBox struct {
    14  	globals.Contextified
    15  	encryptedDB *encrypteddb.EncryptedDB
    16  }
    17  
    18  type SecretUI struct {
    19  }
    20  
    21  func (d SecretUI) GetPassphrase(pinentry keybase1.GUIEntryArg, terminal *keybase1.SecretEntryArg) (keybase1.GetPassphraseRes, error) {
    22  	return keybase1.GetPassphraseRes{}, fmt.Errorf("no secret UI available")
    23  }
    24  
    25  func newBaseBox(g *globals.Context) *baseBox {
    26  	keyFn := func(ctx context.Context) ([32]byte, error) {
    27  		return GetSecretBoxKey(ctx, g.ExternalG())
    28  	}
    29  	dbFn := func(g *libkb.GlobalContext) *libkb.JSONLocalDb {
    30  		return g.LocalChatDb
    31  	}
    32  	return &baseBox{
    33  		Contextified: globals.NewContextified(g),
    34  		encryptedDB:  encrypteddb.New(g.ExternalG(), dbFn, keyFn),
    35  	}
    36  }
    37  
    38  func (i *baseBox) readDiskBox(ctx context.Context, key libkb.DbKey, res interface{}) (bool, error) {
    39  	return i.encryptedDB.Get(ctx, key, res)
    40  }
    41  
    42  func (i *baseBox) writeDiskBox(ctx context.Context, key libkb.DbKey, data interface{}) error {
    43  	return i.encryptedDB.Put(ctx, key, data)
    44  }
    45  
    46  func (i *baseBox) maybeNuke(err Error, key libkb.DbKey) {
    47  	if err != nil && err.ShouldClear() {
    48  		i.G().Log.Debug("nuking %v on err: %v", key, err)
    49  		if err := i.G().LocalChatDb.Delete(key); err != nil {
    50  			i.G().Log.Error("unable to clear box on error! err: %s", err)
    51  		}
    52  	}
    53  }