Skip to content

Commit

Permalink
Merge pull request #1991 from ffranr/add-json-support
Browse files Browse the repository at this point in the history
Convert `witnessToHex` into a method `ToHexStrings` on `TxWitness`
  • Loading branch information
guggero authored Mar 26, 2024
2 parents 429d00f + 75fe7e4 commit a03259b
Show file tree
Hide file tree
Showing 2 changed files with 20 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
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

0 comments on commit a03259b

Please sign in to comment.