Skip to content

Commit

Permalink
feat(@desktop/general): Use compressed community key in deep links
Browse files Browse the repository at this point in the history
  • Loading branch information
endulab committed Nov 8, 2022
1 parent e687dad commit 1f2a050
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 12 deletions.
9 changes: 8 additions & 1 deletion src/app/core/custom_urls/urls_manager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import NimQml, strutils, chronicles
import ../eventemitter

import ../../global/app_signals
import ../../../app_service/common/conversion
import ../../../app_service/service/accounts/utils

logScope:
topics = "urls-manager"
Expand Down Expand Up @@ -44,6 +46,11 @@ QtObject:
result.protocolUriOnStart = protocolUriOnStart
result.loggedIn = false

proc prepareCommunityId(self: UrlsManager, communityId: string): string =
if isCompressedPubKey(communityId):
return changeCommunityKeyCompression(communityId)
return communityId

proc onUrlActivated*(self: UrlsManager, urlRaw: string) {.slot.} =
if not self.loggedIn:
self.protocolUriOnStart = urlRaw
Expand All @@ -62,7 +69,7 @@ QtObject:
# Open community with `community_key`
elif url.startsWith(UriFormatCommunity):
data.action = StatusUrlAction.OpenCommunity
data.communityId = url[UriFormatCommunity.len .. url.len-1]
data.communityId = self.prepareCommunityId(url[UriFormatCommunity.len .. url.len-1])

# Open community which has a channel with `channel_key` and makes that channel active
elif url.startsWith(UriFormatCommunityChannel):
Expand Down
13 changes: 13 additions & 0 deletions src/app/global/utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,18 @@ QtObject:
proc getCompressedPk*(self: Utils, publicKey: string): string {.slot.} =
compressPk(publicKey)

proc getDecompressedPk*(self: Utils, compressedKey: string): string {.slot.} =
decompressPk(compressedKey)

proc decompressCommunityKey*(self: Utils, publicKey: string): string {.slot.} =
decompressCommunityKey(publicKey)

proc compressCommunityKey*(self: Utils, publicKey: string): string {.slot.} =
compressCommunityKey(publicKey)

proc isCompressedPubKey*(self: Utils, publicKey: string): bool {.slot.} =
conversion.isCompressedPubKey(publicKey)

# Changes publicKey compression between 33-bytes and multiformat zQ..
proc changeCommunityKeyCompression*(self: Utils, publicKey: string): string {.slot.} =
changeCommunityKeyCompression(publicKey)
44 changes: 43 additions & 1 deletion src/app_service/service/accounts/utils.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import ../../../backend/accounts as status_account
import ../../common/conversion

proc compressPk*(publicKey: string): string =
try:
Expand All @@ -11,8 +12,49 @@ proc compressPk*(publicKey: string): string =
except Exception as e:
echo "error: `compressPk` " & $e.name & " msg: " & $e.msg

proc decompressPk*(compressedKey: string): string =
try:
let response = status_account.decompressPk(compressedKey)
if(not response.error.isNil):
echo "error decompressPk: " & response.error.message
result = response.result

except Exception as e:
echo "error: `decompressPk` " & $e.name & " msg: " & $e.msg

proc decompressCommunityKey*(publicKey: string): string =
try:
let response = status_account.decompressCommunityKey(publicKey)
if(not response.error.isNil):
echo "error decompressCommunityKey: " & response.error.message
result = response.result

except Exception as e:
echo "error: `decompressCommunityKey` " & $e.name & " msg: " & $e.msg

proc compressCommunityKey*(publicKey: string): string =
try:
let response = status_account.compressCommunityKey(publicKey)
if(not response.error.isNil):
echo "error compressCommunityKey: " & response.error.message
result = response.result

except Exception as e:
echo "error: `compressCommunityKey` " & $e.name & " msg: " & $e.msg

proc generateAliasFromPk*(publicKey: string): string =
return status_account.generateAlias(publicKey).result.getStr

proc isAlias*(value: string): bool =
return status_account.isAlias(value)
return status_account.isAlias(value)

# Changes publicKey compression between 33-bytes and multiformat zQ..
proc changeCommunityKeyCompression*(publicKey: string): string =
if isCompressedPubKey(publicKey):
# is zQ
let uncompressedKey = decompressPk(publicKey)
return compressCommunityKey(uncompressedKey)
else:
# is 33-bytes
let uncompressedKey = decompressCommunityKey(publicKey)
return compressPk(uncompressedKey)
24 changes: 23 additions & 1 deletion src/backend/accounts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,29 @@ proc decompressPk*(publicKey: string): RpcResponse[string] =
let secp256k1Code = "fe701"
response.removePrefix(secp256k1Code)
result.result = "0x" & response


