refactoed mint and create session
This commit is contained in:
parent
ab92110970
commit
f2e9100766
@ -1,11 +1,10 @@
|
||||
<script lang="ts">
|
||||
import { onMount, tick } from 'svelte';
|
||||
import { isSignInRedirect, getProviderFromUrl } from '@lit-protocol/lit-auth-client';
|
||||
import { IRelayPKP } from '@lit-protocol/types';
|
||||
import type { IRelayPKP } from '@lit-protocol/types';
|
||||
import { ProviderType } from '@lit-protocol/constants';
|
||||
import { LitAccessControlConditionResource, LitAbility } from '@lit-protocol/auth-helpers';
|
||||
import { createMessage } from '$lib/services/messages';
|
||||
// import { setupLitProvider } from '$lib/services/provider/setupLitProvider.ts';
|
||||
import { createLitSession } from '$lib/services/createLitSession/createLitSession';
|
||||
|
||||
const redirectUri = 'http://localhost:5173/';
|
||||
let view = 'sign_in';
|
||||
@ -131,30 +130,11 @@
|
||||
isLoading = true;
|
||||
error = null;
|
||||
try {
|
||||
console.log('Minting started...');
|
||||
createMessage({
|
||||
text: 'Minting started...',
|
||||
sender: '$lib/components/GoogleAuth.svelte',
|
||||
type: 'SYSTEM'
|
||||
});
|
||||
view = 'MINTING';
|
||||
|
||||
// Using the global provider variable, so no need to call getProvider again
|
||||
const txHash: string = await provider.mintPKPThroughRelayer(authMethod);
|
||||
const response = await provider.relay.pollRequestUntilTerminalState(txHash);
|
||||
if (response.status !== 'Succeeded') {
|
||||
throw new Error('Minting failed');
|
||||
}
|
||||
const newPKP: IRelayPKP = {
|
||||
tokenId: response.pkpTokenId,
|
||||
publicKey: response.pkpPublicKey,
|
||||
ethAddress: response.pkpEthAddress
|
||||
};
|
||||
|
||||
const newPKP: IRelayPKP = await mintPkp(provider, authMethod); // Ensure provider implements IProvider
|
||||
pkps = [...pkps, newPKP];
|
||||
|
||||
await createSession(newPKP); // Moved the call to createSession() here
|
||||
view = 'MINTED'; // Update the view after the session is created
|
||||
await createSession(newPKP);
|
||||
view = 'MINTED';
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
error = err;
|
||||
@ -162,12 +142,6 @@
|
||||
} finally {
|
||||
isLoading = false;
|
||||
}
|
||||
console.log('Minted a new PKP.');
|
||||
createMessage({
|
||||
text: 'Minted a new PKP.',
|
||||
sender: '$lib/components/GoogleAuth.svelte',
|
||||
type: 'SYSTEM'
|
||||
});
|
||||
}
|
||||
|
||||
async function createSession(pkp: IRelayPKP) {
|
||||
@ -182,21 +156,9 @@
|
||||
});
|
||||
view = 'CREATING_SESSION';
|
||||
|
||||
const litResource = new LitAccessControlConditionResource('*');
|
||||
sessionSigs = await provider.getSessionSigs({
|
||||
pkpPublicKey: pkp.publicKey,
|
||||
authMethod,
|
||||
sessionSigsParams: {
|
||||
chain: 'ethereum',
|
||||
resourceAbilityRequests: [
|
||||
{
|
||||
resource: litResource,
|
||||
ability: LitAbility.PKPSigning
|
||||
}
|
||||
]
|
||||
}
|
||||
});
|
||||
sessionSigs = await createLitSession(provider, pkp.publicKey, authMethod);
|
||||
currentPKP = pkp;
|
||||
|
||||
createMessage({
|
||||
text: 'Session created.',
|
||||
sender: '$lib/components/GoogleAuth.svelte',
|
||||
@ -210,9 +172,6 @@
|
||||
} finally {
|
||||
isLoading = false;
|
||||
}
|
||||
|
||||
console.log('Session created.');
|
||||
view = 'SESSION_CREATED';
|
||||
}
|
||||
|
||||
function resetView() {
|
||||
|
@ -7,13 +7,8 @@
|
||||
const store = getComponentStore(id);
|
||||
$: console.log('store:', $store);
|
||||
|
||||
export let setupLitProvider;
|
||||
|
||||
let provider;
|
||||
|
||||
onMount(async () => {
|
||||
provider = await setupLitProvider.setupLitProvider();
|
||||
console.log('provider ' + JSON.stringify(provider));
|
||||
console.log('hello Earth');
|
||||
});
|
||||
</script>
|
||||
|
||||
|
23
src/lib/services/createLitSession/createLitSession.ts
Normal file
23
src/lib/services/createLitSession/createLitSession.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import type { IProvider } from '$lib/services/provider/IProvider';
|
||||
import { LitAccessControlConditionResource, LitAbility } from '@lit-protocol/auth-helpers';
|
||||
|
||||
export async function createLitSession(
|
||||
provider: IProvider,
|
||||
pkpPublicKey: string,
|
||||
authMethod: any
|
||||
): Promise<any> {
|
||||
const litResource = new LitAccessControlConditionResource('*');
|
||||
return await provider.getSessionSigs({
|
||||
pkpPublicKey,
|
||||
authMethod,
|
||||
sessionSigsParams: {
|
||||
chain: 'ethereum',
|
||||
resourceAbilityRequests: [
|
||||
{
|
||||
resource: litResource,
|
||||
ability: LitAbility.PKPSigning
|
||||
}
|
||||
]
|
||||
}
|
||||
});
|
||||
}
|
30
src/lib/services/mintPkp/mintPkp.ts
Normal file
30
src/lib/services/mintPkp/mintPkp.ts
Normal file
@ -0,0 +1,30 @@
|
||||
import type { IRelayPKP } from '@lit-protocol/types';
|
||||
import type { IProvider } from '$lib/services/provider/IProvider';
|
||||
import { createMessage } from '$lib/services/messages';
|
||||
|
||||
export async function mintPkp(provider: IProvider, authMethod: any): Promise<IRelayPKP> {
|
||||
createMessage({
|
||||
text: 'Minting started...',
|
||||
sender: '$lib/components/GoogleAuth.svelte',
|
||||
type: 'SYSTEM'
|
||||
});
|
||||
|
||||
const txHash: string = await provider.mintPKPThroughRelayer(authMethod);
|
||||
const response = await provider.relay.pollRequestUntilTerminalState(txHash);
|
||||
if (response.status !== 'Succeeded') {
|
||||
throw new Error('Minting failed');
|
||||
}
|
||||
const newPKP: IRelayPKP = {
|
||||
tokenId: response.pkpTokenId,
|
||||
publicKey: response.pkpPublicKey,
|
||||
ethAddress: response.pkpEthAddress
|
||||
};
|
||||
|
||||
createMessage({
|
||||
text: 'Minted a new PKP.',
|
||||
sender: '$lib/components/GoogleAuth.svelte',
|
||||
type: 'SYSTEM'
|
||||
});
|
||||
|
||||
return newPKP;
|
||||
}
|
6
src/lib/services/provider/IProvider.ts
Normal file
6
src/lib/services/provider/IProvider.ts
Normal file
@ -0,0 +1,6 @@
|
||||
export interface IProvider {
|
||||
mintPKPThroughRelayer(authMethod: any): Promise<string>;
|
||||
relay: {
|
||||
pollRequestUntilTerminalState(txHash: string): Promise<any>;
|
||||
};
|
||||
}
|
Loading…
Reference in New Issue
Block a user