Skip to content

Commit

Permalink
update options parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0neerpat committed Sep 10, 2021
1 parent cd09b1c commit e7010d0
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 21 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down Expand Up @@ -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
Expand Down
20 changes: 11 additions & 9 deletions examples/eth-auth-demo/api/src/graphql/ethereumAuth.sdl.js
Original file line number Diff line number Diff line change
@@ -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
}
`
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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()
Expand All @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion examples/eth-auth-demo/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"module": "src/index.js",
Expand Down
9 changes: 6 additions & 3 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit e7010d0

Please sign in to comment.