Allow the usage of compressed chat keys when sending contact requests
Fixes #5359
This commit is contained in:
parent
dfa5b17275
commit
f749f84886
|
@ -1,6 +1,11 @@
|
||||||
import strutils, strformat, stint, chronicles
|
import strutils, strformat, stint, chronicles
|
||||||
from web3 import Address, fromHex
|
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 =
|
proc parseAddress*(strAddress: string): Address =
|
||||||
fromHex(Address, strAddress)
|
fromHex(Address, strAddress)
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ import os, parseutils
|
||||||
import ../ens/utils as ens_utils
|
import ../ens/utils as ens_utils
|
||||||
|
|
||||||
include ../../common/json_utils
|
include ../../common/json_utils
|
||||||
|
from ../../common/conversion import isCompressedPubKey
|
||||||
include ../../../app/core/tasks/common
|
include ../../../app/core/tasks/common
|
||||||
|
|
||||||
#################################################
|
#################################################
|
||||||
|
@ -19,12 +20,13 @@ const lookupContactTask: Task = proc(argEncoded: string) {.gcsafe, nimcall.} =
|
||||||
var pubkey = arg.value
|
var pubkey = arg.value
|
||||||
var address = ""
|
var address = ""
|
||||||
|
|
||||||
if (pubkey.startsWith("0x")):
|
if (pubkey.startsWith("0x") or isCompressedPubKey(pubkey)):
|
||||||
var num64: int64
|
if pubkey.startsWith("0x"):
|
||||||
let parsedChars = parseHex(pubkey, num64)
|
var num64: int64
|
||||||
if(parsedChars != PK_LENGTH_0X_INCLUDED):
|
let parsedChars = parseHex(pubkey, num64)
|
||||||
pubkey = ""
|
if(parsedChars != PK_LENGTH_0X_INCLUDED):
|
||||||
address = ""
|
pubkey = ""
|
||||||
|
address = ""
|
||||||
else:
|
else:
|
||||||
# TODO refactor those calls to use the new backend and also do it in a signle call
|
# 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)
|
pubkey = ens_utils.publicKeyOf(arg.chainId, arg.value)
|
||||||
|
|
|
@ -17,6 +17,7 @@ import ../../../backend/utils as status_utils
|
||||||
export contacts_dto, status_update_dto, contact_details
|
export contacts_dto, status_update_dto, contact_details
|
||||||
|
|
||||||
const PK_LENGTH_0X_INCLUDED = 132
|
const PK_LENGTH_0X_INCLUDED = 132
|
||||||
|
const PK_LENGTH_COMPRESSED = 49
|
||||||
|
|
||||||
include async_tasks
|
include async_tasks
|
||||||
|
|
||||||
|
@ -282,8 +283,9 @@ QtObject:
|
||||||
# we must keep local contacts updated
|
# we must keep local contacts updated
|
||||||
self.contacts[contact.id] = contact
|
self.contacts[contact.id] = contact
|
||||||
|
|
||||||
proc addContact*(self: Service, publicKey: string) =
|
proc addContact*(self: Service, chatKey: string) =
|
||||||
try:
|
try:
|
||||||
|
let publicKey = status_accounts.decompressPk(chatKey).result
|
||||||
var contact = self.getContactById(publicKey)
|
var contact = self.getContactById(publicKey)
|
||||||
if not contact.added:
|
if not contact.added:
|
||||||
contact.added = true
|
contact.added = true
|
||||||
|
|
|
@ -11,6 +11,7 @@ logScope:
|
||||||
|
|
||||||
const NUMBER_OF_ADDRESSES_TO_GENERATE = 1
|
const NUMBER_OF_ADDRESSES_TO_GENERATE = 1
|
||||||
const MNEMONIC_PHRASE_LENGTH = 12
|
const MNEMONIC_PHRASE_LENGTH = 12
|
||||||
|
const PK_LENGTH_0X_INCLUDED = 132
|
||||||
|
|
||||||
const GENERATED* = "generated"
|
const GENERATED* = "generated"
|
||||||
const SEED* = "seed"
|
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
|
error "error doing rpc request", methodName = "generateAddresses", exception=e.msg
|
||||||
raise newException(RpcException, 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] =
|
proc compressPk*(publicKey: string): RpcResponse[string] =
|
||||||
let secp256k1Code = "0xe701"
|
let secp256k1Code = "0xe701"
|
||||||
let base58btc = "z"
|
let base58btc = "z"
|
||||||
|
|
|
@ -13,7 +13,8 @@ QtObject {
|
||||||
}
|
}
|
||||||
|
|
||||||
function isChatKey(value) {
|
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) {
|
function isValidETHNamePrefix(value) {
|
||||||
|
|
Loading…
Reference in New Issue