Skip to content

Commit

Permalink
fix: migrate BIP39Mnemonic to BIP39
Browse files Browse the repository at this point in the history
Signed-off-by: phuoc <[email protected]>
  • Loading branch information
phuocbitmark committed May 21, 2024
1 parent eeba055 commit f32feb7
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 100 deletions.
12 changes: 5 additions & 7 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ let package = Package(
],
dependencies: [
.package(name: "Web3.swift", url: "https://github.com/bitmark-inc/Web3.swift.git", .branch("master")),
.package(url: "https://github.com/BlockchainCommons/URKit.git", .exact("7.5.0")),
.package(url: "https://github.com/BlockchainCommons/URKit.git", .exact("14.0.2")),
.package(url: "https://github.com/keefertaylor/Base58Swift.git", from: "2.1.0"),
.package(name: "KukaiCoreSwift", url: "https://github.com/autonomy-system/kukai-core-swift.git", .branch("main")),
.package(name: "TweetNacl", url: "https://github.com/bitmark-inc/tweetnacl-swiftwrap", branch: "master")
.package(name: "TweetNacl", url: "https://github.com/bitmark-inc/tweetnacl-swiftwrap", branch: "master"),
.package(url: "https://github.com/BlockchainCommons/BCSwiftFoundation.git", .branch("master"))
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
Expand All @@ -27,19 +28,16 @@ let package = Package(
.target(
name: "LibAuk",
dependencies: [
.target(name: "LibWally"),
.product(name: "Web3", package: "Web3.swift"),
.product(name: "URKit", package: "URKit"),
.product(name: "KukaiCoreSwift", package: "KukaiCoreSwift"),
.product(name: "Base58Swift", package: "Base58Swift"),
.product(name: "TweetNacl", package: "TweetNacl"),
.product(name: "BCFoundation", package: "BCSwiftFoundation"),
]),
.testTarget(
name: "LibAukTests",
dependencies: ["LibAuk"]),
.binaryTarget(
name: "LibWally",
path: "Frameworks/LibWally.xcframework")
dependencies: ["LibAuk"])
],
swiftLanguageVersions: [.v5]
)
11 changes: 5 additions & 6 deletions Sources/LibAuk/LibAuk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import Foundation
import Combine
import LibWally

