Skip to content

Commit

Permalink
Wallet sync fixed (#31)
Browse files Browse the repository at this point in the history
* address import logged

* address import printed

* sets ChangeAddressKey even when import unsuccessful

* sets ChangeAddressKey even when import unsuccessful

* fix

* address able to be debugged

* accounts

* reverts "address able to be debugged"

* imports to default acc

* imports to default acc + addrType=adtChain

* adds change address to External adds

* import returns address even if duplicate

* sync after wallet initialized

* sync after wallet initialized reverted,
hardcoded address

* hardcoded address fixed

* filters imported addresses only

* filters imported addresses only, fix

* introduces GetWalletAddresses

* puts back startWalletRPCServices

* removes ChangeAddress from wallet

* PR comments handled

---------

Co-authored-by: dpiatkivskyi <dmytro.piatkikvskyi@gmailcom>
  • Loading branch information
dmytropiatkivskyi and dpiatkivskyi authored Sep 18, 2024
1 parent 14f4597 commit 72b76fe
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 28 deletions.
7 changes: 6 additions & 1 deletion run/btcwallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,15 @@ func doInit(config *BtcwalletConfig) (*wallet.Wallet, error) {
return nil, err
}

changeAddressKey, err := w.GenerateKeyFromEthAddressAndImport(ethChangeAddr)
changeAddressKey, changeAddress, err := w.GenerateKeyFromEthAddressAndImport(ethChangeAddr)
if err != nil && !strings.Contains(err.Error(), "already exists") {
return nil, fmt.Errorf("cannot import change address: %w", err)
}
log.Infof("Change address: %s", changeAddress)

if changeAddressKey == nil {
return nil, fmt.Errorf("change key is nil")
}
w.ChangeAddressKey = changeAddressKey
}

