Add StatusConfig and more methods to StatusObject

This commit is contained in:
Vitaliy Vlasov 2021-03-19 17:17:27 +02:00 committed by Michael Bradley, Jr
parent 91376957b0
commit 32376442ec
6 changed files with 123 additions and 19 deletions

View File

@ -225,6 +225,10 @@ else
endif
endif
client:
$(ENV_SCRIPT) nim c $(NIM_PARAMS) -o:build/nim_status nim_status/client.nim
ifeq ($(detected_OS),Linux)
PLATFORM_FLAGS_TEST_C ?= -ldl
else ifeq ($(detected_OS),macOS)

View File

@ -1,41 +1,53 @@
import # nim libs
os
os, json
import # vendor libs
confutils,
json_serialization,
sqlcipher
import # nim-status libs
./accounts,
./chats,
./config,
./database,
./migrations/sql_scripts_accounts as acc_migration,
./migrations/sql_scripts_app as app_migration
./migrations/sql_scripts_app as app_migration,
./settings
type StatusObject* = ref object
rootDataDir: string
config*: StatusConfig
accountsDB*: DbConn
userDB*: DbConn
proc init*(dataDir: string): StatusObject =
proc init*(config: StatusConfig): StatusObject =
result = new StatusObject
result.rootDataDir = dataDir
result.accountsDB = initializeDB(dataDir / "accounts.sql", acc_migration.newMigrationDefinition(), false) # Disabling migrations because we are reusing a status-go DB
result.config = config
result.accountsDB = initializeDB(config.rootDataDir / config.accountsDbFileName, acc_migration.newMigrationDefinition(), true) # Disabling migrations because we are reusing a status-go DB
proc openAccounts*(self: StatusObject): seq[Account] =
proc getAccounts*(self: StatusObject): seq[Account] =
getAccounts(self.accountsDB)
proc openUserDB*(self: StatusObject, keyUid: string, password: string) =
self.userDB = initializeDB(self.rootDataDir / keyUid & ".db", password, app_migration.newMigrationDefinition(), false) # Disabling migrations because we are reusing a status-go DB
self.userDB.execScript("PRAGMA cipher_page_size = 1024");
self.userDB.execScript("PRAGMA kdf_iter = 3200");
self.userDB.execScript("PRAGMA cipher_hmac_algorithm = HMAC_SHA1");
self.userDB.execScript("PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1");
proc saveAccount*(self: StatusObject, account: Account) =
saveAccount(self.accountsDB, account)
proc updateAccountTimestamp*(self: StatusObject, timestamp: int64, keyUid: string) =
updateAccountTimestamp(self.accountsDB, timestamp, keyUid)
proc createSettings*(self: StatusObject, settings: Settings, nodeConfig: JsonNode) =
createSettings(self.userDB, settings, nodeConfig)
proc getSettings*(self: StatusObject): Settings =
getSettings(self.userDB)
proc login*(self: StatusObject, keyUid: string, password: string) =
self.userDB = initializeDB(self.config.rootDataDir / keyUid & ".db", password, app_migration.newMigrationDefinition(), true) # Disabling migrations because we are reusing a status-go DB
echo "==============================="
echo "DB path: " & (self.rootDataDir / keyUid & ".db")
echo "DB path: " & (self.config.rootDataDir / keyUid & ".db")
echo "Password: " & password
let result = self.userDB.value("SELECT public_key from settings") # check if decryption worked
echo "Result: "
echo result.get()
echo $result
proc closeUserDB*(self: StatusObject) =
self.userDB.close()
@ -46,3 +58,8 @@ proc loadChats*(self: StatusObject): seq[Chat] =
proc close*(self: StatusObject) =
self.userDB.close()
self.accountsDB.close()
when isMainModule:
let statusConfig = StatusConfig.load()
let statusObj = init(statusConfig)
# Start a REPL ...

11
nim_status/config.nim Normal file
View File

