chore: add utility to compress pubkey

This commit is contained in:
Patryk Osmaczko 2022-03-29 12:53:41 +02:00 committed by Iuri Matias
parent 1f82a784d1
commit 53c38624ef
4 changed files with 45 additions and 1 deletions

View File

@ -141,3 +141,6 @@ QtObject:
proc getColorHashAsJson*(self: Utils, publicKey: string): string {.slot.} = proc getColorHashAsJson*(self: Utils, publicKey: string): string {.slot.} =
procs_from_visual_identity_service.getColorHashAsJson(publicKey) procs_from_visual_identity_service.getColorHashAsJson(publicKey)
proc getCompressedPk*(self: Utils, publicKey: string): string {.slot.} =
procs_from_accounts.compressPk(publicKey)

View File

@ -46,6 +46,16 @@ proc getImportedAccount*(self: Service): GeneratedAccountDto =
proc isFirstTimeAccountLogin*(self: Service): bool = proc isFirstTimeAccountLogin*(self: Service): bool =
return self.isFirstTimeAccountLogin return self.isFirstTimeAccountLogin
proc compressPk*(publicKey: string): string =
try:
let response = status_account.compressPk(publicKey)
if(not response.error.isNil):
error "error compressPk: ", errDescription = response.error.message
result = response.result
except Exception as e:
error "error: ", procName="compressPk", errName = e.name, errDesription = e.msg
proc generateAliasFromPk*(publicKey: string): string = proc generateAliasFromPk*(publicKey: string): string =
return status_account.generateAlias(publicKey).result.getStr return status_account.generateAlias(publicKey).result.getStr

View File

@ -1,4 +1,4 @@
import json, json_serialization, chronicles, nimcrypto import json, json_serialization, chronicles, nimcrypto, strutils
import ./core, ./utils import ./core, ./utils
import ./response_type import ./response_type
@ -52,6 +52,22 @@ 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 compressPk*(publicKey: string): RpcResponse[string] =
let secp256k1Code = "0xe701"
let base58btc = "z"
var multiCodecKey = publicKey
multiCodecKey.removePrefix("0x")
multiCodecKey.insert(secp256k1Code)
let response = status_go.multiformatSerializePublicKey(multiCodecKey, base58btc)
# json response indicates error
try:
let jsonReponse = parseJson(response)
result.error = RpcError(message: jsonReponse["error"].getStr())
except JsonParsingError as e:
result.result = response
proc generateAlias*(publicKey: string): RpcResponse[JsonNode] {.raises: [Exception].} = proc generateAlias*(publicKey: string): RpcResponse[JsonNode] {.raises: [Exception].} =
try: try:
let response = status_go.generateAlias(publicKey) let response = status_go.generateAlias(publicKey)

View File

@ -618,6 +618,21 @@ QtObject {
return JSON.parse(jsonObj) return JSON.parse(jsonObj)
} }
function getCompressedPk(publicKey) {
if (publicKey === "") {
return ""
}
return globalUtils.getCompressedPk(publicKey)
}
function getElidedCompressedPk(publicKey) {
if (publicKey === "") {
return ""
}
let compressedPk = getCompressedPk(publicKey)
return compressedPk.substr(3, 3) + "..." + compressedPk.substr(compressedPk.length - 3)
}
function getTimeDifference(d1, d2) { function getTimeDifference(d1, d2) {
var timeString = "" var timeString = ""
var day1Year = d1.getFullYear() var day1Year = d1.getFullYear()