mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-31 17:51:33 +02:00
macaroons: specify root key ID in bakery
This commit is contained in:
44
macaroons/context.go
Normal file
44
macaroons/context.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package macaroons
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var (
|
||||
// RootKeyIDContextKey is the key to get rootKeyID from context.
|
||||
RootKeyIDContextKey = contextKey{"rootkeyid"}
|
||||
|
||||
// ErrContextRootKeyID is used when the supplied context doesn't have
|
||||
// a root key ID.
|
||||
ErrContextRootKeyID = fmt.Errorf("failed to read root key ID " +
|
||||
"from context")
|
||||
)
|
||||
|
||||
// contextKey is the type we use to identify values in the context.
|
||||
type contextKey struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
// ContextWithRootKeyID passes the root key ID value to context.
|
||||
func ContextWithRootKeyID(ctx context.Context,
|
||||
value interface{}) context.Context {
|
||||
|
||||
return context.WithValue(ctx, RootKeyIDContextKey, value)
|
||||
}
|
||||
|
||||
// RootKeyIDFromContext retrieves the root key ID from context using the key
|
||||
// RootKeyIDContextKey.
|
||||
func RootKeyIDFromContext(ctx context.Context) ([]byte, error) {
|
||||
id, ok := ctx.Value(RootKeyIDContextKey).([]byte)
|
||||
if !ok {
|
||||
return nil, ErrContextRootKeyID
|
||||
}
|
||||
|
||||
// Check that the id is not empty.
|
||||
if len(id) == 0 {
|
||||
return nil, ErrMissingRootKeyID
|
||||
}
|
||||
|
||||
return id, nil
|
||||
}
|
Reference in New Issue
Block a user