Skip to content

Commit f794d34

Browse files
Ravi RanjanRavi Ranjan
authored andcommitted
Updated wallet creat and Private key get helper method
1 parent d91e444 commit f794d34

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//
2+
// CreateWalletWrapper.swift
3+
// Wallet
4+
//
5+
// Created by Ravi Ranjan on 22/08/21.
6+
// Copyright © 2021 [email protected]. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import Foundation
11+
import web3swift
12+
class CreateWalletWrapper: NSObject {
13+
/*
14+
Create wallet method
15+
*/
16+
var prefixPath: String
17+
init(prefixPath:String) {
18+
self.prefixPath = prefixPath
19+
}
20+
21+
public func createWallet() throws {
22+
23+
var web3KeyStore: BIP32Keystore?
24+
let userDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
25+
let web3KeystoreManager = KeystoreManager.managerForPath(userDir + "/keystore")
26+
do {
27+
if (web3KeystoreManager?.addresses.count ?? 0 >= 0) {
28+
let web3Mnemonics = Mnemonics(entropySize: EntropySize.b128, language: .english)
29+
print(web3Mnemonics.description)
30+
/*
31+
save Mnemonics for later user also it can be stored some other place
32+
*/
33+
UserDefaults.standard.set(web3Mnemonics.description, forKey: KeyConstants.UserDefaults.kUserMnemonics)
34+
web3KeyStore = try BIP32Keystore(mnemonics: web3Mnemonics, prefixPath: prefixPath)
35+
print("keystore", web3KeyStore as Any)
36+
guard let kStore = web3KeyStore else {
37+
return
38+
}
39+
let address = kStore.addresses.first
40+
let param = kStore.keystoreParams
41+
#if DEBUG
42+
print("Mnemonics :-> ", web3Mnemonics.description)
43+
print("Address :::>>>>> ", address as Any)
44+
print("Address :::>>>>> ", kStore.addresses as Any)
45+
let privatKey = try kStore.UNSAFE_getPrivateKeyData(password: "", account: address!)
46+
print(privatKey, "Is the private key")
47+
#endif
48+
let keyData = try? JSONEncoder().encode(param)
49+
FileManager.default.createFile(atPath: userDir + "/keystore"+"/key.json", contents: keyData, attributes: nil)
50+
} else {
51+
web3KeyStore = web3KeystoreManager?.walletForAddress((web3KeystoreManager?.addresses[0])!) as? BIP32Keystore
52+
}
53+
} catch {
54+
throw WalletCreationError.error
55+
}
56+
}
57+
58+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// KeystoreWrapper.swift
3+
// Wethio Wallet
4+
//
5+
// Created by Ravi Ranjan on 22/08/21.
6+
// Copyright © 2021 [email protected]. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import web3swift
11+
12+
class KeystoreWrapper: NSObject {
13+
/// Get KeyStore manager Instance
14+
///
15+
/// - Parameters: nil
16+
/// - Returns: Key store manager
17+
18+
static func getKeystoreManager () -> KeystoreManager? {
19+
let userDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
20+
let path = userDir+"/keystore/"
21+
return KeystoreManager.managerForPath(path, scanForHDwallets: true, suffix: "json")
22+
23+
}
24+
/// Get Wallet address
25+
///
26+
/// - Parameters:
27+
/// - Returns: Current wallet address
28+
static func getWalletAddress () -> String? {
29+
return getKeystoreManager()?.addresses.first?.address
30+
}
31+
32+
}

0 commit comments

Comments
 (0)