claim: return error for invalid ID string

This commit is contained in:
Tzu-Jung Lee 2018-08-14 19:42:45 -07:00
parent 38ec2ba48c
commit d06dc8d547

View file

@ -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]