#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()); SetLocked(!result["unlocked"].get()); 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().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().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)) { SiaCurrency sc1 = HastingsStringToSiaCurrency(String(CA2W(result["unconfirmedincomingsiacoins"].get().c_str()))); SiaCurrency sc2 = HastingsStringToSiaCurrency(String(CA2W(result["unconfirmedoutgoingsiacoins"].get().c_str()))); balance = sc1 - sc2; 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().c_str()); ret = SiaApiError::Success; } return ret; }