Skip to content

Commit

Permalink
email waas
Browse files Browse the repository at this point in the history
  • Loading branch information
SamueleA committed May 13, 2024
1 parent 6bd75a2 commit 64f1508
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 5 deletions.
11 changes: 8 additions & 3 deletions examples/react/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const connectionMode: ConnectionMode = searchParams.get('mode') === 'universal'

// append ?debug to url to enable debug mode
const isDebugMode = searchParams.has('debug')
const projectAccessKey = 'AQAAAAAAAEGvyZiWA9FMslYeG_yayXaHnSI'
// const projectAccessKey = 'AQAAAAAAAEGvyZiWA9FMslYeG_yayXaHnSI'

const chains = [arbitrumNova, arbitrumSepolia, mainnet, polygon] as const satisfies Chain[]
const transports = chains.reduce<Record<number, Transport>>((acc, chain) => {
Expand All @@ -33,11 +33,16 @@ chains.forEach(chain => {
transports[chain.id] = http(network.rpcUrl)
})

const waasConfigKey = 'eyJwcm9qZWN0SWQiOjE2ODE1LCJycGNTZXJ2ZXIiOiJodHRwczovL3dhYXMuc2VxdWVuY2UuYXBwIn0='
const googleClientId = '970987756660-35a6tc48hvi8cev9cnknp0iugv9poa23.apps.googleusercontent.com'
// const waasConfigKey = 'eyJwcm9qZWN0SWQiOjE2ODE1LCJycGNTZXJ2ZXIiOiJodHRwczovL3dhYXMuc2VxdWVuY2UuYXBwIn0='
// const googleClientId = '970987756660-35a6tc48hvi8cev9cnknp0iugv9poa23.apps.googleusercontent.com'
const appleClientId = 'com.horizon.sequence.waas'
const appleRedirectURI = 'https://' + window.location.host

// Waas testing params
const projectAccessKey = 'AQAAAAAAAAK2JvvZhWqZ51riasWBftkrVXE'
const waasConfigKey = 'eyJwcm9qZWN0SWQiOjY5NCwicnBjU2VydmVyIjoiaHR0cHM6Ly9kZXYtd2Fhcy5zZXF1ZW5jZS5hcHAiLCJlbWFpbFJlZ2lvbiI6ImNhLWNlbnRyYWwtMSIsImVtYWlsQ2xpZW50SWQiOiI1NGF0bjV1cGk2M3FjNTlhMWVtM3ZiaHJzbiJ9'
const googleClientId = '603294233249-6h5saeg2uiu8akpcbar3r2aqjp6j7oem.apps.googleusercontent.com'

const getWaasConnectors = () => {
const connectors = [
...getDefaultWaasConnectors({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ export class SequenceWaasProvider extends ethers.providers.BaseProvider implemen
super(network)
}

triggerSessionValidation: (() => void ) | undefined
requestConfirmationHandler: WaasRequestConfirmationHandler | undefined
feeConfirmationHandler: WaasFeeOptionConfirmationHandler | undefined

Expand All @@ -255,7 +256,25 @@ export class SequenceWaasProvider extends ethers.providers.BaseProvider implemen
updateNetwork(network: ethers.providers.Network) {
this.currentNetwork = network
}

async checkAndValidateSession() {
let isSessionValid = false
try {
isSessionValid = await this.sequenceWaas.isSessionValid()
} catch(e) {
console.error('session valid error', e)
isSessionValid = false
}

console.log('is session valid.....', isSessionValid)
if (!isSessionValid) {
this.triggerSessionValidation && this.triggerSessionValidation()
console.log('validate sessions s....')
await this.sequenceWaas.validateSession()
console.log('validate sessions e....')
await this.sequenceWaas.waitForSessionValid()
console.log('finished waiting for validation')
}
}
async request({ method, params }: { method: string; params?: any[] }) {
if (method === 'eth_accounts') {
const address = await this.sequenceWaas.getAddress()
Expand Down Expand Up @@ -306,6 +325,8 @@ export class SequenceWaasProvider extends ethers.providers.BaseProvider implemen
}
}

// await this.checkAndValidateSession()

const response = await this.sequenceWaas.sendTransaction({
transactions: [await ethers.utils.resolveProperties(params?.[0])],
network: chainId,
Expand Down Expand Up @@ -349,6 +370,10 @@ export class SequenceWaasProvider extends ethers.providers.BaseProvider implemen
throw new UserRejectedRequestError(new Error('User confirmation ids do not match'))
}
}

console.log('b4 check and validate')
// await this.checkAndValidateSession()
console.log('b4 signing')
const sig = await this.sequenceWaas.signMessage({ message: params?.[0], network: this.currentNetwork.chainId })

return sig.data.signature
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react'
import React, { useState } from 'react'
import {
Box,
Button,
Expand Down
48 changes: 48 additions & 0 deletions packages/kit/src/components/KitProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import { Connector, useAccount, useConfig, useConnections } from 'wagmi'

import '@0xsequence/design-system/styles.css'

import { ConnectWalletContent } from './ConnectWalletContent'
import { WaasCodeInputContent } from './ConnectWalletContent/WaasCodeInputContent'
import { SequenceLogo } from './SequenceLogo'
import { DEFAULT_SESSION_EXPIRATION, LocalStorageKey } from '../../constants'
import {
KitConfigContextProvider,
Expand All @@ -21,6 +24,8 @@ import { useWaasConfirmationHandler } from '../../hooks/useWaasConfirmationHandl
import { ExtendedConnector, ModalPosition, getModalPositionCss } from '../../utils'
import { setStorageItem } from '../../utils/storage'
import { TxnDetails } from '../TxnDetails'
import { useWaasRevalidation } from '../../hooks'


import { ConnectWalletContent } from './ConnectWalletContent'
import { NetworkBadge } from './NetworkBadge'
Expand Down Expand Up @@ -99,6 +104,14 @@ export const KitProvider = (props: KitConnectProviderProps) => {

const [pendingRequestConfirmation, confirmPendingRequest, rejectPendingRequest] = useWaasConfirmationHandler(waasConnector)

const {
openWaasRevalidationModal,
setOpenWaasRevalidationModal,
onVerifyIsLoading,
setOnVerifyIsLoading,
onVerify
} = useWaasRevalidation()

const googleWaasConnector = wagmiConfig.connectors.find(
c => c.id === 'sequence-waas' && (c as ExtendedConnector)._wallet.id === 'google-waas'
) as ExtendedConnector | undefined
Expand Down Expand Up @@ -350,6 +363,41 @@ export const KitProvider = (props: KitConnectProviderProps) => {
</Modal>
)}
</AnimatePresence>
<AnimatePresence>
{openWaasRevalidationModal && (
<Modal
isDismissible={true}
onClose={() => setOpenWaasRevalidationModal(false)}
scroll={false}
backdropColor="backgroundBackdrop"
size="sm"
contentProps={{
style: {
maxWidth: '364px',
...getModalPositionCss(position)
}
}}
>
<Box paddingX="4" paddingTop="4" paddingBottom="2" className={sharedStyles.walletContent}>
<Box
flexDirection="column"
justifyContent="center"
color="text100"
alignItems="center"
fontWeight="medium"
style={{
marginTop: '4px'
}}
>
<Text as="h1" variant="large" marginBottom="5">
Validate Session
</Text>
</Box>
<WaasCodeInputContent onVerify={onVerify} isLoading={onVerifyIsLoading} />
</Box>
</Modal>
)}
</AnimatePresence>
</ThemeProvider>
</div>
{children}
Expand Down
8 changes: 8 additions & 0 deletions packages/kit/src/hooks/useWaasRevalidation.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SequenceWaasProvider } from '@0xsequence/kit-connectors'
import { SequenceWaaS } from '@0xsequence/waas'
import { useState, useEffect } from 'react'
import { useAccount } from 'wagmi'
Expand Down Expand Up @@ -30,6 +31,7 @@ export function useWaasRevalidation(): UseWaasRevalidation {
setOnVerifyIsLoading(true)
await sequenceWaas.finishValidateSession(code)
} catch(e) {
console.log('error...')
console.error(e)
}
setOnVerifyIsLoading(false)
Expand All @@ -55,6 +57,12 @@ export function useWaasRevalidation(): UseWaasRevalidation {
return
}

// @ts-ignore-next-line
// waasConnector.triggerSessionValidation = () => {
// console.log('validation required')
// setOpenWaasRevalidationModal(true)
// }

sequenceWaas.onValidationRequired(() => {
console.log('onValidationRequire')
setOpenWaasRevalidationModal(true)
Expand Down

0 comments on commit 64f1508

Please sign in to comment.