Skip to content

Commit

Permalink
reconstruct priv key better (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
decentralgabe authored Oct 1, 2022
1 parent 56fc273 commit ec86410
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 21 deletions.
4 changes: 2 additions & 2 deletions crypto/jwk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestPublicKeyToPublicKeyJWK(t *testing.T) {
})

t.Run("secp256k1", func(tt *testing.T) {
pubKey, _, err := GenerateSecp256k1Key()
pubKey, _, err := GenerateSECP256k1Key()
assert.NoError(t, err)

jwk, err := PublicKeyToPublicKeyJWK(pubKey)
Expand Down Expand Up @@ -154,7 +154,7 @@ func TestPrivateKeyToPrivateKeyJWK(t *testing.T) {
})

t.Run("secp256k1", func(tt *testing.T) {
_, privKey, err := GenerateSecp256k1Key()
_, privKey, err := GenerateSECP256k1Key()
assert.NoError(t, err)

_, jwk, err := PrivateKeyToPrivateKeyJWK(privKey)
Expand Down
2 changes: 1 addition & 1 deletion crypto/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func AlgFromKeyAndCurve(kty jwa.KeyType, crv jwa.EllipticCurveAlgorithm) (jwa.Si

if kty == jwa.EC {
switch curve {
case jwa.EllipticCurveAlgorithm(Secp256k1):
case jwa.EllipticCurveAlgorithm(SECP256k1):
return jwa.ES256K, nil
case jwa.P256:
return jwa.ES256, nil
Expand Down
47 changes: 40 additions & 7 deletions crypto/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func GenerateKeyByKeyType(kt KeyType) (crypto.PublicKey, crypto.PrivateKey, erro
return GenerateEd25519Key()
case X25519:
return GenerateX25519Key()
case Secp256k1:
return GenerateSecp256k1Key()
case SECP256k1:
return GenerateSECP256k1Key()
case P224:
return GenerateP224Key()
case P256:
Expand Down Expand Up @@ -76,7 +76,7 @@ func BytesToPubKey(keyBytes []byte, kt KeyType) (crypto.PublicKey, error) {
switch kt {
case Ed25519, X25519:
return keyBytes, nil
case Secp256k1:
case SECP256k1:
pubKey, err := secp.ParsePubKey(keyBytes)
if err != nil {
return nil, err
Expand Down Expand Up @@ -121,6 +121,37 @@ func BytesToPubKey(keyBytes []byte, kt KeyType) (crypto.PublicKey, error) {
}
}

// GetKeyTypeFromPrivateKey returns the key type of a private key for known key types
func GetKeyTypeFromPrivateKey(key crypto.PrivateKey) (KeyType, error) {
if _, ok := key.(ed25519.PrivateKey); ok {
return Ed25519, nil
}
if _, ok := key.(x25519.PrivateKey); ok {
return X25519, nil
}
if _, ok := key.(secp.PrivateKey); ok {
return SECP256k1, nil
}
if ecdsaKey, ok := key.(ecdsa.PrivateKey); ok {
switch ecdsaKey.Curve {
case elliptic.P224():
return P224, nil
case elliptic.P256():
return P256, nil
case elliptic.P384():
return P384, nil
case elliptic.P521():
return P521, nil
default:
return "", fmt.Errorf("unsupported curve: %s", ecdsaKey.Curve)
}
}
if _, ok := key.(rsa.PrivateKey); ok {
return RSA, nil
}
return "", errors.New("unknown private key type")
}

// PrivKeyToBytes constructs a byte representation of a private key, for a set number of supported key types
func PrivKeyToBytes(key crypto.PrivateKey) ([]byte, error) {
ed25519Key, ok := key.(ed25519.PrivateKey)
Expand Down Expand Up @@ -155,9 +186,11 @@ func PrivKeyToBytes(key crypto.PrivateKey) ([]byte, error) {
// It is assumed the key was turned into byte form using the sibling method `PrivKeyToBytes`
func BytesToPrivKey(keyBytes []byte, kt KeyType) (crypto.PrivateKey, error) {
switch kt {
case Ed25519, X25519:
return keyBytes, nil
case Secp256k1:
case Ed25519:
return ed25519.PrivateKey(keyBytes), nil
case X25519:
return x25519.PrivateKey(keyBytes), nil
case SECP256k1:
return *secp.PrivKeyFromBytes(keyBytes), nil
case P224, P256, P384, P521:
privKey, err := x509.ParseECPrivateKey(keyBytes)
Expand All @@ -184,7 +217,7 @@ func GenerateX25519Key() (x25519.PublicKey, x25519.PrivateKey, error) {
return x25519.GenerateKey(rand.Reader)
}

func GenerateSecp256k1Key() (secp.PublicKey, secp.PrivateKey, error) {
func GenerateSECP256k1Key() (secp.PublicKey, secp.PrivateKey, error) {
privKey, err := secp.GeneratePrivateKey()
if err != nil {
return secp.PublicKey{}, secp.PrivateKey{}, err
Expand Down
4 changes: 4 additions & 0 deletions crypto/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func TestKeyToBytes(t *testing.T) {
assert.NoError(tt, err)
assert.NotEmpty(tt, reconstructedPriv)
assert.EqualValues(tt, priv, reconstructedPriv)

kt, err := GetKeyTypeFromPrivateKey(priv)
assert.NoError(tt, err)
assert.Equal(tt, keyType, kt)
})
}
}
4 changes: 2 additions & 2 deletions crypto/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type (
const (
Ed25519 KeyType = "Ed25519"
X25519 KeyType = "X25519"
Secp256k1 KeyType = "secp256k1"
SECP256k1 KeyType = "secp256k1"
P224 KeyType = "P-224"
P256 KeyType = "P-256"
P384 KeyType = "P-384"
Expand Down Expand Up @@ -43,7 +43,7 @@ func IsSupportedKeyType(kt KeyType) bool {
}

func GetSupportedKeyTypes() []KeyType {
return []KeyType{Ed25519, X25519, Secp256k1, P224, P256, P384, P521, RSA}
return []KeyType{Ed25519, X25519, SECP256k1, P224, P256, P384, P521, RSA}
}

func IsSupportedSignatureAlg(sa SignatureAlgorithm) bool {
Expand Down
2 changes: 1 addition & 1 deletion cryptosuite/jsonwebkey2020.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func GenerateX25519JSONWebKey2020() (*JSONWebKey2020, error) {
// which is utilized in the widely accepted go bitcoin node implementation from the btcsuite project
// https://github.com/btcsuite/btcd/blob/master/btcec/btcec.go#L23
func GenerateSECP256k1JSONWebKey2020() (*JSONWebKey2020, error) {
_, privKey, err := crypto.GenerateSecp256k1Key()
_, privKey, err := crypto.GenerateSECP256k1Key()
if err != nil {
logrus.WithError(err).Error("could not generate secp256k1 key")
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion did/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,5 +245,5 @@ func isSupportedKeyType(kt crypto.KeyType) bool {
}

func GetSupportedDIDKeyTypes() []crypto.KeyType {
return []crypto.KeyType{crypto.Ed25519, crypto.X25519, crypto.Secp256k1, crypto.P256, crypto.P384, crypto.P521, crypto.RSA}
return []crypto.KeyType{crypto.Ed25519, crypto.X25519, crypto.SECP256k1, crypto.P256, crypto.P384, crypto.P521, crypto.RSA}
}
4 changes: 2 additions & 2 deletions did/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestGenerateDIDKey(t *testing.T) {
},
{
name: "SECP256k1",
keyType: crypto.Secp256k1,
keyType: crypto.SECP256k1,
expectErr: false,
},
{
Expand Down Expand Up @@ -214,7 +214,7 @@ func TestDIDKeySignVerify(t *testing.T) {
})

t.Run("Test secp256k1 did:key", func(t *testing.T) {
privKey, didKey, err := GenerateDIDKey(crypto.Secp256k1)
privKey, didKey, err := GenerateDIDKey(crypto.SECP256k1)
assert.NoError(t, err)
assert.NotNil(t, didKey)
assert.NotEmpty(t, privKey)
Expand Down
2 changes: 1 addition & 1 deletion did/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func KeyTypeToLDKeyType(kt crypto.KeyType) (cryptosuite.LDKeyType, error) {
return Ed25519VerificationKey2018, nil
case crypto.X25519:
return X25519KeyAgreementKey2019, nil
case crypto.Secp256k1:
case crypto.SECP256k1:
return EcdsaSecp256k1VerificationKey2019, nil
case crypto.P256, crypto.P384, crypto.P521, crypto.RSA:
return cryptosuite.JsonWebKey2020, nil
Expand Down
2 changes: 1 addition & 1 deletion did/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestKeyTypeToLDKeyType(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, kt, X25519KeyAgreementKey2019)

kt, err = KeyTypeToLDKeyType(crypto.Secp256k1)
kt, err = KeyTypeToLDKeyType(crypto.SECP256k1)
assert.NoError(t, err)
assert.Equal(t, kt, EcdsaSecp256k1VerificationKey2019)

Expand Down
2 changes: 1 addition & 1 deletion did/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func keyTypeToMultiCodec(kt crypto.KeyType) (multicodec.Code, error) {
return Ed25519MultiCodec, nil
case crypto.X25519:
return X25519MultiCodec, nil
case crypto.Secp256k1:
case crypto.SECP256k1:
return Secp256k1MultiCodec, nil
case crypto.P256:
return P256MultiCodec, nil
Expand Down
2 changes: 1 addition & 1 deletion example/did/did.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func main() {
// To use the private key, it is recommended to re-cast to the associated type.
// The function returns the associated private key value cast to the generic golang crypto.PrivateKey interface.
// See more here: https://github.com/TBD54566975/ssi-sdk/blob/main/did/key.go#L51
_, didKey, err := did.GenerateDIDKey(crypto.Secp256k1)
_, didKey, err := did.GenerateDIDKey(crypto.SECP256k1)
if err != nil {
example.HandleExampleError(err, "failed to generate key")
}
Expand Down
2 changes: 1 addition & 1 deletion example/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (s *SimpleWallet) Init(keyType string) error {
didStr = didk.ToString()
} else {
var didKey *did.DIDKey
privKey, didKey, err = did.GenerateDIDKey(crypto.Secp256k1)
privKey, didKey, err = did.GenerateDIDKey(crypto.SECP256k1)
if err != nil {
return err
}
Expand Down

0 comments on commit ec86410

Please sign in to comment.