2022-01-06 19:29:19 +00:00
# include "accounts/service.h"
# include "accounts/account.h"
# include "accounts/generated_account.h"
# include "accounts/service_interface.h"
# include "app_service.h"
# include "backend/accounts.h"
# include "backend/utils.h"
# include "constants.h"
# include "signing-phrases.h"
# include <QDebug>
# include <QFile>
# include <QJsonArray>
# include <QJsonObject>
# include <QRandomGenerator>
# include <QUuid>
namespace Accounts
{
Service : : Service ( )
2022-02-21 18:03:38 +00:00
: m_isFirstTimeAccountLogin ( false )
2022-01-06 19:29:19 +00:00
{ }
const QVector < QString > PATHS { Backend : : Accounts : : PATH_WALLET_ROOT ,
2022-02-21 18:03:38 +00:00
Backend : : Accounts : : PATH_EIP_1581 ,
Backend : : Accounts : : PATH_WHISPER ,
Backend : : Accounts : : PATH_DEFAULT_WALLET } ;
2022-01-06 19:29:19 +00:00
void Service : : init ( )
{
2022-02-21 18:03:38 +00:00
auto response = Backend : : Accounts : : generateAddresses ( Accounts : : PATHS ) ;
foreach ( QJsonValue generatedAddressJson , response . m_result )
{
auto gAcc = toGeneratedAccountDto ( generatedAddressJson ) ;
gAcc . alias = generateAlias ( gAcc . derivedAccounts . whisper . publicKey ) ;
gAcc . identicon = generateIdenticon ( gAcc . derivedAccounts . whisper . publicKey ) ;
m_generatedAccounts < < gAcc ;
}
2022-01-06 19:29:19 +00:00
}
QVector < AccountDto > Service : : openedAccounts ( )
{
2022-02-21 18:03:38 +00:00
// TODO: if there's an exception, should we return an empty result? or should we look into using
// std::expected or std::optional or boost outcome https://www.boost.org/doc/libs/1_75_0/libs/outcome/doc/html/index.html
try
{
auto response = Backend : : Accounts : : openAccounts ( Constants : : applicationPath ( Constants : : DataDir ) ) ;
QJsonArray multiAccounts = response . m_result ;
QVector < AccountDto > result ;
foreach ( const QJsonValue & value , multiAccounts )
{
result < < toAccountDto ( value ) ;
}
return result ;
}
catch ( Backend : : RpcException & e )
{
qWarning ( ) < < " error: methodName=openedAccounts, errDescription= " < < e . what ( ) ;
return QVector < AccountDto > ( ) ;
}
2022-01-06 19:29:19 +00:00
}
QVector < GeneratedAccountDto > Service : : generatedAccounts ( )
{
2022-02-21 18:03:38 +00:00
if ( m_generatedAccounts . length ( ) = = 0 )
{
qWarning ( " There was some issue initiating account service " ) ;
return QVector < GeneratedAccountDto > ( ) ;
}
2022-01-06 19:29:19 +00:00
2022-02-21 18:03:38 +00:00
return m_generatedAccounts ;
2022-01-06 19:29:19 +00:00
}
bool Service : : setupAccount ( QString accountId , QString password )
{
2022-02-21 18:03:38 +00:00
// TODO: would it make sense to use std::expected or std::optional or boost outcome https://www.boost.org/doc/libs/1_75_0/libs/outcome/doc/html/index.html
try
{
QString installationId ( QUuid : : createUuid ( ) . toString ( QUuid : : WithoutBraces ) ) ;
QJsonObject accountData ( Service : : getAccountDataForAccountId ( accountId ) ) ;
QJsonArray subAccountData ( Service : : getSubaccountDataForAccountId ( accountId ) ) ;
QJsonObject settings ( Service : : getAccountSettings ( accountId , installationId ) ) ;
QJsonObject nodeConfig ( Service : : getDefaultNodeConfig ( installationId ) ) ;
QString hashedPassword ( Backend : : Utils : : hashString ( password ) ) ;
Service : : storeDerivedAccounts ( accountId , hashedPassword , PATHS ) ;
m_loggedInAccount =
Service : : saveAccountAndLogin ( hashedPassword , accountData , subAccountData , settings , nodeConfig ) ;
return Service : : getLoggedInAccount ( ) . isValid ( ) ;
}
catch ( exception & e )
{
qWarning ( ) < < " error: methodName=setupAccount, errDescription= " < < e . what ( ) ;
return false ;
}
2022-01-06 19:29:19 +00:00
}
AccountDto Service : : getLoggedInAccount ( )
{
2022-02-21 18:03:38 +00:00
return m_loggedInAccount ;
2022-01-06 19:29:19 +00:00
}
GeneratedAccountDto Service : : getImportedAccount ( )
{
2022-02-21 18:03:38 +00:00
return m_importedAccount ;
2022-01-06 19:29:19 +00:00
}
bool Service : : isFirstTimeAccountLogin ( )
{
2022-02-21 18:03:38 +00:00
return m_isFirstTimeAccountLogin ;
2022-01-06 19:29:19 +00:00
}
QString Service : : validateMnemonic ( QString mnemonic )
{
2022-02-21 18:03:38 +00:00
// TODO:
return " " ;
2022-01-06 19:29:19 +00:00
}
bool Service : : importMnemonic ( QString mnemonic )
{
2022-02-21 18:03:38 +00:00
// TODO:
return false ;
2022-01-06 19:29:19 +00:00
}
QString Service : : login ( AccountDto account , QString password )
{
2022-02-21 18:03:38 +00:00
// TODO: would it make sense to use std::expected or std::optional or boost outcome https://www.boost.org/doc/libs/1_75_0/libs/outcome/doc/html/index.html
try
{
QString hashedPassword ( Backend : : Utils : : hashString ( password ) ) ;
QString thumbnailImage ;
QString largeImage ;
foreach ( const Accounts : : Image & img , account . images )
{
if ( img . imgType = = " thumbnail " )
{
thumbnailImage = img . uri ;
}
else if ( img . imgType = = " large " )
{
largeImage = img . uri ;
}
}
auto response = Backend : : Accounts : : login (
account . name , account . keyUid , hashedPassword , account . identicon , thumbnailImage , largeImage ) ;
// TODO: check response for errors
qDebug ( ) < < " Account logged in " ;
m_loggedInAccount = account ;
return " " ;
}
catch ( exception & e )
{
qWarning ( ) < < " error: methodName=login, errDescription= " < < e . what ( ) ;
return e . what ( ) ;
}
2022-01-06 19:29:19 +00:00
}
void Service : : clear ( )
{
2022-02-21 18:03:38 +00:00
m_generatedAccounts . clear ( ) ;
m_loggedInAccount = Accounts : : AccountDto ( ) ;
m_importedAccount = Accounts : : GeneratedAccountDto ( ) ;
m_isFirstTimeAccountLogin = false ;
2022-01-06 19:29:19 +00:00
}
QString Service : : generateAlias ( QString publicKey )
{
2022-02-21 18:03:38 +00:00
return Backend : : Accounts : : generateAlias ( publicKey ) . m_result ;
2022-01-06 19:29:19 +00:00
}
QString Service : : generateIdenticon ( QString publicKey )
{
2022-02-21 18:03:38 +00:00
return Backend : : Accounts : : generateIdenticon ( publicKey ) . m_result ;
2022-01-06 19:29:19 +00:00
}
bool Service : : verifyAccountPassword ( QString account , QString password )
{
2022-02-21 18:03:38 +00:00
// TODO:
return false ;
2022-01-06 19:29:19 +00:00
}
DerivedAccounts Service : : storeDerivedAccounts ( QString accountId , QString hashedPassword , QVector < QString > paths )
{
2022-02-21 18:03:38 +00:00
try
{
auto response = Backend : : Accounts : : storeDerivedAccounts ( accountId , hashedPassword , paths ) ;
return toDerivedAccounts ( response . m_result ) ;
}
catch ( Backend : : RpcException & e )
{
qWarning ( ) < < e . what ( ) ;
return DerivedAccounts ( ) ; // TODO: should it return empty?
}
2022-01-06 19:29:19 +00:00
}
Accounts : : AccountDto Service : : saveAccountAndLogin (
2022-02-21 18:03:38 +00:00
QString hashedPassword , QJsonObject account , QJsonArray subaccounts , QJsonObject settings , QJsonObject config )
2022-01-06 19:29:19 +00:00
{
2022-02-21 18:03:38 +00:00
// TODO: would it make sense to use std::expected or std::optional or boost outcome https://www.boost.org/doc/libs/1_75_0/libs/outcome/doc/html/index.html
try
{
auto response = Backend : : Accounts : : saveAccountAndLogin ( hashedPassword , account , subaccounts , settings , config ) ;
m_isFirstTimeAccountLogin = true ;
return toAccountDto ( account ) ;
}
catch ( exception & e )
{
qWarning ( ) < < " error: methodName=saveAccountAndLogin, errDescription= " < < e . what ( ) ;
return Accounts : : AccountDto ( ) ;
}
2022-01-06 19:29:19 +00:00
}
QJsonObject Service : : prepareAccountJsonObject ( const GeneratedAccountDto account )
{
2022-02-21 18:03:38 +00:00
return QJsonObject { { " name " , account . alias } ,
{ " address " , account . address } ,
{ " photo-path " , account . identicon } ,
{ " identicon " , account . identicon } ,
{ " key-uid " , account . keyUid } ,
{ " keycard-pairing " , QJsonValue ( ) } } ;
2022-01-06 19:29:19 +00:00
}
QJsonObject Service : : getAccountDataForAccountId ( QString accountId )
{
2022-02-21 18:03:38 +00:00
foreach ( const GeneratedAccountDto & acc , m_generatedAccounts )
{
if ( acc . id = = accountId )
{
return Service : : prepareAccountJsonObject ( acc ) ;
}
}
if ( m_importedAccount . isValid ( ) )
{
if ( m_importedAccount . id = = accountId )
{
return Service : : prepareAccountJsonObject ( m_importedAccount ) ;
}
}
// TODO: Should we use instead a std::optional?
throw std : : runtime_error ( " account not found " ) ;
2022-01-06 19:29:19 +00:00
}
QJsonArray Service : : prepareSubaccountJsonObject ( GeneratedAccountDto account )
{
2022-02-21 18:03:38 +00:00
return QJsonArray { QJsonObject { { " public-key " , account . derivedAccounts . defaultWallet . publicKey } ,
{ " address " , account . derivedAccounts . defaultWallet . address } ,
{ " color " , " #4360df " } ,
{ " wallet " , true } ,
{ " path " , Backend : : Accounts : : PATH_DEFAULT_WALLET } ,
{ " name " , " Status account " } } ,
QJsonObject { { " public-key " , account . derivedAccounts . whisper . publicKey } ,
{ " address " , account . derivedAccounts . whisper . address } ,
{ " path " , Backend : : Accounts : : PATH_WHISPER } ,
{ " name " , account . alias } ,
{ " identicon " , account . identicon } ,
{ " chat " , true } } } ;
2022-01-06 19:29:19 +00:00
}
QJsonArray Service : : getSubaccountDataForAccountId ( QString accountId )
{
2022-02-21 18:03:38 +00:00
foreach ( const GeneratedAccountDto & acc , m_generatedAccounts )
{
if ( acc . id = = accountId )
{
return prepareSubaccountJsonObject ( acc ) ;
}
}
if ( m_importedAccount . isValid ( ) )
{
if ( m_importedAccount . id = = accountId )
{
return prepareSubaccountJsonObject ( m_importedAccount ) ;
}
}
// TODO: Should we use instead a std::optional?
throw std : : runtime_error ( " account not found " ) ;
2022-01-06 19:29:19 +00:00
}
QString generateSigningPhrase ( int count )
{
2022-02-21 18:03:38 +00:00
QStringList words ;
for ( int i = 0 ; i < count ; i + + )
{
words . append ( phrases [ QRandomGenerator : : global ( ) - > bounded ( static_cast < int > ( phrases . size ( ) ) ) ] ) ;
}
return words . join ( " " ) ;
2022-01-06 19:29:19 +00:00
}
QJsonObject Service : : prepareAccountSettingsJsonObject ( const GeneratedAccountDto account , QString installationId )
{
2022-02-21 18:03:38 +00:00
QFile defaultNetworks ( " :/resources/default-networks.json " ) ;
defaultNetworks . open ( QIODevice : : ReadOnly ) ;
QString defaultNetworksContent = defaultNetworks . readAll ( ) . replace ( " %INFURA_KEY% " , INFURA_KEY ) ;
QJsonArray defaultNetworksJson = QJsonDocument : : fromJson ( defaultNetworksContent . toUtf8 ( ) ) . array ( ) ;
return QJsonObject { { " key-uid " , account . keyUid } ,
{ " mnemonic " , account . mnemonic } ,
{ " public-key " , account . derivedAccounts . whisper . publicKey } ,
{ " name " , account . alias } ,
{ " address " , account . address } ,
{ " eip1581-address " , account . derivedAccounts . eip1581 . address } ,
{ " dapps-address " , account . derivedAccounts . defaultWallet . address } ,
{ " wallet-root-address " , account . derivedAccounts . walletRoot . address } ,
{ " preview-privacy? " , true } ,
{ " signing-phrase " , generateSigningPhrase ( 3 ) } ,
{ " log-level " , " INFO " } ,
{ " latest-derived-path " , 0 } ,
{ " networks/networks " , defaultNetworksJson } ,
{ " currency " , " usd " } ,
{ " identicon " , account . identicon } ,
{ " waku-enabled " , true } ,
{ " wallet/visible-tokens " , { { Constants : : DefaultNetworkName , QJsonArray { " SNT " } } } } ,
{ " appearance " , 0 } ,
{ " networks/current-network " , Constants : : DefaultNetworkName } ,
{ " installation-id " , installationId } } ;
2022-01-06 19:29:19 +00:00
}
QJsonObject Service : : getAccountSettings ( QString accountId , QString installationId )
{
2022-02-21 18:03:38 +00:00
foreach ( const GeneratedAccountDto & acc , m_generatedAccounts )
if ( acc . id = = accountId )
{
return Service : : prepareAccountSettingsJsonObject ( acc , installationId ) ;
}
if ( m_importedAccount . isValid ( ) )
{
if ( m_importedAccount . id = = accountId )
{
return Service : : prepareAccountSettingsJsonObject ( m_importedAccount , installationId ) ;
}
}
// TODO: Should we use instead a std::optional?
throw std : : runtime_error ( " account not found " ) ;
2022-01-06 19:29:19 +00:00
}
QJsonArray getNodes ( const QJsonObject fleet , QString nodeType )
{
2022-02-21 18:03:38 +00:00
auto nodes = fleet [ nodeType ] . toObject ( ) ;
QJsonArray result ;
for ( auto it = nodes . begin ( ) ; it ! = nodes . end ( ) ; + + it )
result < < * it ;
return result ;
2022-01-06 19:29:19 +00:00
}
QJsonObject Service : : getDefaultNodeConfig ( QString installationId )
{
2022-02-21 18:03:38 +00:00
QFile nodeConfig ( " :/resources/node-config.json " ) ;
nodeConfig . open ( QIODevice : : ReadOnly ) ;
2022-01-06 19:29:19 +00:00
2022-02-21 18:03:38 +00:00
QString nodeConfigContent = nodeConfig . readAll ( ) ;
2022-01-06 19:29:19 +00:00
2022-02-21 18:03:38 +00:00
nodeConfigContent = nodeConfigContent . replace ( " %INSTALLATIONID% " , installationId ) ;
nodeConfigContent = nodeConfigContent . replace ( " %INFURA_KEY% " , INFURA_KEY ) ;
2022-01-06 19:29:19 +00:00
2022-02-21 18:03:38 +00:00
QJsonObject nodeConfigJson = QJsonDocument : : fromJson ( nodeConfigContent . toUtf8 ( ) ) . object ( ) ;
2022-01-06 19:29:19 +00:00
2022-02-21 18:03:38 +00:00
QFile fleets ( " :/resources/fleets.json " ) ;
fleets . open ( QIODevice : : ReadOnly ) ;
QJsonObject fleetsJson = QJsonDocument : : fromJson ( fleets . readAll ( ) ) . object ( ) [ " fleets " ] . toObject ( ) ;
2022-01-06 19:29:19 +00:00
2022-02-21 18:03:38 +00:00
auto fleet = fleetsJson [ Constants : : Fleet : : Prod ] . toObject ( ) ;
2022-01-06 19:29:19 +00:00
2022-02-21 18:03:38 +00:00
QJsonObject clusterConfig = nodeConfigJson [ " ClusterConfig " ] . toObject ( ) ;
2022-01-06 19:29:19 +00:00
2022-02-21 18:03:38 +00:00
clusterConfig [ " Fleet " ] = Constants : : Fleet : : Prod ;
clusterConfig [ " BootNodes " ] = getNodes ( fleet , Constants : : FleetNodes : : Bootnodes ) ;
clusterConfig [ " TrustedMailServers " ] = getNodes ( fleet , Constants : : FleetNodes : : Mailservers ) ;
clusterConfig [ " StaticNodes " ] = getNodes ( fleet , Constants : : FleetNodes : : Whisper ) ;
clusterConfig [ " RendezvousNodes " ] = getNodes ( fleet , Constants : : FleetNodes : : Rendezvous ) ;
clusterConfig [ " RelayNodes " ] = getNodes ( fleet , Constants : : FleetNodes : : Waku ) ;
clusterConfig [ " StoreNodes " ] = getNodes ( fleet , Constants : : FleetNodes : : Waku ) ;
clusterConfig [ " FilterNodes " ] = getNodes ( fleet , Constants : : FleetNodes : : Waku ) ;
clusterConfig [ " LightpushNodes " ] = getNodes ( fleet , Constants : : FleetNodes : : Waku ) ;
2022-01-06 19:29:19 +00:00
2022-02-21 18:03:38 +00:00
nodeConfigJson [ " ClusterConfig " ] = clusterConfig ;
2022-01-06 19:29:19 +00:00
2022-02-21 18:03:38 +00:00
return nodeConfigJson ;
2022-01-06 19:29:19 +00:00
}
} // namespace Accounts