Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #378 from vitalibalashka/feat/delete_import_xpub
Browse files Browse the repository at this point in the history
feat(BUX-179): Remove functionality of importing xpubs
  • Loading branch information
mergify[bot] authored Sep 4, 2023
2 parents 994a260 + 071d7a1 commit 7567cd0
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 349 deletions.
156 changes: 0 additions & 156 deletions action_xpub.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ package bux

import (
"context"
"sort"

"github.com/BuxOrg/bux/utils"
"github.com/libsv/go-bt"
"github.com/mrz1836/go-datastore"
"github.com/mrz1836/go-whatsonchain"
)

// NewXpub will parse the xPub and save it into the Datastore
Expand Down Expand Up @@ -95,158 +91,6 @@ func (c *Client) UpdateXpubMetadata(ctx context.Context, xPubID string, metadata
return xPub, nil
}

// ImportXpub will import a given xPub and all related destinations and transactions
//
// xPubKey is the raw public xPub
func (c *Client) ImportXpub(ctx context.Context, xPubKey string, opts ...ModelOps) (*ImportResults, error) {

// Validate the xPub
xPub, err := utils.ValidateXPub(xPubKey)
if err != nil {
return nil, err
}

// Start an accumulator
results := &ImportResults{Key: xPub.String()}

// Set the WOC client
woc := c.Chainstate().WhatsOnChain()
var allTransactions []*whatsonchain.HistoryRecord

// Assume gap of 10 addresses, if no txs are found for 10
maxGapLimit := 10

// internal/external derivations, assume we've found everything
gapHit := false
for !gapHit {

// Derive internal addresses until depth
c.Logger().Info(ctx, "Deriving internal addresses...")
addressList := whatsonchain.AddressList{}
var addresses []string
if addresses, err = c.deriveAddresses(
ctx, xPub.String(), utils.ChainInternal, maxGapLimit,
); err != nil {
return nil, err
}
results.InternalAddresses += maxGapLimit
addressList.Addresses = append(addressList.Addresses, addresses...)

// Derive external addresses until gap limit
c.Logger().Info(ctx, "Deriving external addresses...")
if addresses, err = c.deriveAddresses(
ctx, xPub.String(), utils.ChainExternal, maxGapLimit,
); err != nil {
return nil, err
}
results.ExternalAddresses += maxGapLimit
addressList.Addresses = append(addressList.Addresses, addresses...)

// Get all transactions for those addresses
/*transactions, addressesWithTransactions, err := getAllTransactionsFromAddresses(
ctx, woc, addressList,
)*/
var transactions []*whatsonchain.HistoryRecord
transactions, err = getTransactionsFromAddressesViaBitbus(addressList.Addresses)
if err != nil {
return nil, err
}
if len(transactions) == 0 {
gapHit = true
}
// results.AddressesWithTransactions = append(results.AddressesWithTransactions, addressesWithTransactions...)
allTransactions = append(allTransactions, transactions...)
}
// Remove any duplicate transactions from all historical txs
allTransactions = removeDuplicates(allTransactions)

// Set all the hashes
txHashes := whatsonchain.TxHashes{}
for _, t := range allTransactions {
txHashes.TxIDs = append(txHashes.TxIDs, t.TxHash)
results.TransactionsFound++
}

// Run the bulk transaction processor
var rawTxs []string
var txInfos whatsonchain.TxList
if txInfos, err = woc.BulkRawTransactionDataProcessor(
ctx, &txHashes,
); err != nil {
return nil, err
}

// Loop and build from the inputs
var tx *bt.Tx
for i := 0; i < len(txInfos); i++ {
if tx, err = bt.NewTxFromString(
txInfos[i].Hex,
); err != nil {
return nil, err
}
var inputs []whatsonchain.VinInfo
for _, in := range tx.Inputs {
// todo: upgrade and use go-bt v2
vin := whatsonchain.VinInfo{
TxID: in.PreviousTxID,
}
inputs = append(inputs, vin)
}
txInfos[i].Vin = inputs
rawTxs = append(rawTxs, txInfos[i].Hex)
}

// Sort all transactions by block height
c.Logger().Info(ctx, "Sorting transactions to be recorded...")
sort.Slice(txInfos, func(i, j int) bool {
return txInfos[i].BlockHeight < txInfos[j].BlockHeight
})

// Sort transactions that are in the same block by previous tx
for i := 0; i < len(txInfos); i++ {
info := txInfos[i]
bh := info.BlockHeight
var sameBlockTxs []*whatsonchain.TxInfo
sameBlockTxs = append(sameBlockTxs, info)

// Loop through all remaining txs until block height is not the same
for j := i + 1; j < len(txInfos); j++ {
if txInfos[j].BlockHeight == bh {
sameBlockTxs = append(sameBlockTxs, txInfos[j])
} else {
break
}
}
if len(sameBlockTxs) == 1 {
continue
}

// Sort transactions by whether previous txs are referenced in the inputs
sort.Slice(sameBlockTxs, func(i, j int) bool {
for _, in := range sameBlockTxs[i].Vin {
if in.TxID == sameBlockTxs[j].Hash {
return false
}
}
return true
})
copy(txInfos[i:i+len(sameBlockTxs)], sameBlockTxs)
i += len(sameBlockTxs) - 1
}

// Record transactions in bux
for _, rawTx := range rawTxs {
if _, err = c.RecordTransaction(
ctx, xPubKey, rawTx, "", opts...,
); err != nil {
return nil, err
}
results.TransactionsImported++
}

return results, nil
}

// GetXPubs gets all xpubs matching the conditions
func (c *Client) GetXPubs(ctx context.Context, metadataConditions *Metadata,
conditions *map[string]interface{}, queryParams *datastore.QueryParams, opts ...ModelOps) ([]*Xpub, error) {
Expand Down
192 changes: 0 additions & 192 deletions importer/xpub.go

This file was deleted.

1 change: 0 additions & 1 deletion interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ type UTXOService interface {
type XPubService interface {
GetXpub(ctx context.Context, xPubKey string) (*Xpub, error)
GetXpubByID(ctx context.Context, xPubID string) (*Xpub, error)
ImportXpub(ctx context.Context, xPubKey string, opts ...ModelOps) (*ImportResults, error)
NewXpub(ctx context.Context, xPubKey string, opts ...ModelOps) (*Xpub, error)
UpdateXpubMetadata(ctx context.Context, xPubID string, metadata Metadata) (*Xpub, error)
}
Expand Down

0 comments on commit 7567cd0

Please sign in to comment.