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

Convert witnessToHex into a method ToHexStrings on TxWitness #1991

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 3 additions & 20 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,23 +646,6 @@ func handleDebugLevel(s *rpcServer, cmd interface{}, closeChan <-chan struct{})
return "Done.", nil
}

// witnessToHex formats the passed witness stack as a slice of hex-encoded
// strings to be used in a JSON response.
func witnessToHex(witness wire.TxWitness) []string {
// Ensure nil is returned when there are no entries versus an empty
// slice so it can properly be omitted as necessary.
if len(witness) == 0 {
return nil
}

result := make([]string, 0, len(witness))
for _, wit := range witness {
result = append(result, hex.EncodeToString(wit))
}

return result
}

// createVinList returns a slice of JSON objects for the inputs of the passed
// transaction.
func createVinList(mtx *wire.MsgTx) []btcjson.Vin {
Expand All @@ -672,7 +655,7 @@ func createVinList(mtx *wire.MsgTx) []btcjson.Vin {
txIn := mtx.TxIn[0]
vinList[0].Coinbase = hex.EncodeToString(txIn.SignatureScript)
vinList[0].Sequence = txIn.Sequence
vinList[0].Witness = witnessToHex(txIn.Witness)
vinList[0].Witness = txIn.Witness.ToHexStrings()
return vinList
}

Expand All @@ -692,7 +675,7 @@ func createVinList(mtx *wire.MsgTx) []btcjson.Vin {
}

if mtx.HasWitness() {
vinEntry.Witness = witnessToHex(txIn.Witness)
vinEntry.Witness = txIn.Witness.ToHexStrings()
}
}

Expand Down Expand Up @@ -3052,7 +3035,7 @@ func createVinListPrevOut(s *rpcServer, mtx *wire.MsgTx, chainParams *chaincfg.P
}

if len(txIn.Witness) != 0 {
vinEntry.Witness = witnessToHex(txIn.Witness)
vinEntry.Witness = txIn.Witness.ToHexStrings()
}

// Add the entry to the list now if it already passed the filter
Expand Down
17 changes: 17 additions & 0 deletions wire/msgtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package wire

import (
"encoding/hex"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -302,6 +303,22 @@ func (t TxWitness) SerializeSize() int {
return n
}

// ToHexStrings formats the witness stack as a slice of hex-encoded strings.
func (t TxWitness) ToHexStrings() []string {
// Ensure nil is returned when there are no entries versus an empty
// slice so it can properly be omitted as necessary.
if len(t) == 0 {
return nil
}

result := make([]string, len(t))
for idx, wit := range t {
result[idx] = hex.EncodeToString(wit)
}

return result
}

// TxOut defines a bitcoin transaction output.
type TxOut struct {
Value int64
Expand Down
Loading