Skip to content

Commit

Permalink
Wallet sync fixes (#32)
Browse files Browse the repository at this point in the history
* moves wallet functionality to wallet.go

* changeAddress NPE avoided

* log -> print

---------

Co-authored-by: dpiatkivskyi <dmytro.piatkikvskyi@gmailcom>
  • Loading branch information
dmytropiatkivskyi and dpiatkivskyi authored Sep 18, 2024
1 parent 9a2b55a commit 803e528
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 123 deletions.
5 changes: 3 additions & 2 deletions run/btcwallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,9 @@ func doInit(config *BtcwalletConfig) (*wallet.Wallet, error) {
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 changeAddress != nil {
log.Infof("Change address: %s", changeAddress.Address())
}
if changeAddressKey == nil {
return nil, fmt.Errorf("change key is nil")
}
Expand Down
121 changes: 0 additions & 121 deletions wallet/import_btc_addr.go

This file was deleted.

113 changes: 113 additions & 0 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (
"errors"
"fmt"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/stroomnetwork/frost/crypto"
"sort"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -4145,3 +4148,113 @@ func OpenWithRetry(db walletdb.DB, pubPass []byte, cbs *waddrmgr.OpenCallbacks,

return w, nil
}

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

key, importedAddress, err := w.GenerateKeyFromEthAddressAndImport(ethAddr)
if err != nil {
return nil, err
}

if importedAddress != nil {
address := importedAddress.Address().String()
if btcAddr != "" && address != btcAddr {
return nil, fmt.Errorf("address mismatch: %s != %s", address, btcAddr)
}
}

return key, nil
}

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

lc, err := w.lcFromEthAddr(ethAddr)
if err != nil {
return nil, nil, err
}

pubKey := lc.GetCombinedPubKey()
importedAddress, err := w.ImportPublicKeyReturnAddress(pubKey, waddrmgr.TaprootPubKey)
if err != nil {
return pubKey, importedAddress, err
}

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

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

fmt.Printf("Imported address %s with eth address %s\n", importedAddress.Address(), ethAddr)

return pubKey, importedAddress, nil
}

func (w *Wallet) lcFromEthAddr(ethAddrStr string) (*crypto.LinearCombination, error) {
ethAddr := common.HexToAddress(ethAddrStr)

uint256Ty, _ := abi.NewType("uint256", "uint256", nil)
addressTy, _ := abi.NewType("address", "address", nil)

arguments := abi.Arguments{
{
Type: uint256Ty,
},
{
Type: uint256Ty,
},
{
Type: addressTy,
},
}

if w.Pk1 == nil {
return nil, fmt.Errorf("missing pk1")
}
if w.Pk2 == nil {
return nil, fmt.Errorf("missing pk2")
}

b1, _ := arguments.Pack(
w.Pk1.X(),
w.Pk1.Y(),
ethAddr,
)
h1 := crypto.Sha256(b1)
c1FromAddr, _ := crypto.PrivKeyFromBytes(h1[:])

b2, _ := arguments.Pack(
w.Pk2.X(),
w.Pk2.Y(),
ethAddr,
)
h2 := crypto.Sha256(b2)
c2FromAddr, _ := crypto.PrivKeyFromBytes(h2[:])

lc, err := crypto.NewLinearCombination(
[]*btcec.PublicKey{w.Pk1, w.Pk2},
[]*btcec.PrivateKey{c1FromAddr, c2FromAddr},
crypto.PrivKeyFromInt(0),
)
if err != nil {
return nil, err
}

return lc, nil
}

func (w *Wallet) GetSignerPublicKeys() (*btcec.PublicKey, *btcec.PublicKey, error) {

if w.Pk1 == nil {
return nil, nil, fmt.Errorf("missing pk1")
}

if w.Pk2 == nil {
return nil, nil, fmt.Errorf("missing pk2")
}

return w.Pk1, w.Pk2, nil
}

0 comments on commit 803e528

Please sign in to comment.