Skip to content

Commit

Permalink
move GetSenderAddressByVin to inbound.go as it's for inbound observation
Browse files Browse the repository at this point in the history
  • Loading branch information
ws4charlie committed Sep 19, 2024
1 parent 7fa12b9 commit 0e2a316
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 45 deletions.
42 changes: 42 additions & 0 deletions zetaclient/chains/bitcoin/observer/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,3 +497,45 @@ func GetBtcEvent(
}
return nil, nil
}

// GetSenderAddressByVin get the sender address from the previous transaction
func GetSenderAddressByVin(rpcClient interfaces.BTCRPCClient, vin btcjson.Vin, net *chaincfg.Params) (string, error) {
// query previous raw transaction by txid
hash, err := chainhash.NewHashFromStr(vin.Txid)
if err != nil {
return "", err
}

// this requires running bitcoin node with 'txindex=1'
tx, err := rpcClient.GetRawTransaction(hash)
if err != nil {
return "", errors.Wrapf(err, "error getting raw transaction %s", vin.Txid)
}

// #nosec G115 - always in range
if len(tx.MsgTx().TxOut) <= int(vin.Vout) {
return "", fmt.Errorf("vout index %d out of range for tx %s", vin.Vout, vin.Txid)
}

// decode sender address from previous pkScript
pkScript := tx.MsgTx().TxOut[vin.Vout].PkScript
scriptHex := hex.EncodeToString(pkScript)
if bitcoin.IsPkScriptP2TR(pkScript) {
return bitcoin.DecodeScriptP2TR(scriptHex, net)
}
if bitcoin.IsPkScriptP2WSH(pkScript) {
return bitcoin.DecodeScriptP2WSH(scriptHex, net)
}
if bitcoin.IsPkScriptP2WPKH(pkScript) {
return bitcoin.DecodeScriptP2WPKH(scriptHex, net)
}
if bitcoin.IsPkScriptP2SH(pkScript) {
return bitcoin.DecodeScriptP2SH(scriptHex, net)
}
if bitcoin.IsPkScriptP2PKH(pkScript) {
return bitcoin.DecodeScriptP2PKH(scriptHex, net)
}

// sender address not found, return nil and move on to the next tx
return "", nil
}
45 changes: 0 additions & 45 deletions zetaclient/chains/bitcoin/observer/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package observer

import (
"context"
"encoding/hex"
"fmt"
"math"
"math/big"
Expand All @@ -12,7 +11,6 @@ import (

"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/pkg/errors"
Expand Down Expand Up @@ -315,49 +313,6 @@ func (ob *Observer) PostGasPrice(ctx context.Context) error {
return nil
}

// GetSenderAddressByVin get the sender address from the previous transaction
// TODO(revamp): move in upper package to separate file (e.g., rpc.go)
func GetSenderAddressByVin(rpcClient interfaces.BTCRPCClient, vin btcjson.Vin, net *chaincfg.Params) (string, error) {
// query previous raw transaction by txid
hash, err := chainhash.NewHashFromStr(vin.Txid)
if err != nil {
return "", err
}

// this requires running bitcoin node with 'txindex=1'
tx, err := rpcClient.GetRawTransaction(hash)
if err != nil {
return "", errors.Wrapf(err, "error getting raw transaction %s", vin.Txid)
}

// #nosec G115 - always in range
if len(tx.MsgTx().TxOut) <= int(vin.Vout) {
return "", fmt.Errorf("vout index %d out of range for tx %s", vin.Vout, vin.Txid)
}

// decode sender address from previous pkScript
pkScript := tx.MsgTx().TxOut[vin.Vout].PkScript
scriptHex := hex.EncodeToString(pkScript)
if bitcoin.IsPkScriptP2TR(pkScript) {
return bitcoin.DecodeScriptP2TR(scriptHex, net)
}
if bitcoin.IsPkScriptP2WSH(pkScript) {
return bitcoin.DecodeScriptP2WSH(scriptHex, net)
}
if bitcoin.IsPkScriptP2WPKH(pkScript) {
return bitcoin.DecodeScriptP2WPKH(scriptHex, net)
}
if bitcoin.IsPkScriptP2SH(pkScript) {
return bitcoin.DecodeScriptP2SH(scriptHex, net)
}
if bitcoin.IsPkScriptP2PKH(pkScript) {
return bitcoin.DecodeScriptP2PKH(scriptHex, net)
}

// sender address not found, return nil and move on to the next tx
return "", nil
}

// WatchUTXOs watches bitcoin chain for UTXOs owned by the TSS address
// TODO(revamp): move ticker related functions to a specific file
func (ob *Observer) WatchUTXOs(ctx context.Context) error {
Expand Down

0 comments on commit 0e2a316

Please sign in to comment.