Skip to content

Commit

Permalink
Merge pull request #97 from TalismanSociety/feat/short-sign-in-code
Browse files Browse the repository at this point in the history
feat: short sign in message
  • Loading branch information
chrisling-dev authored Nov 14, 2024
2 parents 7b501b3 + 182e79b commit 8ee7e92
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
5 changes: 4 additions & 1 deletion apps/multisig/src/components/AccountMenu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ export const AccountMenu: React.FC = () => {
}}
>
<PopoverTrigger asChild>
<Button variant="secondary" className="px-[12px] gap-[8px] bg-gray-900 hover:bg-gray-800 max-w-[240px]">
<Button
variant="secondary"
className="px-[12px] py-[8px] h-max gap-[8px] bg-gray-900 hover:bg-gray-800 max-w-[240px]"
>
<AccountDetails
identiconSize={32}
address={user.injected.address}
Expand Down
26 changes: 7 additions & 19 deletions apps/multisig/src/domains/auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { useCallback, useMemo, useState } from 'react'
import { atom, selector, useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil'
import { web3FromSource } from '@polkadot/extension-dapp'
import { SiwsMessage } from '@talismn/siws'
import { InjectedAccount, accountsState } from '../extension'
import persistAtom from '../persist'
import { captureException } from '@sentry/react'
import { useSelectedMultisig } from '@domains/multisig'
import { useToast } from '@components/ui/use-toast'
import { useAzeroID } from '@domains/azeroid/AzeroIDResolver'
import { CONFIG } from '@lib/config'

const SIWS_ENDPOINT = process.env.REACT_APP_SIWS_ENDPOINT ?? ''

Expand Down Expand Up @@ -69,7 +66,6 @@ export const signedInAccountState = atom<string | null>({
})

export const useSignIn = () => {
const { resolve } = useAzeroID()
const [authTokenBook, setAuthTokenBook] = useRecoilState(authTokenBookState)
const setSelectedAccount = useSetRecoilState(selectedAddressState)
const [signingIn, setSigningIn] = useState(false)
Expand Down Expand Up @@ -106,26 +102,18 @@ export const useSignIn = () => {
// should've been captured by `nonceData.error`, but adding this check just to be sure
if (!nonce) throw new Error('Failed to request for nonce.')

// construct siws message
const siws = new SiwsMessage({
const message = `Sign in code: ${nonce}`

const { signature } = await injector.signer.signRaw({
address: ss58Address,
domain: CONFIG.IS_POLKADOT_MULTISIG ? 'polkadotmultisig.com' : 'signet.talisman.xyz',
nonce,
uri: window.location.origin,
statement: CONFIG.IS_POLKADOT_MULTISIG
? `Welcome to Polkadot Multisig! The product is currently in beta. By signing in, you accept the Terms of Use: ${CONFIG.TERMS}`
: `Welcome to ${CONFIG.APP_NAME}! Please sign in to continue.`,
chainName: 'Substrate',
azeroId: resolve(ss58Address)?.a0id,
data: message,
type: 'payload',
})

// sign payload for backend verification
const signed = await siws.sign(injector)

// exchange JWT token from server
const verifyRes = await fetch(`${SIWS_ENDPOINT}/verify`, {
method: 'post',
body: JSON.stringify({ ...signed, address: ss58Address }),
body: JSON.stringify({ signature, message, address: ss58Address }),
headers: { 'Content-Type': 'application/json' },
credentials: 'include',
})
Expand Down Expand Up @@ -162,7 +150,7 @@ export const useSignIn = () => {
setSigningIn(false)
}
},
[authTokenBook, dismiss, resolve, setAuthTokenBook, setSelectedAccount, signingIn, toast]
[authTokenBook, dismiss, setAuthTokenBook, setSelectedAccount, signingIn, toast]
)

return { signIn, signingIn }
Expand Down

0 comments on commit 8ee7e92

Please sign in to comment.