Skip to content

Commit

Permalink
wire: add TxWitness JSON (Un)marshal
Browse files Browse the repository at this point in the history
  • Loading branch information
ffranr committed Jun 5, 2023
1 parent 9c16d23 commit ce89bf6
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions wire/msgtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package wire

import (
"bytes"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -309,6 +311,36 @@ func (t TxWitness) SerializeSize() int {
return n
}

// MarshalJSON serialises the structure as a JSON appropriate value.
//
// NOTE: The 2D byte array is encoded as an array of hex strings.
func (t TxWitness) MarshalJSON() ([]byte, error) {
var hexStrings []string
for _, b := range t {
hexStrings = append(hexStrings, hex.EncodeToString(b))
}
return json.Marshal(hexStrings)
}

// UnmarshalJSON parses a JSON value as a TxWitness.
func (t *TxWitness) UnmarshalJSON(data []byte) error {
var hexStrings []string
if err := json.Unmarshal(data, &hexStrings); err != nil {
return err
}

*t = make([][]byte, len(hexStrings))
for i, hexString := range hexStrings {
decoded, err := hex.DecodeString(hexString)
if err != nil {
return err
}
(*t)[i] = decoded
}

return nil
}

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

0 comments on commit ce89bf6

Please sign in to comment.