mirror of
https://github.com/LBRYFoundation/lbcwallet.git
synced 2025-08-23 17:47:29 +00:00
wallet/wallet: set birthday block when importing private key
This commit is contained in:
parent
e30cebea1b
commit
33629dcfc2
1 changed files with 21 additions and 9 deletions
|
@ -2603,6 +2603,9 @@ func (w *Wallet) DumpWIFPrivateKey(addr btcutil.Address) (string, error) {
|
||||||
|
|
||||||
// ImportPrivateKey imports a private key to the wallet and writes the new
|
// ImportPrivateKey imports a private key to the wallet and writes the new
|
||||||
// wallet to disk.
|
// wallet to disk.
|
||||||
|
//
|
||||||
|
// NOTE: If a block stamp is not provided, then the wallet's birthday will be
|
||||||
|
// set to the genesis block of the corresponding chain.
|
||||||
func (w *Wallet) ImportPrivateKey(scope waddrmgr.KeyScope, wif *btcutil.WIF,
|
func (w *Wallet) ImportPrivateKey(scope waddrmgr.KeyScope, wif *btcutil.WIF,
|
||||||
bs *waddrmgr.BlockStamp, rescan bool) (string, error) {
|
bs *waddrmgr.BlockStamp, rescan bool) (string, error) {
|
||||||
|
|
||||||
|
@ -2613,18 +2616,18 @@ func (w *Wallet) ImportPrivateKey(scope waddrmgr.KeyScope, wif *btcutil.WIF,
|
||||||
|
|
||||||
// The starting block for the key is the genesis block unless otherwise
|
// The starting block for the key is the genesis block unless otherwise
|
||||||
// specified.
|
// specified.
|
||||||
var newBirthday time.Time
|
|
||||||
if bs == nil {
|
if bs == nil {
|
||||||
bs = &waddrmgr.BlockStamp{
|
bs = &waddrmgr.BlockStamp{
|
||||||
Hash: *w.chainParams.GenesisHash,
|
Hash: *w.chainParams.GenesisHash,
|
||||||
Height: 0,
|
Height: 0,
|
||||||
|
Timestamp: w.chainParams.GenesisBlock.Header.Timestamp,
|
||||||
}
|
}
|
||||||
} else {
|
} else if bs.Timestamp.IsZero() {
|
||||||
// Only update the new birthday time from default value if we
|
// Only update the new birthday time from default value if we
|
||||||
// actually have timestamp info in the header.
|
// actually have timestamp info in the header.
|
||||||
header, err := w.chainClient.GetBlockHeader(&bs.Hash)
|
header, err := w.chainClient.GetBlockHeader(&bs.Hash)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
newBirthday = header.Timestamp
|
bs.Timestamp = header.Timestamp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2646,13 +2649,22 @@ func (w *Wallet) ImportPrivateKey(scope waddrmgr.KeyScope, wif *btcutil.WIF,
|
||||||
}
|
}
|
||||||
|
|
||||||
// We'll only update our birthday with the new one if it is
|
// We'll only update our birthday with the new one if it is
|
||||||
// before our current one. Otherwise, we won't rescan for
|
// before our current one. Otherwise, if we do, we can
|
||||||
// potentially relevant chain events that occurred between them.
|
// potentially miss detecting relevant chain events that
|
||||||
if newBirthday.After(w.Manager.Birthday()) {
|
// occurred between them while rescanning.
|
||||||
|
birthdayBlock, err := w.Manager.BirthdayBlock(addrmgrNs)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if bs.Height >= birthdayBlock.Height {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return w.Manager.SetBirthday(addrmgrNs, newBirthday)
|
err = w.Manager.SetBirthday(addrmgrNs, bs.Timestamp)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return w.Manager.SetBirthdayBlock(addrmgrNs, *bs)
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
|
Loading…
Add table
Reference in a new issue