-
Notifications
You must be signed in to change notification settings - Fork 10
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5cbf558
Init eip-191 signing example
spacesailor24 78b9f32
Update browser example
spacesailor24 561f647
Update package description and name
spacesailor24 b9fef5a
Init nodejs example
spacesailor24 c70607e
Add signature verification
spacesailor24 e7c03e8
Remove process.env reads
spacesailor24 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 @@ | ||
PKP_PUBLIC_KEY= |
This file contains hidden or 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,4 @@ | ||
.env | ||
.cache | ||
dist | ||
node_modules |
This file contains hidden or 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,8 @@ | ||
# Running this Example | ||
|
||
1. `yarn` | ||
2. `yarn start` | ||
3. Click the `Click Me` button | ||
4. Connect your wallet | ||
5. Sign a message to generate a SessionSig | ||
6. The PKP signed message will be in the JavaScript console |
This file contains hidden or 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,22 @@ | ||
{ | ||
"name": "generating-eip-191-sigs-in-browser", | ||
"version": "0.1.0", | ||
"description": "Example of generating an EIP-191 signature using a Lit Action", | ||
"source": "src/index.html", | ||
"license": "MIT", | ||
"scripts": { | ||
"start": "parcel ./src/index.html" | ||
}, | ||
"dependencies": { | ||
"@lit-protocol/auth-browser": "^6.0.0-alpha.4", | ||
"@lit-protocol/auth-helpers": "^6.0.0-alpha.4", | ||
"@lit-protocol/constants": "^6.0.0-alpha.10", | ||
"@lit-protocol/contracts-sdk": "^6.0.0-alpha.10", | ||
"@lit-protocol/lit-node-client": "^6.0.0-alpha.4", | ||
"ethers": "5.7.2" | ||
}, | ||
"devDependencies": { | ||
"parcel-bundler": "^1.12.5", | ||
"tslib": "^2.6.2" | ||
} | ||
} |
This file contains hidden or 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 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Lit Session Signature Example</title> | ||
</head> | ||
<body> | ||
<button id="myButton">Click Me</button> | ||
<script src="./index.js"></script> | ||
</body> | ||
</html> |
This file contains hidden or 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,141 @@ | ||
import { LitNodeClient } from "@lit-protocol/lit-node-client"; | ||
import { LitNetwork } from "@lit-protocol/constants"; | ||
import { | ||
createSiweMessageWithRecaps, | ||
generateAuthSig, | ||
LitAbility, | ||
LitActionResource, | ||
LitPKPResource, | ||
} from "@lit-protocol/auth-helpers"; | ||
import { disconnectWeb3 } from "@lit-protocol/auth-browser"; | ||
import { LitContracts } from "@lit-protocol/contracts-sdk"; | ||
import * as ethers from "ethers"; | ||
|
||
import { litActionCode } from "./litAction"; | ||
|
||
const PKP_PUBLIC_KEY = process.env.PKP_PUBLIC_KEY; | ||
|
||
document.addEventListener("DOMContentLoaded", () => { | ||
document.getElementById("myButton").addEventListener("click", buttonClick); | ||
}); | ||
|
||
async function buttonClick() { | ||
try { | ||
console.log("Clicked"); | ||
|
||
const provider = new ethers.providers.Web3Provider(window.ethereum); | ||
await provider.send("eth_requestAccounts", []); | ||
const ethersSigner = provider.getSigner(); | ||
console.log("Connected account:", await ethersSigner.getAddress()); | ||
|
||
const litNodeClient = await getLitNodeClient(); | ||
|
||
const sessionSigs = await getSessionSigs(litNodeClient, ethersSigner); | ||
console.log("Got Session Signatures!"); | ||
|
||
const litActionSignatures = await litNodeClient.executeJs({ | ||
sessionSigs, | ||
code: litActionCode, | ||
jsParams: { | ||
dataToSign: ethers.utils.arrayify( | ||
ethers.utils.keccak256([1, 2, 3, 4, 5]) | ||
), | ||
publicKey: await getPkpPublicKey(ethersSigner), | ||
sigName: "sig", | ||
}, | ||
}); | ||
console.log("litActionSignatures: ", litActionSignatures); | ||
|
||
verifySignature(litActionSignatures.signatures.sig); | ||
} catch (error) { | ||
console.error(error); | ||
} finally { | ||
disconnectWeb3(); | ||
} | ||
} | ||
|
||
async function getLitNodeClient() { | ||
const litNodeClient = new LitNodeClient({ | ||
litNetwork: LitNetwork.Cayenne, | ||
}); | ||
|
||
console.log("Connecting litNodeClient to network..."); | ||
await litNodeClient.connect(); | ||
|
||
console.log("litNodeClient connected!"); | ||
return litNodeClient; | ||
} | ||
|
||
async function getPkpPublicKey(ethersSigner) { | ||
if (PKP_PUBLIC_KEY !== undefined && PKP_PUBLIC_KEY !== "") | ||
return PKP_PUBLIC_KEY; | ||
|
||
const pkp = await mintPkp(ethersSigner); | ||
console.log("Minted PKP!", pkp); | ||
return pkp.publicKey; | ||
} | ||
|
||
async function mintPkp(ethersSigner) { | ||
console.log("Minting new PKP..."); | ||
const litContracts = new LitContracts({ | ||
signer: ethersSigner, | ||
network: LitNetwork.Cayenne, | ||
}); | ||
|
||
await litContracts.connect(); | ||
|
||
return (await litContracts.pkpNftContractUtils.write.mint()).pkp; | ||
} | ||
|
||
async function getSessionSigs(litNodeClient, ethersSigner) { | ||
console.log("Getting Session Signatures..."); | ||
return litNodeClient.getSessionSigs({ | ||
chain: "ethereum", | ||
expiration: new Date(Date.now() + 1000 * 60 * 60 * 24).toISOString(), // 24 hours | ||
resourceAbilityRequests: [ | ||
{ | ||
resource: new LitPKPResource("*"), | ||
ability: LitAbility.PKPSigning, | ||
}, | ||
{ | ||
resource: new LitActionResource("*"), | ||
ability: LitAbility.LitActionExecution, | ||
}, | ||
], | ||
authNeededCallback: getAuthNeededCallback(litNodeClient, ethersSigner), | ||
}); | ||
} | ||
|
||
function getAuthNeededCallback(litNodeClient, ethersSigner) { | ||
return async ({ resourceAbilityRequests, expiration, uri }) => { | ||
const toSign = await createSiweMessageWithRecaps({ | ||
uri, | ||
expiration, | ||
resources: resourceAbilityRequests, | ||
walletAddress: await ethersSigner.getAddress(), | ||
nonce: await litNodeClient.getLatestBlockhash(), | ||
litNodeClient, | ||
}); | ||
|
||
return await generateAuthSig({ | ||
signer: ethersSigner, | ||
toSign, | ||
}); | ||
}; | ||
} | ||
|
||
function verifySignature(signature) { | ||
console.log("Verifying signature..."); | ||
const dataSigned = `0x${signature.dataSigned}`; | ||
const encodedSig = ethersUtils.joinSignature({ | ||
v: signature.recid, | ||
r: `0x${signature.r}`, | ||
s: `0x${signature.s}`, | ||
}); | ||
|
||
const recoveredPubkey = ethersUtils.recoverPublicKey(dataSigned, encodedSig); | ||
console.log("Recovered uncompressed public key: ", recoveredPubkey); | ||
|
||
const recoveredAddress = ethersUtils.recoverAddress(dataSigned, encodedSig); | ||
console.log("Recovered address from signature: ", recoveredAddress); | ||
} |
This file contains hidden or 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,9 @@ | ||
export const litActionCode = ` | ||
(async () => { | ||
const sigShare = await LitActions.signEcdsa({ | ||
toSign: dataToSign, | ||
publicKey, | ||
sigName, | ||
}); | ||
})(); | ||
`; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Init EIP-191 Lit Action signing examples for Auth Unification #5
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