Allow the usage of compressed chat keys when sending contact requests

Fixes #5359
This commit is contained in:
Richard Ramos 2022-04-05 14:33:09 -04:00 committed by Iuri Matias
parent dfa5b17275
commit f749f84886
5 changed files with 36 additions and 8 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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"

View File

@ -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) {