2
0
mirror of https://github.com/lightninglabs/loop synced 2024-11-04 06:00:21 +00:00

Merge pull request #116 from guggero/token-id

lsat: add token ID type
This commit is contained in:
Olaoluwa Osuntokun 2019-12-06 15:38:38 -08:00 committed by GitHub
commit a9033bab2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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