Skip to content

Commit

Permalink
chore: move statics related to invitation code to invitationCode file
Browse files Browse the repository at this point in the history
  • Loading branch information
EmiM committed Oct 31, 2023
1 parent 9a7fc45 commit 3e1f0f3
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 37 deletions.
17 changes: 8 additions & 9 deletions packages/common/src/invitationCode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import {
invitationShareUrl,
composeInvitationShareUrl,
parseInvitationCodeDeepUrl,
PSK_PARAM_KEY,
} from './invitationCode'
import { QUIET_JOIN_PAGE, Site } from './static'
import { QUIET_JOIN_PAGE } from './static'

describe('Invitation code helper', () => {
const peerId1 = 'QmZoiJNAvCffeEHBjk766nLuKVdkxkAT7wfFJDPPLsbKSA'
Expand Down Expand Up @@ -55,7 +56,7 @@ describe('Invitation code helper', () => {
],
psk: pskDecoded,
})
).toEqual(`quiet://?peerID1=address1&peerID2=address2&${Site.PSK_PARAM_KEY}=${psk}`)
).toEqual(`quiet://?peerID1=address1&peerID2=address2&${PSK_PARAM_KEY}=${psk}`)
})

it('creates invitation share url based on invitation data', () => {
Expand All @@ -66,7 +67,7 @@ describe('Invitation code helper', () => {
],
psk: pskDecoded,
}
const expected = `${QUIET_JOIN_PAGE}#peerID1=address1&peerID2=address2&${Site.PSK_PARAM_KEY}=${psk}`
const expected = `${QUIET_JOIN_PAGE}#peerID1=address1&peerID2=address2&${PSK_PARAM_KEY}=${psk}`
expect(composeInvitationShareUrl(pairs)).toEqual(expected)
})

Expand All @@ -77,13 +78,13 @@ describe('Invitation code helper', () => {
'/dns4/somethingElse.onion/tcp/443/wss/p2p/QmZoiJNAvCffeEHBjk766nLuKVdkxkAT7wfFJDPPLsbKSA',
]
expect(invitationShareUrl(peerList, pskDecoded)).toEqual(
`${QUIET_JOIN_PAGE}#QmZoiJNAvCffeEHBjk766nLuKVdkxkAT7wfFJDPPLsbKSE=gloao6h5plwjy4tdlze24zzgcxll6upq2ex2fmu2ohhyu4gtys4nrjad&QmZoiJNAvCffeEHBjk766nLuKVdkxkAT7wfFJDPPLsbKSA=somethingElse&${Site.PSK_PARAM_KEY}=${psk}`
`${QUIET_JOIN_PAGE}#QmZoiJNAvCffeEHBjk766nLuKVdkxkAT7wfFJDPPLsbKSE=gloao6h5plwjy4tdlze24zzgcxll6upq2ex2fmu2ohhyu4gtys4nrjad&QmZoiJNAvCffeEHBjk766nLuKVdkxkAT7wfFJDPPLsbKSA=somethingElse&${PSK_PARAM_KEY}=${psk}`
)
})

