-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add auth method support to executeJs and helper methods
- Loading branch information
Josh Long
committed
Oct 27, 2023
1 parent
d1b7b06
commit 1aec432
Showing
5 changed files
with
171 additions
and
41 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
108 changes: 108 additions & 0 deletions
108
e2e-nodejs/group-pkp-auth-method/test-pkp-auth-method-authentication.mjs
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,108 @@ | ||
import path from 'path'; | ||
import { success, fail, testThis } from '../../tools/scripts/utils.mjs'; | ||
import LITCONFIG from '../../lit.config.json' assert { type: 'json' }; | ||
import { client } from '../00-setup.mjs'; | ||
import { LitAbility, LitActionResource } from '@lit-protocol/auth-helpers'; | ||
import { LitAuthClient } from '@lit-protocol/lit-auth-client'; | ||
import { AuthMethodType, ProviderType } from '@lit-protocol/constants'; | ||
import { ethers } from 'ethers'; | ||
import { PKPEthersWallet } from '@lit-protocol/pkp-ethers'; | ||
|
||
// NOTE: you need to hash data before you send it in. | ||
// If you send something that isn't 32 bytes, the nodes will return an error. | ||
const TO_SIGN = new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEncoder().encode("meow"))); | ||
|
||
export async function main() { | ||
// ==================== Setup ==================== | ||
|
||
const litAuthClient = new LitAuthClient({ | ||
litRelayConfig: { | ||
relayApiKey: '67e55044-10b1-426f-9247-bb680e5fe0c8_relayer', | ||
}, | ||
version: 'V3', | ||
litNodeClient: client, | ||
}); | ||
|
||
// -- eth wallet | ||
const authProvider = litAuthClient.initProvider(ProviderType.EthWallet); | ||
const authMethod = { | ||
authMethodType: AuthMethodType.EthWallet, | ||
accessToken: JSON.stringify(LITCONFIG.CONTROLLER_AUTHSIG), | ||
}; | ||
|
||
let pkps = await authProvider.fetchPKPsThroughRelayer(authMethod); | ||
|
||
if (pkps.length <= 0) { | ||
try { | ||
await authProvider.mintPKPThroughRelayer(authMethod); | ||
} catch (e) { | ||
return fail('Failed to mint PKP'); | ||
} | ||
pkps = await authProvider.fetchPKPsThroughRelayer(authMethod); | ||
} | ||
|
||
const pkp = pkps[pkps.length - 1]; | ||
|
||
// convert BigNumber to string | ||
pkp.tokenId = ethers.BigNumber.from(pkp.tokenId).toString(); | ||
|
||
const pkpPubKey = pkp.publicKey; | ||
|
||
const pkpSignRes = await client?.executeJs({ | ||
authSig: LITCONFIG.CONTROLLER_AUTHSIG, | ||
authMethods: [authMethod], // This is not workingu! | ||
code: `(async () => { | ||
const sigShare = await LitActions.signEcdsa({ | ||
toSign, | ||
publicKey, | ||
sigName: "sig", | ||
}); | ||
LitActions.setResponse({response: JSON.stringify(Lit.Auth)}); | ||
})();`, | ||
jsParams: { | ||
toSign: TO_SIGN, | ||
publicKey: LITCONFIG.PKP_PUBKEY, | ||
}, | ||
}); | ||
|
||
// ==================== Post-Validation ==================== | ||
|
||
if (!pkpSignRes) { | ||
return fail( | ||
'Failed to sign data with sessionSigs generated by eth wallet auth method' | ||
); | ||
} | ||
|
||
let missingKeys = []; | ||
|
||
if (pkpSignRes) { | ||
['r', 's', 'recid', 'signature', 'publicKey', 'dataSigned'].forEach( | ||
(key) => { | ||
if (pkpSignRes.signatures['sig'][key] === undefined) { | ||
missingKeys.push(key); | ||
} | ||
} | ||
); | ||
} | ||
|
||
if (missingKeys.length > 0) { | ||
return fail(`Missing keys: ${missingKeys.join(', ')}`); | ||
} | ||
|
||
if (!pkpSignRes.response.authSigAddress == LITCONFIG.CONTROLLER_AUTHSIG.address) { | ||
return fail(`Missing or non matching auth sig address in Lit.Auth context`); | ||
} | ||
|
||
if(pkpSignRes.response.authMethodContexts.length < 1) { | ||
return fail('not enough authentication material in Lit.Auth context'); | ||
} | ||
|
||
// ==================== Success ==================== | ||
return success( | ||
`it should use sessionSigs generated by eth wallet auth method to sign data. Signature is ${signature} and pkpSignRes is ${JSON.stringify( | ||
pkpSignRes | ||
)}` | ||
); | ||
} | ||
|
||
await testThis({ name: path.basename(import.meta.url), fn: main }); |
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