mirror of
https://github.com/status-im/status-lib.git
synced 2025-01-11 13:04:26 +00:00
Base branch for refactoring (#88)
* added new backend for refactoring purpose, contacts added * initial chat class added * initial community class added * initial accounts class added * login method added * Refactor/wallet 1 (#89) * refactor: add custom token new backend * refactor: add transactions new backend * refactor: add collectible new backend * refactor: add accounts backend * refactor: add settings backend * refactor: Add eth call to fetch balance * refactor: add call to get eth block * refactor: remove bookmarks (#90) * refactor: dapp permissions (#92) * Refactor/wallet part 2 (#91) * refactor: add save account * refactor: add account generation * refactor: save settings * refactor: add update account in new be * add getTransfersByAddress (#93) Co-authored-by: Jonathan Rainville <rainville.jonathan@gmail.com> * initial messages class added * fix: transaction request loading contracts (#96) * add/remove reactions added * - pin/unpin message added - fetch message's details by message id added - fetch reactions for message with id added * bump status-go Co-authored-by: Anthony Laibe <anthony@laibe.cc> Co-authored-by: Richard Ramos <info@richardramos.me> Co-authored-by: Jonathan Rainville <rainville.jonathan@gmail.com> Co-authored-by: Iuri Matias <iuri.matias@gmail.com>
This commit is contained in:
parent
72a32ee725
commit
b9e1c230ca
@ -1,16 +1,11 @@
|
||||
from ./types import Backend, StatusGoBackend, MockBackend
|
||||
export Backend, StatusGoBackend, MockBackend
|
||||
|
||||
from base/bookmarks as bookmarks_methods import storeBookmark, updateBookmark, getBookmarks, deleteBookmark
|
||||
export storeBookmark, updateBookmark, getBookmarks, deleteBookmark
|
||||
|
||||
from base/keycard as keycard_methods import keycardStart, keycardStop, keycardSelect, keycardPair,
|
||||
keycardOpenSecureChannel, keycardVerifyPin, keycardExportKey
|
||||
export keycardStart, keycardStop, keycardSelect, keycardPair,
|
||||
keycardOpenSecureChannel, keycardVerifyPin, keycardExportKey
|
||||
|
||||
import statusgo/bookmarks as statusgo_bookmarks
|
||||
import mock/bookmarks as mock_bookmarks
|
||||
import statusgo/keycard as statusgo_keycard
|
||||
import mock/keycard as mock_keycard
|
||||
|
||||
|
@ -1,14 +0,0 @@
|
||||
import ../../types/[bookmark]
|
||||
import ../types
|
||||
|
||||
method storeBookmark*(self: Backend, bookmark: Bookmark): Bookmark =
|
||||
raise newException(ValueError, "No implementation available")
|
||||
|
||||
method updateBookmark*(self: Backend, originalUrl: string, bookmark: Bookmark) =
|
||||
raise newException(ValueError, "No implementation available")
|
||||
|
||||
method getBookmarks*(self: Backend): seq[Bookmark] =
|
||||
raise newException(ValueError, "No implementation available")
|
||||
|
||||
method deleteBookmark*(self: Backend, url: string) =
|
||||
raise newException(ValueError, "No implementation available")
|
@ -1,37 +0,0 @@
|
||||
import json, chronicles
|
||||
|
||||
import ../../types/[bookmark]
|
||||
import ../types
|
||||
import ./core
|
||||
|
||||
method storeBookmark*(self: StatusGoBackend, bookmark: Bookmark): Bookmark =
|
||||
let payload = %* [{"url": bookmark.url, "name": bookmark.name}]
|
||||
try:
|
||||
let resp = callPrivateRPC("browsers_storeBookmark", payload).parseJson["result"]
|
||||
bookmark.imageUrl = resp["imageUrl"].getStr
|
||||
return bookmark
|
||||
except Exception as e:
|
||||
error "Error updating bookmark", msg = e.msg
|
||||
discard
|
||||
|
||||
method updateBookmark*(self: StatusGoBackend, originalUrl: string, bookmark: Bookmark) =
|
||||
let payload = %* [originalUrl, {"url": bookmark.url, "name": bookmark.name}]
|
||||
try:
|
||||
discard callPrivateRPC("browsers_updateBookmark", payload)
|
||||
except Exception as e:
|
||||
error "Error updating bookmark", msg = e.msg
|
||||
discard
|
||||
|
||||
method getBookmarks*(self: StatusGoBackend): seq[Bookmark] =
|
||||
let payload = %* []
|
||||
try:
|
||||
let responseResult = callPrivateRPC("browsers_getBookmarks", payload).parseJson["result"]
|
||||
if responseResult.kind != JNull:
|
||||
for bookmark in responseResult:
|
||||
result.add(Bookmark(url: bookmark{"url"}.getStr, name: bookmark{"name"}.getStr, imageUrl: bookmark{"imageUrl"}.getStr))
|
||||
except:
|
||||
discard
|
||||
|
||||
method deleteBookmark*(self: StatusGoBackend, url: string) =
|
||||
let payload = %* [url]
|
||||
discard callPrivateRPC("browsers_deleteBookmark", payload)
|
@ -1,28 +0,0 @@
|
||||
# import statusgo_backend/browser as status_browser
|
||||
import ../eventemitter
|
||||
|
||||
import ../../types/[bookmark]
|
||||
|
||||
import ../backends/backend
|
||||
|
||||
type
|
||||
BrowserModel* = ref object
|
||||
events*: EventEmitter
|
||||
backend*: Backend
|
||||
|
||||
proc newBrowserModel*(events: EventEmitter, backend: Backend): BrowserModel =
|
||||
result = BrowserModel()
|
||||
result.events = events
|
||||
result.backend = backend
|
||||
|
||||
proc storeBookmark*(self: BrowserModel, bookmark: Bookmark): Bookmark =
|
||||
return self.backend.storeBookmark(bookmark)
|
||||
|
||||
proc updateBookmark*(self: BrowserModel, originalUrl: string, bookmark: Bookmark) =
|
||||
self.backend.updateBookmark(originalUrl, bookmark)
|
||||
|
||||
proc getBookmarks*(self: BrowserModel): seq[Bookmark] =
|
||||
result = self.backend.getBookmarks()
|
||||
|
||||
proc deleteBookmark*(self: BrowserModel, url: string) =
|
||||
self.backend.deleteBookmark(url)
|
@ -285,7 +285,7 @@ proc findContract*(chainId: int, name: string): Contract =
|
||||
result = if found.len > 0: found[0] else: nil
|
||||
|
||||
proc allErc20Contracts*(): seq[Erc20Contract] =
|
||||
result = allContracts().map(contract => Erc20Contract(contract))
|
||||
result = allContracts().filter(contract => contract of Erc20Contract).map(contract => Erc20Contract(contract))
|
||||
|
||||
proc allErc20ContractsByChainId*(chainId: int): seq[Erc20Contract] =
|
||||
result = allContracts().filter(contract => contract of Erc20Contract and contract.chainId == chainId).map(contract => Erc20Contract(contract))
|
||||
|
@ -1,7 +1,7 @@
|
||||
import statusgo_backend/accounts as statusgo_backend_accounts
|
||||
import statusgo_backend/core as statusgo_backend_core
|
||||
import statusgo_backend/settings as statusgo_backend_settings
|
||||
import chat, accounts, wallet, wallet2, node, network, messages, contacts, profile, stickers, permissions, fleet, settings, mailservers, browser, tokens, provider
|
||||
import chat, accounts, wallet, wallet2, node, network, messages, contacts, profile, stickers, permissions, fleet, settings, mailservers, tokens, provider
|
||||
import notifications/os_notifications
|
||||
import ../eventemitter
|
||||
import bitops, stew/byteutils, chronicles
|
||||
@ -29,7 +29,6 @@ type Status* = ref object
|
||||
permissions*: PermissionsModel
|
||||
settings*: SettingsModel
|
||||
mailservers*: MailserversModel
|
||||
browser*: BrowserModel
|
||||
tokens*: TokensModel
|
||||
provider*: ProviderModel
|
||||
osnotifications*: OsNotifications
|
||||
@ -54,7 +53,6 @@ proc newStatusInstance*(fleetConfig: string, backendName: string = "statusgo"):
|
||||
result.permissions = permissions.newPermissionsModel(result.events)
|
||||
result.settings = settings.newSettingsModel(result.events)
|
||||
result.mailservers = mailservers.newMailserversModel(result.events)
|
||||
result.browser = browser.newBrowserModel(result.events, result.backend)
|
||||
result.tokens = tokens.newTokensModel(result.events)
|
||||
result.provider = provider.newProviderModel(result.events, result.permissions, result.wallet)
|
||||
result.osnotifications = newOsNotifications(result.events)
|
||||
|
250
status/statusgo_backend_new/accounts.nim
Normal file
250
status/statusgo_backend_new/accounts.nim
Normal file
@ -0,0 +1,250 @@
|
||||
import json, json_serialization, chronicles, nimcrypto
|
||||
import ./core, ./utils
|
||||
import ./response_type
|
||||
|
||||
import status_go
|
||||
|
||||
export response_type
|
||||
|
||||
logScope:
|
||||
topics = "rpc-accounts"
|
||||
|
||||
const NUMBER_OF_ADDRESSES_TO_GENERATE = 5
|
||||
const MNEMONIC_PHRASE_LENGTH = 12
|
||||
|
||||
const GENERATED* = "generated"
|
||||
const SEED* = "seed"
|
||||
const KEY* = "key"
|
||||
const WATCH* = "watch"
|
||||
|
||||
proc getAccounts*(): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
return core.callPrivateRPC("accounts_getAccounts")
|
||||
|
||||
proc deleteAccount*(address: string): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
return core.callPrivateRPC("accounts_deleteAccount", %* [address])
|
||||
|
||||
proc updateAccount*(name, address, publicKey, walletType, color: string) {.raises: [Exception].} =
|
||||
discard core.callPrivateRPC("accounts_saveAccounts", %* [
|
||||
[{
|
||||
"color": color,
|
||||
"name": name,
|
||||
"address": address,
|
||||
"public-key": publicKey,
|
||||
"type": walletType,
|
||||
"path": "m/44'/60'/0'/0/1" # <--- TODO: fix this. Derivation path is not supposed to change
|
||||
}]
|
||||
])
|
||||
|
||||
proc generateAddresses*(paths: seq[string]): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let payload = %* {
|
||||
"n": NUMBER_OF_ADDRESSES_TO_GENERATE,
|
||||
"mnemonicPhraseLength": MNEMONIC_PHRASE_LENGTH,
|
||||
"bip39Passphrase": "",
|
||||
"paths": paths
|
||||
}
|
||||
|
||||
try:
|
||||
let response = status_go.multiAccountGenerateAndDeriveAddresses($payload)
|
||||
result.result = Json.decode(response, JsonNode)
|
||||
|
||||
except RpcException as e:
|
||||
error "error doing rpc request", methodName = "generateAddresses", exception=e.msg
|
||||
raise newException(RpcException, e.msg)
|
||||
|
||||
proc generateAlias*(publicKey: string): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
try:
|
||||
let response = status_go.generateAlias(publicKey)
|
||||
result.result = %* response
|
||||
|
||||
except RpcException as e:
|
||||
error "error doing rpc request", methodName = "generateAlias", exception=e.msg
|
||||
raise newException(RpcException, e.msg)
|
||||
|
||||
proc generateIdenticon*(publicKey: string): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
try:
|
||||
let response = status_go.identicon(publicKey)
|
||||
result.result = %* response
|
||||
|
||||
except RpcException as e:
|
||||
error "error doing rpc request", methodName = "generateIdenticon", exception=e.msg
|
||||
raise newException(RpcException, e.msg)
|
||||
|
||||
proc multiAccountImportMnemonic*(mnemonic: string): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let payload = %* {
|
||||
"mnemonicPhrase": mnemonic,
|
||||
"Bip39Passphrase": ""
|
||||
}
|
||||
|
||||
try:
|
||||
let response = status_go.multiAccountImportMnemonic($payload)
|
||||
result.result = Json.decode(response, JsonNode)
|
||||
|
||||
except RpcException as e:
|
||||
error "error doing rpc request", methodName = "multiAccountImportMnemonic", exception=e.msg
|
||||
raise newException(RpcException, e.msg)
|
||||
|
||||
proc deriveAccounts*(accountId: string, paths: seq[string]): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let payload = %* {
|
||||
"accountID": accountId,
|
||||
"paths": paths
|
||||
}
|
||||
|
||||
try:
|
||||
let response = status_go.multiAccountDeriveAddresses($payload)
|
||||
result.result = Json.decode(response, JsonNode)
|
||||
|
||||
except RpcException as e:
|
||||
error "error doing rpc request", methodName = "deriveAccounts", exception=e.msg
|
||||
raise newException(RpcException, e.msg)
|
||||
|
||||
proc openedAccounts*(path: string): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
try:
|
||||
let response = status_go.openAccounts(path)
|
||||
result.result = Json.decode(response, JsonNode)
|
||||
|
||||
except RpcException as e:
|
||||
error "error doing rpc request", methodName = "openedAccounts", exception=e.msg
|
||||
raise newException(RpcException, e.msg)
|
||||
|
||||
proc storeDerivedAccounts*(id, hashedPassword: string, paths: seq[string]):
|
||||
RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let payload = %* {
|
||||
"accountID": id,
|
||||
"paths": paths,
|
||||
"password": hashedPassword
|
||||
}
|
||||
|
||||
try:
|
||||
let response = status_go.multiAccountStoreDerivedAccounts($payload)
|
||||
result.result = Json.decode(response, JsonNode)
|
||||
|
||||
except RpcException as e:
|
||||
error "error doing rpc request", methodName = "storeDerivedAccounts", exception=e.msg
|
||||
raise newException(RpcException, e.msg)
|
||||
|
||||
proc storeAccounts*(id, hashedPassword: string): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let payload = %* {
|
||||
"accountID": id,
|
||||
"password": hashedPassword
|
||||
}
|
||||
|
||||
try:
|
||||
let response = status_go.multiAccountStoreAccount($payload)
|
||||
result.result = Json.decode(response, JsonNode)
|
||||
|
||||
except RpcException as e:
|
||||
error "error doing rpc request", methodName = "storeAccounts", exception=e.msg
|
||||
raise newException(RpcException, e.msg)
|
||||
|
||||
proc hashPassword*(password: string): string =
|
||||
result = "0x" & $keccak_256.digest(password)
|
||||
|
||||
proc saveAccount*(
|
||||
address: string,
|
||||
name: string,
|
||||
password: string,
|
||||
color: string,
|
||||
accountType: string,
|
||||
isADerivedAccount = true,
|
||||
walletIndex: int = 0,
|
||||
id: string = "",
|
||||
publicKey: string = "",
|
||||
) {.raises: [Exception].} =
|
||||
var derivationPath = "m/44'/60'/0'/0/0"
|
||||
let hashedPassword = hashPassword(password)
|
||||
|
||||
if (isADerivedAccount):
|
||||
let derivationPath = (if accountType == GENERATED: "m/" else: "m/44'/60'/0'/0/") & $walletIndex
|
||||
discard storeDerivedAccounts(id, hashedPassword, @[derivationPath])
|
||||
elif accountType == KEY:
|
||||
discard storeAccounts(id, hashedPassword)
|
||||
|
||||
discard callPrivateRPC("accounts_saveAccounts", %* [
|
||||
[{
|
||||
"color": color,
|
||||
"name": name,
|
||||
"address": address,
|
||||
"public-key": publicKey,
|
||||
"type": accountType,
|
||||
"path": derivationPath
|
||||
}]
|
||||
])
|
||||
|
||||
proc loadAccount*(address: string, password: string): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let hashedPassword = hashPassword(password)
|
||||
let payload = %* {
|
||||
"address": address,
|
||||
"password": hashedPassword
|
||||
}
|
||||
|
||||
try:
|
||||
let response = status_go.multiAccountLoadAccount($payload)
|
||||
result.result = Json.decode(response, JsonNode)
|
||||
|
||||
except RpcException as e:
|
||||
error "error doing rpc request", methodName = "storeAccounts", exception=e.msg
|
||||
raise newException(RpcException, e.msg)
|
||||
|
||||
proc addPeer*(peer: string): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
try:
|
||||
let response = status_go.addPeer(peer)
|
||||
result.result = %* response
|
||||
|
||||
except RpcException as e:
|
||||
error "error doing rpc request", methodName = "addPeer", exception=e.msg
|
||||
raise newException(RpcException, e.msg)
|
||||
|
||||
proc saveAccountAndLogin*(hashedPassword: string, account, subaccounts, settings,
|
||||
config: JsonNode): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
try:
|
||||
let response = status_go.saveAccountAndLogin($account, hashedPassword,
|
||||
$settings, $config, $subaccounts)
|
||||
result.result = Json.decode(response, JsonNode)
|
||||
|
||||
except RpcException as e:
|
||||
error "error doing rpc request", methodName = "saveAccountAndLogin", exception=e.msg
|
||||
raise newException(RpcException, e.msg)
|
||||
|
||||
proc login*(name, keyUid, hashedPassword, identicon, thumbnail, large: string):
|
||||
RpcResponse[JsonNode]
|
||||
{.raises: [Exception].} =
|
||||
try:
|
||||
var payload = %* {
|
||||
"name": name,
|
||||
"key-uid": keyUid,
|
||||
"identityImage": newJNull(),
|
||||
"identicon": identicon
|
||||
}
|
||||
|
||||
if(thumbnail.len>0 and large.len > 0):
|
||||
payload["identityImage"] = %* {"thumbnail": thumbnail, "large": large}
|
||||
|
||||
let response = status_go.login($payload, hashedPassword)
|
||||
result.result = Json.decode(response, JsonNode)
|
||||
|
||||
except RpcException as e:
|
||||
error "error doing rpc request", methodName = "login", exception=e.msg
|
||||
raise newException(RpcException, e.msg)
|
||||
|
||||
proc multiAccountImportPrivateKey*(privateKey: string): RpcResponse[JsonNode] =
|
||||
let payload = %* {
|
||||
"privateKey": privateKey
|
||||
}
|
||||
try:
|
||||
let response = status_go.multiAccountImportPrivateKey($payload)
|
||||
result.result = Json.decode(response, JsonNode)
|
||||
|
||||
except RpcException as e:
|
||||
error "error doing rpc request", methodName = "multiAccountImportPrivateKey", exception=e.msg
|
||||
raise newException(RpcException, e.msg)
|
||||
|
||||
proc verifyAccountPassword*(address: string, password: string, keystoreDir: string): bool =
|
||||
let hashedPassword = hashPassword(password)
|
||||
let verifyResult = status_go.verifyAccountPassword(keystoreDir, address, hashedPassword)
|
||||
let error = parseJson(verifyResult)["error"].getStr
|
||||
|
||||
if error == "":
|
||||
return true
|
||||
|
||||
return false
|
||||
|
21
status/statusgo_backend_new/bookmarks.nim
Normal file
21
status/statusgo_backend_new/bookmarks.nim
Normal file
@ -0,0 +1,21 @@
|
||||
import json, strmisc
|
||||
import core, utils
|
||||
import response_type
|
||||
|
||||
export response_type
|
||||
|
||||
proc getBookmarks*(): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let payload = %* []
|
||||
result = callPrivateRPC("browsers_getBookmarks", payload)
|
||||
|
||||
proc storeBookmark*(url, name: string): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let payload = %* [{"url": url, "name": name}]
|
||||
result = callPrivateRPC("browsers_storeBookmark", payload)
|
||||
|
||||
proc deleteBookmark*(url: string): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let payload = %* [url]
|
||||
result = callPrivateRPC("browsers_deleteBookmark", payload)
|
||||
|
||||
proc updateBookmark*(originalUrl, newUrl, name: string): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let payload = %* [originalUrl, {"url": newUrl, "name": name}]
|
||||
result = callPrivateRPC("browsers_updateBookmark", payload)
|
9
status/statusgo_backend_new/chat.nim
Normal file
9
status/statusgo_backend_new/chat.nim
Normal file
@ -0,0 +1,9 @@
|
||||
import json
|
||||
import core, utils
|
||||
import response_type
|
||||
|
||||
export response_type
|
||||
|
||||
proc getChats*(): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let payload = %* []
|
||||
result = callPrivateRPC("chats".prefix, payload)
|
14
status/statusgo_backend_new/collectibles.nim
Normal file
14
status/statusgo_backend_new/collectibles.nim
Normal file
@ -0,0 +1,14 @@
|
||||
import json
|
||||
import ./core, ./response_type
|
||||
|
||||
export response_type
|
||||
|
||||
proc getOpenseaCollections*(chainId: int, address: string): RpcResponse[JsonNode] =
|
||||
let payload = %* [chainId, address]
|
||||
return callPrivateRPC("wallet_getOpenseaCollectionsByOwner", payload)
|
||||
|
||||
proc getOpenseaAssets*(
|
||||
chainId: int, address: string, collectionSlug: string, limit: int
|
||||
): RpcResponse[JsonNode] =
|
||||
let payload = %* [chainId, address, collectionSlug, limit]
|
||||
return callPrivateRPC("wallet_getOpenseaAssetsByOwnerAndCollection", payload)
|
9
status/statusgo_backend_new/communities.nim
Normal file
9
status/statusgo_backend_new/communities.nim
Normal file
@ -0,0 +1,9 @@
|
||||
import json
|
||||
import core, utils
|
||||
import response_type
|
||||
|
||||
export response_type
|
||||
|
||||
proc getJoinedComunities*(): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let payload = %* []
|
||||
result = callPrivateRPC("joinedCommunities".prefix, payload)
|
41
status/statusgo_backend_new/contacts.nim
Normal file
41
status/statusgo_backend_new/contacts.nim
Normal file
@ -0,0 +1,41 @@
|
||||
import json, strmisc
|
||||
import core, utils
|
||||
import response_type
|
||||
|
||||
export response_type
|
||||
|
||||
proc getContacts*(): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let payload = %* []
|
||||
result = callPrivateRPC("contacts".prefix, payload)
|
||||
|
||||
proc getContactById*(id: string): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let payload = %* [id]
|
||||
result = callPrivateRPC("getContactByID".prefix, payload)
|
||||
|
||||
proc saveContact*(id: string, ensVerified: bool, ensName: string, alias: string,
|
||||
identicon: string, thumbnail: string, largeImage: string, added: bool,
|
||||
blocked: bool, hasAddedUs: bool, localNickname: string)
|
||||
{.raises: [Exception].} =
|
||||
let payload = %* [{
|
||||
"id": id,
|
||||
"name": ensName,
|
||||
"ensVerified": ensVerified,
|
||||
"alias": alias,
|
||||
"identicon": identicon,
|
||||
"images": {
|
||||
"thumbnail": {"Payload": thumbnail.partition(",")[2]},
|
||||
"large": {"Payload": largeImage.partition(",")[2]}
|
||||
},
|
||||
"added": added,
|
||||
"blocked": blocked,
|
||||
"hasAddedUs": hasAddedUs,
|
||||
"localNickname": localNickname
|
||||
}]
|
||||
|
||||
discard callPrivateRPC("saveContact".prefix, payload)
|
||||
|
||||
proc sendContactUpdate*(publicKey, ensName, thumbnail: string)
|
||||
{.raises: [Exception].} =
|
||||
let payload = %* [publicKey, ensName, thumbnail]
|
||||
discard callPrivateRPC("sendContactUpdate".prefix, payload)
|
||||
|
35
status/statusgo_backend_new/conversions.nim
Normal file
35
status/statusgo_backend_new/conversions.nim
Normal file
@ -0,0 +1,35 @@
|
||||
import
|
||||
json, options, strutils
|
||||
|
||||
import
|
||||
web3/[conversions, ethtypes], stint
|
||||
|
||||
import ../types/transaction
|
||||
|
||||
# TODO: make this public in nim-web3 lib
|
||||
template stripLeadingZeros*(value: string): string =
|
||||
var cidx = 0
|
||||
# ignore the last character so we retain '0' on zero value
|
||||
while cidx < value.len - 1 and value[cidx] == '0':
|
||||
cidx.inc
|
||||
value[cidx .. ^1]
|
||||
|
||||
proc `%`*(x: TransactionData): JsonNode =
|
||||
result = newJobject()
|
||||
result["from"] = %x.source
|
||||
result["type"] = %x.txType
|
||||
if x.to.isSome:
|
||||
result["to"] = %x.to.unsafeGet
|
||||
if x.gas.isSome:
|
||||
result["gas"] = %x.gas.unsafeGet
|
||||
if x.gasPrice.isSome:
|
||||
result["gasPrice"] = %("0x" & x.gasPrice.unsafeGet.toHex.stripLeadingZeros)
|
||||
if x.maxFeePerGas.isSome:
|
||||
result["maxFeePerGas"] = %("0x" & x.maxFeePerGas.unsafeGet.toHex)
|
||||
if x.maxPriorityFeePerGas.isSome:
|
||||
result["maxPriorityFeePerGas"] = %("0x" & x.maxPriorityFeePerGas.unsafeGet.toHex)
|
||||
if x.value.isSome:
|
||||
result["value"] = %("0x" & x.value.unsafeGet.toHex)
|
||||
result["data"] = %x.data
|
||||
if x.nonce.isSome:
|
||||
result["nonce"] = %x.nonce.unsafeGet
|
41
status/statusgo_backend_new/core.nim
Normal file
41
status/statusgo_backend_new/core.nim
Normal file
@ -0,0 +1,41 @@
|
||||
import json, json_serialization, strformat, chronicles
|
||||
import status_go
|
||||
import response_type
|
||||
|
||||
export response_type
|
||||
|
||||
logScope:
|
||||
topics = "rpc"
|
||||
|
||||
proc callRPC*(inputJSON: string): string =
|
||||
return $status_go.callRPC(inputJSON)
|
||||
|
||||
proc callPrivateRPCRaw*(inputJSON: string): string {.raises: [].} =
|
||||
result = $status_go.callPrivateRPC(inputJSON)
|
||||
|
||||
proc callPrivateRPC*(methodName: string, payload = %* []): RpcResponse[JsonNode]
|
||||
{.raises: [RpcException, ValueError, Defect, SerializationError].} =
|
||||
try:
|
||||
let inputJSON = %* {
|
||||
"jsonrpc": "2.0",
|
||||
"method": methodName,
|
||||
"params": %payload
|
||||
}
|
||||
debug "NewBE_callPrivateRPC", rpc_method=methodName
|
||||
let rpcResponseRaw = status_go.callPrivateRPC($inputJSON)
|
||||
result = Json.decode(rpcResponseRaw, RpcResponse[JsonNode])
|
||||
|
||||
if(not result.error.isNil):
|
||||
var err = "\nstatus-go error ["
|
||||
err &= fmt"methodName:{methodName}, "
|
||||
err &= fmt"code:{result.error.code}, "
|
||||
err &= fmt"message:{result.error.message} "
|
||||
err &= "]\n"
|
||||
error "rpc response error", err
|
||||
raise newException(ValueError, err)
|
||||
|
||||
except RpcException as e:
|
||||
error "error doing rpc request", methodName = methodName, exception=e.msg
|
||||
raise newException(RpcException, e.msg)
|
||||
|
||||
|
15
status/statusgo_backend_new/custom_tokens.nim
Normal file
15
status/statusgo_backend_new/custom_tokens.nim
Normal file
@ -0,0 +1,15 @@
|
||||
import json
|
||||
import ./core, ./response_type
|
||||
|
||||
export response_type
|
||||
|
||||
proc getCustomTokens*(): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
return callPrivateRPC("wallet_getCustomTokens", %* [])
|
||||
|
||||
proc addCustomToken*(address: string, name: string, symbol: string, decimals: int, color: string) {.raises: [Exception].} =
|
||||
discard callPrivateRPC("wallet_addCustomToken", %* [
|
||||
{"address": address, "name": name, "symbol": symbol, "decimals": decimals, "color": color}
|
||||
])
|
||||
|
||||
proc removeCustomToken*(address: string) {.raises: [Exception].} =
|
||||
discard callPrivateRPC("wallet_deleteCustomToken", %* [address])
|
23
status/statusgo_backend_new/eth.nim
Normal file
23
status/statusgo_backend_new/eth.nim
Normal file
@ -0,0 +1,23 @@
|
||||
import json, strutils, strformat
|
||||
import ./core, ./response_type
|
||||
|
||||
export response_type
|
||||
|
||||
proc getAccounts*(): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
return core.callPrivateRPC("eth_accounts")
|
||||
|
||||
proc getBlockByNumber*(blockNumber: string): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let payload = %* [blockNumber, false]
|
||||
return core.callPrivateRPC("eth_getBlockByNumber", payload)
|
||||
|
||||
proc getEthBalance*(address: string): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let payload = %* [address, "latest"]
|
||||
return core.callPrivateRPC("eth_getBalance", payload)
|
||||
|
||||
proc getTokenBalance*(tokenAddress: string, accountAddress: string): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
var postfixedAccount: string = accountAddress
|
||||
postfixedAccount.removePrefix("0x")
|
||||
let payload = %* [{
|
||||
"to": tokenAddress, "from": accountAddress, "data": fmt"0x70a08231000000000000000000000000{postfixedAccount}"
|
||||
}, "latest"]
|
||||
return core.callPrivateRPC("eth_call", payload)
|
19
status/statusgo_backend_new/general.nim
Normal file
19
status/statusgo_backend_new/general.nim
Normal file
@ -0,0 +1,19 @@
|
||||
import json, strutils, json_serialization, chronicles
|
||||
import core
|
||||
import response_type
|
||||
|
||||
import status_go
|
||||
|
||||
export response_type
|
||||
|
||||
logScope:
|
||||
topics = "rpc-general"
|
||||
|
||||
proc validateMnemonic*(mnemonic: string): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
try:
|
||||
let response = status_go.validateMnemonic(mnemonic.strip())
|
||||
result.result = Json.decode(response, JsonNode)
|
||||
|
||||
except RpcException as e:
|
||||
error "error doing rpc request", methodName = "validateMnemonic", exception=e.msg
|
||||
raise newException(RpcException, e.msg)
|
41
status/statusgo_backend_new/messages.nim
Normal file
41
status/statusgo_backend_new/messages.nim
Normal file
@ -0,0 +1,41 @@
|
||||
import json
|
||||
import core, utils
|
||||
import response_type
|
||||
|
||||
export response_type
|
||||
|
||||
proc fetchMessages*(chatId: string, cursorVal: string, limit: int): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let payload = %* [chatId, cursorVal, limit]
|
||||
result = callPrivateRPC("chatMessages".prefix, payload)
|
||||
|
||||
proc fetchPinnedMessages*(chatId: string, cursorVal: string, limit: int): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let payload = %* [chatId, cursorVal, limit]
|
||||
result = callPrivateRPC("chatPinnedMessages".prefix, payload)
|
||||
|
||||
proc fetchReactions*(chatId: string, cursorVal: string, limit: int): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let payload = %* [chatId, cursorVal, limit]
|
||||
result = callPrivateRPC("emojiReactionsByChatID".prefix, payload)
|
||||
|
||||
proc addReaction*(chatId: string, messageId: string, emojiId: int): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let payload = %* [chatId, messageId, emojiId]
|
||||
result = callPrivateRPC("sendEmojiReaction".prefix, payload)
|
||||
|
||||
proc removeReaction*(reactionId: string): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let payload = %* [reactionId]
|
||||
result = callPrivateRPC("sendEmojiReactionRetraction".prefix, payload)
|
||||
|
||||
proc pinUnpinMessage*(chatId: string, messageId: string, pin: bool): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let payload = %*[{
|
||||
"message_id": messageId,
|
||||
"pinned": pin,
|
||||
"chat_id": chatId
|
||||
}]
|
||||
result = callPrivateRPC("sendPinMessage".prefix, payload)
|
||||
|
||||
proc fetchMessageByMessageId*(messageId: string): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let payload = %* [messageId]
|
||||
result = callPrivateRPC("messageByMessageID".prefix, payload)
|
||||
|
||||
proc fetchReactionsForMessageWithId*(chatId: string, messageId: string): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let payload = %* [chatId, messageId]
|
||||
result = callPrivateRPC("emojiReactionsByChatIDMessageID".prefix, payload)
|
20
status/statusgo_backend_new/permissions.nim
Normal file
20
status/statusgo_backend_new/permissions.nim
Normal file
@ -0,0 +1,20 @@
|
||||
import json, strmisc
|
||||
import core, utils
|
||||
import response_type
|
||||
|
||||
export response_type
|
||||
|
||||
proc getDappPermissions*(): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let payload = %* []
|
||||
result = callPrivateRPC("permissions_getDappPermissions", payload)
|
||||
|
||||
proc addDappPermissions*(dapp: string, permissions: seq[string]): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let payload = %*[{
|
||||
"dapp": dapp,
|
||||
"permissions": permissions
|
||||
}]
|
||||
result = callPrivateRPC("permissions_addDappPermissions", payload)
|
||||
|
||||
proc deleteDappPermissions*(dapp: string): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let payload = %* [dapp]
|
||||
result = callPrivateRPC("permissions_deleteDappPermissions", payload)
|
24
status/statusgo_backend_new/response_type.nim
Normal file
24
status/statusgo_backend_new/response_type.nim
Normal file
@ -0,0 +1,24 @@
|
||||
{.used.}
|
||||
|
||||
import strformat
|
||||
|
||||
type
|
||||
RpcException* = object of CatchableError
|
||||
|
||||
type
|
||||
RpcError* = ref object
|
||||
code*: int
|
||||
message*: string
|
||||
|
||||
type
|
||||
RpcResponse*[T] = object
|
||||
jsonrpc*: string
|
||||
result*: T
|
||||
id*: int
|
||||
error*: RpcError
|
||||
|
||||
proc `$`*(self: RpcError): string =
|
||||
result = fmt"""RpcError(
|
||||
code: {self.code},
|
||||
message: {self.message},
|
||||
]"""
|
10
status/statusgo_backend_new/settings.nim
Normal file
10
status/statusgo_backend_new/settings.nim
Normal file
@ -0,0 +1,10 @@
|
||||
import json
|
||||
import ./core, ./response_type
|
||||
|
||||
export response_type
|
||||
|
||||
proc getSettings*(): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
return core.callPrivateRPC("settings_getSettings")
|
||||
|
||||
proc saveSettings*(key: string, value: string | JsonNode | bool | int) {.raises: [Exception].} =
|
||||
discard core.callPrivateRPC("settings_saveSetting", %* [key, value])
|
17
status/statusgo_backend_new/transactions.nim
Normal file
17
status/statusgo_backend_new/transactions.nim
Normal file
@ -0,0 +1,17 @@
|
||||
import json, stint, chronicles, strutils, conversions
|
||||
|
||||
|
||||
import ../types/transaction
|
||||
import ./core as core
|
||||
|
||||
proc checkRecentHistory*(addresses: seq[string]) {.raises: [Exception].} =
|
||||
let payload = %* [addresses]
|
||||
discard callPrivateRPC("wallet_checkRecentHistory", payload)
|
||||
|
||||
proc getTransfersByAddress*(address: string, toBlock: Uint256, limit: int, loadMore: bool = false): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let
|
||||
toBlockParsed = if not loadMore: newJNull() else: %("0x" & stint.toHex(toBlock))
|
||||
limitParsed = "0x" & limit.toHex.stripLeadingZeros
|
||||
|
||||
callPrivateRPC("wallet_getTransfersByAddress", %* [address, toBlockParsed, limitParsed, loadMore])
|
||||
|
7
status/statusgo_backend_new/utils.nim
Normal file
7
status/statusgo_backend_new/utils.nim
Normal file
@ -0,0 +1,7 @@
|
||||
proc isWakuEnabled(): bool =
|
||||
true # TODO:
|
||||
|
||||
proc prefix*(methodName: string, isExt:bool = true): string =
|
||||
result = if isWakuEnabled(): "waku" else: "shh"
|
||||
result = result & (if isExt: "ext_" else: "_")
|
||||
result = result & methodName
|
@ -1,5 +0,0 @@
|
||||
|
||||
type Bookmark* = ref object
|
||||
name*: string
|
||||
url*: string
|
||||
imageUrl*: string
|
2
vendor/status-go
vendored
2
vendor/status-go
vendored
@ -1 +1 @@
|
||||
Subproject commit c3ced098390f3d4e8d4850d9c0bf6e7dbd627c20
|
||||
Subproject commit dbac362bc716df8444e6d9c142739798c6762fb4
|
Loading…
x
Reference in New Issue
Block a user