-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds Sign-In With Wallet auth to the Acre dapp. When a user connects the wallet we create a session by sending a request to the Acre API backend. The session is valid for 3 hours. ### SIWW flow: 1. Dapp asks backend for the session. 2. If the session exists and it matches the current connected address, a user is logged in. 3. If a session exists but does not match the current connected address the dapp deletes the session and asks backend for a new nonce (session id). 4. The user must sign the SIWW message with a given nonce. 5. Dapp sends the signature, message, and public key to the backend to verify the signature. 6. If the message is valid, the backend returns the session id in cookies and the user is logged in.
- Loading branch information
Showing
11 changed files
with
140 additions
and
22 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
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,57 @@ | ||
import { OrangeKitConnector } from "#/types" | ||
import { acreApi, orangeKit } from "#/utils" | ||
import { useCallback } from "react" | ||
import { useSignMessage } from "wagmi" | ||
|
||
function useSignMessageAndCreateSession() { | ||
const { signMessageAsync, status: signMessageStatus } = useSignMessage() | ||
|
||
const signMessageAndCreateSession = useCallback( | ||
async (connectedConnector: OrangeKitConnector, btcAddress: string) => { | ||
let session = await acreApi.getSession() | ||
const hasSessionAddress = "address" in session | ||
|
||
const isSessionAddressEqual = hasSessionAddress | ||
? (session as { address: string }).address === btcAddress | ||
: false | ||
|
||
if (hasSessionAddress && isSessionAddressEqual) { | ||
return | ||
} | ||
|
||
if (hasSessionAddress && !isSessionAddressEqual) { | ||
// Delete session. | ||
await acreApi.deleteSession() | ||
// Ask for nonce to create new session. | ||
session = await acreApi.getSession() | ||
} | ||
|
||
if (!("nonce" in session)) { | ||
throw new Error("Session nonce not available") | ||
} | ||
|
||
const message = orangeKit.createSignInWithWalletMessage( | ||
btcAddress, | ||
session.nonce, | ||
) | ||
|
||
const signedMessage = await signMessageAsync({ | ||
message, | ||
connector: orangeKit.typeConversionToConnector(connectedConnector), | ||
}) | ||
|
||
const publicKey = await connectedConnector | ||
.getBitcoinProvider() | ||
.getPublicKey() | ||
|
||
await acreApi.createSession(message, signedMessage, publicKey) | ||
}, | ||
[signMessageAsync], | ||
) | ||
|
||
return { | ||
signMessageAndCreateSession, | ||
signMessageStatus, | ||
} | ||
} | ||
export default useSignMessageAndCreateSession |
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,41 @@ | ||
import { env } from "#/constants" | ||
import axiosStatic from "axios" | ||
|
||
const axios = axiosStatic.create({ | ||
baseURL: env.ACRE_API_ENDPOINT, | ||
withCredentials: true, | ||
}) | ||
|
||
async function getSession() { | ||
const response = await axios.get<{ nonce: string } | { address: string }>( | ||
"session", | ||
) | ||
|
||
return response.data | ||
} | ||
|
||
async function createSession( | ||
message: string, | ||
signature: string, | ||
publicKey: string, | ||
) { | ||
const response = await axios.post<{ success: boolean }>("session", { | ||
message, | ||
signature, | ||
publicKey, | ||
}) | ||
|
||
if (!response.data.success) { | ||
throw new Error("Failed to create Acre session") | ||
} | ||
} | ||
|
||
async function deleteSession() { | ||
await axios.delete("session") | ||
} | ||
|
||
export default { | ||
createSession, | ||
getSession, | ||
deleteSession, | ||
} |
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
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