1
0
This repository has been archived on 2025-07-27. You can view files and clone it, but cannot push or open issues or pull requests.
Files
siadrive/SiaDrive.Api/SiaWallet.cpp
Scott E. Graves 3949ba30e1 Refactoring
2017-02-18 10:15:01 -06:00

177 lines
3.9 KiB
C++

#include "stdafx.h"
#include "SiaApi.h"
using namespace Sia::Api;
static String SeedLangToString(const SiaSeedLanguage& lang)
{
switch (lang)
{
case SiaSeedLanguage::English:
return L"english";
case SiaSeedLanguage::German:
return L"german";
case SiaSeedLanguage::Japanese:
return L"japanese";
default:
throw std::exception("Seed language not implemented");
}
}
CSiaApi::_CSiaWallet::_CSiaWallet(const CSiaCurl& siaCurl) :
CSiaBase(siaCurl),
_Created(false),
_Locked(false)
{
Refresh();
}
CSiaApi::_CSiaWallet::~_CSiaWallet()
{
}
bool CSiaApi::_CSiaWallet::Refresh()
{
json result;
SiaCurlError error = GetSiaCurl().Get(L"/wallet", result);
if (ApiSuccess(error))
{
SetCreated(result["encrypted"].get<bool>());
SetLocked(!result["unlocked"].get<bool>());
return true;
}
return false;
}
SiaApiError CSiaApi::_CSiaWallet::Create(const SiaSeedLanguage& seedLanguage, String& seed)
{
SiaApiError error = SiaApiError::RequestError;
if (Refresh())
{
error = SiaApiError::WalletExists;
if (!GetCreated())
{
error = SiaApiError::RequestError;
json result;
SiaCurlError cerror = GetSiaCurl().Post(L"/wallet/init", { {L"dictionary", SeedLangToString(seedLanguage)} }, result);
if (ApiSuccess(cerror))
{
error = SiaApiError::Success;
seed = CA2W(result["primaryseed"].get<std::string>().c_str());
Refresh();
}
}
}
return error;
}
SiaApiError CSiaApi::_CSiaWallet::Restore(const String& seed)
{
SiaApiError error = SiaApiError::NotImplemented;
// TODO Future enhancement
return error;
}
SiaApiError CSiaApi::_CSiaWallet::Lock()
{
SiaApiError error = GetCreated() ? (GetLocked() ? SiaApiError::WalletLocked : SiaApiError::Success) : SiaApiError::WalletNotCreated;
if (ApiSuccess(error))
{
error = SiaApiError::RequestError;
json result;
SiaCurlError cerror = GetSiaCurl().Post(L"/wallet/lock", {}, result);
if (ApiSuccess(cerror))
{
Refresh();
error = SiaApiError::Success;
}
}
return error;
}
SiaApiError CSiaApi::_CSiaWallet::Unlock(const String& password)
{
SiaApiError error = GetCreated() ? (GetLocked() ? SiaApiError::Success : SiaApiError::WalletUnlocked) : SiaApiError::WalletNotCreated;
if (ApiSuccess(error))
{
error = SiaApiError::RequestError;
json result;
SiaCurlError cerror = GetSiaCurl().Post(L"/wallet/unlock", { {L"encryptionpassword", password} }, result);
if (ApiSuccess(cerror))
{
Refresh();
error = SiaApiError::Success;
}
}
return error;
}
/*{
"encrypted": true,
"unlocked": true,
"confirmedsiacoinbalance": "123456", // hastings, big int
"unconfirmedoutgoingsiacoins": "0", // hastings, big int
"unconfirmedincomingsiacoins": "789", // hastings, big int
"siafundbalance": "1", // siafunds, big int
"siacoinclaimbalance": "9001", // hastings, big int
}*/
SiaApiError CSiaApi::_CSiaWallet::GetConfirmedBalance(SiaCurrency& balance) const
{
SiaApiError ret = SiaApiError::RequestError;
balance = 0;
json result;
SiaCurlError cerror = GetSiaCurl().Get(L"/wallet", result);
if (ApiSuccess(cerror))
{
balance = HastingsStringToSiaCurrency(String(CA2W(result["confirmedsiacoinbalance"].get<std::string>().c_str())));
ret = SiaApiError::Success;
}
return ret;
}
SiaApiError CSiaApi::_CSiaWallet::GetUnonfirmedBalance(SiaCurrency& balance) const
{
SiaApiError ret = SiaApiError::RequestError;
balance = 0;
json result;
SiaCurlError cerror = GetSiaCurl().Get(L"/wallet", result);
if (ApiSuccess(cerror))
{
balance = HastingsStringToSiaCurrency(String(CA2W(result["unconfirmedincomingsiacoins"].get<std::string>().c_str())));
ret = SiaApiError::Success;
}
return ret;
}
SiaApiError CSiaApi::_CSiaWallet::GetAddress(String& address) const
{
SiaApiError ret = SiaApiError::RequestError;
address = L"";
json result;
SiaCurlError cerror = GetSiaCurl().Get(L"/wallet/address", result);
if (ApiSuccess(cerror))
{
address = CA2W(result["address"].get<std::string>().c_str());
ret = SiaApiError::Success;
}
return ret;
}