it('retrieves invitation codes from deep url', () => {
const codes = parseInvitationCodeDeepUrl(
`quiet://?${peerId1}=${address1}&${peerId2}=${address2}&${Site.PSK_PARAM_KEY}=${psk}`
`quiet://?${peerId1}=${address1}&${peerId2}=${address2}&${PSK_PARAM_KEY}=${psk}`
)
expect(codes).toEqual({
pairs: [
Expand All @@ -98,9 +99,7 @@ describe('Invitation code helper', () => {
'parsing invitation code throws error if psk is invalid: (%s)',
(psk: string) => {
expect(() => {
parseInvitationCodeDeepUrl(
`quiet://?${peerId1}=${address1}&${peerId2}=${address2}&${Site.PSK_PARAM_KEY}=${psk}`
)
parseInvitationCodeDeepUrl(`quiet://?${peerId1}=${address1}&${peerId2}=${address2}&${PSK_PARAM_KEY}=${psk}`)
}).toThrow()
}
)
Expand All @@ -109,7 +108,7 @@ describe('Invitation code helper', () => {
const peerId2 = 'QmZoiJNAvCffeEHBjk766nLuKVdkxkAT7wfFJDPPLs'
const address2 = 'y7yczmugl2tekami7sbdz5pfaemvx7bahwthrdvcbzw5vex2crsr26qd'
const parsed = parseInvitationCodeDeepUrl(
`quiet://?${peerId1}=${address1}&${peerId2}=${address2}&${Site.PSK_PARAM_KEY}=${psk}`
`quiet://?${peerId1}=${address1}&${peerId2}=${address2}&${PSK_PARAM_KEY}=${psk}`
)
expect(parsed).toEqual({ pairs: [{ peerId: peerId1, onionAddress: address1 }], psk: pskDecoded })
})
Expand Down
27 changes: 19 additions & 8 deletions packages/common/src/invitationCode.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
import { InvitationData, InvitationPair } from '@quiet/types'
import { ONION_ADDRESS_REGEX, PEER_ID_REGEX, QUIET_JOIN_PAGE, Site } from './static'
import { QUIET_JOIN_PAGE } from './static'
import { createLibp2pAddress, isPSKcodeValid } from './libp2p'

const parseDeepUrl = ({ url, expectedProtocol = `quiet:` }: { url: string; expectedProtocol?: string }) => {
export const PSK_PARAM_KEY = 'k'
const DEEP_URL_SCHEME_WITH_SEPARATOR = 'quiet://'
const DEEP_URL_SCHEME = 'quiet'
const ONION_ADDRESS_REGEX = /^[a-z0-9]{56}$/g
const PEER_ID_REGEX = /^[a-zA-Z0-9]{46}$/g

interface ParseDeepUrlParams {
url: string
expectedProtocol?: string
}

const parseDeepUrl = ({ url, expectedProtocol = `${DEEP_URL_SCHEME}:` }: ParseDeepUrlParams): InvitationData => {
let _url = url
let validUrl: URL | null = null

if (!expectedProtocol) {
// Create a full url to be able to use the same URL parsing mechanism
expectedProtocol = `${Site.DEEP_URL_SCHEME}:`
_url = `${Site.DEEP_URL_SCHEME}://?${url}`
expectedProtocol = `${DEEP_URL_SCHEME}:`
_url = `${DEEP_URL_SCHEME}://?${url}`
}

try {
Expand All @@ -24,13 +35,13 @@ const parseDeepUrl = ({ url, expectedProtocol = `quiet:` }: { url: string; expec
}
const params = validUrl.searchParams
const codes: InvitationPair[] = []
let psk = params.get(Site.PSK_PARAM_KEY)
let psk = params.get(PSK_PARAM_KEY)
if (!psk) throw new Error(`No psk found in invitation code '${url}'`)

psk = decodeURIComponent(psk)
if (!isPSKcodeValid(psk)) throw new Error(`Invalid psk in invitation code '${url}'`)

params.delete(Site.PSK_PARAM_KEY)
params.delete(PSK_PARAM_KEY)

params.forEach((onionAddress, peerId) => {
if (!peerDataValid({ peerId, onionAddress })) return
Expand Down Expand Up @@ -110,15 +121,15 @@ export const composeInvitationShareUrl = (data: InvitationData) => {
}

export const composeInvitationDeepUrl = (data: InvitationData): string => {
return composeInvitationUrl(`${Site.DEEP_URL_SCHEME_WITH_SEPARATOR}`, data)
return composeInvitationUrl(`${DEEP_URL_SCHEME_WITH_SEPARATOR}`, data)
}

const composeInvitationUrl = (baseUrl: string, data: InvitationData): string => {
const url = new URL(baseUrl)
for (const pair of data.pairs) {
url.searchParams.append(pair.peerId, pair.onionAddress)
}
url.searchParams.append(Site.PSK_PARAM_KEY, data.psk)
url.searchParams.append(PSK_PARAM_KEY, data.psk)
return url.href
}

Expand Down
6 changes: 0 additions & 6 deletions packages/common/src/static.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
export const ONION_ADDRESS_REGEX = /^[a-z0-9]{56}$/g
export const PEER_ID_REGEX = /^[a-zA-Z0-9]{46}$/g

export enum Site {
DEEP_URL_SCHEME_WITH_SEPARATOR = 'quiet://',
DEEP_URL_SCHEME = 'quiet',
DOMAIN = 'tryquiet.org',
MAIN_PAGE = 'https://tryquiet.org/',
JOIN_PAGE = 'join',
PSK_PARAM_KEY = 'k',
}

export const QUIET_JOIN_PAGE = `${Site.MAIN_PAGE}${Site.JOIN_PAGE}`
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import PerformCommunityActionComponent from '../PerformCommunityActionComponent'
import { inviteLinkField } from '../../../forms/fields/communityFields'
import { InviteLinkErrors } from '../../../forms/fieldsErrors'
import { CommunityOwnership } from '@quiet/types'
import { Site, QUIET_JOIN_PAGE, validInvitationUrlTestData } from '@quiet/common'
import { Site, QUIET_JOIN_PAGE, validInvitationUrlTestData, PSK_PARAM_KEY } from '@quiet/common'

describe('join community', () => {
const validCode = validInvitationUrlTestData.code()
Expand Down Expand Up @@ -199,7 +199,7 @@ describe('join community', () => {

it.each([
[`http://${validCode}`, InviteLinkErrors.InvalidCode],
[`QmZoiJNAvCffeEHBjk766nLuKVdkxkAT7wfFJDPPLsbKSE=bbb&${Site.PSK_PARAM_KEY}=${psk}`, InviteLinkErrors.InvalidCode],
[`QmZoiJNAvCffeEHBjk766nLuKVdkxkAT7wfFJDPPLsbKSE=bbb&${PSK_PARAM_KEY}=${psk}`, InviteLinkErrors.InvalidCode],
['bbb=y7yczmugl2tekami7sbdz5pfaemvx7bahwthrdvcbzw5vex2crsr26qd', InviteLinkErrors.InvalidCode],
['QmZoiJNAvCffeEHBjk766nLuKVdkxkAT7wfFJDPPLsbKSE= ', InviteLinkErrors.InvalidCode],
['nqnw4kc4c77fb47lk52m5l57h4tc', InviteLinkErrors.InvalidCode],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { InviteLinkErrors } from '../../forms/fieldsErrors'
import { IconButton, InputAdornment } from '@mui/material'
import VisibilityOff from '@mui/icons-material/VisibilityOff'
import Visibility from '@mui/icons-material/Visibility'
import { ONION_ADDRESS_REGEX, composeInvitationShareUrl, parseName } from '@quiet/common'
import { composeInvitationShareUrl, parseName } from '@quiet/common'
import { getInvitationCodes } from '@quiet/state-manager'

const PREFIX = 'PerformCommunityActionComponent'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getInvitationCodes } from './invitationCode'
import { QUIET_JOIN_PAGE, Site } from '@quiet/common'
import { PSK_PARAM_KEY, QUIET_JOIN_PAGE } from '@quiet/common'

describe('Invitation code helper', () => {
const peerId1 = 'QmZoiJNAvCffeEHBjk766nLuKVdkxkAT7wfFJDPPLsbKSA'
Expand All @@ -11,7 +11,7 @@ describe('Invitation code helper', () => {

it('retrieves invitation code if url is a proper share url', () => {
const result = getInvitationCodes(
`${QUIET_JOIN_PAGE}#${peerId1}=${address1}&${peerId2}=${address2}&${Site.PSK_PARAM_KEY}=${encodedPsk}`
`${QUIET_JOIN_PAGE}#${peerId1}=${address1}&${peerId2}=${address2}&${PSK_PARAM_KEY}=${encodedPsk}`
)
expect(result).toEqual({
pairs: [
Expand All @@ -31,15 +31,11 @@ describe('Invitation code helper', () => {
})

it('throws error if psk has invalid format', () => {
expect(() =>
getInvitationCodes(`${peerId1}=${address1}&${peerId2}=${address2}&${Site.PSK_PARAM_KEY}=12345`)
).toThrow()
expect(() => getInvitationCodes(`${peerId1}=${address1}&${peerId2}=${address2}&${PSK_PARAM_KEY}=12345`)).toThrow()
})

it('retrieves invitation code if url is a proper code', () => {
const result = getInvitationCodes(
`${peerId1}=${address1}&${peerId2}=${address2}&${Site.PSK_PARAM_KEY}=${encodedPsk}`
)
const result = getInvitationCodes(`${peerId1}=${address1}&${peerId2}=${address2}&${PSK_PARAM_KEY}=${encodedPsk}`)
expect(result).toEqual({
pairs: [
{ peerId: peerId1, onionAddress: address1 },
Expand All @@ -50,9 +46,7 @@ describe('Invitation code helper', () => {
})

it('retrieves invitation code if url is a proper code', () => {
const result = getInvitationCodes(
`${peerId1}=${address1}&${peerId2}=${address2}&${Site.PSK_PARAM_KEY}=${encodedPsk}`
)
const result = getInvitationCodes(`${peerId1}=${address1}&${peerId2}=${address2}&${PSK_PARAM_KEY}=${encodedPsk}`)
expect(result).toEqual({
pairs: [
{ peerId: peerId1, onionAddress: address1 },
Expand Down

0 comments on commit 3e1f0f3

Please sign in to comment.