-
Notifications
You must be signed in to change notification settings - Fork 652
wallet: SQL-only signer account-secret fallback #1254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yyforyongyu
wants to merge
3
commits into
sql-wallet
Choose a base branch
from
impl-signer-account-secret
base: sql-wallet
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package db | ||
|
|
||
| import ( | ||
| "errors" | ||
| ) | ||
|
|
||
| // ErrAccountSecretUnavailable is returned when a backend does not expose | ||
| // store-side account secret material through AccountStore. | ||
| var ErrAccountSecretUnavailable = errors.New("account secret unavailable") | ||
|
|
||
| // Validate checks whether a GetAccountSecretQuery identifies exactly one | ||
| // account selector. | ||
| func (query GetAccountSecretQuery) Validate() error { | ||
| if query.Name == nil && query.AccountNumber == nil { | ||
| return ErrInvalidAccountQuery | ||
| } | ||
|
|
||
| if query.Name != nil && query.AccountNumber != nil { | ||
| return ErrInvalidAccountQuery | ||
| } | ||
|
|
||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
wallet/internal/db/itest/accountstore_getaccountsecret_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| //go:build itest | ||
|
|
||
| package itest | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/btcsuite/btcwallet/wallet/internal/db" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // TestGetAccountSecret verifies that GetAccountSecret returns account rows | ||
| // with encrypted private key material, watch-only nil material, and not-found | ||
| // errors as distinct outcomes. | ||
| func TestGetAccountSecret(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| store := NewTestStore(t) | ||
| walletID := newWallet(t, store, "wallet-get-account-secret") | ||
| scope := db.KeyScopeBIP0084 | ||
| pubKey := []byte("derived-account-pubkey") | ||
| privKey := []byte("encrypted-account-privkey") | ||
|
|
||
| const fingerprint = uint32(0xAABBCCDD) | ||
|
|
||
| derived, err := store.CreateDerivedAccount( | ||
| t.Context(), db.CreateDerivedAccountParams{ | ||
| WalletID: walletID, | ||
| Scope: scope, | ||
| Name: "derived", | ||
| }, func(_ context.Context, _ db.KeyScope, _ uint32, | ||
| walletIsWatchOnly bool) (*db.DerivedAccountData, error) { | ||
|
|
||
| require.False(t, walletIsWatchOnly) | ||
|
|
||
| return &db.DerivedAccountData{ | ||
| PublicKey: pubKey, | ||
| EncryptedPrivateKey: privKey, | ||
| MasterKeyFingerprint: fingerprint, | ||
| }, nil | ||
| }, | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| secret, err := store.GetAccountSecret( | ||
| t.Context(), db.GetAccountSecretQuery{ | ||
| WalletID: walletID, | ||
| Scope: scope, | ||
| AccountNumber: &derived.AccountNumber, | ||
| }, | ||
| ) | ||
| require.NoError(t, err) | ||
| require.Equal(t, walletID, secret.WalletID) | ||
| require.Equal(t, scope, secret.Scope) | ||
| require.Equal(t, derived.AccountNumber, secret.AccountNumber) | ||
| require.Equal(t, "derived", secret.AccountName) | ||
| require.Equal(t, pubKey, secret.PublicKey) | ||
| require.Equal(t, privKey, secret.EncryptedPrivateKey) | ||
| require.Equal(t, fingerprint, secret.MasterKeyFingerprint) | ||
|
|
||
| watchOnlyName := "watch-only-import" | ||
| _, err = store.CreateImportedAccount( | ||
| t.Context(), db.CreateImportedAccountParams{ | ||
| WalletID: walletID, | ||
| Name: watchOnlyName, | ||
| Scope: scope, | ||
| PublicKey: []byte("watch-only-pubkey"), | ||
| }, | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| secret, err = store.GetAccountSecret( | ||
| t.Context(), db.GetAccountSecretQuery{ | ||
| WalletID: walletID, | ||
| Scope: scope, | ||
| Name: &watchOnlyName, | ||
| }, | ||
| ) | ||
| require.NoError(t, err) | ||
| require.Equal(t, watchOnlyName, secret.AccountName) | ||
| require.Nil(t, secret.EncryptedPrivateKey) | ||
|
|
||
| missing := uint32(999) | ||
| _, err = store.GetAccountSecret( | ||
| t.Context(), db.GetAccountSecretQuery{ | ||
| WalletID: walletID, | ||
| Scope: scope, | ||
| AccountNumber: &missing, | ||
| }, | ||
| ) | ||
| require.ErrorIs(t, err, db.ErrAccountNotFound) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package kvdb | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/btcsuite/btcwallet/wallet/internal/db" | ||
| ) | ||
|
|
||
| // GetAccountSecret reports that kvdb account secrets are not exposed through | ||
| // the store-side account-secret contract. | ||
| func (s *Store) GetAccountSecret(_ context.Context, | ||
| query db.GetAccountSecretQuery) (*db.AccountSecret, error) { | ||
|
|
||
| err := query.Validate() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return nil, db.ErrAccountSecretUnavailable | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| package pg | ||
|
|
||
| import ( | ||
| "context" | ||
| "database/sql" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/btcsuite/btcwallet/wallet/internal/db" | ||
| "github.com/btcsuite/btcwallet/wallet/internal/sql/pg/sqlc" | ||
| ) | ||
|
|
||
| // GetAccountSecret retrieves encrypted account-level signing material for one | ||
| // account. | ||
| func (s *Store) GetAccountSecret(ctx context.Context, | ||
| query db.GetAccountSecretQuery) (*db.AccountSecret, error) { | ||
|
|
||
| err := query.Validate() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| var secret *db.AccountSecret | ||
|
|
||
| err = s.execRead(ctx, func(q *sqlc.Queries) error { | ||
| row, err := q.GetAccountSecret(ctx, sqlc.GetAccountSecretParams{ | ||
| WalletID: int64(query.WalletID), | ||
| Purpose: int64(query.Scope.Purpose), | ||
| CoinType: int64(query.Scope.Coin), | ||
| AccountNumber: db.NullableUint32ToSQLInt64(query.AccountNumber), | ||
| AccountName: db.NullableStringToSQLNullString(query.Name), | ||
| }) | ||
| if err != nil { | ||
| return mapGetAccountSecretErr(err, query) | ||
| } | ||
|
|
||
| secret, err = accountSecretRowToInfo(row) | ||
|
|
||
| return err | ||
| }) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return secret, nil | ||
| } | ||
|
|
||
| // accountSecretRowToInfo converts a PostgreSQL account-secret row to the | ||
| // backend-independent AccountSecret shape. | ||
| func accountSecretRowToInfo( | ||
| row sqlc.GetAccountSecretRow) (*db.AccountSecret, error) { | ||
|
|
||
| walletID, err := db.Int64ToUint32(row.WalletID) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("wallet ID: %w", err) | ||
| } | ||
|
|
||
| purpose, err := db.Int64ToUint32(row.Purpose) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("scope purpose: %w", err) | ||
| } | ||
|
|
||
| coin, err := db.Int64ToUint32(row.CoinType) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("scope coin type: %w", err) | ||
| } | ||
|
|
||
| var accountNumber uint32 | ||
| if row.AccountNumber.Valid { | ||
| accountNumber, err = db.Int64ToUint32(row.AccountNumber.Int64) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("account number: %w", err) | ||
| } | ||
| } | ||
|
|
||
| var masterFingerprint uint32 | ||
| if row.MasterFingerprint.Valid { | ||
| masterFingerprint, err = db.Int64ToUint32( | ||
| row.MasterFingerprint.Int64, | ||
| ) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("master fingerprint: %w", err) | ||
| } | ||
| } | ||
|
|
||
| return &db.AccountSecret{ | ||
| WalletID: walletID, | ||
| Scope: db.KeyScope{Purpose: purpose, Coin: coin}, | ||
| AccountNumber: accountNumber, | ||
| AccountName: row.AccountName, | ||
| PublicKey: row.PublicKey, | ||
| EncryptedPrivateKey: row.EncryptedPrivateKey, | ||
| MasterKeyFingerprint: masterFingerprint, | ||
| }, nil | ||
| } | ||
|
|
||
| // mapGetAccountSecretErr returns the typed ErrAccountNotFound when err is | ||
| // sql.ErrNoRows, falling back to a wrapped form otherwise. | ||
| func mapGetAccountSecretErr(err error, | ||
| query db.GetAccountSecretQuery) error { | ||
|
|
||
| if !errors.Is(err, sql.ErrNoRows) { | ||
| return fmt.Errorf("get account secret: %w", err) | ||
| } | ||
|
|
||
| if query.Name != nil { | ||
| return fmt.Errorf("account %q in scope %d/%d: %w", *query.Name, | ||
| query.Scope.Purpose, query.Scope.Coin, | ||
| db.ErrAccountNotFound) | ||
| } | ||
|
|
||
| return fmt.Errorf("account %d in scope %d/%d: %w", | ||
| *query.AccountNumber, query.Scope.Purpose, query.Scope.Coin, | ||
| db.ErrAccountNotFound) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i am questioning why do we need this method - when the wallet is unlocked, we will cache the secrets in the
cache? thus that whenever we need to create secrets, we will use thecacheto do so?in addition what is the relationship among account secret, wallet secret, keyvault and cache?