public class LibAuk {

Expand All @@ -33,15 +32,15 @@ public class LibAuk {
}

public func calculateEthFirstAddress(words: [String], passphrase: String) -> AnyPublisher<String, Error> {
Future<BIP39Mnemonic, Error> { promise in
Future<BIP39, Error> { promise in
guard let entropy = Keys.entropy(words),
let mnemonic = Keys.mnemonic(entropy) else {
let bip39 = Keys.mnemonic(entropy) else {
promise(.failure(LibAukError.invalidMnemonicError))
return
}
promise(.success(mnemonic))
}.tryMap({ mnemonic in
let ethPrivateKey = try Keys.ethereumPrivateKey(mnemonic: mnemonic, passphrase: passphrase)
promise(.success(bip39))
}.tryMap({ bip39 in
let ethPrivateKey = try Keys.ethereumPrivateKey(bip39: bip39, passphrase: passphrase)
return ethPrivateKey.address.hex(eip55: true)
})
.eraseToAnyPublisher()
Expand Down
77 changes: 39 additions & 38 deletions Sources/LibAuk/Storage/SecureStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation
import Combine
import LibWally
import BCFoundation
import Web3
import KukaiCoreSwift
import Base58Swift
Expand Down Expand Up @@ -54,13 +54,13 @@ class SecureStorage: SecureStorageProtocol {
self.keychain = keychain
}

private func generateEthAddresses(mnemonic: BIP39Mnemonic, passphrase: String?, start: Int = 0, end: Int = 100) -> [Int: String] {
private func generateEthAddresses(bip39: BIP39, passphrase: String?, start: Int = 0, end: Int = 100) -> [Int: String] {
var ethAddresses: [Int: String] = [:]

for index in start...end {
do {
// Generate Ethereum private key for the current index
let privateKey = try Keys.ethereumPrivateKeyWithIndex(mnemonic: mnemonic, passphrase: passphrase, index: index)
let privateKey = try Keys.ethereumPrivateKeyWithIndex(bip39: bip39, passphrase: passphrase, index: index)

// Derive Ethereum address from the private key
let address = privateKey.address.hex(eip55: true)
Expand All @@ -76,12 +76,12 @@ class SecureStorage: SecureStorageProtocol {
return ethAddresses
}

private func generateTezosPublicKeys(mnemonic: BIP39Mnemonic, passphrase: String?, start: Int, end: Int) -> [Int: String] {
private func generateTezosPublicKeys(bip39: BIP39, passphrase: String?, start: Int, end: Int) -> [Int: String] {
var tezosPublicKeys: [Int: String] = [:]

for index in start...end {
// Generate Tezos wallet for the current index
let tezosWallet = Keys.tezosWalletWithIndex(mnemonic: mnemonic, passphrase: passphrase, index: index)
let tezosWallet = Keys.tezosWalletWithIndex(bip39: bip39, passphrase: passphrase, index: index)

// Get the public key for the Tezos wallet
let publicKey = tezosWallet?.publicKeyBase58encoded()
Expand All @@ -93,8 +93,8 @@ class SecureStorage: SecureStorageProtocol {
return tezosPublicKeys
}

private func generateEthAddress(mnemonic: BIP39Mnemonic, passphrase: String?) throws -> String {
let ethPrivateKey = try Keys.ethereumPrivateKey(mnemonic: mnemonic, passphrase: passphrase)
private func generateEthAddress(bip39: BIP39, passphrase: String?) throws -> String {
let ethPrivateKey = try Keys.ethereumPrivateKey(bip39: bip39, passphrase: passphrase)
let ethAddress = ethPrivateKey.address.hex(eip55: true)
return ethAddress
}
Expand All @@ -106,28 +106,28 @@ class SecureStorage: SecureStorageProtocol {
let name = seed.name

/* accountDidKey */
let mnemonic = Keys.mnemonic(seed.data)!
let bip39 = Keys.mnemonic(seed.data)!
let passphrase = seed.passphrase
let privateKey = try Keys.accountDIDPrivateKey(mnemonic: mnemonic)
let privateKey = try Keys.accountDIDPrivateKey(bip39: bip39)

// Multicodec encoded with prefix 0xe7
var bytes: [UInt8] = [231, 1]
bytes.append(contentsOf: privateKey.publicKey.rawRepresentation.bytes)
let did = "did:key:z\(Base58.base58Encode(bytes))"

/* pre-generate 100 eth addresses */
let ethAddresses = self.generateEthAddresses(mnemonic: mnemonic, passphrase: passphrase, start: 0, end: self.preGenerateAddressLimit)
let ethAddresses = self.generateEthAddresses(bip39: bip39, passphrase: passphrase, start: 0, end: self.preGenerateAddressLimit)

/* encryptionPrivateKey */
let encryptionPrivateKey = try Keys.encryptionPrivateKey(mnemonic: mnemonic, passphrase: passphrase)
let encryptionPrivateKey = try Keys.encryptionPrivateKey(bip39: bip39, passphrase: passphrase)

/* accountDIDPrivateKey */
let accountDIDPrivateKey = try Keys.accountDIDPrivateKey(mnemonic: mnemonic, passphrase: passphrase)
let accountDIDPrivateKey = try Keys.accountDIDPrivateKey(bip39: bip39, passphrase: passphrase)

/* tezos public key */
let tezosPublicKeys = self.generateTezosPublicKeys(mnemonic: mnemonic, passphrase: passphrase, start: 0, end: self.preGenerateAddressLimit)
let tezosPublicKeys = self.generateTezosPublicKeys(bip39: bip39, passphrase: passphrase, start: 0, end: self.preGenerateAddressLimit)

let ethAddress = try self.generateEthAddress(mnemonic: mnemonic, passphrase: passphrase)
let ethAddress = try self.generateEthAddress(bip39: bip39, passphrase: passphrase)

var seedPublicData = SeedPublicData(ethAddress: ethAddress,
creationDate: Date(),
Expand Down Expand Up @@ -164,6 +164,7 @@ class SecureStorage: SecureStorageProtocol {
return
}


guard let entropy = KeyCreator.createEntropy() else {
promise(.failure(LibAukError.keyCreationError))
return
Expand Down Expand Up @@ -320,13 +321,13 @@ class SecureStorage: SecureStorageProtocol {
promise(.success(seed))
}
.compactMap { seed in
guard let mnemonic = Keys.mnemonic(seed.data) else {
guard let bip39 = Keys.mnemonic(seed.data) else {
return nil
}
return (mnemonic, seed.passphrase)
return (bip39, seed.passphrase)
}
.tryMap { (mnemonic: BIP39Mnemonic, passphrase: String?) in
let ethPrivateKey = try Keys.ethereumPrivateKeyWithIndex(mnemonic: mnemonic, passphrase: passphrase, index: index)
.tryMap { (bip39: BIP39, passphrase: String?) in
let ethPrivateKey = try Keys.ethereumPrivateKeyWithIndex(bip39: bip39, passphrase: passphrase, index: index)
return ethPrivateKey.address.hex(eip55: true)
}
.eraseToAnyPublisher()
Expand Down Expand Up @@ -358,13 +359,13 @@ class SecureStorage: SecureStorageProtocol {
promise(.success(seed))
}
.compactMap { seed in
guard let mnemonic = Keys.mnemonic(seed.data) else {
guard let bip39 = Keys.mnemonic(seed.data) else {
return nil
}
return (mnemonic, seed.passphrase)
return (bip39, seed.passphrase)
}
.tryMap { (mnemonic: BIP39Mnemonic, passphrase: String?) in
let ethPrivateKey = try Keys.ethereumPrivateKey(mnemonic: mnemonic, passphrase: passphrase)
.tryMap { (bip39: BIP39, passphrase: String?) in
let ethPrivateKey = try Keys.ethereumPrivateKey(bip39: bip39, passphrase: passphrase)
return try ethPrivateKey.sign(message: message)
}
.eraseToAnyPublisher()
Expand All @@ -381,13 +382,13 @@ class SecureStorage: SecureStorageProtocol {
promise(.success(seed))
}
.compactMap { seed in
guard let mnemonic = Keys.mnemonic(seed.data) else {
guard let bip39 = Keys.mnemonic(seed.data) else {
return nil
}
return (mnemonic, seed.passphrase)
return (bip39, seed.passphrase)
}
.tryMap { (mnemonic: BIP39Mnemonic, passphrase: String?) in
let ethPrivateKey = try Keys.ethereumPrivateKeyWithIndex(mnemonic: mnemonic, passphrase: passphrase, index: index)
.tryMap { (bip39: BIP39, passphrase: String?) in
let ethPrivateKey = try Keys.ethereumPrivateKeyWithIndex(bip39: bip39, passphrase: passphrase, index: index)
return try ethPrivateKey.sign(message: message)
}
.eraseToAnyPublisher()
Expand All @@ -404,13 +405,13 @@ class SecureStorage: SecureStorageProtocol {
promise(.success(seed))
}
.compactMap { seed in
guard let mnemonic = Keys.mnemonic(seed.data) else {
guard let bip39 = Keys.mnemonic(seed.data) else {
return nil
}
return (mnemonic, seed.passphrase)
return (bip39, seed.passphrase)
}
.tryMap { (mnemonic: BIP39Mnemonic, passphrase: String?) in
let ethPrivateKey = try Keys.ethereumPrivateKey(mnemonic: mnemonic, passphrase: passphrase)
.tryMap { (bip39: BIP39, passphrase: String?) in
let ethPrivateKey = try Keys.ethereumPrivateKey(bip39: bip39, passphrase: passphrase)

return try transaction.sign(with: ethPrivateKey, chainId: chainId)
}
Expand All @@ -428,13 +429,13 @@ class SecureStorage: SecureStorageProtocol {
promise(.success(seed))
}
.compactMap { seed in
guard let mnemonic = Keys.mnemonic(seed.data) else {
guard let bip39 = Keys.mnemonic(seed.data) else {
return nil
}
return (mnemonic, seed.passphrase)
return (bip39, seed.passphrase)
}
.tryMap { (mnemonic: BIP39Mnemonic, passphrase: String?) in
let ethPrivateKey = try Keys.ethereumPrivateKeyWithIndex(mnemonic: mnemonic, passphrase: passphrase, index: index)
.tryMap { (bip39: BIP39, passphrase: String?) in
let ethPrivateKey = try Keys.ethereumPrivateKeyWithIndex(bip39: bip39, passphrase: passphrase, index: index)

return try transaction.sign(with: ethPrivateKey, chainId: chainId)
}
Expand Down Expand Up @@ -616,8 +617,8 @@ class SecureStorage: SecureStorageProtocol {
}
return (mnemonic, seed.passphrase)
}
.compactMap { (mnemonic: BIP39Mnemonic, passphrase: String?) in
Keys.tezosWallet(mnemonic: mnemonic, passphrase: passphrase)
.compactMap { (bip39: BIP39, passphrase: String?) in
Keys.tezosWallet(bip39: bip39, passphrase: passphrase)
}
.eraseToAnyPublisher()
}
Expand All @@ -638,8 +639,8 @@ class SecureStorage: SecureStorageProtocol {
}
return (mnemonic, seed.passphrase)
}
.compactMap { (mnemonic: BIP39Mnemonic, passphrase: String?) in
Keys.tezosWalletWithIndex(mnemonic: mnemonic, passphrase: passphrase, index: index)
.compactMap { (bip39: BIP39, passphrase: String?) in
Keys.tezosWalletWithIndex(bip39: bip39, passphrase: passphrase, index: index)
}
.eraseToAnyPublisher()
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/LibAuk/Utils/KeyCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
//

import Foundation
import LibWally
import BCFoundation

class KeyCreator {

static func createEntropy() -> Data? {
guard let hex = Data.secureRandom(16)?.hexString,
let entropy = try? BIP39Mnemonic.Entropy(hex: hex) else {
let entropy = try? BIP39(hex: hex) else {
return nil
}

Expand Down
Loading

0 comments on commit f32feb7

Please sign in to comment.