feat(btc): add witness utils

Co-authored-by: Gaze <dev@gaze.network>
This commit is contained in:
Gaze
2024-04-17 17:34:34 +07:00
parent d316950098
commit 996676e76e

59
pkg/btcutils/witness.go Normal file
View File

@@ -0,0 +1,59 @@
package btcutils
import (
"encoding/hex"
"strings"
"github.com/btcsuite/btcd/wire"
"github.com/cockroachdb/errors"
)
const (
witnessSeparator = " "
)
// WitnessToHex formats the passed witness stack as a slice of hex-encoded strings.
func WitnessToHex(witness wire.TxWitness) []string {
if len(witness) == 0 {
return nil
}
result := make([]string, 0, len(witness))
for _, wit := range witness {
result = append(result, hex.EncodeToString(wit))
}
return result
}
// WitnessToString formats the passed witness stack as a space-separated string of hex-encoded strings.
func WitnessToString(witness wire.TxWitness) string {
w := WitnessToHex(witness)
if w == nil {
return ""
}
return strings.Join(w, witnessSeparator)
}
// WitnessFromHex parses the passed slice of hex-encoded strings into a witness stack.
func WitnessFromHex(witnesses []string) (wire.TxWitness, error) {
if len(witnesses) == 0 {
return nil, nil
}
result := make(wire.TxWitness, 0, len(witnesses))
for _, wit := range witnesses {
decoded, err := hex.DecodeString(wit)
if err != nil {
return nil, errors.WithStack(err)
}
result = append(result, decoded)
}
return result, nil
}
// WitnessFromString parses the passed space-separated string of hex-encoded strings into a witness stack.
func WitnessFromString(witnesses string) (wire.TxWitness, error) {
return WitnessFromHex(strings.Split(witnesses, witnessSeparator))
}