@ -0,0 +1,11 @@
import confutils/defs
type StatusConfig* = object
rootDataDir* {.
defaultValue: "data"
desc: "Root data directory"
abbr: "d" .}: string
accountsDbFileName* {.
defaultValue: "accounts.db"
name: "accountsDB",
desc: "Name of accounts db file under rootDataDir" .}: string

View File

@ -1,15 +1,20 @@
import results, sqlcipher
import os, results, sqlcipher
import ./migration
proc initializeDB*(path:string, definition: MigrationDefinition, runMigrations = true):DbConn =
proc initializeDB*(path:string, definition: MigrationDefinition, runMigrations = true): DbConn =
createDir path.parentDir()
result = openDatabase(path)
if runMigrations and not result.migrate(definition).isOk:
raise newException(SqliteError, "Failure executing migrations")
proc initializeDB*(path, password: string, definition: MigrationDefinition, runMigrations = true):DbConn =
proc initializeDB*(path, password: string, definition: MigrationDefinition, runMigrations = true): DbConn =
createDir path.parentDir()
result = openDatabase(path)
result.key(password)
result.execScript("PRAGMA cipher_page_size = 1024")
result.execScript("PRAGMA kdf_iter = 3200")
result.execScript("PRAGMA cipher_hmac_algorithm = HMAC_SHA1")
result.execScript("PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1")
if runMigrations and not result.migrate(definition).isOk:
raise newException(SqliteError, "Failure executing migrations")

66
test/client.nim Normal file
View File

@ -0,0 +1,66 @@
import # nim libs
json, options, os, times, unittest
import # vendor libs
chronos, confutils, json_serialization, sqlcipher, web3/conversions as web3_conversions
import # nim-status libs
../nim_status/[accounts, client, config, conversions, database, settings],
./test_helpers
procSuite "client":
asyncTest "client":
let dataDir = currentSourcePath.parentDir() / "build" / "data"
var config = StatusConfig.load(
@["--rootDataDir=" & dataDir]
)
let statusObj = init(config)
var account:Account = Account(
name: "Test",
loginTimestamp: 1,
identicon: "data:image/png;base64,something",
keycardPairing: "",
keyUid: "0x1234"
)
statusObj.saveAccount(account)
statusObj.updateAccountTimestamp(1, "0x1234")
let accounts = statusObj.getAccounts()
check:
statusObj.config.rootDataDir == dataDir
accounts[0].keyUid == "0x1234"
let password = "qwerty"
statusObj.login(account.keyUid, password)
let settingsStr = """{
"address": "0x1122334455667788990011223344556677889900",
"chaos-mode": true,
"networks/current-network": "mainnet",
"dapps-address": "0x1122334455667788990011223344556677889900",
"eip1581-address": "0x1122334455667788990011223344556677889900",
"installation-id": "ABC-DEF-GHI",
"key-uid": "XYZ",
"latest-derived-path": 0,
"networks/networks": [{"id":"mainnet_rpc","etherscan-link":"https://etherscan.io/address/","name":"Mainnet with upstream RPC","config":{"NetworkId":1,"DataDir":"/ethereum/mainnet_rpc","UpstreamConfig":{"Enabled":true,"URL":"wss://mainnet.infura.io/ws/v3/7230123556ec4a8aac8d89ccd0dd74d7"}}}],
"name": "test",
"photo-path": "ABXYZC",
"preview-privacy?": false,
"public-key": "0x123",
"signing-phrase": "ABC DEF GHI",
"wallet-root-address": "0x1122334455667788990011223344556677889900"
}"""
let settingsObj = Json.decode(settingsStr, Settings, allowUnknownFields = true)
let nodeConfig = %* {"config": 1}
statusObj.createSettings(settingsObj, nodeConfig)
let dbSettings = statusObj.getSettings()
check:
dbSettings.keyUID == settingsObj.keyUID
statusObj.close()
removeDir(datadir)

View File

@ -8,6 +8,7 @@ import
./bip32,
./callrpc,
./chats,
./client,
./contacts,
./db_smoke,