Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update social demo app #255

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions apps/demo-pkp-social-auth-next-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
"lint": "next lint"
},
"dependencies": {
"@lit-protocol/auth-helpers": "^3.0.6",
"@lit-protocol/constants": "^3.0.6",
"@lit-protocol/lit-auth-client": "^3.0.6",
"@lit-protocol/pkp-ethers": "^3.0.6",
"@lit-protocol/types": "^3.0.6",
"@lit-protocol/auth-helpers": "3.0.21",
"@lit-protocol/constants": "3.0.21",
"@lit-protocol/contracts-sdk": "^3.0.21",
"@lit-protocol/lit-auth-client": "3.0.21",
"@lit-protocol/pkp-ethers": "3.0.21",
"@lit-protocol/types": "3.0.21",
"@radix-ui/react-radio-group": "^1.1.3",
"@stytch/nextjs": "^11.0.0",
"@stytch/vanilla-js": "^2.2.0",
Expand Down
22 changes: 22 additions & 0 deletions apps/demo-pkp-social-auth-next-ts/src/hooks/useSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { getSessionSigs } from '../utils/lit';
import { LitAbility, LitActionResource } from '@lit-protocol/auth-helpers';
import { IRelayPKP } from '@lit-protocol/types';
import { SessionSigs } from '@lit-protocol/types';
import { LitContracts } from '@lit-protocol/contracts-sdk';
import { LitAuthClient } from '@lit-protocol/lit-auth-client';

export default function useSession() {
const [sessionSigs, setSessionSigs] = useState<SessionSigs>();
Expand All @@ -30,6 +32,26 @@ export default function useSession() {
Date.now() + 1000 * 60 * 60 * 24 * 7
).toISOString(); // 1 week

// -- check permissions
const contractClient = new LitContracts();
await contractClient.connect();

const authId = await LitAuthClient.getAuthIdByAuthMethod(authMethod);

const scopes = await contractClient.pkpPermissionsContract.read.getPermittedAuthMethodScopes(
pkp.tokenId,
authMethod.authMethodType,
authId,
3
);

if (!scopes[1] && !scopes[2]) {
const msg = `Your PKP does not have the required permissions! Please use the 'addPermittedAuthMethodScope' method from the PKPPermissions contract to add the required permissions.\nRead more at https://developer.litprotocol.com/v3/sdk/wallets/auth-methods/#auth-method-scopes`;
console.error(msg);
alert(msg);
return;
}

// Generate session sigs
const sessionSigs = await getSessionSigs({
pkpPublicKey: pkp.publicKey,
Expand Down
14 changes: 12 additions & 2 deletions apps/demo-pkp-social-auth-next-ts/src/utils/lit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
AuthMethod,
GetSessionSigsProps,
IRelayPKP,
MintRequestBody,
SessionSigs,
} from '@lit-protocol/types';

Expand Down Expand Up @@ -180,6 +181,7 @@ export async function getSessionSigs({
authMethod: AuthMethod;
sessionSigsParams: GetSessionSigsProps;
}): Promise<SessionSigs> {

// const provider = getProviderByAuthMethod(authMethod);
// if (provider) {
// const sessionSigs = await provider.getSessionSigs({
Expand Down Expand Up @@ -236,6 +238,10 @@ export async function getPKPs(authMethod: AuthMethod): Promise<IRelayPKP[]> {
export async function mintPKP(authMethod: AuthMethod): Promise<IRelayPKP> {
const provider = getProviderByAuthMethod(authMethod);

const authMethodScopePrompt = prompt('Enter the auth method scope.\n0 - no permissions\n1 - to sign anything\n2 - to only sign messages. \n\nRead more at https://developer.litprotocol.com/v3/sdk/wallets/auth-methods/#auth-method-scopes');
const authMethodScope = parseInt(authMethodScopePrompt);
console.log("authMethodScope:", authMethodScope);

let txHash: string;

if (authMethod.authMethodType === AuthMethodType.WebAuthn) {
Expand All @@ -245,10 +251,14 @@ export async function mintPKP(authMethod: AuthMethod): Promise<IRelayPKP> {
// Verify registration and mint PKP through relay server
txHash = await (
provider as WebAuthnProvider
).verifyAndMintPKPThroughRelayer(options);
).verifyAndMintPKPThroughRelayer(options, {
permittedAuthMethodScopes: [[authMethodScope]],
});
} else {
// Mint PKP through relay server
txHash = await provider.mintPKPThroughRelayer(authMethod);
txHash = await provider.mintPKPThroughRelayer(authMethod, {
permittedAuthMethodScopes: [[authMethodScope]],
});
}

const response = await provider.relay.pollRequestUntilTerminalState(txHash);
Expand Down
1 change: 1 addition & 0 deletions packages/lit-auth-client/src/lib/providers/BaseProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export abstract class BaseProvider {
* Mint a new PKP for the given auth method through the relay server
*
* @param {AuthMethod} authMethod - Auth method object
* @param {MintRequestBody} [customArgs] - Extra data to overwrite default params
*
* @returns {Promise<string>} - Mint transaction hash
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ export default class WebAuthnProvider extends BaseProvider {
* Mint PKP with verified registration data
*
* @param {PublicKeyCredentialCreationOptionsJSON} options - Registration options to pass to the authenticator
*
* @param {MintRequestBody} [customArgs] - Extra data to overwrite default params
*
* @returns {Promise<string>} - Mint transaction hash
*/
public async verifyAndMintPKPThroughRelayer(
Expand Down Expand Up @@ -74,6 +75,7 @@ export default class WebAuthnProvider extends BaseProvider {
sendPkpToItself: true,
};


const args = {
...defaultArgs,
...customArgs
Expand Down
Loading