fyrchik
d29d7f9c3e
* [#15] ecdsa: Fix signature marshaling with go1.19 Signed-off-by: Evgenii Stratonikov <stratonikov@runbox.com> * [#15] go.mod: Update go version to 1.16 Signed-off-by: Evgenii Stratonikov <stratonikov@runbox.com> * [#15] go.mod: Drop `pkg/errors` dependency Signed-off-by: Evgenii Stratonikov <stratonikov@runbox.com> * [#15] *: Perform `goimports -w` Signed-off-by: Evgenii Stratonikov <stratonikov@runbox.com> Signed-off-by: Evgenii Stratonikov <stratonikov@runbox.com> Co-authored-by: Evgenii Stratonikov <stratonikov@runbox.com>
24 lines
631 B
Go
24 lines
631 B
Go
package crypto
|
|
|
|
import (
|
|
"crypto/ecdsa"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
// LoadPrivateKey allows to load private key from various formats:
|
|
// - wif string
|
|
// - hex string
|
|
// - file path (D-bytes or SEC 1 / ASN.1 DER form)
|
|
func LoadPrivateKey(val string) (*ecdsa.PrivateKey, error) {
|
|
if data, err := os.ReadFile(val); err == nil {
|
|
return UnmarshalPrivateKey(data)
|
|
} else if data, err = hex.DecodeString(val); err == nil {
|
|
return UnmarshalPrivateKey(data)
|
|
} else if key, err := WIFDecode(val); err == nil {
|
|
return key, nil
|
|
}
|
|
|
|
return nil, fmt.Errorf("unknown key format (%q), expect: hex-string, wif or file-path", val)
|
|
}
|