Skip to content

Commit

Permalink
Add sanity checks
Browse files Browse the repository at this point in the history
  • Loading branch information
altafan committed Jul 12, 2023
1 parent 33b2132 commit 517c3f9
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions internal/core/application/transaction_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,10 @@ func (ts *TransactionService) SignTransaction(
func (ts *TransactionService) BroadcastTransaction(
ctx context.Context, txHex string,
) (string, error) {
keys := utxoKeysFromRawTx(txHex)
keys, err := utxoKeysFromRawTx(txHex)
if err != nil {
return "", fmt.Errorf("invalid tx: %s", err)
}
utxos, err := ts.repoManager.UtxoRepository().GetUtxosByKey(ctx, keys)
if err != nil {
return "", err
Expand Down Expand Up @@ -807,9 +810,13 @@ func (ts *TransactionService) findLockedInputs(
rawTx, _ := transaction.NewTxFromHex(tx)
var keys = make([]domain.UtxoKey, 0)
if rawTx != nil {
keys = utxoKeysFromRawTx(tx)
keys, _ = utxoKeysFromRawTx(tx)
} else {
keys = utxoKeysFromPartialTx(tx)
var err error
keys, err = utxoKeysFromPartialTx(tx)
if err != nil {
return nil, fmt.Errorf("invalid partial transaction: %s", err)
}
}

utxos, err := ts.repoManager.UtxoRepository().GetUtxosByKey(ctx, keys)
Expand Down Expand Up @@ -894,28 +901,36 @@ func (ts *TransactionService) getExternalInputs(
return externalInputs, nil
}

func utxoKeysFromRawTx(txHex string) []domain.UtxoKey {
tx, _ := transaction.NewTxFromHex(txHex)
func utxoKeysFromRawTx(txHex string) ([]domain.UtxoKey, error) {
tx, err := transaction.NewTxFromHex(txHex)
if err != nil {
return nil, err
}

keys := make([]domain.UtxoKey, 0, len(tx.Inputs))
for _, in := range tx.Inputs {
keys = append(keys, domain.UtxoKey{
TxID: elementsutil.TxIDFromBytes(in.Hash),
VOut: in.Index,
})
}
return keys
return keys, nil
}

func utxoKeysFromPartialTx(psetBase64 string) []domain.UtxoKey {
tx, _ := psetv2.NewPsetFromBase64(psetBase64)
func utxoKeysFromPartialTx(psetBase64 string) ([]domain.UtxoKey, error) {
tx, err := psetv2.NewPsetFromBase64(psetBase64)
if err != nil {
return nil, err
}

keys := make([]domain.UtxoKey, 0, len(tx.Inputs))
for _, in := range tx.Inputs {
keys = append(keys, domain.UtxoKey{
TxID: elementsutil.TxIDFromBytes(in.PreviousTxid),
VOut: in.PreviousTxIndex,
})
}
return keys
return keys, nil
}

func findUtxoIndexInTx(tx string, utxo *domain.Utxo) uint32 {
Expand Down

0 comments on commit 517c3f9

Please sign in to comment.