wire: add string parser to outpoint

This commit is contained in:
George Tsagkarelis 2022-08-16 13:33:58 +03:00
parent 0f49e10006
commit 571f9c69ec
No known key found for this signature in database
GPG Key ID: 0807D1013F48208A

View File

@ -6,9 +6,11 @@ package wire
import (
"bytes"
"errors"
"fmt"
"io"
"strconv"
"strings"
"github.com/btcsuite/btcd/chaincfg/chainhash"
)
@ -215,6 +217,29 @@ func NewOutPoint(hash *chainhash.Hash, index uint32) *OutPoint {
}
}
// NewOutPointFromString returns a new bitcoin transaction outpoint parsed from
// the provided string, which should be in the format "hash:index".
func NewOutPointFromString(outpoint string) (*OutPoint, error) {
parts := strings.Split(outpoint, ":")
if len(parts) != 2 {
return nil, errors.New("outpoint should be of the form txid:index")
}
hash, err := chainhash.NewHashFromStr(parts[0])
if err != nil {
return nil, err
}
outputIndex, err := strconv.ParseUint(parts[1], 10, 32)
if err != nil {
return nil, fmt.Errorf("invalid output index: %v", err)
}
return &OutPoint{
Hash: *hash,
Index: uint32(outputIndex),
}, nil
}
// String returns the OutPoint in the human-readable form "hash:index".
func (o OutPoint) String() string {
// Allocate enough for hash string, colon, and 10 digits. Although