-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
database: Combine xpub and index concepts.
Fee xpub key and last used address index are now wrapped into a struct, with both fields being set and retrieved together rather than individually. The underlying format for storing these values in the database does not change. The only change is the interface between the database code and the caller.
- Loading branch information
1 parent
be31d54
commit 0633260
Showing
7 changed files
with
108 additions
and
96 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains 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 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 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,75 @@ | ||
// Copyright (c) 2020-2024 The Decred developers | ||
// Use of this source code is governed by an ISC | ||
// license that can be found in the LICENSE file. | ||
|
||
package database | ||
|
||
import ( | ||
"fmt" | ||
|
||
bolt "go.etcd.io/bbolt" | ||
) | ||
|
||
type FeeXPub struct { | ||
Key string | ||
LastUsedIdx uint32 | ||
} | ||
|
||
// insertFeeXPub stores the provided pubkey in the database, regardless of | ||
// whether a value pre-exists. | ||
func insertFeeXPub(tx *bolt.Tx, xpub FeeXPub) error { | ||
vspBkt := tx.Bucket(vspBktK) | ||
|
||
err := vspBkt.Put(feeXPubK, []byte(xpub.Key)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return vspBkt.Put(lastAddressIndexK, uint32ToBytes(xpub.LastUsedIdx)) | ||
} | ||
|
||
// FeeXPub retrieves the extended pubkey used for generating fee addresses | ||
// from the database. | ||
func (vdb *VspDatabase) FeeXPub() (FeeXPub, error) { | ||
var feeXPub string | ||
var idx uint32 | ||
err := vdb.db.View(func(tx *bolt.Tx) error { | ||
vspBkt := tx.Bucket(vspBktK) | ||
|
||
// Get the key. | ||
xpubBytes := vspBkt.Get(feeXPubK) | ||
if xpubBytes == nil { | ||
return nil | ||
} | ||
feeXPub = string(xpubBytes) | ||
|
||
// Get the last used address index. | ||
idxBytes := vspBkt.Get(lastAddressIndexK) | ||
if idxBytes == nil { | ||
return nil | ||
} | ||
idx = bytesToUint32(idxBytes) | ||
|
||
return nil | ||
}) | ||
if err != nil { | ||
return FeeXPub{}, fmt.Errorf("could not retrieve fee xpub: %w", err) | ||
} | ||
|
||
return FeeXPub{Key: feeXPub, LastUsedIdx: idx}, nil | ||
} | ||
|
||
// SetLastAddressIndex updates the last index used to derive a new fee address | ||
// from the fee xpub key. | ||
func (vdb *VspDatabase) SetLastAddressIndex(idx uint32) error { | ||
current, err := vdb.FeeXPub() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
current.LastUsedIdx = idx | ||
|
||
return vdb.db.Update(func(tx *bolt.Tx) error { | ||
return insertFeeXPub(tx, current) | ||
}) | ||
} |
This file contains 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 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 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