move logic to model

This commit is contained in:
Iuri Matias 2020-05-21 11:42:52 -04:00
parent 81a478d797
commit ff25fc818a
3 changed files with 105 additions and 95 deletions

View File

@ -13,93 +13,6 @@ import uuids
import eventemitter import eventemitter
import view import view
proc storeAccountAndLogin(model: AccountModel, selectedAccountIndex: int, password: string): string =
# let account = to(json.parseJson(selectedAccount), Models.GeneratedAccount)
echo "account index selected is "
echo selectedAccountIndex
let account: GeneratedAccount = model.generatedAddresses[selectedAccountIndex]
echo "account selected is "
echo account
let password = "0x" & $keccak_256.digest(password)
let multiAccount = %* {
"accountID": account.id,
"paths": [constants.PATH_WALLET_ROOT, constants.PATH_EIP_1581, constants.PATH_WHISPER,
constants.PATH_DEFAULT_WALLET],
"password": password
}
let storeResult = $libstatus.multiAccountStoreDerivedAccounts($multiAccount);
let multiAccounts = storeResult.parseJson
let whisperPubKey = account.derived[constants.PATH_WHISPER]["publicKey"].getStr
let alias = $libstatus.generateAlias(whisperPubKey.toGoString)
let identicon = $libstatus.identicon(whisperPubKey.toGoString)
let accountData = %* {
"name": alias,
"address": account.address,
"photo-path": identicon,
"key-uid": account.keyUid,
"keycard-pairing": nil
}
var nodeConfig = constants.NODE_CONFIG
let defaultNetworks = constants.DEFAULT_NETWORKS
let settingsJSON = %* {
"key-uid": account.keyUid,
"mnemonic": account.mnemonic,
"public-key": multiAccounts[constants.PATH_WHISPER]["publicKey"].getStr,
"name": alias,
"address": account.address,
"eip1581-address": multiAccounts[constants.PATH_EIP_1581]["address"].getStr,
"dapps-address": multiAccounts[constants.PATH_DEFAULT_WALLET]["address"].getStr,
"wallet-root-address": multiAccounts[constants.PATH_WALLET_ROOT]["address"].getStr,
"preview-privacy?": true,
"signing-phrase": generateSigningPhrase(3),
"log-level": "INFO",
"latest-derived-path": 0,
"networks/networks": $defaultNetworks,
"currency": "usd",
"photo-path": identicon,
"waku-enabled": true,
"wallet/visible-tokens": {
"mainnet": ["SNT"]
},
"appearance": 0,
"networks/current-network": "mainnet_rpc",
"installation-id": $genUUID()
}
let subaccountData = %* [
{
"public-key": multiAccounts[constants.PATH_DEFAULT_WALLET]["publicKey"],
"address": multiAccounts[constants.PATH_DEFAULT_WALLET]["address"],
"color": "#4360df",
"wallet": true,
"path": constants.PATH_DEFAULT_WALLET,
"name": "Status account"
},
{
"public-key": multiAccounts[constants.PATH_WHISPER]["publicKey"],
"address": multiAccounts[constants.PATH_WHISPER]["address"],
"name": alias,
"photo-path": identicon,
"path": constants.PATH_WHISPER,
"chat": true
}
]
result = $libstatus.saveAccountAndLogin($accountData, password, $settingsJSON,
$nodeConfig, $subaccountData)
let saveResult = result.parseJson
if saveResult["error"].getStr == "":
model.events.emit("node:ready", Args())
echo "Account saved succesfully"
# TODO: this is temporary and will be removed once accounts import and creation is working
proc generateRandomAccountAndLogin*(events: EventEmitter) =
discard status_test.setupNewAccount()
events.emit("node:ready", Args())
type OnboardingController* = ref object of SignalSubscriber type OnboardingController* = ref object of SignalSubscriber
view*: OnboardingView view*: OnboardingView
variant*: QVariant variant*: QVariant
@ -109,7 +22,8 @@ proc newController*(events: EventEmitter): OnboardingController =
result = OnboardingController() result = OnboardingController()
# TODO: events should be specific to the model itself # TODO: events should be specific to the model itself
result.model = newAccountModel(events) result.model = newAccountModel(events)
result.view = newOnboardingView(result.model, storeAccountAndLogin, generateRandomAccountAndLogin) # result.view = newOnboardingView(result.model, storeAccountAndLogin, generateRandomAccountAndLogin)
result.view = newOnboardingView(result.model)
result.variant = newQVariant(result.view) result.variant = newQVariant(result.view)
proc delete*(self: OnboardingController) = proc delete*(self: OnboardingController) =

View File

