Skip to content

Commit

Permalink
addressing some not needed fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ac4ch committed May 15, 2024
1 parent 8f37026 commit b54efea
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 48 deletions.
36 changes: 33 additions & 3 deletions client_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"net/http"

"github.com/bitcoinschema/go-bitcoin/v2"
"github.com/libsv/go-bk/bec"
"github.com/libsv/go-bk/wif"
"github.com/pkg/errors"
)

// WalletClientConfigurator is the interface for configuring WalletClient
Expand All @@ -17,7 +20,10 @@ type WithXPriv struct {
}

func (w *WithXPriv) Configure(c *WalletClient) {
c.xPrivString = w.XPrivString
var err error
if c.xPriv, err = bitcoin.GenerateHDKeyFromString(*w.XPrivString); err != nil {
c.xPriv = nil
}
}

// WithXPub sets the xPubString on the client
Expand All @@ -26,7 +32,11 @@ type WithXPub struct {
}

func (w *WithXPub) Configure(c *WalletClient) {
c.xPubString = w.XPubString
var err error
if c.xPub, err = bitcoin.GetHDKeyFromExtendedPublicKey(*w.XPubString); err != nil {
w.XPubString = nil
}

}

// WithAccessKey sets the accessKeyString on the client
Expand All @@ -35,7 +45,10 @@ type WithAccessKey struct {
}

func (w *WithAccessKey) Configure(c *WalletClient) {
c.accessKeyString = w.AccessKeyString
var err error
if c.accessKey, err = w.initializeAccessKey(); err != nil {
c.accessKey = nil
}
}

// WithAdminKey sets the admin key for creating new xpubs
Expand Down Expand Up @@ -75,3 +88,20 @@ type WithSignRequest struct {
func (w *WithSignRequest) Configure(c *WalletClient) {
c.signRequest = w.Sign
}

// initializeAccessKey handles the specific initialization of the access key.
func (c *WithAccessKey) initializeAccessKey() (*bec.PrivateKey, error) {
var err error
var privateKey *bec.PrivateKey
var decodedWIF *wif.WIF

if decodedWIF, err = wif.DecodeWIF(*c.AccessKeyString); err != nil {
if privateKey, err = bitcoin.PrivateKeyFromString(*c.AccessKeyString); err != nil {
return nil, errors.Wrap(err, "failed to decode access key")
}
} else {
privateKey = decodedWIF.PrivKey
}

return privateKey, nil
}
58 changes: 13 additions & 45 deletions walletclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,20 @@ import (
"net/http"

"github.com/bitcoin-sv/spv-wallet/models"
"github.com/bitcoinschema/go-bitcoin/v2"
"github.com/libsv/go-bk/bec"
"github.com/libsv/go-bk/bip32"
"github.com/libsv/go-bk/wif"
"github.com/pkg/errors"
)

// WalletClient is the spv wallet Go client representation.
type WalletClient struct {
signRequest *bool
server *string
accessKeyString *string
xPrivString *string
xPubString *string
httpClient *http.Client
accessKey *bec.PrivateKey
adminXPriv *bip32.ExtendedKey
xPriv *bip32.ExtendedKey
xPub *bip32.ExtendedKey
signRequest *bool
server *string
httpClient *http.Client
accessKey *bec.PrivateKey
adminXPriv *bip32.ExtendedKey
xPriv *bip32.ExtendedKey
xPub *bip32.ExtendedKey
}

// NewWalletClientWithXPrivate creates a new WalletClient instance using a private key (xPriv).
Expand Down Expand Up @@ -91,45 +86,18 @@ func newWalletClient(configurators ...WalletClientConfigurator) (*WalletClient,

// initializeKeys handles the initialization of keys based on the existing fields.
func (c *WalletClient) initializeKeys() error {
var err error
switch {
case c.xPrivString != nil:
if c.xPriv, err = bitcoin.GenerateHDKeyFromString(*c.xPrivString); err != nil {
return err
}
if c.xPub, err = c.xPriv.Neuter(); err != nil {
return err
}
case c.xPubString != nil:
if c.xPub, err = bitcoin.GetHDKeyFromExtendedPublicKey(*c.xPubString); err != nil {
return err
}
case c.accessKeyString != nil:
return c.initializeAccessKey()
case c.xPriv != nil:
return nil
case c.xPub != nil:
return nil
case c.accessKey != nil:
return nil
case c.adminXPriv != nil:
return nil
default:
return errors.New("no keys provided for initialization")
}
return nil
}

// initializeAccessKey handles the specific initialization of the access key.
func (c *WalletClient) initializeAccessKey() error {
var err error
var privateKey *bec.PrivateKey
var decodedWIF *wif.WIF

if decodedWIF, err = wif.DecodeWIF(*c.accessKeyString); err != nil {
if privateKey, err = bitcoin.PrivateKeyFromString(*c.accessKeyString); err != nil {
return errors.Wrap(err, "failed to decode access key")
}
} else {
privateKey = decodedWIF.PrivKey
}

c.accessKey = privateKey
return nil
}

// processMetadata will process the metadata
Expand Down

0 comments on commit b54efea

Please sign in to comment.