@@ -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}
0 commit comments