@ -26,8 +26,8 @@ QtObject:
addresses*: seq[Address] addresses*: seq[Address]
model: AccountModel model: AccountModel
# m_generatedAddresses: string # m_generatedAddresses: string
doStoreAccountAndLogin: proc(model: AccountModel, selectedAccount: int, password: string): string # doStoreAccountAndLogin: proc(model: AccountModel, selectedAccount: int, password: string): string
doGenerateRandomAccountAndLogin: proc(events: EventEmitter) # doGenerateRandomAccountAndLogin: proc(events: EventEmitter)
proc setup(self: OnboardingView) = proc setup(self: OnboardingView) =
self.QAbstractListModel.setup self.QAbstractListModel.setup
@ -38,11 +38,12 @@ QtObject:
address.delete address.delete
self.addresses = @[] self.addresses = @[]
proc newOnboardingView*(model: AccountModel, doStoreAccountAndLogin: proc, doGenerateRandomAccountAndLogin: proc(events: EventEmitter)): OnboardingView = # proc newOnboardingView*(model: AccountModel, doStoreAccountAndLogin: proc, doGenerateRandomAccountAndLogin: proc(events: EventEmitter)): OnboardingView =
proc newOnboardingView*(model: AccountModel): OnboardingView =
new(result, delete) new(result, delete)
result.model = model result.model = model
result.doStoreAccountAndLogin = doStoreAccountAndLogin # result.doStoreAccountAndLogin = doStoreAccountAndLogin
result.doGenerateRandomAccountAndLogin = doGenerateRandomAccountAndLogin # result.doGenerateRandomAccountAndLogin = doGenerateRandomAccountAndLogin
result.addresses = @[] result.addresses = @[]
result.setup result.setup
@ -109,8 +110,8 @@ QtObject:
echo "--------------------" echo "--------------------"
echo "--------------------" echo "--------------------"
# var selectedAccountIndex = self.addresses # var selectedAccountIndex = self.addresses
result = self.doStoreAccountAndLogin(self.model, selectedAccountIndex, password) result = self.model.storeAccountAndLogin(selectedAccountIndex, password)
# TODO: this is temporary and will be removed once accounts import and creation is working # TODO: this is temporary and will be removed once accounts import and creation is working
proc generateRandomAccountAndLogin*(self: OnboardingView) {.slot.} = proc generateRandomAccountAndLogin*(self: OnboardingView) {.slot.} =
self.doGenerateRandomAccountAndLogin(self.model.events) self.model.generateRandomAccountAndLogin()

View File

@ -2,8 +2,16 @@ import json
import eventemitter import eventemitter
import ../status/libstatus import ../status/libstatus
import ../status/accounts as status_accounts import ../status/accounts as status_accounts
import ../status/test as status_test
import ../constants/constants import ../constants/constants
import ../status/utils import ../status/utils
import nimcrypto
import ../status/utils
import ../status/libstatus
import ../constants/constants
# import "../../status/core" as status
import ../app/signals/types
import uuids
type type
GeneratedAccount* = object GeneratedAccount* = object
@ -59,3 +67,90 @@ proc generateAddresses*(self: AccountModel): seq[GeneratedAccount] =
self.generatedAddresses.add(generatedAccount) self.generatedAddresses.add(generatedAccount)
self.generatedAddresses self.generatedAddresses
# TODO: this is temporary and will be removed once accounts import and creation is working
proc generateRandomAccountAndLogin*(self: AccountModel) =
discard status_test.setupNewAccount()
self.events.emit("node:ready", Args())
proc storeAccountAndLogin*(self: AccountModel, selectedAccountIndex: int, password: string): string =
# let account = to(json.parseJson(selectedAccount), Models.GeneratedAccount)
echo "account index selected is "
echo selectedAccountIndex
let account: GeneratedAccount = self.generatedAddresses[selectedAccountIndex]
echo "account selected is "
echo account
let password = "0x" & $keccak_256.digest(password)
let multiAccount = %* {
"accountID": account.id,
"paths": [constants.PATH_WALLET_ROOT, constants.PATH_EIP_1581, constants.PATH_WHISPER,
constants.PATH_DEFAULT_WALLET],
"password": password
}
let storeResult = $libstatus.multiAccountStoreDerivedAccounts($multiAccount);
let multiAccounts = storeResult.parseJson
let whisperPubKey = account.derived[constants.PATH_WHISPER]["publicKey"].getStr
let alias = $libstatus.generateAlias(whisperPubKey.toGoString)
let identicon = $libstatus.identicon(whisperPubKey.toGoString)
let accountData = %* {
"name": alias,
"address": account.address,
"photo-path": identicon,
"key-uid": account.keyUid,
"keycard-pairing": nil
}
var nodeConfig = constants.NODE_CONFIG
let defaultNetworks = constants.DEFAULT_NETWORKS
let settingsJSON = %* {
"key-uid": account.keyUid,
"mnemonic": account.mnemonic,
"public-key": multiAccounts[constants.PATH_WHISPER]["publicKey"].getStr,
"name": alias,
"address": account.address,
"eip1581-address": multiAccounts[constants.PATH_EIP_1581]["address"].getStr,
"dapps-address": multiAccounts[constants.PATH_DEFAULT_WALLET]["address"].getStr,
"wallet-root-address": multiAccounts[constants.PATH_WALLET_ROOT]["address"].getStr,
"preview-privacy?": true,
"signing-phrase": generateSigningPhrase(3),
"log-level": "INFO",
"latest-derived-path": 0,
"networks/networks": $defaultNetworks,
"currency": "usd",
"photo-path": identicon,
"waku-enabled": true,
"wallet/visible-tokens": {
"mainnet": ["SNT"]
},
"appearance": 0,
"networks/current-network": "mainnet_rpc",
"installation-id": $genUUID()
}
let subaccountData = %* [
{
"public-key": multiAccounts[constants.PATH_DEFAULT_WALLET]["publicKey"],
"address": multiAccounts[constants.PATH_DEFAULT_WALLET]["address"],
"color": "#4360df",
"wallet": true,
"path": constants.PATH_DEFAULT_WALLET,
"name": "Status account"
},
{
"public-key": multiAccounts[constants.PATH_WHISPER]["publicKey"],
"address": multiAccounts[constants.PATH_WHISPER]["address"],
"name": alias,
"photo-path": identicon,
"path": constants.PATH_WHISPER,
"chat": true
}
]
result = $libstatus.saveAccountAndLogin($accountData, password, $settingsJSON,
$nodeConfig, $subaccountData)
let saveResult = result.parseJson
if saveResult["error"].getStr == "":
self.events.emit("node:ready", Args())
echo "Account saved succesfully"