Skip to content
This repository has been archived by the owner on Aug 30, 2022. It is now read-only.

Commit

Permalink
Merge pull request #72 from EOSIO/sm/fix/public-key-recovery
Browse files Browse the repository at this point in the history
Potential fix for extracting public key from some private keys.
  • Loading branch information
opi-smccoole authored Nov 5, 2019
2 parents a5d5d31 + 3d204dd commit 41a78a5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
9 changes: 8 additions & 1 deletion EosioSwiftEcc/Recover/EccRecoverKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,22 @@ public class EccRecoverKey {

let xBNstr = BN_bn2hex(xBN)!
let yBNstr = BN_bn2hex(yBN)!

let xHex = String(cString: xBNstr)
let xPad = max(64, xHex.count)
let xHexPadded = String(repeatElement("0", count: xPad - xHex.count) + xHex)

let yHex = String(cString: yBNstr)
let yPad = max(64, yHex.count)
let yHexPadded = String(repeatElement("0", count: yPad - yHex.count) + yHex)

CRYPTO_free(xBNstr)
CRYPTO_free(yBNstr)

BN_free(xBN)
BN_free(yBN)
EC_POINT_free(pubKeyPoint)
recoveredPubKeyHex = "04" + xHex + yHex
recoveredPubKeyHex = "04" + xHexPadded + yHexPadded
}
EC_GROUP_free(group)
BN_CTX_free(ctx)
Expand Down
14 changes: 14 additions & 0 deletions EosioSwiftEccTests/EosioSwiftEccRecoverKeyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ class EosioSwiftEccRecoverKeyTests: XCTestCase {

}

func test_eosioK1_private_to_public_needs_padding() {
do {
let privateKey = "5KLuCa3aEXW2kLyj2xbHjn9fsoZmmBNBTbVHhnkzVXtgjipDyQF"
let publicKey = try EccRecoverKey.recoverPublicKey(privateKey: Data(eosioPrivateKey: privateKey), curve: .k1)
guard let eosLegacyPublicKey = publicKey.toCompressedPublicKey?.toEosioLegacyPublicKey else {
XCTFail("Should not fail to convert to EOSIO Legacy Public key.")
return
}
XCTAssert(eosLegacyPublicKey == "EOS7mbBaD7UFLQKVE3oGkAp4ToFaFjaedjJA2WBpTBY8yXgnwK53e")
} catch let error {
XCTFail("Should not error extracting public from private key: \(error.localizedDescription)")
}
}

func test_eosioK1_private_to_public() {
do {
let privateKey = try Data(eosioPrivateKey: privateKeyK1)
Expand Down

0 comments on commit 41a78a5

Please sign in to comment.