Skip to content

Commit 8ec0833

Browse files
committed
refactored to smaller parts
1 parent 5975073 commit 8ec0833

3 files changed

Lines changed: 42 additions & 19 deletions

File tree

src/chat/keys.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { bytesToHex } from '@noble/hashes/utils'
44
import { CERT } from './signature'
55
import { store } from 'solid-logic'
66
import * as $rdf from 'rdflib'
7-
import { publicKeyExists, pubKeyUrl, privKeyUrl, privateKeyExists } from '../utils/cryptoKeyHelpers'
7+
import { getOrCreatePublicKey, pubKeyUrl, privKeyUrl, getOrCreatePrivateKey } from '../utils/cryptoKeyHelpers'
88

99
export function generatePrivateKey (): string {
1010
return bytesToHex(schnorr.utils.randomPrivateKey())
@@ -15,7 +15,7 @@ export function generatePublicKey (privateKey: string): string {
1515
}
1616

1717
export async function getPublicKey (webId) {
18-
const publicKey = await publicKeyExists(webId)
18+
const publicKey = await getOrCreatePublicKey(webId)
1919
return publicKey
2020
}
2121

@@ -27,10 +27,10 @@ export async function getPrivateKey (webId: string) {
2727
const privateKeyUrl = privKeyUrl(webId)
2828

2929
// find publickey
30-
let publicKey = await publicKeyExists(webId)
30+
let publicKey = await getOrCreatePublicKey(webId)
3131
// debug.warn('publicKey ' + publicKey)
3232
// find privateKey
33-
let privateKey = await privateKeyExists(webId)
33+
let privateKey = await getOrCreatePrivateKey(webId)
3434
// debug.warn('privateKey ' + privateKey)
3535
if (privateKey && (publicKey !== generatePublicKey(privateKey as string))) debug.warn('publicKey is not valid')
3636

src/utils/cryptoKeyHelpers.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,28 @@ export const pubKeyUrl = (webId: string) => {
88
return publicKeyUrl
99
}
1010

11-
export async function publicKeyExists (webId: string) {
11+
export const getExistingPublicKey = async (webId: string) => {
12+
const publicKeyUrl = pubKeyUrl(webId)
13+
try {
14+
await store.fetcher.load(publicKeyUrl) // url.href)
15+
const publicKey = store.any(store.sym(webId), store.sym(CERT + 'PublicKey'))
16+
return publicKey?.value // as NamedNode
17+
} catch (err) {
18+
throw Error(err)
19+
}
20+
}
21+
22+
export async function getOrCreatePublicKey (webId: string) {
1223
// find publickey
1324
/* const url = new URL(webId)
1425
url.hash = '' */
1526
/* debug.warn('Alain publicKeyExists')
1627
debug.warn(webId)
1728
debug.warn(url.href) */
1829
const publicKeyUrl = pubKeyUrl(webId)
30+
debug.log(publicKeyUrl)
1931
try {
20-
await store.fetcher.load(publicKeyUrl) // url.href)
21-
const publicKey = store.any(store.sym(webId), store.sym(CERT + 'PublicKey'))
22-
debug.log('publicKeyExists ' + webId)
23-
debug.log(publicKey?.value)
24-
return publicKey?.value // as NamedNode
32+
const publicKey = await getExistingPublicKey(webId)
2533
} catch (err) {
2634
if (err?.response?.status === 404) {
2735
try {
@@ -40,7 +48,7 @@ export async function publicKeyExists (webId: string) {
4048
delete store.fetcher.requested[publicKeyUrl] // delete cached 404 error
4149
return undefined
4250
}
43-
debug.log('createIfNotExists doc FAILED: ' + publicKeyUrl + ': ' + err)
51+
debug.log('getExistingPublicKey failed non 404 Error: ' + publicKeyUrl + ': ' + err)
4452
throw err
4553
}
4654
}
@@ -51,17 +59,26 @@ export const privKeyUrl = (webId: string) => {
5159
return privateKeyUrl
5260
}
5361

54-
export async function privateKeyExists (webId: string) {
62+
export const getExistingPrivateKey = async (webId: string) => {
63+
const privateKeyUrl = privKeyUrl(webId)
64+
try {
65+
await store.fetcher.load(privateKeyUrl)
66+
const privateKey = store.any(store.sym(webId), store.sym(CERT + 'PrivateKey'))
67+
return privateKey?.value // as NamedNode
68+
} catch (err) {
69+
throw Error(err)
70+
}
71+
}
72+
73+
export async function getOrCreatePrivateKey (webId: string) {
5574
/* const url = new URL(webId)
5675
const privateKeyUrl = url.protocol + '//' + url.host + '/profile/privateKey.ttl' */
5776
const privateKeyUrl = privKeyUrl(webId)
5877
/* debug.warn('Alain privateKeyExists')
5978
debug.warn(webId)
6079
debug.warn(privateKeyUrl) */
6180
try {
62-
await store.fetcher.load(privateKeyUrl)
63-
const privateKey = store.any(store.sym(webId), store.sym(CERT + 'PrivateKey'))
64-
return privateKey?.value // as NamedNode
81+
return await getExistingPrivateKey(webId)
6582
} catch (err) {
6683
if (err?.response?.status === 404) {
6784
try {
@@ -80,7 +97,7 @@ export async function privateKeyExists (webId: string) {
8097
delete store.fetcher.requested[privateKeyUrl] // delete cached 404 error
8198
return undefined
8299
}
83-
debug.log('createIfNotExists doc FAILED: ' + privateKeyUrl + ': ' + err)
100+
debug.log('getExistingPrivateKey failed non 404 Error: ' + privateKeyUrl + ': ' + err)
84101
throw err
85102
}
86103
}

test/unit/utils/cryptoKeyHelpers.test.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { pubKeyUrl, publicKeyExists, privKeyUrl, privateKeyExists } from '../../../src/utils/cryptoKeyHelpers'
1+
import * as debug from '../../../src/debug'
2+
import { pubKeyUrl, getExistingPublicKey, privKeyUrl, getExistingPrivateKey } from '../../../src/utils/cryptoKeyHelpers'
3+
4+
// need to mock store.fetcher.load
5+
// and store.any
26

37
describe('cryptoKeyHelpers', () => {
48
describe('pubKeyUrl', () => {
@@ -9,8 +13,10 @@ describe('cryptoKeyHelpers', () => {
913
})
1014

1115
describe('publicKeyExists', () => {
12-
it('returns...', () => {
13-
16+
it('returns...', async () => {
17+
const exists = await getExistingPublicKey('https://sstratsianis.solidcommunity.net/profile/card#me')
18+
debug.log('atesting: ' + exists)
19+
expect(exists).toEqual(undefined)
1420
})
1521
})
1622

0 commit comments

Comments
 (0)