From a6a0568c6ba4475c7de6628ae7be8d64d703b5c7 Mon Sep 17 00:00:00 2001 From: Thiago Souza Date: Sat, 23 Sep 2023 19:46:44 +0100 Subject: [PATCH] add ImportPrivKeyHex --- crypto/keyring/keyring.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/crypto/keyring/keyring.go b/crypto/keyring/keyring.go index f96c8635243c..a6fb62bd0673 100644 --- a/crypto/keyring/keyring.go +++ b/crypto/keyring/keyring.go @@ -41,6 +41,7 @@ const ( keyringFileDirName = "keyring-file" keyringTestDirName = "keyring-test" passKeyringPrefix = "keyring-%s" + hexPrefix = "0x" ) var ( @@ -305,6 +306,29 @@ func (ks keystore) ImportPrivKey(uid, armor, passphrase string) error { return nil } +func (ks keystore) ImportPrivKeyHex(uid, privKey, algoStr string) error { + if _, err := ks.Key(uid); err == nil { + return fmt.Errorf("cannot overwrite key: %s", uid) + } + if privKey[:2] == hexPrefix { + privKey = privKey[2:] + } + decodedPriv, err := hex.DecodeString(privKey) + if err != nil { + return err + } + algo, err := NewSigningAlgoFromString(algoStr, ks.options.SupportedAlgos) + if err != nil { + return err + } + priv := algo.Generate()(decodedPriv) + _, err = ks.writeLocalKey(uid, priv, algo.Name()) + if err != nil { + return err + } + return nil +} + func (ks keystore) ImportPubKey(uid string, armor string) error { if _, err := ks.Key(uid); err == nil { return fmt.Errorf("cannot overwrite key: %s", uid)