|
| 1 | +//go:build linux |
| 2 | +// +build linux |
| 3 | + |
| 4 | +package keyring |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + |
| 10 | + "github.com/99designs/keyring" |
| 11 | + |
| 12 | + "github.com/cosmos/cosmos-sdk/codec" |
| 13 | + "github.com/cosmos/cosmos-sdk/crypto/ledger" |
| 14 | + "github.com/cosmos/cosmos-sdk/crypto/types" |
| 15 | +) |
| 16 | + |
| 17 | +// Linux-only backend options. |
| 18 | +const BackendKeyctl = "keyctl" |
| 19 | + |
| 20 | +func KeyctlScopeUser(options *Options) { setKeyctlScope(options, "user") } |
| 21 | +func KeyctlScopeUserSession(options *Options) { setKeyctlScope(options, "usersession") } |
| 22 | +func KeyctlScopeSession(options *Options) { setKeyctlScope(options, "session") } |
| 23 | +func KeyctlScopeProcess(options *Options) { setKeyctlScope(options, "process") } |
| 24 | +func KeyctlScopeThread(options *Options) { setKeyctlScope(options, "thread") } |
| 25 | + |
| 26 | +// Options define the options of the Keyring. |
| 27 | +type Options struct { |
| 28 | + // supported signing algorithms for keyring |
| 29 | + SupportedAlgos SigningAlgoList |
| 30 | + // supported signing algorithms for Ledger |
| 31 | + SupportedAlgosLedger SigningAlgoList |
| 32 | + // define Ledger Derivation function |
| 33 | + LedgerDerivation func() (ledger.SECP256K1, error) |
| 34 | + // define Ledger key generation function |
| 35 | + LedgerCreateKey func([]byte) types.PubKey |
| 36 | + // define Ledger app name |
| 37 | + LedgerAppName string |
| 38 | + // indicate whether Ledger should skip DER Conversion on signature, |
| 39 | + // depending on which format (DER or BER) the Ledger app returns signatures |
| 40 | + LedgerSigSkipDERConv bool |
| 41 | + // KeyctlScope defines the scope of the keyctl's keyring. |
| 42 | + KeyctlScope string |
| 43 | +} |
| 44 | + |
| 45 | +func newKeyctlBackendConfig(appName, _ string, _ io.Reader, opts ...Option) keyring.Config { |
| 46 | + options := Options{ |
| 47 | + KeyctlScope: keyctlDefaultScope, // currently "process" |
| 48 | + } |
| 49 | + |
| 50 | + for _, optionFn := range opts { |
| 51 | + optionFn(&options) |
| 52 | + } |
| 53 | + |
| 54 | + return keyring.Config{ |
| 55 | + AllowedBackends: []keyring.BackendType{keyring.KeyCtlBackend}, |
| 56 | + ServiceName: appName, |
| 57 | + KeyCtlScope: options.KeyctlScope, |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// New creates a new instance of a keyring. |
| 62 | +// Keyring options can be applied when generating the new instance. |
| 63 | +// Available backends are "os", "file", "kwallet", "memory", "pass", "test", "keyctl". |
| 64 | +func New( |
| 65 | + appName, backend, rootDir string, userInput io.Reader, cdc codec.Codec, opts ...Option, |
| 66 | +) (Keyring, error) { |
| 67 | + if backend != BackendKeyctl { |
| 68 | + return newKeyringGeneric(appName, backend, rootDir, userInput, cdc, opts...) |
| 69 | + } |
| 70 | + |
| 71 | + db, err := keyring.Open(newKeyctlBackendConfig(appName, "", userInput, opts...)) |
| 72 | + if err != nil { |
| 73 | + return nil, fmt.Errorf("couldn't open keyring for %q: %w", appName, err) |
| 74 | + } |
| 75 | + |
| 76 | + return newKeystore(db, cdc, backend, opts...), nil |
| 77 | +} |
| 78 | + |
| 79 | +func setKeyctlScope(options *Options, scope string) { options.KeyctlScope = scope } |
| 80 | + |
| 81 | +// this is private as it is meant to be here for SDK devs convenience |
| 82 | +// as the user does not need to pick any default when he wants to |
| 83 | +// initialize keyctl with the default scope. |
| 84 | +const keyctlDefaultScope = "process" |
0 commit comments