-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
packages/chain-ethereum: add sign in with farcaster (#438)
* add siwfsigner, verify fc message to farcasterSignerAddress * use requestId to encode canvas pubkey, update siwf message encoding, add integration test for siwf verification * examples/chat: prototype siwf login to chat example * add static methods for generating requestId, update fixtures, tests pass * fix parsing of delegate did, return custody address * workaround for creating messages * examples/chat: default back to burner wallet * examples/chat: handle signer switching * factor out newSIWFSession * add eip712signer to chat * sync fixes: add eip712signer and siwfsigner to cli, add siwfsigner to chat init
- Loading branch information
Showing
14 changed files
with
969 additions
and
40 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,106 @@ | ||
import "@farcaster/auth-kit/styles.css" | ||
|
||
import React, { useContext, useEffect, useRef, useState } from "react" | ||
import { hexlify, getBytes } from "ethers" | ||
import { SIWFSigner } from "@canvas-js/chain-ethereum" | ||
import { ed25519 } from "@canvas-js/signatures" | ||
import { SignInButton, useProfile } from "@farcaster/auth-kit" | ||
|
||
import { topic } from "../App.js" | ||
import { AppContext } from "../AppContext.js" | ||
|
||
export interface ConnectSIWFProps {} | ||
|
||
export const ConnectSIWF: React.FC<ConnectSIWFProps> = ({}) => { | ||
const { app, setSessionSigner, setAddress } = useContext(AppContext) | ||
|
||
const profile = useProfile() | ||
const { | ||
isAuthenticated, | ||
profile: { fid, displayName, custody, verifications }, | ||
} = profile | ||
|
||
const [error, setError] = useState<Error | null>(null) | ||
const [requestId, setRequestId] = useState<string | null>(null) | ||
const [privateKey, setPrivateKey] = useState<string | null>(null) | ||
|
||
const initialRef = useRef(false) | ||
useEffect(() => { | ||
if (initialRef.current) { | ||
return | ||
} | ||
|
||
initialRef.current = true | ||
|
||
const { requestId, privateKey } = SIWFSigner.newSIWFRequestId(topic) | ||
setRequestId(requestId) | ||
setPrivateKey(hexlify(privateKey)) | ||
}, []) | ||
|
||
if (error !== null) { | ||
return ( | ||
<div className="p-2 border rounded bg-red-100 text-sm"> | ||
<code>{error.message}</code> | ||
</div> | ||
) | ||
} else if (!privateKey || !requestId || !app) { | ||
return ( | ||
<div className="p-2 border rounded bg-gray-200"> | ||
<button disabled>Loading...</button> | ||
</div> | ||
) | ||
} else { | ||
return ( | ||
<div style={{ marginTop: "12px", right: "12px" }}> | ||
{isAuthenticated && ( | ||
<div> | ||
<p> | ||
Hello, {displayName}! Your FID is {fid}. | ||
</p> | ||
<p>Your custody address is: </p> | ||
<pre>{custody}</pre> | ||
<p>Your connected signers: </p> | ||
{verifications?.map((v, i) => <pre key={i}>{v}</pre>)} | ||
</div> | ||
)} | ||
<SignInButton | ||
requestId={requestId} | ||
onSuccess={async (result) => { | ||
const { signature, message } = result | ||
if (!message || !signature) { | ||
setError(new Error("login succeeded but did not return a valid SIWF message")) | ||
return | ||
} | ||
|
||
const { authorizationData, topic, custodyAddress } = SIWFSigner.parseSIWFMessage(message, signature) | ||
const signer = new SIWFSigner({ custodyAddress, privateKey: privateKey.slice(2) }) | ||
const address = await signer.getDid() | ||
|
||
const timestamp = new Date(authorizationData.siweIssuedAt).valueOf() | ||
const { payload, signer: delegateSigner } = await signer.newSIWFSession( | ||
topic, | ||
authorizationData, | ||
timestamp, | ||
getBytes(privateKey), | ||
) | ||
setAddress(address) | ||
setSessionSigner(signer) | ||
app.updateSigners([ | ||
signer, | ||
...app.signers.getAll().filter((signer) => signer.key !== "chain-ethereum-farcaster"), | ||
]) | ||
app.messageLog.append(payload, { signer: delegateSigner }) | ||
console.log("started SIWF chat session", authorizationData) | ||
}} | ||
onError={(...args) => { | ||
console.log("received SIWF error", args) | ||
}} | ||
onSignOut={(...args) => { | ||
setAddress(null) | ||
setSessionSigner(null) | ||
}} | ||
/> | ||
</div> | ||
) | ||
} | ||
} |
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
Oops, something went wrong.