|
| 1 | +package keyring |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/cosmos/cosmos-sdk/codec" |
| 7 | + codectypes "github.com/cosmos/cosmos-sdk/codec/types" |
| 8 | + cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" |
| 9 | + "github.com/cosmos/cosmos-sdk/crypto/hd" |
| 10 | + "github.com/cosmos/cosmos-sdk/crypto/keyring" |
| 11 | + "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" |
| 12 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 13 | + "github.com/cosmos/go-bip39" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + // Default BIP39 passphrase |
| 18 | + DefaultBIP39Passphrase = "" |
| 19 | + |
| 20 | + // Default HD path for Cosmos accounts |
| 21 | + DefaultHDPath = "m/44'/118'/0'/0/0" // Cosmos HD path |
| 22 | + |
| 23 | + // Lumera address prefixes |
| 24 | + AccountAddressPrefix = "lumera" |
| 25 | + Name = "lumera" |
| 26 | + |
| 27 | + // Default keyring name |
| 28 | + KeyringServiceName = "lumera-keyring" |
| 29 | +) |
| 30 | + |
| 31 | +// InitSDKConfig initializes the SDK configuration with Lumera-specific settings |
| 32 | +func InitSDKConfig() { |
| 33 | + // Set prefixes |
| 34 | + accountPubKeyPrefix := AccountAddressPrefix + "pub" |
| 35 | + validatorAddressPrefix := AccountAddressPrefix + "valoper" |
| 36 | + validatorPubKeyPrefix := AccountAddressPrefix + "valoperpub" |
| 37 | + consNodeAddressPrefix := AccountAddressPrefix + "valcons" |
| 38 | + consNodePubKeyPrefix := AccountAddressPrefix + "valconspub" |
| 39 | + |
| 40 | + // Set and seal config |
| 41 | + config := sdk.GetConfig() |
| 42 | + config.SetBech32PrefixForAccount(AccountAddressPrefix, accountPubKeyPrefix) |
| 43 | + config.SetBech32PrefixForValidator(validatorAddressPrefix, validatorPubKeyPrefix) |
| 44 | + config.SetBech32PrefixForConsensusNode(consNodeAddressPrefix, consNodePubKeyPrefix) |
| 45 | + config.Seal() |
| 46 | +} |
| 47 | + |
| 48 | +// InitKeyring initializes the keyring |
| 49 | +func InitKeyring(backend, dir string) (keyring.Keyring, error) { |
| 50 | + // Determine keyring backend type |
| 51 | + var backendType string |
| 52 | + switch backend { |
| 53 | + case "file": |
| 54 | + backendType = "file" |
| 55 | + case "os": |
| 56 | + backendType = "os" |
| 57 | + case "test": |
| 58 | + backendType = "test" |
| 59 | + case "memory", "mem": |
| 60 | + backendType = "memory" |
| 61 | + default: |
| 62 | + return nil, fmt.Errorf("unsupported keyring backend: %s", backend) |
| 63 | + } |
| 64 | + |
| 65 | + // Create interface registry and codec |
| 66 | + interfaceRegistry := codectypes.NewInterfaceRegistry() |
| 67 | + cryptocodec.RegisterInterfaces(interfaceRegistry) |
| 68 | + cdc := codec.NewProtoCodec(interfaceRegistry) |
| 69 | + |
| 70 | + // Create keyring |
| 71 | + kr, err := keyring.New( |
| 72 | + KeyringServiceName, |
| 73 | + backendType, |
| 74 | + dir, |
| 75 | + nil, //TODO : Fix this, Using nil for stdin to avoid interactive prompts when using test backend |
| 76 | + cdc, |
| 77 | + ) |
| 78 | + if err != nil { |
| 79 | + return nil, fmt.Errorf("failed to initialize keyring: %w", err) |
| 80 | + } |
| 81 | + |
| 82 | + return kr, nil |
| 83 | +} |
| 84 | + |
| 85 | +// GenerateMnemonic generates a new BIP39 mnemonic |
| 86 | +func GenerateMnemonic(entropySize int) (string, error) { |
| 87 | + entropy, err := bip39.NewEntropy(entropySize) |
| 88 | + if err != nil { |
| 89 | + return "", fmt.Errorf("failed to generate entropy: %w", err) |
| 90 | + } |
| 91 | + |
| 92 | + mnemonic, err := bip39.NewMnemonic(entropy) |
| 93 | + if err != nil { |
| 94 | + return "", fmt.Errorf("failed to generate mnemonic: %w", err) |
| 95 | + } |
| 96 | + |
| 97 | + return mnemonic, nil |
| 98 | +} |
| 99 | + |
| 100 | +// CreateNewAccount creates a new account in the keyring |
| 101 | +func CreateNewAccount(kr keyring.Keyring, name string, entropySize int) (string, *keyring.Record, error) { |
| 102 | + // Generate a new mnemonic |
| 103 | + mnemonic, err := GenerateMnemonic(entropySize) |
| 104 | + if err != nil { |
| 105 | + return "", nil, fmt.Errorf("failed to generate mnemonic: %w", err) |
| 106 | + } |
| 107 | + |
| 108 | + // Create a new account with the generated mnemonic |
| 109 | + info, err := kr.NewAccount( |
| 110 | + name, |
| 111 | + mnemonic, |
| 112 | + DefaultBIP39Passphrase, |
| 113 | + DefaultHDPath, |
| 114 | + hd.Secp256k1, |
| 115 | + ) |
| 116 | + if err != nil { |
| 117 | + return "", nil, fmt.Errorf("failed to create new account: %w", err) |
| 118 | + } |
| 119 | + |
| 120 | + return mnemonic, info, nil |
| 121 | +} |
| 122 | + |
| 123 | +// RecoverAccountFromMnemonic recovers an account from a mnemonic |
| 124 | +func RecoverAccountFromMnemonic(kr keyring.Keyring, name, mnemonic string) (*keyring.Record, error) { |
| 125 | + // Import account from mnemonic |
| 126 | + info, err := kr.NewAccount( |
| 127 | + name, |
| 128 | + mnemonic, |
| 129 | + DefaultBIP39Passphrase, |
| 130 | + DefaultHDPath, |
| 131 | + hd.Secp256k1, |
| 132 | + ) |
| 133 | + if err != nil { |
| 134 | + return nil, fmt.Errorf("failed to recover account from mnemonic: %w", err) |
| 135 | + } |
| 136 | + |
| 137 | + return info, nil |
| 138 | +} |
| 139 | + |
| 140 | +// DerivePrivKeyFromMnemonic derives a private key directly from a mnemonic |
| 141 | +func DerivePrivKeyFromMnemonic(mnemonic, hdPath string) (*secp256k1.PrivKey, error) { |
| 142 | + if hdPath == "" { |
| 143 | + hdPath = DefaultHDPath |
| 144 | + } |
| 145 | + |
| 146 | + seed := bip39.NewSeed(mnemonic, DefaultBIP39Passphrase) |
| 147 | + master, ch := hd.ComputeMastersFromSeed(seed) |
| 148 | + |
| 149 | + derivedKey, err := hd.DerivePrivateKeyForPath(master, ch, hdPath) |
| 150 | + if err != nil { |
| 151 | + return nil, fmt.Errorf("failed to derive private key: %w", err) |
| 152 | + } |
| 153 | + |
| 154 | + return &secp256k1.PrivKey{Key: derivedKey}, nil |
| 155 | +} |
0 commit comments