Skip to content

Commit

Permalink
- bump crypto library to support new key gen
Browse files Browse the repository at this point in the history
- add helper methods to regualrwallet
  • Loading branch information
simonmcl committed May 28, 2024
1 parent 3f3702e commit d4c5933
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let package = Package(
.library(name: "KukaiCoreSwift", targets: ["KukaiCoreSwift"]),
],
dependencies: [
.package(name: "KukaiCryptoSwift", url: "https://github.com/kukai-wallet/kukai-crypto-swift", from: "1.0.22" /*.branch("develop")*/),
.package(name: "KukaiCryptoSwift", url: "https://github.com/kukai-wallet/kukai-crypto-swift", from: "1.0.23" /*.branch("develop")*/),
.package(name: "CustomAuth", url: "https://github.com/torusresearch/customauth-swift-sdk", from: "10.0.1"),
.package(name: "SignalRClient", url: "https://github.com/moozzyk/SignalR-Client-Swift", from: "0.8.0"),
.package(url: "https://github.com/SDWebImage/SDWebImage.git", from: "5.18.10")
Expand Down
17 changes: 17 additions & 0 deletions Sources/KukaiCoreSwift/Models/RegularWallet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,23 @@ public class RegularWallet: Wallet {
self.mnemonic = shiftedMnemonic
}

/**
Create a `RegularWallet` by supplying a a Base58 encoded string containing a secret key. Both encrypted and unencrypted are supported. Supports Tz1 and Tz2
- Parameter fromSecretKey: A String containing a Base58Check encoded secret key
- Parameter passphrase: An optional string containing the passphrase used to encrypt the secret key
*/
public init?(fromSecretKey secretKey: String, passphrase: String?) {
guard let keyPair = KeyPair.regular(fromSecretKey: secretKey, andPassphrase: passphrase), let address = keyPair.publicKey.publicKeyHash else {
return nil
}

self.type = .regular
self.address = address
self.privateKey = keyPair.privateKey
self.publicKey = keyPair.publicKey
self.mnemonic = nil
}

/**
Create a `RegularWallet` by asking for a mnemonic of a given number of words and a passphrase (or "" if none).
- Parameter withMnemonicLength: `Mnemonic.NumberOfWords` the number of words to use when creating a mnemonic
Expand Down
22 changes: 22 additions & 0 deletions Tests/KukaiCoreSwiftTests/Models/RegularWalletTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,26 @@ class RegularWalletTests: XCTestCase {
let encoded = MockConstants.defaultLinearWallet.publicKeyBase58encoded()
XCTAssert(encoded == MockConstants.linearWalletEd255519.base58Encoded, encoded)
}

func testSecretKeyImport() {
let tz1UnencryptedSeed = RegularWallet(fromSecretKey: "edsk3KvXD8SVD9GCyU4jbzaFba2HZRad5pQ7ajL79n7rUoc3nfHv5t", passphrase: nil)
XCTAssert(tz1UnencryptedSeed?.address == "tz1Qvpsq7UZWyQ4yabf9wGpG97testZCjoCH", tz1UnencryptedSeed?.address ?? "-")
XCTAssert(tz1UnencryptedSeed?.privateKey.signingCurve == .ed25519, tz1UnencryptedSeed?.privateKey.signingCurve.rawValue ?? "-")

let tz1UnencryptedSecret = RegularWallet(fromSecretKey: "edskRgQqEw17KMib89AzChu8DiJjmVeDfGmbCMpp7MpmhgTdNVvZ3TTaLfwNoux4hDDVeLxmEJxKiYE1cYp1Vgj6QATKaJa58L", passphrase: nil)
XCTAssert(tz1UnencryptedSecret?.address == "tz1Ue76bLW7boAcJEZf2kSGcamdBKVi4Kpss", tz1UnencryptedSecret?.address ?? "-")
XCTAssert(tz1UnencryptedSecret?.privateKey.signingCurve == .ed25519, tz1UnencryptedSecret?.privateKey.signingCurve.rawValue ?? "-")

let tz1EncryptedSecret = RegularWallet(fromSecretKey: "edesk1L8uVSYd3aug7jbeynzErQTnBxq6G6hJwmeue3yUBt11wp3ULXvcLwYRzDp4LWWvRFNJXRi3LaN7WGiEGhh", passphrase: "pa55word")
XCTAssert(tz1EncryptedSecret?.address == "tz1XztestvvcXSQZUbZav5YgVLRQbxC4GuMF", tz1EncryptedSecret?.address ?? "-")
XCTAssert(tz1EncryptedSecret?.privateKey.signingCurve == .ed25519, tz1EncryptedSecret?.privateKey.signingCurve.rawValue ?? "-")

let tz2UnencryptedSecret = RegularWallet(fromSecretKey: "spsk29hF9oJ6koNnnJMs1rXz4ynBs8hL8FyubTNPCu2tCVP5beGDbw", passphrase: nil)
XCTAssert(tz2UnencryptedSecret?.address == "tz2RbUirt95UQHa9YyxcLj9GusNctxwn3Xi1", tz2UnencryptedSecret?.address ?? "-")
XCTAssert(tz2UnencryptedSecret?.privateKey.signingCurve == .secp256k1, tz2UnencryptedSecret?.privateKey.signingCurve.rawValue ?? "-")

let tz2EncryptedSecret = RegularWallet(fromSecretKey: "spesk1S5bMTCyH9z4mHSpnbn6DBY831DD6Rxgq7ANfEKkngoHSwy6B5odh942TKL6DtLbfTkpTHfSTAQu2d72Qd6", passphrase: "pa55word")
XCTAssert(tz2EncryptedSecret?.address == "tz2C8APAjnQfffdkHssxdFRctkD1iPLGaGEg", tz2EncryptedSecret?.address ?? "-")
XCTAssert(tz2EncryptedSecret?.privateKey.signingCurve == .secp256k1, tz2EncryptedSecret?.privateKey.signingCurve.rawValue ?? "-")
}
}

0 comments on commit d4c5933

Please sign in to comment.