|
| 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 | +} |
0 commit comments