diff --git a/src/api/users.ts b/src/api/users.ts index 5b900e9..dc3b9b5 100644 --- a/src/api/users.ts +++ b/src/api/users.ts @@ -81,6 +81,28 @@ export async function getAccessToken(user: UserLoginForm): Promise { } } +export async function getAccessTokenEip4361( + message: string, + signature: string, +): Promise { + const url = `${BACKEND_URL}/oauth/token` + const tokenRequestData = { + grant_type: "eip4361", + message: message, + signature: signature, + } + const response = await http(url, { + method: "POST", + json: tokenRequestData, + }) + const data = await response.json() + if (response.status !== 200) { + throw new Error(data.message) + } else { + return data.access_token + } +} + export async function getCurrentUser(authToken: string): Promise { const url = `${BACKEND_URL}/api/v1/accounts/verify_credentials` const response = await http(url, { authToken }) diff --git a/src/utils/ethereum.ts b/src/utils/ethereum.ts index 2c7679c..7a5f3a6 100644 --- a/src/utils/ethereum.ts +++ b/src/utils/ethereum.ts @@ -58,3 +58,33 @@ export async function getWalletSignature( } return signature } + +function generateRandomString(len: number): string { + const arr = new Uint8Array(len / 2) + window.crypto.getRandomValues(arr) + return Array.from(arr, (val) => val.toString(16).padStart(2, "0")).join("") +} + +export async function createEip4361_SignedMessage( + signer: Signer, + domain: string, + statement: string, +): Promise<{ message: string, signature: string }> { + const address = await signer.getAddress() + const uri = window.location.origin + const nonce = generateRandomString(16) + const issuedAt = new Date().toISOString() + const message = `${domain} wants you to sign in with your Ethereum account: +${address} + +${statement} + +URI: ${uri} +Version: 1 +Chain ID: 1 +Nonce: ${nonce} +Issued At: ${issuedAt}` + + const signature = await signer.signMessage(message) + return { message, signature } +} diff --git a/src/views/LandingPage.vue b/src/views/LandingPage.vue index 55f0680..67ef9e5 100644 --- a/src/views/LandingPage.vue +++ b/src/views/LandingPage.vue @@ -53,12 +53,23 @@