claim: return error for invalid ID string
This commit is contained in:
parent
38ec2ba48c
commit
d06dc8d547
1 changed files with 7 additions and 0 deletions
|
@ -4,10 +4,14 @@ import (
|
|||
"bytes"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
|
||||
"github.com/btcsuite/btcutil"
|
||||
)
|
||||
|
||||
// ErrInvalidID is returned when the ID does not conform to the format.
|
||||
var ErrInvalidID = errors.New("ID must be a 20-character hexadecimal string")
|
||||
|
||||
// NewID returns a Claim ID caclculated from Ripemd160(Sha256(OUTPOINT).
|
||||
func NewID(op OutPoint) ID {
|
||||
w := bytes.NewBuffer(op.Hash[:])
|
||||
|
@ -22,6 +26,9 @@ func NewID(op OutPoint) ID {
|
|||
// NewIDFromString returns a Claim ID from a string.
|
||||
func NewIDFromString(s string) (ID, error) {
|
||||
var id ID
|
||||
if len(s) != 40 {
|
||||
return id, ErrInvalidID
|
||||
}
|
||||
_, err := hex.Decode(id[:], []byte(s))
|
||||
for i, j := 0, len(id)-1; i < j; i, j = i+1, j-1 {
|
||||
id[i], id[j] = id[j], id[i]
|
||||
|
|
Loading…
Reference in a new issue