diff --git a/src/app_service/common/conversion.nim b/src/app_service/common/conversion.nim index bd38d0127e..1efa946aa9 100644 --- a/src/app_service/common/conversion.nim +++ b/src/app_service/common/conversion.nim @@ -1,6 +1,11 @@ import strutils, strformat, stint, chronicles from web3 import Address, fromHex +const CompressedKeyChars* = {'0'..'9', 'A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','m','n','o','p','q','r','s','t','u','v','w','x','y','z'} + +proc isCompressedPubKey*(strPubKey: string): bool = + return strPubKey.startsWith("z") and strPubKey.len == 49 and allCharsInSet(strPubKey, CompressedKeyChars) + proc parseAddress*(strAddress: string): Address = fromHex(Address, strAddress) diff --git a/src/app_service/service/contacts/async_tasks.nim b/src/app_service/service/contacts/async_tasks.nim index 3f32ce46d0..38c9b523c9 100644 --- a/src/app_service/service/contacts/async_tasks.nim +++ b/src/app_service/service/contacts/async_tasks.nim @@ -2,6 +2,7 @@ import os, parseutils import ../ens/utils as ens_utils include ../../common/json_utils +from ../../common/conversion import isCompressedPubKey include ../../../app/core/tasks/common ################################################# @@ -19,12 +20,13 @@ const lookupContactTask: Task = proc(argEncoded: string) {.gcsafe, nimcall.} = var pubkey = arg.value var address = "" - if (pubkey.startsWith("0x")): - var num64: int64 - let parsedChars = parseHex(pubkey, num64) - if(parsedChars != PK_LENGTH_0X_INCLUDED): - pubkey = "" - address = "" + if (pubkey.startsWith("0x") or isCompressedPubKey(pubkey)): + if pubkey.startsWith("0x"): + var num64: int64 + let parsedChars = parseHex(pubkey, num64) + if(parsedChars != PK_LENGTH_0X_INCLUDED): + pubkey = "" + address = "" else: # TODO refactor those calls to use the new backend and also do it in a signle call pubkey = ens_utils.publicKeyOf(arg.chainId, arg.value) diff --git a/src/app_service/service/contacts/service.nim b/src/app_service/service/contacts/service.nim index 1d83dd5295..3c07ea0878 100644 --- a/src/app_service/service/contacts/service.nim +++ b/src/app_service/service/contacts/service.nim @@ -17,6 +17,7 @@ import ../../../backend/utils as status_utils export contacts_dto, status_update_dto, contact_details const PK_LENGTH_0X_INCLUDED = 132 +const PK_LENGTH_COMPRESSED = 49 include async_tasks @@ -282,8 +283,9 @@ QtObject: # we must keep local contacts updated self.contacts[contact.id] = contact - proc addContact*(self: Service, publicKey: string) = + proc addContact*(self: Service, chatKey: string) = try: + let publicKey = status_accounts.decompressPk(chatKey).result var contact = self.getContactById(publicKey) if not contact.added: contact.added = true diff --git a/src/backend/accounts.nim b/src/backend/accounts.nim index a744e7c054..fa4c4e297f 100644 --- a/src/backend/accounts.nim +++ b/src/backend/accounts.nim @@ -11,6 +11,7 @@ logScope: const NUMBER_OF_ADDRESSES_TO_GENERATE = 1 const MNEMONIC_PHRASE_LENGTH = 12 +const PK_LENGTH_0X_INCLUDED = 132 const GENERATED* = "generated" const SEED* = "seed" @@ -52,6 +53,23 @@ proc generateAddresses*(paths: seq[string]): RpcResponse[JsonNode] {.raises: [Ex error "error doing rpc request", methodName = "generateAddresses", exception=e.msg raise newException(RpcException, e.msg) +proc decompressPk*(publicKey: string): RpcResponse[string] = + discard + if publicKey.startsWith("0x04") and publicKey.len == PK_LENGTH_0X_INCLUDED: + # already decompressed + result.result = publicKey + return + + var response = status_go.multiformatDeserializePublicKey(publicKey, "f") + # json response indicates error + try: + let jsonReponse = parseJson(response) + result.error = RpcError(message: jsonReponse["error"].getStr()) + except JsonParsingError as e: + let secp256k1Code = "fe701" + response.removePrefix(secp256k1Code) + result.result = "0x" & response + proc compressPk*(publicKey: string): RpcResponse[string] = let secp256k1Code = "0xe701" let base58btc = "z" diff --git a/ui/imports/utils/Utils.qml b/ui/imports/utils/Utils.qml index ffb40b4c57..c6c54335b2 100644 --- a/ui/imports/utils/Utils.qml +++ b/ui/imports/utils/Utils.qml @@ -13,7 +13,8 @@ QtObject { } function isChatKey(value) { - return startsWith0x(value) && isHex(value) && value.length === 132 + return (startsWith0x(value) && isHex(value) && value.length === 132) || + /^z[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{48}$/.test(inputValue) } function isValidETHNamePrefix(value) {