-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support decode psbtHex & txHex detail.
- Loading branch information
1 parent
59c707e
commit 01f72a4
Showing
3 changed files
with
222 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
package btc | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/btcsuite/btcd/blockchain" | ||
"github.com/btcsuite/btcd/btcutil" | ||
"github.com/btcsuite/btcd/chaincfg" | ||
"github.com/btcsuite/btcd/txscript" | ||
"github.com/btcsuite/btcd/wire" | ||
"github.com/coming-chat/wallet-SDK/core/base" | ||
"github.com/coming-chat/wallet-SDK/core/base/inter" | ||
) | ||
|
||
type TxOut struct { | ||
Hash string `json:"hash,omitempty"` | ||
Index int64 `json:"index,omitempty"` | ||
|
||
Value int64 `json:"value,omitempty"` | ||
Address string `json:"address,omitempty"` | ||
} | ||
|
||
type TxOutArray struct { | ||
inter.AnyArray[*TxOut] | ||
} | ||
|
||
func (ta *TxOutArray) detailDesc() string { | ||
desc := "" | ||
for idx, out := range ta.AnyArray { | ||
if out.Address != "" { | ||
addr := out.Address | ||
if len(addr) > 13 { | ||
addr = addr[:5] + "..." + addr[len(addr)-5:] | ||
} | ||
valueBTC := btcutil.Amount(out.Value).String() | ||
if idx == 0 { | ||
desc += "\t" + addr + "\t" + valueBTC | ||
} else { | ||
desc += "\n\t" + addr + "\t" + valueBTC | ||
} | ||
} else { | ||
if idx == 0 { | ||
desc += "\thash: " + out.Hash + "\n" + "\tindex: " + strconv.FormatInt(out.Index, 10) | ||
} else { | ||
desc += "\n\thash: " + out.Hash + "\n" + "\tindex: " + strconv.FormatInt(out.Index, 10) | ||
} | ||
} | ||
} | ||
return desc | ||
} | ||
|
||
type TransactionDetail struct { | ||
NetworkFee int64 `json:"networkFee"` | ||
FeeRate float64 `json:"feeRate"` | ||
Inputs *TxOutArray `json:"inputs"` | ||
Outputs *TxOutArray `json:"outputs"` | ||
} | ||
|
||
func (td *TransactionDetail) JsonString() (*base.OptionalString, error) { | ||
return base.JsonString(td) | ||
} | ||
|
||
func (td *TransactionDetail) Desc() string { | ||
feeBTC := "" | ||
feeRate := "" | ||
if td.NetworkFee == 0 { | ||
feeBTC = "Unknow" | ||
feeRate = "Unknow" | ||
} else { | ||
feeBTC = btcutil.Amount(td.NetworkFee).String() | ||
feeRate = fmt.Sprintf("%.2f sat/vB", td.FeeRate) | ||
} | ||
|
||
return fmt.Sprintf(`Network Fee: | ||
%v | ||
Network FeeRate: | ||
%v | ||
Inputs: | ||
%v | ||
Outputs: | ||
%v`, feeBTC, feeRate, td.Inputs.detailDesc(), td.Outputs.detailDesc()) | ||
} | ||
|
||
func DecodePsbtTransactionDetail(psbtHex string, chainnet string) (d *TransactionDetail, err error) { | ||
chainParams, err := netParamsOf(chainnet) | ||
if err != nil { | ||
return | ||
} | ||
packet, err := DecodePsbtTxToPacket(psbtHex) | ||
if err != nil { | ||
return | ||
} | ||
txFee, err := packet.GetTxFee() | ||
if err != nil { | ||
return | ||
} | ||
feeFloat := txFee.ToUnit(btcutil.AmountSatoshi) | ||
vSize := virtualSize(packet.UnsignedTx) | ||
feeRate := feeFloat / float64(vSize) | ||
|
||
inputs := make([]*TxOut, len(packet.Inputs)) | ||
for idx, in := range packet.Inputs { | ||
switch { | ||
case in.WitnessUtxo != nil: | ||
inputs[idx], err = txOutFromWireTxOut(in.WitnessUtxo, chainParams) | ||
if err != nil { | ||
return | ||
} | ||
case in.NonWitnessUtxo != nil: | ||
utxOuts := in.NonWitnessUtxo.TxOut | ||
txIn := packet.UnsignedTx.TxIn[idx] | ||
opIdx := txIn.PreviousOutPoint.Index | ||
txOut := utxOuts[opIdx] | ||
inputs[idx], err = txOutFromWireTxOut(txOut, chainParams) | ||
if err != nil { | ||
return | ||
} | ||
default: | ||
return nil, fmt.Errorf("input %d has no UTXO information", | ||
idx) | ||
} | ||
} | ||
|
||
outputs := make([]*TxOut, len(packet.UnsignedTx.TxOut)) | ||
for idx, out := range packet.UnsignedTx.TxOut { | ||
outputs[idx], err = txOutFromWireTxOut(out, chainParams) | ||
if err != nil { | ||
return | ||
} | ||
} | ||
|
||
return &TransactionDetail{ | ||
NetworkFee: int64(feeFloat), | ||
FeeRate: float64(feeRate), | ||
Inputs: &TxOutArray{inputs}, | ||
Outputs: &TxOutArray{outputs}, | ||
}, nil | ||
} | ||
|
||
func DecodeTxHexTransactionDetail(txHex string, chainnet string) (detail *TransactionDetail, err error) { | ||
chainParams, err := netParamsOf(chainnet) | ||
if err != nil { | ||
return | ||
} | ||
msgTx, err := DecodeTx(txHex) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// msgTx cannot get `fee`, `feeRate` | ||
|
||
inputs := make([]*TxOut, len(msgTx.TxIn)) | ||
for idx, in := range msgTx.TxIn { | ||
inputs[idx] = &TxOut{ | ||
Hash: in.PreviousOutPoint.Hash.String(), | ||
Index: int64(in.PreviousOutPoint.Index), | ||
} | ||
} | ||
outputs := make([]*TxOut, len(msgTx.TxOut)) | ||
for idx, out := range msgTx.TxOut { | ||
outputs[idx], err = txOutFromWireTxOut(out, chainParams) | ||
if err != nil { | ||
return | ||
} | ||
} | ||
|
||
return &TransactionDetail{ | ||
NetworkFee: 0, | ||
FeeRate: 0, | ||
Inputs: &TxOutArray{inputs}, | ||
Outputs: &TxOutArray{outputs}, | ||
}, nil | ||
} | ||
|
||
func txOutFromWireTxOut(txout *wire.TxOut, params *chaincfg.Params) (*TxOut, error) { | ||
pkobj, err := txscript.ParsePkScript(txout.PkScript) | ||
if err != nil { | ||
return nil, err | ||
} | ||
addr, err := pkobj.Address(params) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &TxOut{ | ||
Value: txout.Value, | ||
Address: addr.EncodeAddress(), | ||
}, nil | ||
} | ||
|
||
// calculation reference: | ||
// https://github.com/btcsuite/btcd/blob/569155bc6a502f45b4a514bc6b9d5f814a980b6c/mempool/policy.go#L382 | ||
func virtualSize(tx *wire.MsgTx) int64 { | ||
// vSize := (((baseSize * 3) + totalSize) + 3) / 4 | ||
baseSize := int64(tx.SerializeSizeStripped()) | ||
totalSize := int64(tx.SerializeSize()) | ||
weight := (baseSize * (blockchain.WitnessScaleFactor - 1)) + totalSize | ||
return (weight + blockchain.WitnessScaleFactor - 1) / blockchain.WitnessScaleFactor | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters