From e7010d08c5d25424dedf6ad1d39b122786cc77bc Mon Sep 17 00:00:00 2001 From: patrick Date: Fri, 10 Sep 2021 13:05:06 -0400 Subject: [PATCH] update options parameters --- README.md | 4 +++- .../api/src/graphql/ethereumAuth.sdl.js | 20 +++++++++------- .../src/services/ethereumAuth/ethereumAuth.js | 24 +++++++++++++++---- examples/eth-auth-demo/web/package.json | 2 +- .../web/src/pages/LoginPage/LoginPage.js | 2 +- package.json | 2 +- src/client.js | 9 ++++--- 7 files changed, 42 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 58f6dcf..5b82c2c 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,8 @@ yarn rw prisma migrate dev We're almost there! Create a server secret for issuing jwt tokens: ```bash +yarn rw g secret +# or openssl rand -base64 48 ``` @@ -207,7 +209,7 @@ Specify the wallet type by passing a String to `logIn()` const { logIn, logOut, getCurrentUser } = useAuth() const onClickWalletConnect = async () => { - await logIn("walletConnect") + await logIn({type: "walletConnect"}) ``` > Note: Don't forget to update Redwood.toml if you add new environment variables diff --git a/examples/eth-auth-demo/api/src/graphql/ethereumAuth.sdl.js b/examples/eth-auth-demo/api/src/graphql/ethereumAuth.sdl.js index 0651a7d..d05481a 100644 --- a/examples/eth-auth-demo/api/src/graphql/ethereumAuth.sdl.js +++ b/examples/eth-auth-demo/api/src/graphql/ethereumAuth.sdl.js @@ -1,23 +1,25 @@ export const schema = gql` - type Mutation { - authChallenge(input: AuthChallengeInput!): AuthChallengeResult - authVerify(input: AuthVerifyInput!): AuthVerifyResult + type AuthChallengeResult { + message: String! } - input AuthChallengeInput { - address: String! + type AuthVerifyResult { + token: String! } - type AuthChallengeResult { - message: String! + input AuthChallengeInput { + address: String! + options: JSON } input AuthVerifyInput { signature: String! address: String! + options: JSON } - type AuthVerifyResult { - token: String! + type Mutation { + authChallenge(input: AuthChallengeInput!): AuthChallengeResult + authVerify(input: AuthVerifyInput!): AuthVerifyResult } ` diff --git a/examples/eth-auth-demo/api/src/services/ethereumAuth/ethereumAuth.js b/examples/eth-auth-demo/api/src/services/ethereumAuth/ethereumAuth.js index 416ca49..2f6731f 100644 --- a/examples/eth-auth-demo/api/src/services/ethereumAuth/ethereumAuth.js +++ b/examples/eth-auth-demo/api/src/services/ethereumAuth/ethereumAuth.js @@ -9,13 +9,27 @@ import { db } from 'src/lib/db' const NONCE_MESSAGE = 'Please prove you control this wallet by signing this random text: ' -const getNonceMessage = (nonce) => NONCE_MESSAGE + nonce +const getNonceMessage = (nonce, options) => { + let optionsText = '' + if (options) + optionsText = + '&' + + Object.keys(options) + .map( + (key) => + encodeURIComponent(key) + '=' + encodeURIComponent(options[key]) + ) + .join('&') + return NONCE_MESSAGE + nonce + optionsText +} export const beforeResolver = (rules) => { rules.skip({ only: ['authChallenge', 'authVerify'] }) } -export const authChallenge = async ({ input: { address: addressRaw } }) => { +export const authChallenge = async ({ + input: { address: addressRaw, options }, +}) => { const nonce = Math.floor(Math.random() * 1000000).toString() const address = addressRaw.toLowerCase() await db.user.upsert({ @@ -38,11 +52,11 @@ export const authChallenge = async ({ input: { address: addressRaw } }) => { }, }) - return { message: getNonceMessage(nonce) } + return { message: getNonceMessage(nonce, options) } } export const authVerify = async ({ - input: { signature, address: addressRaw }, + input: { signature, address: addressRaw, options }, }) => { try { const address = addressRaw.toLowerCase() @@ -62,7 +76,7 @@ export const authVerify = async ({ 'The challenge must have been generated within the last 5 minutes' ) const signerAddress = recoverPersonalSignature({ - data: bufferToHex(Buffer.from(getNonceMessage(nonce), 'utf8')), + data: bufferToHex(Buffer.from(getNonceMessage(nonce, options), 'utf8')), sig: signature, }) if (address !== signerAddress.toLowerCase()) diff --git a/examples/eth-auth-demo/web/package.json b/examples/eth-auth-demo/web/package.json index bdd9b06..137013f 100644 --- a/examples/eth-auth-demo/web/package.json +++ b/examples/eth-auth-demo/web/package.json @@ -14,7 +14,7 @@ }, "dependencies": { "@apollo/client": "^3.4.10", - "@oneclickdapp/ethereum-auth": "^0.3.0", + "@oneclickdapp/ethereum-auth": "^0.3.2", "@redwoodjs/auth": "^0.36.2", "@redwoodjs/forms": "0.36.2", "@redwoodjs/router": "0.36.2", diff --git a/examples/eth-auth-demo/web/src/pages/LoginPage/LoginPage.js b/examples/eth-auth-demo/web/src/pages/LoginPage/LoginPage.js index 9043946..92cffd4 100644 --- a/examples/eth-auth-demo/web/src/pages/LoginPage/LoginPage.js +++ b/examples/eth-auth-demo/web/src/pages/LoginPage/LoginPage.js @@ -10,7 +10,7 @@ const LoginPage = () => { const onLogin = async (walletType) => { try { - await logIn(walletType) + await logIn({ type: walletType }) navigate(redirectTo || routes.home()) } catch (e) { console.log(e) diff --git a/package.json b/package.json index 559c698..b2392b5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@oneclickdapp/ethereum-auth", - "version": "0.3.1", + "version": "0.3.2", "description": "Ethereum wallet login provider for RedwoodJS projects", "author": "Patrick Gallagher ", "module": "src/index.js", diff --git a/src/client.js b/src/client.js index a9b7d2b..b638c38 100644 --- a/src/client.js +++ b/src/client.js @@ -45,8 +45,11 @@ class EthereumAuthClient { this.onDisconnect = onDisconnect; } - async login(type = WALLET_TYPES.browser) { + async login(options) { try { + let type = WALLET_TYPES.browser; + let rest; + if (options) ({ type, ...rest } = options); const isWalletConnect = type === WALLET_TYPES.walletConnect; let unlock = unlockBrowser; if (isWalletConnect) { @@ -83,7 +86,7 @@ class EthereumAuthClient { authChallenge: { message } } } = await this.makeRequest(AUTH_CHALLENGE_MUTATION, { - input: { address: walletAddress } + input: { address: walletAddress, options: rest } })); } catch (e) { console.log(e); @@ -108,7 +111,7 @@ class EthereumAuthClient { authVerify: { token } } } = await this.makeRequest(AUTH_VERIFY_MUTATION, { - input: { address: walletAddress, signature } + input: { address: walletAddress, signature, options: rest } })); } catch (e) { console.log(e);