From 9d892440fe8379e510d763c729cf8be4a3213f67 Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Thu, 28 Nov 2019 14:35:45 +0100 Subject: [PATCH] lsat: add token ID type --- lsat/identifier.go | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/lsat/identifier.go b/lsat/identifier.go index 99b0654..252540e 100644 --- a/lsat/identifier.go +++ b/lsat/identifier.go @@ -2,6 +2,7 @@ package lsat import ( "encoding/binary" + "encoding/hex" "errors" "fmt" "io" @@ -32,6 +33,31 @@ var ( ErrUnknownVersion = errors.New("unknown LSAT version") ) +// TokenID is the type that stores the token identifier of an LSAT token. +type TokenID [TokenIDSize]byte + +// String returns the hex encoded representation of the token ID as a string. +func (t *TokenID) String() string { + return hex.EncodeToString(t[:]) +} + +// MakeIDFromString parses the hex encoded string and parses it into a token ID. +func MakeIDFromString(newID string) (TokenID, error) { + if len(newID) != hex.EncodedLen(TokenIDSize) { + return TokenID{}, fmt.Errorf("invalid id string length of %v, "+ + "want %v", len(newID), hex.EncodedLen(TokenIDSize)) + } + + idBytes, err := hex.DecodeString(newID) + if err != nil { + return TokenID{}, err + } + var id TokenID + copy(id[:], idBytes) + + return id, nil +} + // Identifier contains the static identifying details of an LSAT. This is // intended to be used as the identifier of the macaroon within an LSAT. type Identifier struct { @@ -46,7 +72,7 @@ type Identifier struct { PaymentHash lntypes.Hash // TokenID is the unique identifier of an LSAT. - TokenID [TokenIDSize]byte + TokenID TokenID } // EncodeIdentifier encodes an LSAT's identifier according to its version. @@ -85,7 +111,7 @@ func DecodeIdentifier(r io.Reader) (*Identifier, error) { if _, err := r.Read(paymentHash[:]); err != nil { return nil, err } - var tokenID [TokenIDSize]byte + var tokenID TokenID if _, err := r.Read(tokenID[:]); err != nil { return nil, err }