Skip to content

Commit

Permalink
remove commented out code, as test was successful
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmcl committed Nov 13, 2023
1 parent 80d08d6 commit cd04080
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 70 deletions.
24 changes: 4 additions & 20 deletions Sources/KukaiCryptoSwift/KeyPair.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@ public struct KeyPair {
// MARK: - Public Helpers

/**
Create a regular (non HD) `KeyPair` from a hex seed string and optional `EllipticalCurve` (defaults to `.ed25519`, which returtns a TZ1 address)
Create a regular (non HD) `KeyPair` from a hex seed string
- parameter seedString: A hex string representing a cryptographic seed (can be created from `Mnemonic`)
- parameter signingCurve: The `EllipticalCurve` to use to create the keys
- Returns: A `KeyPair` instance, if able, nil otherwise
*/
public static func regular(fromSeedString seedString: String/*, andSigningCurve signingCurve: EllipticalCurve = .ed25519*/) -> KeyPair? {
public static func regular(fromSeedString seedString: String) -> KeyPair? {
var shortenedSeed = seedString
if seedString.count > 64 {
shortenedSeed = String(seedString[..<seedString.index(seedString.startIndex, offsetBy: 64)])
Expand All @@ -56,33 +55,18 @@ public struct KeyPair {
let secretKeyBytes = keyPair.secretKey
let publicKeyBytes = keyPair.publicKey

//switch signingCurve {
//case .ed25519:
return KeyPair(privateKey: PrivateKey(secretKeyBytes, signingCurve: .ed25519), publicKey: PublicKey(publicKeyBytes, signingCurve: .ed25519))
/*
case .secp256k1:
let privateKeyBytes = Array(secretKeyBytes[..<32])
let privateKey = PrivateKey(privateKeyBytes, signingCurve: signingCurve)

guard let publicKey = secp256k1PublicKey(fromPrivateKeyBytes: privateKeyBytes) else {
return nil
}

return KeyPair(privateKey: privateKey, publicKey: publicKey)
*/
//}
}

/**
Create a regular (non HD) `KeyPair` from a `Mnemonic` instance
- parameter mnemonic: An instance of `Mnemonic`
- parameter signingCurve: The `EllipticalCurve` to use to create the keys
- Returns: A `KeyPair` instance, if able, nil otherwise
*/
public static func regular(fromMnemonic mnemonic: Mnemonic, passphrase: String/*, andSigningCurve signingCurve: EllipticalCurve = .ed25519*/) -> KeyPair? {
public static func regular(fromMnemonic mnemonic: Mnemonic, passphrase: String) -> KeyPair? {
do {
let seed = try mnemonic.seed(passphrase: passphrase).hexString
return regular(fromSeedString: seed/*, andSigningCurve: signingCurve*/)
return regular(fromSeedString: seed)

} catch (let error) {
os_log("KeyPair Error - regular: %@", log: .default, type: .error, "\(error)")
Expand Down
64 changes: 14 additions & 50 deletions Tests/KukaiCryptoSwiftTests/KeyPairTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class KeyPairTests: XCTestCase {
let watermarkedBytes = messageToSign.addOperationWatermarkAndHash() ?? []
let mnemonic = try Mnemonic(seedPhrase: "kit trigger pledge excess payment sentence dutch mandate start sense seed venture")

let keyPair1 = KeyPair.regular(fromMnemonic: mnemonic, passphrase: ""/*, andSigningCurve: .ed25519*/)
let keyPair1 = KeyPair.regular(fromMnemonic: mnemonic, passphrase: "")
let signature1 = keyPair1?.privateKey.sign(bytes: watermarkedBytes) ?? []
let signatureHex1 = signature1.hexString
XCTAssert(keyPair1?.privateKey.bytes.hexString == "7d85c254fa624f29ae54e981295594212cba5767ebd5f763851d97c55b6a88d65c4da5f73069ef888317361cc035736716683a6bbf417336f1988bd78756e93f", keyPair1?.privateKey.bytes.hexString ?? "-")
Expand All @@ -26,42 +26,16 @@ final class KeyPairTests: XCTestCase {
XCTAssert(signatureHex1 == "a48c9404671f257f4aa088dad8862a4a39ada8ee88f223e98f67892fc9964c3026be89dbdd57e4a6800dda78f303eba7e5ce19cf3d6934435471682961dcaf0c", signatureHex1)
XCTAssert(keyPair1?.publicKey.verify(message: watermarkedBytes, signature: signature1, hex: signatureHex1) == true)

/*
let keyPair2 = KeyPair.regular(fromMnemonic: mnemonic, passphrase: "", andSigningCurve: .secp256k1)
let keyPair2 = KeyPair.regular(fromMnemonic: mnemonic, passphrase: "superSecurePassphrase")
let signature2 = keyPair2?.privateKey.sign(bytes: watermarkedBytes) ?? []
let signatureHex2 = signature2.hexString
XCTAssert(keyPair2?.privateKey.bytes.hexString == "7d85c254fa624f29ae54e981295594212cba5767ebd5f763851d97c55b6a88d6", keyPair2?.privateKey.bytes.hexString ?? "-")
XCTAssert(keyPair2?.publicKey.bytes.hexString == "025b4cb98848c2288eda85a8083d07d595721e89d3694bd3fb2a4c497ceeac66ca", keyPair2?.publicKey.bytes.hexString ?? "-")
XCTAssert(keyPair2?.publicKey.publicKeyHash == "tz2HpbGQcmU3UyusJ78Sbqeg9fYteamSMDGo", keyPair2?.publicKey.publicKeyHash ?? "-")
XCTAssert(keyPair2?.publicKey.base58CheckRepresentation == "sppk7Zzqz2AjP4yXqr5ys99gZkaPLFKfGKnUxn3u1T1xfNSArZ5CKX6", keyPair2?.publicKey.base58CheckRepresentation ?? "-")
XCTAssert(keyPair2?.privateKey.base58CheckRepresentation == "spsk2Nqz6AW1zVwLJ3QgcXhzPNdT3mpRskUKA2UXza5kNRd3NLKrMy", keyPair2?.privateKey.base58CheckRepresentation ?? "-")
XCTAssert(signatureHex2 == "2c9f14f18a21867fd2fe3130ad3aaeca7cb1c9421d78d32537173b98b25ed07d054837a878c7e9fe2d237b42c90e5aa2a63a58774833221707cc303a2121b3e7", signatureHex2)
XCTAssert(keyPair2?.privateKey.bytes.hexString == "02484d505196451ebbbde85c4ee3d2089219b51c1767798e4eb63cc9bfb5e62b7b31e1abbe67284d92fc523302f39e46611850e5d3c22d37b577dfcd464cb72d", keyPair2?.privateKey.bytes.hexString ?? "-")
XCTAssert(keyPair2?.publicKey.bytes.hexString == "7b31e1abbe67284d92fc523302f39e46611850e5d3c22d37b577dfcd464cb72d", keyPair2?.publicKey.bytes.hexString ?? "-")
XCTAssert(keyPair2?.publicKey.publicKeyHash == "tz1ZYoRJ2iouRi5r6CT83Ptp9Bof7RMRkxXe", keyPair2?.publicKey.publicKeyHash ?? "-")
XCTAssert(keyPair2?.publicKey.base58CheckRepresentation == "edpkuaUnRZQzwP1QYHFFXzbhN919wg17KHm7vHH86pxxgSSkqT7U4a", keyPair2?.publicKey.base58CheckRepresentation ?? "-")
XCTAssert(keyPair2?.privateKey.base58CheckRepresentation == "edskRcK6XU6Bhvjic9daFwgXH3DchwNNDzJCHjvCpB3PKvXdu3dPdBnf5nk1WrSt5zaZiXyrAsLqrgDvGKeP7F7GkZRZTmwo78", keyPair2?.privateKey.base58CheckRepresentation ?? "-")
XCTAssert(signatureHex2 == "07d0220bd7bb5b0dff17fb1c26f9e171bc836fe4a90f72646c11a5228e0d25fb4b61b40e0145476c2be2c6220cf8f204a1b9f7d6ba2b6ea77dfa6edd17a66a08", signatureHex2)
XCTAssert(keyPair2?.publicKey.verify(message: watermarkedBytes, signature: signature2, hex: signatureHex2) == true)
*/

let keyPair3 = KeyPair.regular(fromMnemonic: mnemonic, passphrase: "superSecurePassphrase"/*, andSigningCurve: .ed25519*/)
let signature3 = keyPair3?.privateKey.sign(bytes: watermarkedBytes) ?? []
let signatureHex3 = signature3.hexString
XCTAssert(keyPair3?.privateKey.bytes.hexString == "02484d505196451ebbbde85c4ee3d2089219b51c1767798e4eb63cc9bfb5e62b7b31e1abbe67284d92fc523302f39e46611850e5d3c22d37b577dfcd464cb72d", keyPair3?.privateKey.bytes.hexString ?? "-")
XCTAssert(keyPair3?.publicKey.bytes.hexString == "7b31e1abbe67284d92fc523302f39e46611850e5d3c22d37b577dfcd464cb72d", keyPair3?.publicKey.bytes.hexString ?? "-")
XCTAssert(keyPair3?.publicKey.publicKeyHash == "tz1ZYoRJ2iouRi5r6CT83Ptp9Bof7RMRkxXe", keyPair3?.publicKey.publicKeyHash ?? "-")
XCTAssert(keyPair3?.publicKey.base58CheckRepresentation == "edpkuaUnRZQzwP1QYHFFXzbhN919wg17KHm7vHH86pxxgSSkqT7U4a", keyPair3?.publicKey.base58CheckRepresentation ?? "-")
XCTAssert(keyPair3?.privateKey.base58CheckRepresentation == "edskRcK6XU6Bhvjic9daFwgXH3DchwNNDzJCHjvCpB3PKvXdu3dPdBnf5nk1WrSt5zaZiXyrAsLqrgDvGKeP7F7GkZRZTmwo78", keyPair3?.privateKey.base58CheckRepresentation ?? "-")
XCTAssert(signatureHex3 == "07d0220bd7bb5b0dff17fb1c26f9e171bc836fe4a90f72646c11a5228e0d25fb4b61b40e0145476c2be2c6220cf8f204a1b9f7d6ba2b6ea77dfa6edd17a66a08", signatureHex3)
XCTAssert(keyPair3?.publicKey.verify(message: watermarkedBytes, signature: signature3, hex: signatureHex3) == true)

/*
let keyPair4 = KeyPair.regular(fromMnemonic: mnemonic, passphrase: "superSecurePassphrase", andSigningCurve: .secp256k1)
let signature4 = keyPair4?.privateKey.sign(bytes: watermarkedBytes) ?? []
let signatureHex4 = signature4.hexString
XCTAssert(keyPair4?.privateKey.bytes.hexString == "02484d505196451ebbbde85c4ee3d2089219b51c1767798e4eb63cc9bfb5e62b", keyPair4?.privateKey.bytes.hexString ?? "-")
XCTAssert(keyPair4?.publicKey.bytes.hexString == "03b27e4b1b124275836f25048afedddddbc41e968a15b0c76176791d3d7ec4d105", keyPair4?.publicKey.bytes.hexString ?? "-")
XCTAssert(keyPair4?.publicKey.publicKeyHash == "tz2HDQCEMqJvKpcJvVBh4PnidJUSJepdBcbt", keyPair4?.publicKey.publicKeyHash ?? "-")
XCTAssert(keyPair4?.publicKey.base58CheckRepresentation == "sppk7cbzQR4wYsBozfzKvT4vk3GGCss9aoWouvJi6nirhMVZX7JRghQ", keyPair4?.publicKey.base58CheckRepresentation ?? "-")
XCTAssert(keyPair4?.privateKey.base58CheckRepresentation == "spsk1SZyy6bEGBzF7DXNnKpcDUmadnonaRKFkYW3WaHjosry7HxGN9", keyPair4?.privateKey.base58CheckRepresentation ?? "-")
XCTAssert(signatureHex4 == "2637597234233ccb2b05daba8f1b94d76659ffe6be542abacbc7e1ba6c4a95fe25997b474c617f5c42f792d017ef91a8ca4d2bfa8d971c999d97ab0e6fbe09e3", signatureHex4)
XCTAssert(keyPair4?.publicKey.verify(message: watermarkedBytes, signature: signature4, hex: signatureHex4) == true)
*/
}

func testHD() throws {
Expand Down Expand Up @@ -140,24 +114,14 @@ final class KeyPairTests: XCTestCase {

func testUncompress() throws {
let mnemonic = try Mnemonic(seedPhrase: "gym exact clown can answer hope sample mirror knife twenty powder super imitate lion churn almost shed chalk dust civil gadget pyramid helmet trade")
/*let keyPair1 = KeyPair.regular(fromMnemonic: mnemonic, passphrase: "", andSigningCurve: .secp256k1)

let uncompressed = KeyPair.secp256k1PublicKey_uncompressed(fromBytes: keyPair1?.publicKey.bytes ?? [])
let data = Data(bytes: uncompressed, count: uncompressed.count)
let dataString = data.hexString

XCTAssert(dataString.count == 130, dataString.count.description)
XCTAssert(dataString == "047b6d7bf2cbb376149211eacab517359cb035b0f0c36f57f7fe923a2453c7a8f1a594260c94501bc7ad63324b42638f768840948d1f1cadf9cb6d1ce456b7c8dc", dataString)
*/

let keyPair2 = KeyPair.regular(fromMnemonic: mnemonic, passphrase: ""/*, andSigningCurve: .ed25519*/)
let keyPair1 = KeyPair.regular(fromMnemonic: mnemonic, passphrase: "")

let uncompressed2 = KeyPair.secp256k1PublicKey_uncompressed(fromBytes: keyPair2?.publicKey.bytes ?? [])
let data2 = Data(bytes: uncompressed2, count: uncompressed2.count)
let dataString2 = data2.hexString
let uncompressed1 = KeyPair.secp256k1PublicKey_uncompressed(fromBytes: keyPair1?.publicKey.bytes ?? [])
let data1 = Data(bytes: uncompressed1, count: uncompressed1.count)
let dataString1 = data1.hexString

XCTAssert(dataString2.count == 0, dataString2.count.description)
XCTAssert(dataString2 == "", dataString2)
XCTAssert(dataString1.count == 0, dataString1.count.description)
XCTAssert(dataString1 == "", dataString1)
}

func testSafetyChecks() throws {
Expand Down

0 comments on commit cd04080

Please sign in to comment.