@@ -2,6 +2,7 @@ package lsat
2
2
3
3
import (
4
4
"encoding/binary"
5
+ "encoding/hex"
5
6
"errors"
6
7
"fmt"
7
8
"io"
32
33
ErrUnknownVersion = errors .New ("unknown LSAT version" )
33
34
)
34
35
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
+
35
61
// Identifier contains the static identifying details of an LSAT. This is
36
62
// intended to be used as the identifier of the macaroon within an LSAT.
37
63
type Identifier struct {
@@ -46,7 +72,7 @@ type Identifier struct {
46
72
PaymentHash lntypes.Hash
47
73
48
74
// TokenID is the unique identifier of an LSAT.
49
- TokenID [ TokenIDSize ] byte
75
+ TokenID TokenID
50
76
}
51
77
52
78
// EncodeIdentifier encodes an LSAT's identifier according to its version.
@@ -85,7 +111,7 @@ func DecodeIdentifier(r io.Reader) (*Identifier, error) {
85
111
if _ , err := r .Read (paymentHash [:]); err != nil {
86
112
return nil , err
87
113
}
88
- var tokenID [ TokenIDSize ] byte
114
+ var tokenID TokenID
89
115
if _ , err := r .Read (tokenID [:]); err != nil {
90
116
return nil , err
91
117
}
0 commit comments