Skip to content

Commit a9033ba

Browse files
authored
Merge pull request #116 from guggero/token-id
lsat: add token ID type
2 parents 5f8c876 + 9d89244 commit a9033ba

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

lsat/identifier.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package lsat
22

33
import (
44
"encoding/binary"
5+
"encoding/hex"
56
"errors"
67
"fmt"
78
"io"
@@ -32,6 +33,31 @@ var (
3233
ErrUnknownVersion = errors.New("unknown LSAT version")
3334
)
3435

36+
// TokenID is the type that stores the token identifier of an LSAT token.
37+
type TokenID [TokenIDSize]byte
38+
39+
// String returns the hex encoded representation of the token ID as a string.
40+
func (t *TokenID) String() string {
41+
return hex.EncodeToString(t[:])
42+
}
43+
44+
// MakeIDFromString parses the hex encoded string and parses it into a token ID.
45+
func MakeIDFromString(newID string) (TokenID, error) {
46+
if len(newID) != hex.EncodedLen(TokenIDSize) {
47+
return TokenID{}, fmt.Errorf("invalid id string length of %v, "+
48+
"want %v", len(newID), hex.EncodedLen(TokenIDSize))
49+
}
50+
51+
idBytes, err := hex.DecodeString(newID)
52+
if err != nil {
53+
return TokenID{}, err
54+
}
55+
var id TokenID
56+
copy(id[:], idBytes)
57+
58+
return id, nil
59+
}
60+
3561
// Identifier contains the static identifying details of an LSAT. This is
3662
// intended to be used as the identifier of the macaroon within an LSAT.
3763
type Identifier struct {
@@ -46,7 +72,7 @@ type Identifier struct {
4672
PaymentHash lntypes.Hash
4773

4874
// TokenID is the unique identifier of an LSAT.
49-
TokenID [TokenIDSize]byte
75+
TokenID TokenID
5076
}
5177

5278
// EncodeIdentifier encodes an LSAT's identifier according to its version.
@@ -85,7 +111,7 @@ func DecodeIdentifier(r io.Reader) (*Identifier, error) {
85111
if _, err := r.Read(paymentHash[:]); err != nil {
86112
return nil, err
87113
}
88-
var tokenID [TokenIDSize]byte
114+
var tokenID TokenID
89115
if _, err := r.Read(tokenID[:]); err != nil {
90116
return nil, err
91117
}

0 commit comments

Comments
 (0)