Skip to content

Commit

Permalink
wire: make witnessToHex a TxWitness method called ToHexStrings
Browse files Browse the repository at this point in the history
  • Loading branch information
ffranr committed Mar 25, 2024
1 parent e39d2eb commit c5de509
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
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
18 changes: 18 additions & 0 deletions wire/msgtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package wire

import (
"bytes"

Check failure on line 8 in wire/msgtx.go

View workflow job for this annotation

GitHub Actions / Build

imported and not used: "bytes"

Check failure on line 8 in wire/msgtx.go

View workflow job for this annotation

GitHub Actions / Unit race

imported and not used: "bytes"

Check failure on line 8 in wire/msgtx.go

View workflow job for this annotation

GitHub Actions / Unit race

imported and not used: "bytes"
"encoding/hex"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -302,6 +304,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

0 comments on commit c5de509

Please sign in to comment.