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

fix: Fix key pair creation from private key using SECP256K1 #55

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
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,17 @@ class ApolloImpl : Apollo {
override fun createKeyPair(seed: Seed?, privateKey: PrivateKey): KeyPair {
return when (privateKey.keyCurve.curve) {
Curve.SECP256K1 -> {
val derivationPath = DerivationPath.fromPath("m/${privateKey.keyCurve.index}'/0'/0'")
if (seed == null) {
throw ApolloError.InvalidMnemonicWord()
}
val extendedKey = KeyDerivation.deriveKey(seed.value, derivationPath)
val kmmKeyPair = extendedKey.keyPair()
val mPrivateKey = kmmKeyPair.privateKey as KMMECSecp256k1PrivateKey
val mPublicKey = kmmKeyPair.publicKey as KMMECSecp256k1PublicKey
val kmmPrivateKey = KMMECSecp256k1PrivateKey.secp256k1FromBytes(privateKey.value)

KeyPair(
keyCurve = privateKey.keyCurve,
privateKey = PrivateKey(
keyCurve = privateKey.keyCurve,
value = mPrivateKey.getEncoded(),
value = kmmPrivateKey.getEncoded(),
),
publicKey = PublicKey(
curve = privateKey.keyCurve,
value = mPublicKey.getEncoded(),
value = kmmPrivateKey.getPublicKey().getEncoded(),
),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,19 @@ class ApolloTests {
assertEquals(32, publicKey.value.size)
}

@Test
fun testCreateKeyPair_whenSecp256k1FromPrivateKey_thenPublicKeyCorrect() {
val seed = apollo.createRandomSeed()
val keyPair = apollo.createKeyPair(seed = seed.seed, curve = KeyCurve(Curve.SECP256K1))
val expectedPrivateKey = keyPair.privateKey
val expectedPublicKey = keyPair.publicKey

val resultKeyPair = apollo.createKeyPair(privateKey = keyPair.privateKey)

assertEquals(expectedPublicKey, resultKeyPair.publicKey)
assertEquals(expectedPrivateKey, resultKeyPair.privateKey)
}

@Test
fun testCreateKeyPair_whenUsingSeedAndMnemonics_thenKeyPairIsCorrect() {
val mnemonics = arrayOf("blade", "multiply", "coil", "rare", "fox", "doll", "tongue", "please", "icon", "mind", "gesture", "moral", "old", "laugh", "symptom", "assume", "burden", "appear", "always", "oil", "ticket", "vault", "return", "height")
Expand Down