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

feat(SPV-898): replaces with go sdk #731

Merged
merged 27 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
5376097
feat(SPV-898): replacements for old structures using new sdk-kit
wregulski Oct 9, 2024
a838b29
feat(SPV-898): part 2 replace structures to new sdk kit
wregulski Oct 9, 2024
83c6dd4
feat(SPV-898): remove libsv references completely
wregulski Oct 9, 2024
acca77a
Merge branch 'main' into feat/spv-898-replaces-with-go-sdk
wregulski Oct 9, 2024
c04e0ce
Merge branch 'main' into feat/spv-898-replaces-with-go-sdk
wregulski Oct 12, 2024
5a7d45a
fix(SPV-898): move fee unit to a bsv package
wregulski Oct 12, 2024
18ef98d
fix(SPV-898): replace utils package with bsv one on fee unit model
wregulski Oct 12, 2024
2c9cf70
fix(SPV-898): resolve linter issues
wregulski Oct 12, 2024
299e532
fix(SPV-898): resolve pending errors after feeunit move
wregulski Oct 12, 2024
4d84659
fix(SPV-898): avoid ineffectual assignment to err (ineffassign)
wregulski Oct 12, 2024
9d6cd36
fix(SPV-898): wrap errors in decrypt func in a correct way
wregulski Oct 12, 2024
f21a2ae
temp(SPV-XXX): temporarily comment out failing tests
wregulski Oct 12, 2024
27c5027
temp(SPV-898): temporarily comment out failing test
wregulski Oct 12, 2024
b989e1a
fix(SPV-898): fixing failing tests
wregulski Oct 14, 2024
1b8f7c3
rev(SPV-898): revert skipped test
wregulski Oct 14, 2024
10df9dc
chore(SPV-898): apply gci on action_transaction_test.go file
wregulski Oct 14, 2024
5de22f9
chore(SPV-898): organize imports to match gci convention
wregulski Oct 14, 2024
583f290
fix(SPV-898): organize imports
wregulski Oct 14, 2024
4c9a72b
fix(SPV-898): proper order gci
wregulski Oct 14, 2024
2955601
fix(SPV-898): codebase improvments - simplifications
wregulski Oct 14, 2024
c7b88cf
fix(SPV-898): wrap external errors with local wrapper
wregulski Oct 14, 2024
0165155
fix(SPV-898): gci-ed engine/paymail_service_provider.go file
wregulski Oct 14, 2024
cfe4101
fix(SPV-898): return proper type
wregulski Oct 14, 2024
0553af8
Merge branch 'main' into feat/spv-898-replaces-with-go-sdk
wregulski Oct 15, 2024
86d8b2b
fix(SPV-898): code review fixes
wregulski Oct 15, 2024
5fcc8b4
Update engine/beef_tx_sorting.go
wregulski Oct 15, 2024
810dc04
fix(SPV-898): replacements for better performance
wregulski Oct 15, 2024
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
15 changes: 0 additions & 15 deletions config/config_to_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ func (c *AppConfig) ToEngineOptions(logger zerolog.Logger) (options []engine.Cli
return nil, err
}

options = c.addFeeQuotes(options)

return options, nil
}

Expand All @@ -71,19 +69,6 @@ func (c *AppConfig) addHttpClientOpts(options []engine.ClientOps) []engine.Clien
return append(options, engine.WithHTTPClient(client))
}

func (c *AppConfig) addFeeQuotes(options []engine.ClientOps) []engine.ClientOps {
options = append(options, engine.WithFeeQuotes(c.ARC.UseFeeQuotes))

if c.ARC.FeeUnit != nil {
options = append(options, engine.WithFeeUnit(&utils.FeeUnit{
Satoshis: c.ARC.FeeUnit.Satoshis,
Bytes: c.ARC.FeeUnit.Bytes,
}))
}

return options
}

