mirror of
https://github.com/btcsuite/btcd.git
synced 2025-01-19 05:33:36 +01:00
wire: add string parser to outpoint
This commit is contained in:
parent
0f49e10006
commit
571f9c69ec
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user