Skip to content
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

Fix/split generate key and import address #71

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions waddrmgr/scoped_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -1979,6 +1979,17 @@ func (s *ScopedKeyManager) ImportPublicKey(ns walletdb.ReadWriteBucket,
return s.toImportedPublicManagedAddress(pubKey, true)
}

func (s *ScopedKeyManager) GeneratePubKey(pubKey *btcec.PublicKey) (ManagedAddress, error) {
managedAddr, err := newManagedAddressWithoutPrivKey(
s, ImportedDerivationPath, pubKey, true,
s.addrSchema.ExternalAddrType,
)
if err != nil {
return nil, err
}
return managedAddr, nil
}

// importPublicKey imports a public key into the address manager and updates the
// wallet's start block if necessary. An error is returned if the public key
// already exists.
Expand Down Expand Up @@ -2093,6 +2104,20 @@ func (s *ScopedKeyManager) toImportedPrivateManagedAddress(
return managedAddr, nil
}

func (s *ScopedKeyManager) toNonImportedPublicManagedAddress(
pubKey *btcec.PublicKey, compressed bool) (*managedAddress, error) {

managedAddr, err := newManagedAddressWithoutPrivKey(
s, ImportedDerivationPath, pubKey, compressed,
s.addrSchema.ExternalAddrType,
)
if err != nil {
return nil, err
}

return managedAddr, nil
}

// toPublicManagedAddress converts an imported public key to an imported managed
// address.
func (s *ScopedKeyManager) toImportedPublicManagedAddress(
Expand Down
29 changes: 29 additions & 0 deletions wallet/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,35 @@ func (w *Wallet) ImportPublicKey(pubKey *btcec.PublicKey,
return err
}

func (w *Wallet) GenerateAddressFromKey(pubKey *btcec.PublicKey, addrType waddrmgr.AddressType) (waddrmgr.ManagedAddress, error) {
var keyScope waddrmgr.KeyScope
switch addrType {
case waddrmgr.NestedWitnessPubKey:
keyScope = waddrmgr.KeyScopeBIP0049Plus

case waddrmgr.WitnessPubKey:
keyScope = waddrmgr.KeyScopeBIP0084

case waddrmgr.TaprootPubKey:
keyScope = waddrmgr.KeyScopeBIP0086

default:
return nil, fmt.Errorf("address type %v is not supported", addrType)
}

scopedKeyManager, err := w.Manager.FetchScopedKeyManager(keyScope)
if err != nil {
return nil, err
}

addr, err := scopedKeyManager.GeneratePubKey(pubKey)
if err != nil {
return nil, err
}

return addr, nil
}

func (w *Wallet) ImportPublicKeyReturnAddress(pubKey *btcec.PublicKey, addrType waddrmgr.AddressType) (waddrmgr.ManagedAddress, error) {

// Determine what key scope the public key should belong to and import
Expand Down
50 changes: 39 additions & 11 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ import (
"encoding/hex"
"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"
"time"

"github.com/btcsuite/btcd/btcec/v2"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/stroomnetwork/frost/crypto"

"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/btcutil"
Expand Down Expand Up @@ -4231,19 +4232,46 @@ func OpenWithRetry(db walletdb.DB, pubPass []byte, cbs *waddrmgr.OpenCallbacks,
}

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

key, importedAddress, err := w.GenerateKeyFromEthAddressAndImport(ethAddr)
lc, err := w.lcFromEthAddr(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 (in wallet) != %s (in contract)", address, btcAddr)
}
key := lc.GetCombinedPubKey()

generatedBtcAddress, err := w.GenerateAddressFromKey(key, waddrmgr.TaprootPubKey)
if err != nil {
return nil, err
}
if btcAddr != "" && generatedBtcAddress.Address().String() != btcAddr {
return nil, fmt.Errorf("address mismatch: %s (in contract) != %s (in wallet)", generatedBtcAddress.Address().String(), btcAddr)
}

importedAddresses, addrErr := w.AccountAddresses(waddrmgr.ImportedAddrAccount)
if addrErr != nil {
return nil, addrErr
}
for _, importedAddr := range importedAddresses {
if importedAddr.String() == generatedBtcAddress.Address().String() {
return nil, fmt.Errorf("already exists")
}
}

importedAddress, err := w.ImportPublicKeyReturnAddress(key, waddrmgr.TaprootPubKey)
if err != nil {
return key, err
}
if importedAddress == nil {
return key, fmt.Errorf("imported address is nil")
}

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

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

return key, nil
}

Expand Down
Loading