Skip to content

Commit

Permalink
Clean up siww auth function
Browse files Browse the repository at this point in the history
Move the Acre API requests to separate functions and set the Acre API
endpoint via env variables.
  • Loading branch information
r-czajkowski committed Sep 11, 2024
1 parent 8b1b61c commit bb0c208
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 23 deletions.
1 change: 1 addition & 0 deletions dapp/.env
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ VITE_REFERRAL=0

# TODO: Set this env variable in CI.
VITE_TBTC_API_ENDPOINT=""
VITE_ACRE_API_ENDPOINT="http://localhost:8788/api/v1/"

# API KEYS
VITE_GELATO_RELAY_API_KEY="htaJCy_XHj8WsE3w53WBMurfySDtjLP_TrNPPa6IPIc_" # this key should not be used on production
Expand Down
51 changes: 31 additions & 20 deletions dapp/src/components/ConnectWalletModal/ConnectWalletButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import {
} from "#/hooks"
import { setIsSignedMessage } from "#/store/wallet"
import { OrangeKitConnector, OrangeKitError, OnSuccessCallback } from "#/types"
import { logPromiseFailure, orangeKit } from "#/utils"
import {
createSession,
deleteSession,
getSession,
logPromiseFailure,
orangeKit,
} from "#/utils"
import {
Button,
Card,
Expand All @@ -21,9 +27,9 @@ import {
VStack,
} from "@chakra-ui/react"
import { useSignMessage } from "wagmi"
import { isAddressEqual } from "viem"
import { IconArrowNarrowRight } from "@tabler/icons-react"
import { AnimatePresence, Variants, motion } from "framer-motion"
import axios from "axios"
import ArrivingSoonTooltip from "../ArrivingSoonTooltip"
import { TextLg, TextMd } from "../shared/Typography"
import ConnectWalletStatusLabel from "./ConnectWalletStatusLabel"
Expand Down Expand Up @@ -86,39 +92,44 @@ export default function ConnectWalletButton({
const handleSignMessage = useCallback(
async (connectedConnector: OrangeKitConnector, btcAddress: string) => {
try {
const response = await axios.get<
{ nonce: string } | { address: string }
>("http://localhost:8788/api/v1/session", { withCredentials: true })
const session = await getSession()
const [ethAddress] = await connectedConnector.getAccounts()

const hasSessionAddress = "address" in session

const isSessionAddressEqual = hasSessionAddress
? isAddressEqual(ethAddress, session.address as `0x${string}`)
: false

if ("address" in response.data) {
console.log("Nothing to sign. Session already exists")
if (hasSessionAddress && isSessionAddressEqual) {
onSuccessSignMessage()
return
}

const { nonce } = response.data
console.log("nonce", nonce)
if (!hasSessionAddress && !isAddressEqual) {
await deleteSession()
}

const hasNonce = "nonce" in session
if (!hasNonce) {
throw new Error("Session nonce not available")
}

const message = orangeKit.createSignInWithWalletMessage(
btcAddress,
nonce,
session.nonce,
)
console.log("message", message)

const signedMessage = await signMessageAsync({
message,
connector: orangeKit.typeConversionToConnector(connectedConnector),
})
const createSessionResponse = await axios.post<{ success: boolean }>(
"http://localhost:8788/api/v1/session",
{ message, signature: signedMessage },
{ withCredentials: true },
)

if (createSessionResponse.data.success) {
onSuccessSignMessage()
}
await createSession(message, signedMessage)

onSuccessSignMessage()
} catch (error) {
console.error("error", error)
console.error("Failed to sign siww message", error)
setConnectionError(CONNECTION_ERRORS.INVALID_SIWW_SIGNATURE)
}
},
Expand Down
3 changes: 3 additions & 0 deletions dapp/src/constants/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const NETWORK_TYPE = USE_TESTNET ? "testnet" : "mainnet"

const LATEST_COMMIT_HASH = import.meta.env.VITE_LATEST_COMMIT_HASH

const ACRE_API_ENDPOINT = import.meta.env.VITE_ACRE_API_ENDPOINT

export default {
PROD,
USE_TESTNET,
Expand All @@ -35,4 +37,5 @@ export default {
MEZO_PORTAL_API_KEY,
NETWORK_TYPE,
LATEST_COMMIT_HASH,
ACRE_API_ENDPOINT,
}
3 changes: 3 additions & 0 deletions dapp/src/constants/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ export const REFETCH_INTERVAL_IN_MILLISECONDS =
ONE_SEC_IN_MILLISECONDS * ONE_MINUTE_IN_SECONDS * 5

export const DATE_FORMAT_LOCALE_TAG = "us-US"

// 3 hours
export const ACRE_SESSION_EXPIRATION_TIME = 10800000
31 changes: 31 additions & 0 deletions dapp/src/utils/acre-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { env } from "#/constants"
import axiosStatic from "axios"

const axios = axiosStatic.create({ baseURL: env.ACRE_API_ENDPOINT })

export async function getSession() {
const response = await axios.get<{ nonce: string } | { address: string }>(
"session",
{
withCredentials: true,
},
)

return response.data
}

export async function createSession(message: string, signature: string) {
const response = await axios.post<{ success: boolean }>(
"session",
{ message, signature },
{ withCredentials: true },
)

if (!response.data) {
throw new Error("Failed to create Acre session")
}
}

export async function deleteSession() {
await axios.delete("session")
}
1 change: 1 addition & 0 deletions dapp/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ export { default as userAgent } from "./userAgent"
export { default as referralProgram } from "./referralProgram"
export { default as mezoPortalAPI } from "./mezoPortalApi"
export { default as router } from "./router"
export * from "./acre-api"
8 changes: 5 additions & 3 deletions dapp/src/utils/orangeKit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { CONNECTION_ERRORS, wallets } from "#/constants"
import {
ACRE_SESSION_EXPIRATION_TIME,
CONNECTION_ERRORS,
wallets,
} from "#/constants"
import {
ConnectionErrorData,
OrangeKitError,
Expand All @@ -24,8 +28,6 @@ const getWalletInfo = (connector: OrangeKitConnector) => {
}
}

const ACRE_SESSION_EXPIRATION_TIME = 10800000

const isWalletInstalled = (connector: OrangeKitConnector) => {
const provider = connector.getBitcoinProvider()
return provider.isInstalled()
Expand Down
1 change: 1 addition & 0 deletions dapp/src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface ImportMetaEnv {
readonly VITE_SUBGRAPH_API_KEY: string
readonly VITE_MEZO_PORTAL_API_KEY: string
readonly VITE_LATEST_COMMIT_HASH: string
readonly VITE_ACRE_API_ENDPOINT: string
}

interface ImportMeta {
Expand Down

0 comments on commit bb0c208

Please sign in to comment.