func (c *AppConfig) addUserAgentOpts(options []engine.ClientOps) []engine.ClientOps {
return append(options, engine.WithUserAgent(c.GetUserAgent()))
}
Expand Down
13 changes: 6 additions & 7 deletions engine/action_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import (
"math"
"time"

trx "github.com/bitcoin-sv/go-sdk/transaction"
chainmodels "github.com/bitcoin-sv/spv-wallet/engine/chain/models"
"github.com/bitcoin-sv/spv-wallet/engine/datastore"
"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
"github.com/bitcoin-sv/spv-wallet/engine/utils"
"github.com/libsv/go-bc"
"github.com/libsv/go-bt/v2"
)

// RecordTransaction will parse the outgoing transaction and save it into the Datastore
Expand All @@ -19,8 +18,7 @@ import (
// draftID is the unique draft id from a previously started New() transaction (draft_transaction.ID)
// opts are model options and can include "metadata"
func (c *Client) RecordTransaction(ctx context.Context, xPubKey, txHex, draftID string, opts ...ModelOps) (*Transaction, error) {

tx, err := bt.NewTxFromString(txHex)
tx, err := trx.NewTransactionFromHex(txHex)
if err != nil {
return nil, spverrors.ErrInvalidHex
}
Expand Down Expand Up @@ -122,12 +120,12 @@ func (c *Client) GetTransactionsByIDs(ctx context.Context, txIDs []string) ([]*T
// GetTransactionByHex will get a transaction from the Datastore by its full hex string
// uses GetTransaction
func (c *Client) GetTransactionByHex(ctx context.Context, hex string) (*Transaction, error) {
tx, err := bt.NewTxFromString(hex)
tx, err := trx.NewTransactionFromHex(hex)
if err != nil {
return nil, spverrors.Wrapf(err, "failed to parse transaction hex: %s", hex)
}

return c.GetTransaction(ctx, "", tx.TxID())
return c.GetTransaction(ctx, "", tx.TxID().String())
}

// GetTransactions will get all the transactions from the Datastore
Expand Down Expand Up @@ -346,7 +344,8 @@ func (c *Client) RevertTransaction(ctx context.Context, id string) error {
// HandleTxCallback will update the broadcast callback transaction info, like: block height, block hash, status, bump.
func (c *Client) HandleTxCallback(ctx context.Context, callbackResp *chainmodels.TXInfo) error {
logger := c.options.logger
bump, err := bc.NewBUMPFromStr(callbackResp.MerklePath)
bump, err := trx.NewMerklePathFromHex(callbackResp.MerklePath)

if err != nil {
logger.Err(err).Msgf("failed to parse merkle path from broadcast callback - tx: %v", callbackResp)
return spverrors.Wrapf(err, "failed to parse merkle path from broadcast callback - tx: %v", callbackResp)
Expand Down
15 changes: 8 additions & 7 deletions engine/action_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"testing"

broadcast_client_mock "github.com/bitcoin-sv/go-broadcast-client/broadcast/broadcast-client-mock"
compat "github.com/bitcoin-sv/go-sdk/compat/bip32"
"github.com/bitcoin-sv/spv-wallet/engine/utils"
"github.com/libsv/go-bk/bip32"
"github.com/bitcoin-sv/spv-wallet/models/bsv"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -242,7 +243,7 @@ func Test_RecordTransaction(t *testing.T) {
})
}

func initRevertTransactionData(t *testing.T, clientOpts ...ClientOps) (context.Context, ClientInterface, *Transaction, *bip32.ExtendedKey, func()) {
func initRevertTransactionData(t *testing.T, clientOpts ...ClientOps) (context.Context, ClientInterface, *Transaction, *compat.ExtendedKey, func()) {
// this creates an xpub, destination and utxo
ctx, client, deferMe := initSimpleTestCase(t, clientOpts...)

Expand All @@ -269,8 +270,8 @@ func initRevertTransactionData(t *testing.T, clientOpts ...ClientOps) (context.C
err = draftTransaction.Save(ctx)
require.NoError(t, err)

var xPriv *bip32.ExtendedKey
xPriv, err = bip32.NewKeyFromString(testXPriv)
var xPriv *compat.ExtendedKey
xPriv, err = compat.NewKeyFromString(testXPriv)
require.NoError(t, err)

var hex string
Expand Down Expand Up @@ -335,8 +336,8 @@ func BenchmarkAction_Transaction_recordTransaction(b *testing.B) {
b.Fail()
}

var xPriv *bip32.ExtendedKey
if xPriv, err = bip32.NewKeyFromString(testXPriv); err != nil {
var xPriv *compat.ExtendedKey
if xPriv, err = compat.NewKeyFromString(testXPriv); err != nil {
return
}

Expand Down Expand Up @@ -408,7 +409,7 @@ func initBenchmarkData(b *testing.B) (context.Context, ClientInterface, *Xpub, *
}

config := &TransactionConfig{
FeeUnit: &utils.FeeUnit{
FeeUnit: &bsv.FeeUnit{
Satoshis: 5,
Bytes: 100,
},
Expand Down
52 changes: 26 additions & 26 deletions engine/beef_bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"sort"

trx "github.com/bitcoin-sv/go-sdk/transaction"
"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
"github.com/libsv/go-bt/v2"
)

func calculateMergedBUMP(txs []*Transaction) (BUMPs, error) {
Expand Down Expand Up @@ -55,8 +55,8 @@ func validateBumps(bumps BUMPs) error {
return nil
}

func prepareBEEFFactors(ctx context.Context, tx *Transaction, store TransactionGetter) ([]*bt.Tx, []*Transaction, error) {
btTxsNeededForBUMP, txsNeededForBUMP, err := initializeRequiredTxsCollection(tx)
func prepareBEEFFactors(ctx context.Context, tx *Transaction, store TransactionGetter) ([]*trx.Transaction, []*Transaction, error) {
sdkTxsNeededForBUMP, txsNeededForBUMP, err := initializeRequiredTxsCollection(tx)
if err != nil {
return nil, nil, err
}
Expand All @@ -72,32 +72,32 @@ func prepareBEEFFactors(ctx context.Context, tx *Transaction, store TransactionG
}

for _, inputTx := range inputTxs {
inputBtTx, err := bt.NewTxFromString(inputTx.Hex)
inputSDKTx, err := trx.NewTransactionFromHex(inputTx.Hex)
if err != nil {
return nil, nil, spverrors.Wrapf(err, "cannot convert to bt.Tx from hex (tx.ID: %s)", inputTx.ID)
return nil, nil, spverrors.Wrapf(err, "cannot convert to SDK Tx from hex (tx.ID: %s)", inputTx.ID)
}

txsNeededForBUMP = append(txsNeededForBUMP, inputTx)
btTxsNeededForBUMP = append(btTxsNeededForBUMP, inputBtTx)
sdkTxsNeededForBUMP = append(sdkTxsNeededForBUMP, inputSDKTx)

if inputTx.BUMP.BlockHeight == 0 && len(inputTx.BUMP.Path) == 0 {
parentBtTransactions, parentTransactions, err := checkParentTransactions(ctx, store, inputBtTx)
parentSDKTransactions, parentTransactions, err := checkParentTransactions(ctx, store, inputSDKTx)
if err != nil {
return nil, nil, err
}

txsNeededForBUMP = append(txsNeededForBUMP, parentTransactions...)
btTxsNeededForBUMP = append(btTxsNeededForBUMP, parentBtTransactions...)
sdkTxsNeededForBUMP = append(sdkTxsNeededForBUMP, parentSDKTransactions...)
}
}

return btTxsNeededForBUMP, txsNeededForBUMP, nil
return sdkTxsNeededForBUMP, txsNeededForBUMP, nil
}

func checkParentTransactions(ctx context.Context, store TransactionGetter, btTx *bt.Tx) ([]*bt.Tx, []*Transaction, error) {
parentTxIDs := make([]string, 0, len(btTx.Inputs))
for _, txIn := range btTx.Inputs {
parentTxIDs = append(parentTxIDs, txIn.PreviousTxIDStr())
func checkParentTransactions(ctx context.Context, store TransactionGetter, sdkTx *trx.Transaction) ([]*trx.Transaction, []*Transaction, error) {
parentTxIDs := make([]string, 0, len(sdkTx.Inputs))
for _, txIn := range sdkTx.Inputs {
parentTxIDs = append(parentTxIDs, txIn.SourceTXID.String())
}

parentTxs, err := getRequiredTransactions(ctx, parentTxIDs, store)
Expand All @@ -106,26 +106,26 @@ func checkParentTransactions(ctx context.Context, store TransactionGetter, btTx
}

validTxs := make([]*Transaction, 0, len(parentTxs))
validBtTxs := make([]*bt.Tx, 0, len(parentTxs))
validSDKTxs := make([]*trx.Transaction, 0, len(parentTxs))
for _, parentTx := range parentTxs {
parentBtTx, err := bt.NewTxFromString(parentTx.Hex)
parentSDKTx, err := trx.NewTransactionFromHex(parentTx.Hex)
if err != nil {
return nil, nil, spverrors.Wrapf(err, "cannot convert to bt.Tx from hex (tx.ID: %s)", parentTx.ID)
return nil, nil, spverrors.Wrapf(err, "cannot convert to SDK Tx from hex (tx.ID: %s)", parentTx.ID)
}
validTxs = append(validTxs, parentTx)
validBtTxs = append(validBtTxs, parentBtTx)
validSDKTxs = append(validSDKTxs, parentSDKTx)

if parentTx.BUMP.BlockHeight == 0 && len(parentTx.BUMP.Path) == 0 {
parentValidBtTxs, parentValidTxs, err := checkParentTransactions(ctx, store, parentBtTx)
parentValidSDKTxs, parentValidTxs, err := checkParentTransactions(ctx, store, parentSDKTx)
if err != nil {
return nil, nil, err
}
validTxs = append(validTxs, parentValidTxs...)
validBtTxs = append(validBtTxs, parentValidBtTxs...)
validSDKTxs = append(validSDKTxs, parentValidSDKTxs...)
}
}

return validBtTxs, validTxs, nil
return validSDKTxs, validTxs, nil
}

func getRequiredTransactions(ctx context.Context, txIDs []string, store TransactionGetter) ([]*Transaction, error) {
Expand Down Expand Up @@ -157,17 +157,17 @@ func getMissingTxs(txIDs []string, foundTxs []*Transaction) []string {
return missingTxIDs
}

func initializeRequiredTxsCollection(tx *Transaction) ([]*bt.Tx, []*Transaction, error) {
var btTxsNeededForBUMP []*bt.Tx
func initializeRequiredTxsCollection(tx *Transaction) ([]*trx.Transaction, []*Transaction, error) {
var sdkTxsNeededForBUMP []*trx.Transaction
var txsNeededForBUMP []*Transaction

processedBtTx, err := bt.NewTxFromString(tx.Hex)
processedSDKTx, err := trx.NewTransactionFromHex(tx.Hex)
if err != nil {
return nil, nil, spverrors.Wrapf(err, "cannot convert processed tx to bt.Tx from hex (tx.ID: %s)", tx.ID)
return nil, nil, spverrors.Wrapf(err, "cannot convert processed tx to SDK Tx from hex (tx.ID: %s)", tx.ID)
}

btTxsNeededForBUMP = append(btTxsNeededForBUMP, processedBtTx)
sdkTxsNeededForBUMP = append(sdkTxsNeededForBUMP, processedSDKTx)
txsNeededForBUMP = append(txsNeededForBUMP, tx)

return btTxsNeededForBUMP, txsNeededForBUMP, nil
return sdkTxsNeededForBUMP, txsNeededForBUMP, nil
}
8 changes: 4 additions & 4 deletions engine/beef_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import (
"context"
"encoding/hex"

trx "github.com/bitcoin-sv/go-sdk/transaction"
"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
"github.com/libsv/go-bt/v2"
)

const maxBeefVer = uint32(0xFFFF) // value from BRC-62

type beefTx struct {
version uint32
bumps BUMPs
transactions []*bt.Tx
transactions []*trx.Transaction
}

// ToBeef generates BEEF Hex for transaction
Expand All @@ -40,7 +40,7 @@ func ToBeef(ctx context.Context, tx *Transaction, store TransactionGetter) (stri
return beefHex, nil
}

func toBeefHex(bumps BUMPs, parentTxs []*bt.Tx) (string, error) {
func toBeefHex(bumps BUMPs, parentTxs []*trx.Transaction) (string, error) {
beef, err := newBeefTx(1, bumps, parentTxs)
if err != nil {
return "", spverrors.Wrapf(err, "ToBeefHex() error")
Expand All @@ -54,7 +54,7 @@ func toBeefHex(bumps BUMPs, parentTxs []*bt.Tx) (string, error) {
return hex.EncodeToString(beefBytes), nil
}

func newBeefTx(version uint32, bumps BUMPs, parentTxs []*bt.Tx) (*beefTx, error) {
func newBeefTx(version uint32, bumps BUMPs, parentTxs []*trx.Transaction) (*beefTx, error) {
if version > maxBeefVer {
return nil, spverrors.Newf("version above 0x%X", maxBeefVer)
}
Expand Down
18 changes: 10 additions & 8 deletions engine/beef_tx_bytes.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package engine

import (
trx "github.com/bitcoin-sv/go-sdk/transaction"
"github.com/bitcoin-sv/go-sdk/util"
"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
"github.com/libsv/go-bt/v2"
)

var (
Expand All @@ -18,18 +19,18 @@ func (beefTx *beefTx) toBeefBytes() ([]byte, error) {
// get beef bytes
beefSize := 0

ver := bt.LittleEndianBytes(beefTx.version, 4)
ver := util.LittleEndianBytes(beefTx.version, 4)
ver[2] = 0xBE
ver[3] = 0xEF
beefSize += len(ver)

nBUMPS := bt.VarInt(len(beefTx.bumps)).Bytes()
nBUMPS := trx.VarInt(len(beefTx.bumps)).Bytes()
beefSize += len(nBUMPS)

bumps := beefTx.bumps.Bytes()
beefSize += len(bumps)

nTransactions := bt.VarInt(uint64(len(beefTx.transactions))).Bytes()
nTransactions := trx.VarInt(uint64(len(beefTx.transactions))).Bytes()
beefSize += len(nTransactions)

transactions := make([][]byte, 0, len(beefTx.transactions))
Expand All @@ -55,26 +56,27 @@ func (beefTx *beefTx) toBeefBytes() ([]byte, error) {
return buffer, nil
}

func toBeefBytes(tx *bt.Tx, bumps BUMPs) []byte {
func toBeefBytes(tx *trx.Transaction, bumps BUMPs) []byte {
wregulski marked this conversation as resolved.
Show resolved Hide resolved
txBeefBytes := tx.Bytes()

bumpIdx := getBumpPathIndex(tx, bumps)
if bumpIdx > -1 {
txBeefBytes = append(txBeefBytes, hasBUMP)
txBeefBytes = append(txBeefBytes, bt.VarInt(bumpIdx).Bytes()...)
txBeefBytes = append(txBeefBytes, trx.VarInt(bumpIdx).Bytes()...)
} else {
txBeefBytes = append(txBeefBytes, hasNoBUMP)
}

return txBeefBytes
}

func getBumpPathIndex(tx *bt.Tx, bumps BUMPs) int {
func getBumpPathIndex(tx *trx.Transaction, bumps BUMPs) int {
bumpIndex := -1
txID := tx.TxID().String()

for i, bump := range bumps {
for _, path := range bump.Path[0] {
if path.Hash == tx.TxID() {
if path.Hash == txID {
bumpIndex = i
}
}
Expand Down
Loading
Loading