Expand Down
14 changes: 8 additions & 6 deletions waddrmgr/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2398,9 +2398,10 @@ func putBirthday(ns walletdb.ReadWriteBucket, t time.Time) error {
// FetchBirthdayBlock retrieves the birthday block from the database.
//
// The block is serialized as follows:
// [0:4] block height
// [4:36] block hash
// [36:44] block timestamp
//
// [0:4] block height
// [4:36] block hash
// [36:44] block timestamp
func FetchBirthdayBlock(ns walletdb.ReadBucket) (BlockStamp, error) {
var block BlockStamp

Expand Down Expand Up @@ -2438,9 +2439,10 @@ func DeleteBirthdayBlock(ns walletdb.ReadWriteBucket) error {
// PutBirthdayBlock stores the provided birthday block to the database.
//
// The block is serialized as follows:
// [0:4] block height
// [4:36] block hash
// [36:44] block timestamp
//
// [0:4] block height
// [4:36] block hash
// [36:44] block timestamp
//
// NOTE: This does not alter the birthday block verification state.
func PutBirthdayBlock(ns walletdb.ReadWriteBucket, block BlockStamp) error {
Expand Down
3 changes: 2 additions & 1 deletion waddrmgr/scoped_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/sha256"
"encoding/binary"
"fmt"
"strings"
"sync"

"github.com/btcsuite/btcd/btcec/v2"
Expand Down Expand Up @@ -1971,7 +1972,7 @@ func (s *ScopedKeyManager) ImportPublicKey(ns walletdb.ReadWriteBucket,
err := s.importPublicKey(
ns, serializedPubKey, nil, s.addrSchema.ExternalAddrType, bs,
)
if err != nil {
if err != nil && !strings.Contains(err.Error(), "already exists") {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion wallet/frost_signing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestFrostSigning(t *testing.T) {
err = w.Unlock([]byte("world"), time.After(10*time.Minute))
require.NoError(t, err)

pubKey, err := w.GenerateKeyFromEthAddressAndImport("0x7b3f4f4b3cCf7f3fDf3f3f3f3f3f3f3f3f3f3f3f")
pubKey, _, err := w.GenerateKeyFromEthAddressAndImport("0x7b3f4f4b3cCf7f3fDf3f3f3f3f3f3f3f3f3f3f3f")
require.NoError(t, err)

p2shAddr, err := txscript.PayToTaprootScript(pubKey)
Expand Down
17 changes: 7 additions & 10 deletions wallet/import_btc_addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func (w *Wallet) GenerateAndImportKeyWithCheck(btcAddr, ethAddr string) (*btcec.PublicKey, error) {

key, importedAddress, err := w.generateKeyFromEthAddressAndImport(ethAddr)
key, importedAddress, err := w.GenerateKeyFromEthAddressAndImport(ethAddr)
if err != nil {
return nil, err
}
Expand All @@ -27,12 +27,7 @@ func (w *Wallet) GenerateAndImportKeyWithCheck(btcAddr, ethAddr string) (*btcec.
return key, nil
}

func (w *Wallet) GenerateKeyFromEthAddressAndImport(ethAddr string) (*btcec.PublicKey, error) {
key, _, err := w.generateKeyFromEthAddressAndImport(ethAddr)
return key, err
}

func (w *Wallet) generateKeyFromEthAddressAndImport(ethAddr string) (*btcec.PublicKey, waddrmgr.ManagedAddress, error) {
func (w *Wallet) GenerateKeyFromEthAddressAndImport(ethAddr string) (*btcec.PublicKey, waddrmgr.ManagedAddress, error) {

lc, err := w.lcFromEthAddr(ethAddr)
if err != nil {
Expand All @@ -42,18 +37,20 @@ func (w *Wallet) generateKeyFromEthAddressAndImport(ethAddr string) (*btcec.Publ
pubKey := lc.GetCombinedPubKey()
importedAddress, err := w.ImportPublicKeyReturnAddress(pubKey, waddrmgr.TaprootPubKey)
if err != nil {
return nil, nil, err
return pubKey, importedAddress, err
}

if importedAddress == nil {
return nil, nil, fmt.Errorf("imported address is nil")
return pubKey, nil, fmt.Errorf("imported address is nil")
}

err = w.AddressMapStorage.SetEthAddress(importedAddress.Address().EncodeAddress(), ethAddr)
if err != nil {
return nil, nil, err
return pubKey, nil, err
}

log.Infof("Imported address %s with eth address %s", importedAddress.Address().EncodeAddress(), ethAddr)

return pubKey, importedAddress, nil
}

Expand Down
21 changes: 12 additions & 9 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ expandHorizons:
// construct the filter blocks request. The request includes the range
// of blocks we intend to scan, in addition to the scope-index -> addr
// map for all internal and external branches.
filterReq := newFilterBlocksRequest(batch, scopedMgrs, recoveryState)
filterReq := newFilterBlocksRequest(w, batch, scopedMgrs, recoveryState)

// Initiate the filter blocks request using our chain backend. If an
// error occurs, we are unable to proceed with the recovery.
Expand Down Expand Up @@ -1031,9 +1031,7 @@ func internalKeyPath(index uint32) waddrmgr.DerivationPath {

// newFilterBlocksRequest constructs FilterBlocksRequests using our current
// block range, scoped managers, and recovery state.
func newFilterBlocksRequest(batch []wtxmgr.BlockMeta,
scopedMgrs map[waddrmgr.KeyScope]*waddrmgr.ScopedKeyManager,
recoveryState *RecoveryState) *chain.FilterBlocksRequest {
func newFilterBlocksRequest(w *Wallet, batch []wtxmgr.BlockMeta, scopedMgrs map[waddrmgr.KeyScope]*waddrmgr.ScopedKeyManager, recoveryState *RecoveryState) *chain.FilterBlocksRequest {

filterReq := &chain.FilterBlocksRequest{
Blocks: batch,
Expand All @@ -1046,13 +1044,18 @@ func newFilterBlocksRequest(batch []wtxmgr.BlockMeta,
// sets belong to all currently tracked scopes.
for scope := range scopedMgrs {
scopeState := recoveryState.StateForScope(scope)
for index, addr := range scopeState.ExternalBranch.Addrs() {
scopedIndex := waddrmgr.ScopedIndex{
Scope: scope,
Index: index,

addresses, err := w.AccountAddresses(waddrmgr.ImportedAddrAccount)
if err == nil {
for index, addr := range addresses {
scopedIndex := waddrmgr.ScopedIndex{
Scope: scope,
Index: uint32(index),
}
filterReq.ExternalAddrs[scopedIndex] = addr
}
filterReq.ExternalAddrs[scopedIndex] = addr
}

for index, addr := range scopeState.InternalBranch.Addrs() {
scopedIndex := waddrmgr.ScopedIndex{
Scope: scope,
Expand Down

0 comments on commit 72b76fe

Please sign in to comment.