-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement ceramic session (#246)
* feat: implement ceramic session * feat: implement ceramic session in connector * fix: linting errors * fix: wrong type * fix: tests * fix: formatting * fix: pr review * fix: linting errors * fix: pr review * chore: remove duplicate types, small code refactor * chore: better check for getVCStore error --------- Co-authored-by: Martin Domajnko <domajnko.martin@gmail.com>
- Loading branch information
1 parent
75cdf01
commit dc6f985
Showing
30 changed files
with
311 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { isError } from '@blockchain-lab-um/utils'; | ||
import { EthereumWebAuth, getAccountId } from '@didtools/pkh-ethereum'; | ||
import { DIDSession } from 'did-session'; | ||
|
||
import { Masca } from './snap.js'; | ||
|
||
export async function validateAndSetCeramicSession( | ||
masca: Masca | ||
): Promise<void> { | ||
// Check if there is valid session in Masca | ||
const api = masca.getMascaApi(); | ||
|
||
const enabledVCStoresResult = await api.getVCStore(); | ||
if (isError(enabledVCStoresResult)) { | ||
throw new Error('Failed to get enabled VC stores.'); | ||
} | ||
|
||
// Check if ceramic is enabled | ||
if (enabledVCStoresResult.data.ceramic === false) { | ||
return; | ||
} | ||
|
||
const session = await api.validateStoredCeramicSession(); | ||
if (!isError(session)) { | ||
return; | ||
} | ||
|
||
const addresses: string[] = await window.ethereum.request({ | ||
method: 'eth_requestAccounts', | ||
}); | ||
|
||
const accountId = await getAccountId(window.ethereum, addresses[0]); | ||
|
||
const authMethod = await EthereumWebAuth.getAuthMethod( | ||
window.ethereum, | ||
accountId | ||
); | ||
|
||
let newSession; | ||
try { | ||
newSession = await DIDSession.authorize(authMethod, { | ||
expiresInSecs: 60 * 60 * 24, | ||
resources: [`ceramic://*`], | ||
}); | ||
} catch (e) { | ||
throw new Error('User failed to sign session.'); | ||
} | ||
const serializedSession = newSession.serialize(); | ||
const result = await api.setCeramicSession(serializedSession); | ||
if (isError(result)) { | ||
throw new Error('Failed to set session in Masca.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { ApiParams } from '../interfaces'; | ||
import { updateSnapState } from '../utils/stateUtils'; | ||
|
||
export async function setCeramicSession( | ||
params: ApiParams, | ||
serializedSession: string | ||
): Promise<boolean> { | ||
const { state } = params; | ||
state.accountState[state.currentAccount].ceramicSession = serializedSession; | ||
await updateSnapState(snap, state); | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { ApiParams } from '../interfaces'; | ||
import { validateSession } from '../utils/ceramicUtils'; | ||
|
||
export async function validateStoredCeramicSession( | ||
params: ApiParams | ||
): Promise<string> { | ||
const { state } = params; | ||
const serializedSession = await validateSession( | ||
state.accountState[state.currentAccount].ceramicSession | ||
); | ||
return serializedSession; | ||
} |
Oops, something went wrong.