From 132df2b5b4743790246c49b5696cf036f795fda6 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 11 Sep 2021 08:20:32 -0400 Subject: [PATCH] Refactor/libstatus stickers (#24) * move decodeContentHash from libstatus to utils * refactor stickers; move logic from libstatus back to status-lib; abstract eth_call --- status/eth/transactions.nim | 13 +-- status/stickers.nim | 6 +- .../stickers.nim => stickers_backend.nim} | 79 ++++--------------- status/utils.nim | 53 +++++++++++++ status/wallet/collectibles.nim | 3 +- status/wallet2/collectibles.nim | 3 +- 6 files changed, 84 insertions(+), 73 deletions(-) rename status/{libstatus/stickers.nim => stickers_backend.nim} (71%) diff --git a/status/eth/transactions.nim b/status/eth/transactions.nim index 130b99c..2b6b06d 100644 --- a/status/eth/transactions.nim +++ b/status/eth/transactions.nim @@ -1,8 +1,5 @@ import - json - -import - json_serialization, chronicles, web3/ethtypes + json, json_serialization, chronicles, web3/ethtypes import ../libstatus/core, ../types/[rpc_response, transaction], ../libstatus/conversions @@ -27,4 +24,10 @@ proc call*(tx: TransactionData): RpcResponse = let responseStr = core.callPrivateRPC("eth_call", %*[%tx, "latest"]) result = Json.decode(responseStr, RpcResponse) if not result.error.isNil: - raise newException(RpcException, "Error calling method: " & result.error.message) \ No newline at end of file + raise newException(RpcException, "Error calling method: " & result.error.message) + +proc eth_call*(payload = %* []): RpcResponse = + let responseStr = core.callPrivateRPC("eth_call", payload) + result = Json.decode(responseStr, RpcResponse) + if not result.error.isNil: + raise newException(RpcException, "Error calling method: " & result.error.message) diff --git a/status/stickers.nim b/status/stickers.nim index eba49a6..a6f59da 100644 --- a/status/stickers.nim +++ b/status/stickers.nim @@ -5,13 +5,13 @@ import # project deps chronicles, web3/[ethtypes, conversions], stint import # local deps + utils as status_utils, eth/contracts as status_contracts, - libstatus/stickers as status_stickers, transactions, + stickers_backend as status_stickers, transactions, libstatus/wallet, ../eventemitter import ./types/[sticker, transaction, rpc_response] from utils as libstatus_utils import eth2Wei, gwei2Wei, toUInt64, parseAddress - logScope: topics = "stickers-model" @@ -143,7 +143,7 @@ proc addStickerToRecent*(self: StickersModel, sticker: Sticker, save: bool = fal status_stickers.saveRecentStickers(self.recentStickers) proc decodeContentHash*(value: string): string = - result = status_stickers.decodeContentHash(value) + result = status_utils.decodeContentHash(value) proc getPackIdFromTokenId*(tokenId: Stuint[256]): int = result = status_stickers.getPackIdFromTokenId(tokenId) diff --git a/status/libstatus/stickers.nim b/status/stickers_backend.nim similarity index 71% rename from status/libstatus/stickers.nim rename to status/stickers_backend.nim index bf9aed2..5f6866b 100644 --- a/status/libstatus/stickers.nim +++ b/status/stickers_backend.nim @@ -1,64 +1,20 @@ +# used to be libstatus, should be merged with stickers + import # std libs - atomics, json, tables, sequtils, httpclient, net + atomics, json, tables, sequtils, httpclient, net, + json, json_serialization, chronicles, web3/ethtypes, + stint, strutils from strutils import parseHexInt, parseInt - +import nbaser + import # vendor libs json_serialization, chronicles, libp2p/[multihash, multicodec, cid], stint, web3/[ethtypes, conversions] from nimcrypto import fromHex import # status-desktop libs - ./core as status, ../types/[sticker, setting, rpc_response], - ../eth/contracts, ./settings, ./edn_helpers - -proc decodeContentHash*(value: string): string = - if value == "": - return "" - - # eg encoded sticker multihash cid: - # e30101701220eab9a8ef4eac6c3e5836a3768d8e04935c10c67d9a700436a0e53199e9b64d29 - # e3017012205c531b83da9dd91529a4cf8ecd01cb62c399139e6f767e397d2f038b820c139f (testnet) - # e3011220c04c617170b1f5725070428c01280b4c19ae9083b7e6d71b7a0d2a1b5ae3ce30 (testnet) - # - # The first 4 bytes (in hex) represent: - # e3 = codec identifier "ipfs-ns" for content-hash - # 01 = unused - sometimes this is NOT included (ie ropsten) - # 01 = CID version (effectively unused, as we will decode with CIDv0 regardless) - # 70 = codec identifier "dag-pb" - - # ipfs-ns - if value[0..1] != "e3": - warn "Could not decode sticker. It may still be valid, but requires a different codec to be used", hash=value - return "" - - try: - # dag-pb - let defaultCodec = parseHexInt("70") #dag-pb - var codec = defaultCodec # no codec specified - var codecStartIdx = 2 # idx of where codec would start if it was specified - # handle the case when starts with 0xe30170 instead of 0xe3010170 - if value[2..5] == "0101": - codecStartIdx = 6 - codec = parseHexInt(value[6..7]) - elif value[2..3] == "01" and value[4..5] != "12": - codecStartIdx = 4 - codec = parseHexInt(value[4..5]) - - # strip the info we no longer need - var multiHashStr = value[codecStartIdx + 2..`, `->` import stint from times import getTime, toUnix, nanosecond @@ -6,6 +10,55 @@ import signing_phrases from web3 import Address, fromHex import web3/ethhexstrings +proc decodeContentHash*(value: string): string = + if value == "": + return "" + + # eg encoded sticker multihash cid: + # e30101701220eab9a8ef4eac6c3e5836a3768d8e04935c10c67d9a700436a0e53199e9b64d29 + # e3017012205c531b83da9dd91529a4cf8ecd01cb62c399139e6f767e397d2f038b820c139f (testnet) + # e3011220c04c617170b1f5725070428c01280b4c19ae9083b7e6d71b7a0d2a1b5ae3ce30 (testnet) + # + # The first 4 bytes (in hex) represent: + # e3 = codec identifier "ipfs-ns" for content-hash + # 01 = unused - sometimes this is NOT included (ie ropsten) + # 01 = CID version (effectively unused, as we will decode with CIDv0 regardless) + # 70 = codec identifier "dag-pb" + + # ipfs-ns + if value[0..1] != "e3": + warn "Could not decode sticker. It may still be valid, but requires a different codec to be used", hash=value + return "" + + try: + # dag-pb + let defaultCodec = parseHexInt("70") #dag-pb + var codec = defaultCodec # no codec specified + var codecStartIdx = 2 # idx of where codec would start if it was specified + # handle the case when starts with 0xe30170 instead of 0xe3010170 + if value[2..5] == "0101": + codecStartIdx = 6 + codec = parseHexInt(value[6..7]) + elif value[2..3] == "01" and value[4..5] != "12": + codecStartIdx = 4 + codec = parseHexInt(value[4..5]) + + # strip the info we no longer need + var multiHashStr = value[codecStartIdx + 2..