Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Pallas to support ImportPrivateKey #380

Merged
merged 3 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions keys/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func Err(err error) bool {
ErrKeyGenSecp256k1Failed,
ErrKeyGenSecp256r1Failed,
ErrKeyGenEdwards25519Failed,
ErrKeyGenPallasFailed,
ErrCurveTypeNotSupported,
ErrSignUnsupportedPayloadSignatureType,
ErrSignUnsupportedSignatureType,
Expand Down
14 changes: 14 additions & 0 deletions keys/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,20 @@ func ImportPrivateKey(privKeyHex string, curve types.CurveType) (*KeyPair, error
PublicKey: pubKey,
PrivateKey: rawPrivKey.D.Bytes(),
}
case types.Pallas:
rawPrivKey := &mina.SecretKey{}
_ = rawPrivKey.UnmarshalBinary(privKey)

pubKey, _ := rawPrivKey.GetPublicKey().MarshalBinary()
priKeyBytes, _ := rawPrivKey.MarshalBinary()
keyPair = &KeyPair{
PublicKey: &types.PublicKey{
Bytes: pubKey,
CurveType: curve,
},
PrivateKey: priKeyBytes,
}

default:
return nil, fmt.Errorf("%w: %s", ErrCurveTypeNotSupported, curve)
}
Expand Down
20 changes: 20 additions & 0 deletions keys/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ func TestGenerateKeypairEdwards25519(t *testing.T) {
assert.Len(t, keypair.PrivateKey, PrivKeyBytesLen)
}

func TestGenerateKeypairPallas(t *testing.T) {
curve := types.Pallas
keypair, err := GenerateKeypair(curve)

assert.NoError(t, err)
assert.Equal(t, keypair.PublicKey.CurveType, curve)
assert.Len(t, keypair.PrivateKey, PrivKeyBytesLen)
}

func mockKeyPair(privKey []byte, curveType types.CurveType) *KeyPair {
keypair, _ := GenerateKeypair(curveType)
keypair.PrivateKey = privKey
Expand Down Expand Up @@ -125,8 +134,14 @@ func TestImportPrivateKey(t *testing.T) {
types.Secp256k1,
nil,
},
"simple Pallas": {
"92D872DA7B3C90CF69D347908C3D3D692EA033A1D6E4A1695FCDCF6BBED87F37",
types.Pallas,
nil,
},
"short ed25519": {"asd", types.Secp256k1, ErrPrivKeyUndecodable},
"short Secp256k1": {"asd", types.Edwards25519, ErrPrivKeyUndecodable},
"short pallas": {"asd", types.Pallas, ErrPrivKeyUndecodable},
"long ed25519": {
"aeb121b4c545f0f850e1480492508c65a250e9965b0d90176fab4d7506398ebbaeb121b4c545f0f850e1480492508c65a250e9965b0d90176fab4d7506398ebb", // nolint:lll
types.Secp256k1,
Expand All @@ -137,6 +152,11 @@ func TestImportPrivateKey(t *testing.T) {
types.Edwards25519,
ErrPrivKeyLengthInvalid,
},
"long Pallas": {
"92D872DA7B3C90CF69D347908C3D3D692EA033A1D6E4A1695FCDCF6BBED87F3792D872DA7B3C90CF69D347908C3D3D692EA033A1D6E4A1695FCDCF6BBED87F37", // nolint:lll
types.Pallas,
ErrPrivKeyLengthInvalid,
},
}

for name, test := range importPrivKeyTests {
Expand Down