Skip to content

Commit

Permalink
Merge pull request #10 from bitmark-inc/tezos_support
Browse files Browse the repository at this point in the history
Tezos support
  • Loading branch information
anhnguyenbitmark authored Nov 10, 2021
2 parents 8622e18 + 85f37de commit 83d99b1
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Build
run: swift build -v
run: xcodebuild -scheme LibAuk build -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 12'
- name: Run tests
run: swift test -v
run: xcodebuild -scheme LibAuk test -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 12'
4 changes: 3 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import PackageDescription

let package = Package(
name: "LibAuk",
platforms: [.macOS(.v10_15), .iOS(.v14)],
platforms: [.iOS(.v14)],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
Expand All @@ -15,6 +15,7 @@ let package = Package(
dependencies: [
.package(name: "Web3", url: "https://github.com/bitmark-inc/Web3.swift.git", .branch("master")),
.package(url: "https://github.com/BlockchainCommons/URKit.git", .exact("2.4.0")),
.package(name: "KukaiCoreSwift", url: "https://github.com/kukai-wallet/kukai-core-swift", from: "0.1.0")
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
Expand All @@ -27,6 +28,7 @@ let package = Package(
.target(name: "LibWally"),
.product(name: "Web3", package: "Web3"),
.product(name: "URKit", package: "URKit"),
.product(name: "KukaiCoreSwift", package: "KukaiCoreSwift"),
]),
.testTarget(
name: "LibAukTests",
Expand Down
2 changes: 1 addition & 1 deletion Sources/LibAuk/LibAuk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by Ho Hien on 8/9/21.
//

import LibWally
import Foundation

public class LibAuk {

Expand Down
21 changes: 21 additions & 0 deletions Sources/LibAuk/Storage/SecureStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Foundation
import Combine
import LibWally
import Web3
import KukaiCoreSwift

public protocol SecureStorageProtocol {
func createKey(name: String) -> AnyPublisher<Void, Error>
Expand All @@ -19,6 +20,7 @@ public protocol SecureStorageProtocol {
func getETHAddress() -> String?
func sign(message: Bytes) -> AnyPublisher<(v: UInt, r: Bytes, s: Bytes), Error>
func signTransaction(transaction: EthereumTransaction, chainId: EthereumQuantity) -> AnyPublisher<EthereumSignedTransaction, Error>
func getTezosWallet() -> AnyPublisher<Wallet, Error>
func exportSeed() -> AnyPublisher<Seed, Error>
func exportMnemonicWords() -> AnyPublisher<[String], Error>
func removeKeys() -> AnyPublisher<Void, Error>
Expand Down Expand Up @@ -179,6 +181,25 @@ class SecureStorage: SecureStorageProtocol {
.eraseToAnyPublisher()
}

func getTezosWallet() -> AnyPublisher<Wallet, Error> {
Future<Seed, Error> { promise in
guard let seedUR = self.keychain.getData(Constant.KeychainKey.seed, isSync: true),
let seed = try? Seed(urString: seedUR.utf8) else {
promise(.failure(LibAukError.emptyKey))
return
}

promise(.success(seed))
}
.compactMap {
Keys.mnemonic($0.data)
}
.compactMap {
Keys.tezosWallet(mnemonic: $0)
}
.eraseToAnyPublisher()
}

func exportSeed() -> AnyPublisher<Seed, Error> {
Future<Seed, Error> { promise in
guard let seedUR = self.keychain.getData(Constant.KeychainKey.seed, isSync: true),
Expand Down
5 changes: 5 additions & 0 deletions Sources/LibAuk/Utils/Keys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import Foundation
import LibWally
import Web3
import KukaiCoreSwift

class Keys {

Expand Down Expand Up @@ -58,4 +59,8 @@ class Keys {

return try EthereumPrivateKey(privateKey)
}

static func tezosWallet(mnemonic: BIP39Mnemonic, passphrase: String = "") -> HDWallet? {
HDWallet.create(withMnemonic: mnemonic.words.joined(separator: " "), passphrase: passphrase)
}
}
29 changes: 29 additions & 0 deletions Tests/LibAukTests/Storage/SecureStorage_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,35 @@ class SecureStorage_Tests: XCTestCase {
waitForExpectations(timeout: 1, handler: nil)
}

func testGetTezosWalletSuccessfully() throws {
let words = "daring mix cradle palm crowd sea observe whisper rubber either uncle oak"
let seed = Seed(data: Keys.entropy(words)!, name: "account1", creationDate: Date(timeIntervalSince1970: 1628656699))
let seedData = seed.urString.utf8
keychain.set(seedData, forKey: Constant.KeychainKey.seed, isSync: true)

let keyInfo = KeyInfo(fingerprint: "0a3df912", ethAddress: "0xA00cbE6a45102135A210F231901faA6c05D51465", creationDate: Date(timeIntervalSince1970: 1628656699))
let keyInfoData = try JSONEncoder().encode(keyInfo)
keychain.set(keyInfoData, forKey: Constant.KeychainKey.ethInfoKey, isSync: true)

let receivedExpectation = expectation(description: "all values received")

storage.getTezosWallet()
.sink(receiveCompletion: { completion in
switch completion {
case .finished:
receivedExpectation.fulfill()
case .failure(let error):
XCTFail("exportSeed failed \(error)")
}

}, receiveValue: { wallet in
XCTAssertEqual(wallet.address, "tz1TK8o3WKrPLKGwMgsdn3duPtT9tJcdh3FQ")
})
.store(in: &cancelBag)

waitForExpectations(timeout: 1, handler: nil)
}

func testExportSeedSuccessfully() throws {
let words = "daring mix cradle palm crowd sea observe whisper rubber either uncle oak"
let seed = Seed(data: Keys.entropy(words)!, name: "account1", creationDate: Date(timeIntervalSince1970: 1628656699))
Expand Down

0 comments on commit 83d99b1

Please sign in to comment.