Skip to content

Commit

Permalink
Updated code regards to comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Nazarii-4chain committed Dec 6, 2023
1 parent b819d8c commit 877aee3
Show file tree
Hide file tree
Showing 15 changed files with 175 additions and 170 deletions.
14 changes: 7 additions & 7 deletions buxclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ func New(opts ...ClientOps) (*BuxClient, error) {
var err error
if client.xPrivString != "" {
if client.xPriv, err = bitcoin.GenerateHDKeyFromString(client.xPrivString); err != nil {
Log.Error().Err(err).Str("buxclient", "New/xpriv").Msg(err.Error())
Log.Error().Stack().Msg(err.Error())
return nil, err
}
if client.xPub, err = client.xPriv.Neuter(); err != nil {
Log.Error().Err(err).Str("buxclient", "New/xpub").Msg(err.Error())
Log.Error().Stack().Msg(err.Error())
return nil, err
}
} else if client.xPubString != "" {
client.xPriv = nil
if client.xPub, err = bitcoin.GetHDKeyFromExtendedPublicKey(client.xPubString); err != nil {
Log.Error().Err(err).Str("buxclient", "New").Msg(err.Error())
Log.Error().Stack().Msg(err.Error())
return nil, err
}
} else if client.accessKeyString != "" {
Expand All @@ -69,15 +69,15 @@ func New(opts ...ClientOps) (*BuxClient, error) {
// try as a hex string
var errHex error
if privateKey, errHex = bitcoin.PrivateKeyFromString(client.accessKeyString); errHex != nil {
Log.Error().Err(errHex).Str("buxclient", "New").Msg(errHex.Error())
Log.Error().Stack().Msg(err.Error())
return nil, errors.Wrap(err, errHex.Error())
}
} else {
privateKey = decodedWIF.PrivKey
}
client.accessKey = privateKey
} else {
Log.Error().Str("buxclient", "New").Msg("no keys available")
Log.Error().Stack().Msg("no keys available")
return nil, errors.New("no keys available")
}

Expand All @@ -95,7 +95,7 @@ func New(opts ...ClientOps) (*BuxClient, error) {
}

if client.transport, err = transports.NewTransport(transportOptions...); err != nil {
Log.Error().Err(err).Str("buxclient", "New").Msg(err.Error())
Log.Error().Stack().Msg(err.Error())
return nil, err
}

Expand All @@ -106,7 +106,7 @@ func New(opts ...ClientOps) (*BuxClient, error) {
func (b *BuxClient) SetAdminKey(adminKeyString string) error {
adminKey, err := bip32.NewKeyFromString(adminKeyString)
if err != nil {
Log.Error().Err(err).Str("buxclient", "SetAdminKey").Msg(err.Error())
Log.Error().Stack().Msg(err.Error())
return err
}

Expand Down
2 changes: 1 addition & 1 deletion examples/graphql/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func main() {
buxclient.WithSignRequest(true),
)
if err != nil {
log.Fatal().Err(err).Str("examples", "graphql").Msg(err.Error())
log.Fatal().Stack().Msg(err.Error())
}

log.Printf("client loaded - bux debug: %v", buxClient.IsDebug())
Expand Down
2 changes: 1 addition & 1 deletion examples/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func main() {
buxclient.WithSignRequest(true),
)
if err != nil {
log.Fatal().Err(err).Str("examples", "http").Msg(err.Error())
log.Fatal().Stack().Msg(err.Error())
}

log.Printf("client loaded - bux debug: %v", buxClient.IsDebug())
Expand Down
2 changes: 1 addition & 1 deletion examples/http_with_access_key/http_with_access_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func main() {
buxclient.WithSignRequest(true),
)
if err != nil {
log.Fatal().Err(err).Str("examples", "http_with_access_key").Msg(err.Error())
log.Fatal().Stack().Msg(err.Error())
}

log.Printf("client loaded - bux debug: %v", client.IsDebug())
Expand Down
4 changes: 2 additions & 2 deletions examples/new_paymail/new_paymail.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ func main() {
buxclient.WithSignRequest(true),
)
if err != nil {
log.Fatal().Err(err).Str("examples", "new_paymail").Msg(err.Error())
log.Fatal().Stack().Msg(err.Error())
}

log.Printf("client loaded - bux debug: %v", buxClient.IsDebug())
err = buxClient.NewPaymail(context.Background(), rawXPub, "foo@domain.com", "", "Foo", nil)

if err != nil {
log.Fatal().Err(err).Str("examples", "new_paymail").Msg(err.Error())
log.Fatal().Stack().Msg(err.Error())
}
log.Printf("paymail added")

Expand Down
4 changes: 2 additions & 2 deletions examples/register_xpub/register_xpub.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ func main() {
buxclient.WithSignRequest(true),
)
if err != nil {
log.Fatal().Err(err).Str("examples", "register_xpub").Msg(err.Error())
log.Fatal().Stack().Msg(err.Error())
}

if err = buxClient.NewXpub(
context.Background(), rawXPub, &buxmodels.Metadata{"example_field": "example_data"},
); err != nil {
log.Fatal().Err(err).Str("examples", "register_xpub").Msg(err.Error())
log.Fatal().Stack().Msg(err.Error())
}

log.Print("registered xPub: " + rawXPub)
Expand Down
3 changes: 3 additions & 0 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logger

import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/pkgerrors"
"os"
"sync"
)
Expand All @@ -12,6 +13,8 @@ var log zerolog.Logger

func Get() zerolog.Logger {
once.Do(func() {
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack

log = zerolog.New(os.Stdout).With().Timestamp().Str("service", "go-buxclient").Logger()

})
Expand Down
6 changes: 3 additions & 3 deletions transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ func (b *BuxClient) SendToRecipients(ctx context.Context, recipients []*transpor
) (*buxmodels.Transaction, transports.ResponseError) {
draft, err := b.DraftToRecipients(ctx, recipients, metadata)
if err != nil {
Log.Error().Err(err).Str("transactions", "SendToRecipients").Msg(err.Error())
Log.Error().Stack().Msg(err.Error())
return nil, err
} else if draft == nil {
Log.Error().Err(buxerrors.ErrDraftNotFound).Str("transactions", "SendToRecipients").Msg(buxerrors.ErrDraftNotFound.Error())
Log.Error().Stack().Msg(buxerrors.ErrDraftNotFound.Error())
return nil, transports.WrapError(buxerrors.ErrDraftNotFound)
}

var hex string
if hex, err = b.FinalizeTransaction(draft); err != nil {
Log.Error().Err(err).Str("transactions", "SendToRecipients").Msg(err.Error())
Log.Error().Stack().Msg(err.Error())
return nil, err
}

Expand Down
32 changes: 17 additions & 15 deletions transports/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func SetSignature(header *http.Header, xPriv *bip32.ExtendedKey, bodyString stri
// Create the signature
authData, err := createSignature(xPriv, bodyString)
if err != nil {
Log.Error().Err(err).Str("authentication", "SetSignature").Msg(err.Error())
Log.Error().Stack().Msg(err.Error())
return WrapError(err)
}

Expand All @@ -41,7 +41,7 @@ func SignInputs(dt *buxmodels.DraftTransaction, xPriv *bip32.ExtendedKey) (signe
// Start a bt draft transaction
var txDraft *bt.Tx
if txDraft, err = bt.NewTxFromString(dt.Hex); err != nil {
Log.Error().Err(err).Str("authentication", "SignInputs").Msg(err.Error())
Log.Error().Stack().Msg(err.Error())
resError = WrapError(err)
return
}
Expand All @@ -54,7 +54,7 @@ func SignInputs(dt *buxmodels.DraftTransaction, xPriv *bip32.ExtendedKey) (signe
if ls, err = bscript.NewFromHexString(
input.Destination.LockingScript,
); err != nil {
Log.Error().Err(err).Str("authentication", "SignInputs").Msg(err.Error())
Log.Error().Stack().Msg(err.Error())
resError = WrapError(err)
return
}
Expand All @@ -66,7 +66,7 @@ func SignInputs(dt *buxmodels.DraftTransaction, xPriv *bip32.ExtendedKey) (signe
if chainKey, err = xPriv.Child(
input.Destination.Chain,
); err != nil {
Log.Error().Err(err).Str("authentication", "SignInputs").Msg(err.Error())
Log.Error().Stack().Msg(err.Error())
resError = WrapError(err)
return
}
Expand All @@ -76,7 +76,7 @@ func SignInputs(dt *buxmodels.DraftTransaction, xPriv *bip32.ExtendedKey) (signe
if numKey, err = chainKey.Child(
input.Destination.Num,
); err != nil {
Log.Error().Err(err).Str("authentication", "SignInputs").Msg(err.Error())
Log.Error().Stack().Msg(err.Error())
resError = WrapError(err)
return
}
Expand All @@ -86,7 +86,7 @@ func SignInputs(dt *buxmodels.DraftTransaction, xPriv *bip32.ExtendedKey) (signe
if privateKey, err = bitcoin.GetPrivateKeyFromHDKey(
numKey,
); err != nil {
Log.Error().Err(err).Str("authentication", "SignInputs").Msg(err.Error())
Log.Error().Stack().Msg(err.Error())
resError = WrapError(err)
return
}
Expand All @@ -96,7 +96,7 @@ func SignInputs(dt *buxmodels.DraftTransaction, xPriv *bip32.ExtendedKey) (signe
if s, err = GetUnlockingScript(
txDraft, uint32(index), privateKey,
); err != nil {
Log.Error().Err(err).Str("authentication", "SignInputs").Msg(err.Error())
Log.Error().Stack().Msg(err.Error())
resError = WrapError(err)
return
}
Expand All @@ -105,7 +105,7 @@ func SignInputs(dt *buxmodels.DraftTransaction, xPriv *bip32.ExtendedKey) (signe
if err = txDraft.InsertInputUnlockingScript(
uint32(index), s,
); err != nil {
Log.Error().Err(err).Str("authentication", "SignInputs").Msg(err.Error())
Log.Error().Stack().Msg(err.Error())
resError = WrapError(err)
return
}
Expand All @@ -122,13 +122,13 @@ func GetUnlockingScript(tx *bt.Tx, inputIndex uint32, privateKey *bec.PrivateKey

sigHash, err := tx.CalcInputSignatureHash(inputIndex, sigHashFlags)
if err != nil {
Log.Error().Err(err).Str("authentication", "GetUnlockingScript").Msg(err.Error())
Log.Error().Stack().Msg(err.Error())
return nil, err
}

var sig *bec.Signature
if sig, err = privateKey.Sign(sigHash); err != nil {
Log.Error().Err(err).Str("authentication", "GetUnlockingScript").Msg(err.Error())
Log.Error().Stack().Msg(err.Error())
return nil, err
}

Expand All @@ -137,6 +137,7 @@ func GetUnlockingScript(tx *bt.Tx, inputIndex uint32, privateKey *bec.PrivateKey

var script *bscript.Script
if script, err = bscript.NewP2PKHUnlockingScript(pubKey, signature, sigHashFlags); err != nil {
Log.Error().Stack().Msg(err.Error())
return nil, err
}

Expand All @@ -147,7 +148,7 @@ func GetUnlockingScript(tx *bt.Tx, inputIndex uint32, privateKey *bec.PrivateKey
func createSignature(xPriv *bip32.ExtendedKey, bodyString string) (payload *buxmodels.AuthPayload, err error) {
// No key?
if xPriv == nil {
Log.Error().Err(err).Str("authentication", "createSignature").Msg(err.Error())
Log.Error().Stack().Msg(err.Error())
err = buxerrors.ErrMissingXPriv
return
}
Expand All @@ -157,14 +158,14 @@ func createSignature(xPriv *bip32.ExtendedKey, bodyString string) (payload *buxm
if payload.XPub, err = bitcoin.GetExtendedPublicKey(
xPriv,
); err != nil { // Should never error if key is correct
Log.Error().Err(err).Str("authentication", "createSignature").Msg(err.Error())
Log.Error().Stack().Msg(err.Error())
return
}

// auth_nonce is a random unique string to seed the signing message
// this can be checked server side to make sure the request is not being replayed
if payload.AuthNonce, err = utils.RandomHex(32); err != nil { // Should never error if key is correct
Log.Error().Err(err).Str("authentication", "createSignature").Msg(err.Error())
Log.Error().Stack().Msg(err.Error())
return
}

Expand All @@ -173,12 +174,13 @@ func createSignature(xPriv *bip32.ExtendedKey, bodyString string) (payload *buxm
if key, err = utils.DeriveChildKeyFromHex(
xPriv, payload.AuthNonce,
); err != nil {
Log.Error().Err(err).Str("authentication", "createSignature").Msg(err.Error())
Log.Error().Stack().Msg(err.Error())
return
}

var privateKey *bec.PrivateKey
if privateKey, err = bitcoin.GetPrivateKeyFromHDKey(key); err != nil {
Log.Error().Stack().Msg(err.Error())
return // Should never error if key is correct
}

Expand All @@ -205,7 +207,7 @@ func createSignatureCommon(payload *buxmodels.AuthPayload, bodyString string, pr
getSigningMessage(key, payload),
true,
); err != nil {
Log.Error().Err(err).Str("authentication", "createSignatureCommon").Msg(err.Error())
Log.Error().Stack().Msg(err.Error())
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion transports/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func WrapResponseError(res *http.Response) ResponseError {
if err != nil {
// if EOF, then body is empty and we return response status as error message
if !errors.Is(err, io.EOF) {
Log.Error().Err(err).Str("errors", "WrapResponseError").Msg(err.Error())
Log.Error().Stack().Msg(err.Error())
errorMsg = fmt.Sprintf("bux-server error message can't be decoded. Reason: %s", err.Error())
}
errorMsg = res.Status
Expand Down
Loading

0 comments on commit 877aee3

Please sign in to comment.