proc decompressCommunityKey*(publicKey: string): RpcResponse[string] =

let response = status_go.decompressPublicKey(publicKey)

# json response indicates error
try:
let jsonReponse = parseJson(response)
result.error = RpcError(message: jsonReponse["error"].getStr())
except JsonParsingError as e:
result.result = response

proc compressCommunityKey*(publicKey: string): RpcResponse[string] =

let response = status_go.compressPublicKey(publicKey)

# json response indicates error
try:
let jsonReponse = parseJson(response)
result.error = RpcError(message: jsonReponse["error"].getStr())
except JsonParsingError as e:
result.result = response

proc compressPk*(publicKey: string): RpcResponse[string] =
let secp256k1Code = "0xe701"
let base58btc = "z"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ ColumnLayout {

StatusDescriptionListItem {
title: qsTr("Share community")
subTitle: `${Constants.communityLinkPrefix}${root.community && root.community.id.substring(0, 4)}...${root.community && root.community.id.substring(root.community.id.length -2)}`
subTitle: Utils.getCommunityShareLink(root.community.id, true)
tooltip.text: qsTr("Copied!")
asset.name: "copy"
iconButton.onClicked: {
let link = `${Constants.communityLinkPrefix}${root.community.id}`
let link = Utils.getCommunityShareLink(root.community.id)
root.rootStore.copyToClipboard(link)
tooltip.visible = !tooltip.visible
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ Column {

StatusDescriptionListItem {
title: qsTr("Share community")
subTitle: `${Constants.communityLinkPrefix}${root.community.id.substring(0, 4)}...${root.community.id.substring(root.community.id.length -2)}`
subTitle: Utils.getCommunityShareLink(root.community.id, true)
tooltip.text: qsTr("Copied!")
asset.name: "copy"
iconButton.onClicked: {
let link = `${Constants.communityLinkPrefix}${root.community.id}`
let link = Utils.getCommunityShareLink(root.community.id)
root.copyToClipboard(link);
tooltip.visible = !tooltip.visible
}
Expand Down
8 changes: 3 additions & 5 deletions ui/app/AppLayouts/Chat/stores/RootStore.qml
Original file line number Diff line number Diff line change
Expand Up @@ -388,15 +388,13 @@ QtObject {
}*/

// Community
let index = link.lastIndexOf("/c/")
if (index > -1) {
const communityId = link.substring(index + 3)

const communityId = Utils.getCommunityIdFromShareLink(link)
if (communityId !== "") {
const communityName = getSectionNameById(communityId)

if (!communityName) {
// Unknown community, fetch the info if possible
communitiesModuleInst.requestCommunityInfo(communityId)
root.requestCommunityInfo(communityId)
result.communityId = communityId
result.fetching = true
return result
Expand Down
26 changes: 26 additions & 0 deletions ui/imports/utils/Utils.qml
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,19 @@ QtObject {
return colorForColorId(pubKeyColorId)
}

function getCommunityShareLink(communityId, elided = false) {
if (communityId === "") {
return ""
}

let compressedPk = communityId
if (!globalUtilsInst.isCompressedPubKey(compressedPk)) {
compressedPk = globalUtilsInst.changeCommunityKeyCompression(compressedPk)
}
return Constants.communityLinkPrefix +
(elided ? StatusQUtils.Utils.elideText(compressedPk, 4, 2) : compressedPk)
}

function getChatKeyFromShareLink(link) {
let index = link.lastIndexOf("/u/")
if (index === -1) {
Expand All @@ -616,6 +629,19 @@ QtObject {
return link.substring(index + 3)
}

function getCommunityIdFromShareLink(link) {
let index = link.lastIndexOf("/c/")
if (index === -1) {
return ""
}
const communityKey = link.substring(index + 3)
if (globalUtilsInst.isCompressedPubKey(communityKey)) {
// is zQ.., need to be converted to standard compression
return globalUtilsInst.changeCommunityKeyCompression(communityKey)
}
return communityKey
}

function getCompressedPk(publicKey) {
if (publicKey === "") {
return ""
Expand Down

0 comments on commit 1f2a050

Please sign in to comment.