mirror of
https://github.com/lil-org/tokenary.git
synced 2024-12-12 12:33:42 +03:00
Support importing mnemonic
This commit is contained in:
parent
ca52a484b5
commit
fe831129a2
@ -736,7 +736,7 @@
|
||||
<constraint firstAttribute="width" constant="160" id="Pi7-Hn-MhX"/>
|
||||
<constraint firstAttribute="height" constant="120" id="WsU-g6-Y2a"/>
|
||||
</constraints>
|
||||
<textFieldCell key="cell" controlSize="large" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" state="on" borderStyle="border" focusRingType="none" alignment="left" placeholderString="Ethereum Private Key" drawsBackground="YES" id="qLb-bZ-IWI">
|
||||
<textFieldCell key="cell" controlSize="large" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" state="on" borderStyle="border" focusRingType="none" alignment="left" placeholderString="Ethereum Private Key or Secret Words" drawsBackground="YES" id="qLb-bZ-IWI">
|
||||
<font key="font" metaFont="system"/>
|
||||
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
|
||||
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
|
||||
|
@ -18,7 +18,7 @@ class ImportViewController: NSViewController {
|
||||
}
|
||||
|
||||
@IBAction func actionButtonTapped(_ sender: Any) {
|
||||
let account = AccountsService.addAccount(privateKey: textField.stringValue)
|
||||
let account = AccountsService.addAccount(input: textField.stringValue)
|
||||
if let account = account, AccountsService.getAccounts().count == 1, let onSelectedAccount = onSelectedAccount {
|
||||
onSelectedAccount(account)
|
||||
} else {
|
||||
@ -41,7 +41,7 @@ class ImportViewController: NSViewController {
|
||||
extension ImportViewController: NSTextFieldDelegate {
|
||||
|
||||
func controlTextDidChange(_ obj: Notification) {
|
||||
okButton.isEnabled = AccountsService.validateAccountKey(textField.stringValue)
|
||||
okButton.isEnabled = AccountsService.validateAccountInput(textField.stringValue)
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,20 +5,29 @@ import WalletCore
|
||||
|
||||
struct AccountsService {
|
||||
|
||||
static func validateAccountKey(_ key: String) -> Bool {
|
||||
if let data = Data(hexString: key) {
|
||||
static func validateAccountInput(_ input: String) -> Bool {
|
||||
if Mnemonic.isValid(mnemonic: input) {
|
||||
return true
|
||||
} else if let data = Data(hexString: input) {
|
||||
return PrivateKey.isValid(data: data, curve: CoinType.ethereum.curve)
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
static func addAccount(privateKey: String) -> Account? {
|
||||
guard let data = Data(hexString: privateKey),
|
||||
let key = PrivateKey(data: data) else { return nil }
|
||||
static func addAccount(input: String) -> Account? {
|
||||
let key: PrivateKey
|
||||
if Mnemonic.isValid(mnemonic: input) {
|
||||
key = HDWallet(mnemonic: input, passphrase: "").getKeyForCoin(coin: .ethereum)
|
||||
} else if let data = Data(hexString: input), let privateKey = PrivateKey(data: data) {
|
||||
key = privateKey
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let address = CoinType.ethereum.deriveAddress(privateKey: key).lowercased()
|
||||
// TODO: use checksum address
|
||||
let account = Account(privateKey: privateKey, address: address)
|
||||
let account = Account(privateKey: key.data.hexString, address: address)
|
||||
var accounts = getAccounts()
|
||||
guard !accounts.contains(where: { $0.address == address }) else { return nil }
|
||||
accounts.append(account)
|
||||
|
Loading…
Reference in New Issue
Block a user