From 5947a915b0db7d9fbc21a22d02e48813bf16ec90 Mon Sep 17 00:00:00 2001 From: Patryk Kalinowski Date: Thu, 15 Feb 2024 19:31:23 +0100 Subject: [PATCH] Pass sessionAddress/nonce when authenticating (#10) --- package.json | 2 +- pnpm-lock.yaml | 211 ++++++---------------- src/Login.tsx | 7 +- src/components/views/ListSessionsView.tsx | 2 +- src/main.tsx | 10 +- src/utils/useEmailAuth.ts | 3 +- src/utils/useSessionHash.ts | 26 +++ 7 files changed, 99 insertions(+), 162 deletions(-) create mode 100644 src/utils/useSessionHash.ts diff --git a/package.json b/package.json index d58d643..a975c99 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "@0xsequence/design-system": "^1.0.13", "@0xsequence/indexer": "^1.4.0", "@0xsequence/network": "^1.1.3", - "@0xsequence/waas": "0.0.0-20240119113726", + "@0xsequence/waas": "0.0.0-20240215172023", "@react-oauth/google": "^0.11.1", "@vanilla-extract/css": "^1.12.0", "axios": "^1.6.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d4974f4..ccf6844 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -15,8 +15,8 @@ dependencies: specifier: ^1.1.3 version: 1.4.0(ethers@5.7.2) '@0xsequence/waas': - specifier: 0.0.0-20240119113726 - version: 0.0.0-20240119113726(ethers@5.7.2) + specifier: 0.0.0-20240215172023 + version: 0.0.0-20240215172023(ethers@5.7.2) '@react-oauth/google': specifier: ^0.11.1 version: 0.11.1(react-dom@18.2.0)(react@18.2.0) @@ -88,10 +88,23 @@ devDependencies: packages: + /@0xsequence/abi@0.0.0-20240215172023: + resolution: {integrity: sha512-oDuh7O48BIe/rJPLoCyASj412vu2XEI5zPzfO9OyFEbBrjUJD7nHeGyNfQW14g1BkXPim0167ss8Z68GhWj3xg==} + dev: false + /@0xsequence/abi@1.4.0: resolution: {integrity: sha512-rb8CdJvA1rCQk4YeV/1ppYOW7gFEbrBskfbL+FCld1yjvE+cdO/ldLCIYVvQJLH9beRiTYNmc6JbsyoaWP8Vlw==} dev: false + /@0xsequence/core@0.0.0-20240215172023(ethers@5.7.2): + resolution: {integrity: sha512-D5iDRFV0HmxVidj2E3vneUS/rmzrViksvgVzxy3ZZSniMewqsmi1dz5mh4rUConL4I09odKUBrkK+rGTxZdq3A==} + peerDependencies: + ethers: '>=5.5' + dependencies: + '@0xsequence/abi': 0.0.0-20240215172023 + ethers: 5.7.2 + dev: false + /@0xsequence/core@1.4.0(ethers@5.7.2): resolution: {integrity: sha512-awAf3qApQvaanTE0tshzpLzI/2UjBxE1om25o9H3TXR57EeMG+JQtM/PP6T/Fw6bMZ9+ixiuTxjr1BqlWyaKaQ==} peerDependencies: @@ -129,10 +142,26 @@ packages: - '@types/react-dom' dev: false + /@0xsequence/indexer@0.0.0-20240215172023: + resolution: {integrity: sha512-OyMmmAUHy7Fxz7nMLxkh+8Pay9mbPCxzpcRAjzBncTlfa2J7mUuEwOeOKXTXhlO7ElWcULnf2Tk9WtcnEs+nVw==} + dev: false + /@0xsequence/indexer@1.4.0: resolution: {integrity: sha512-CK/vZAlO0lOev1llSWmFztBQYzTs6eSt4LRYiFNS6NWsQouRfaPzBtEqqlMoUfWmfGNU6RfDXYNbjBpLXW9Q2g==} dev: false + /@0xsequence/network@0.0.0-20240215172023(ethers@5.7.2): + resolution: {integrity: sha512-oUMyPnO2mqTZZzo4/xR299o2vSCCONIsvDlgV5+96u3DoomtYsIlQIVBWSs2Z+qBXT2qzNHsA49bxS5szs5HAA==} + peerDependencies: + ethers: '>=5.5 < 6' + dependencies: + '@0xsequence/core': 0.0.0-20240215172023(ethers@5.7.2) + '@0xsequence/indexer': 0.0.0-20240215172023 + '@0xsequence/relayer': 0.0.0-20240215172023(ethers@5.7.2) + '@0xsequence/utils': 0.0.0-20240215172023(ethers@5.7.2) + ethers: 5.7.2 + dev: false + /@0xsequence/network@1.4.0(ethers@5.7.2): resolution: {integrity: sha512-Ks1MOCOmgrDuiDUgcA3HBm0K/LZLJ603oaMJp7aEwL8x46bTY9UYU0+luf5FVdt5pk1qTwt/SXzpQHjL4K0S4g==} peerDependencies: @@ -145,6 +174,17 @@ packages: ethers: 5.7.2 dev: false + /@0xsequence/relayer@0.0.0-20240215172023(ethers@5.7.2): + resolution: {integrity: sha512-4J8xkri4IotJD35F4Uc+hZbR2DfhKSlSrdUrFOjnv0Q9afx7zuKs5MaKVhkuJzvENVcIfzViC4FlE6shvPVOVQ==} + peerDependencies: + ethers: '>=5.5 < 6' + dependencies: + '@0xsequence/abi': 0.0.0-20240215172023 + '@0xsequence/core': 0.0.0-20240215172023(ethers@5.7.2) + '@0xsequence/utils': 0.0.0-20240215172023(ethers@5.7.2) + ethers: 5.7.2 + dev: false + /@0xsequence/relayer@1.4.0(ethers@5.7.2): resolution: {integrity: sha512-AnxIl4w4O+Qn3VIgHrz73xW7nsyGjjIloxp6X1lmbe37r6Rqjm3RvVz5Gp92lA1UF1ja17NloyxP1Y8h6qaXWg==} peerDependencies: @@ -156,6 +196,15 @@ packages: ethers: 5.7.2 dev: false + /@0xsequence/utils@0.0.0-20240215172023(ethers@5.7.2): + resolution: {integrity: sha512-JmoH5D8fc/IApVgGVjwl91vaEV2AoAuqCHDQbvpx+c8KV1dTsjuvR/gHrKOB6oFvUZJ+xJlBZFFbymCuXA9cuQ==} + peerDependencies: + ethers: '>=5.5 < 6' + dependencies: + ethers: 5.7.2 + js-base64: 3.7.5 + dev: false + /@0xsequence/utils@1.4.0(ethers@5.7.2): resolution: {integrity: sha512-z071zMgubwySPyr65veg6mS8aNJK3hJDj9Fo0KKw5w7JIOafqlgGGWtJBfncGV/bsKBQlf7Ude1morOtLKLEEw==} peerDependencies: @@ -165,15 +214,15 @@ packages: js-base64: 3.7.5 dev: false - /@0xsequence/waas@0.0.0-20240119113726(ethers@5.7.2): - resolution: {integrity: sha512-2KsJFjBDNRxhqZgDr+i8uyKIP97Ns9XWs+lrAGvyvjBh6z1Ne9LG5AAZrkwnz0Z8oWbd4n8f6CnAB600m/Z3eg==} + /@0xsequence/waas@0.0.0-20240215172023(ethers@5.7.2): + resolution: {integrity: sha512-/viAmdOLGCa2qD/YjepoCXF6LBoWv3n0WqDf2t6sJTqZkBXulQSJXsYqE03TZiOBBTjpSYkx3VW/bJchODKXDg==} peerDependencies: ethers: '>=5.5' dependencies: + '@0xsequence/network': 0.0.0-20240215172023(ethers@5.7.2) '@aws-sdk/client-cognito-identity-provider': 3.462.0 - '@aws-sdk/client-kms': 3.462.0 - '@aws-sdk/credential-providers': 3.462.0 ethers: 5.7.2 + idb: 7.1.1 json-canonicalize: 1.0.6 jwt-decode: 4.0.0 transitivePeerDependencies: @@ -289,100 +338,6 @@ packages: - aws-crt dev: false - /@aws-sdk/client-cognito-identity@3.462.0: - resolution: {integrity: sha512-NGsFxb2ysXWUPKCRoa4KIJTcL8kddY73EV0hbw9li7+pTFzzkiYiNg6RxfHp3U+sBlhN8poItQrkAXZTDIJ1yQ==} - engines: {node: '>=14.0.0'} - dependencies: - '@aws-crypto/sha256-browser': 3.0.0 - '@aws-crypto/sha256-js': 3.0.0 - '@aws-sdk/client-sts': 3.462.0 - '@aws-sdk/core': 3.451.0 - '@aws-sdk/credential-provider-node': 3.460.0 - '@aws-sdk/middleware-host-header': 3.460.0 - '@aws-sdk/middleware-logger': 3.460.0 - '@aws-sdk/middleware-recursion-detection': 3.460.0 - '@aws-sdk/middleware-signing': 3.461.0 - '@aws-sdk/middleware-user-agent': 3.460.0 - '@aws-sdk/region-config-resolver': 3.451.0 - '@aws-sdk/types': 3.460.0 - '@aws-sdk/util-endpoints': 3.460.0 - '@aws-sdk/util-user-agent-browser': 3.460.0 - '@aws-sdk/util-user-agent-node': 3.460.0 - '@smithy/config-resolver': 2.0.19 - '@smithy/fetch-http-handler': 2.2.7 - '@smithy/hash-node': 2.0.16 - '@smithy/invalid-dependency': 2.0.14 - '@smithy/middleware-content-length': 2.0.16 - '@smithy/middleware-endpoint': 2.2.1 - '@smithy/middleware-retry': 2.0.21 - '@smithy/middleware-serde': 2.0.14 - '@smithy/middleware-stack': 2.0.8 - '@smithy/node-config-provider': 2.1.6 - '@smithy/node-http-handler': 2.1.10 - '@smithy/protocol-http': 3.0.10 - '@smithy/smithy-client': 2.1.16 - '@smithy/types': 2.6.0 - '@smithy/url-parser': 2.0.14 - '@smithy/util-base64': 2.0.1 - '@smithy/util-body-length-browser': 2.0.0 - '@smithy/util-body-length-node': 2.1.0 - '@smithy/util-defaults-mode-browser': 2.0.20 - '@smithy/util-defaults-mode-node': 2.0.26 - '@smithy/util-endpoints': 1.0.5 - '@smithy/util-retry': 2.0.7 - '@smithy/util-utf8': 2.0.2 - tslib: 2.6.2 - transitivePeerDependencies: - - aws-crt - dev: false - - /@aws-sdk/client-kms@3.462.0: - resolution: {integrity: sha512-657cP9zauc1GLHJ33cqyYNda8otZwpnab/ZlxxiGUYFz6E8cuR4AMZuLLaD0hHo7QcMHodZZ4oepw3Tzdwwpcw==} - engines: {node: '>=14.0.0'} - dependencies: - '@aws-crypto/sha256-browser': 3.0.0 - '@aws-crypto/sha256-js': 3.0.0 - '@aws-sdk/client-sts': 3.462.0 - '@aws-sdk/core': 3.451.0 - '@aws-sdk/credential-provider-node': 3.460.0 - '@aws-sdk/middleware-host-header': 3.460.0 - '@aws-sdk/middleware-logger': 3.460.0 - '@aws-sdk/middleware-recursion-detection': 3.460.0 - '@aws-sdk/middleware-signing': 3.461.0 - '@aws-sdk/middleware-user-agent': 3.460.0 - '@aws-sdk/region-config-resolver': 3.451.0 - '@aws-sdk/types': 3.460.0 - '@aws-sdk/util-endpoints': 3.460.0 - '@aws-sdk/util-user-agent-browser': 3.460.0 - '@aws-sdk/util-user-agent-node': 3.460.0 - '@smithy/config-resolver': 2.0.19 - '@smithy/fetch-http-handler': 2.2.7 - '@smithy/hash-node': 2.0.16 - '@smithy/invalid-dependency': 2.0.14 - '@smithy/middleware-content-length': 2.0.16 - '@smithy/middleware-endpoint': 2.2.1 - '@smithy/middleware-retry': 2.0.21 - '@smithy/middleware-serde': 2.0.14 - '@smithy/middleware-stack': 2.0.8 - '@smithy/node-config-provider': 2.1.6 - '@smithy/node-http-handler': 2.1.10 - '@smithy/protocol-http': 3.0.10 - '@smithy/smithy-client': 2.1.16 - '@smithy/types': 2.6.0 - '@smithy/url-parser': 2.0.14 - '@smithy/util-base64': 2.0.1 - '@smithy/util-body-length-browser': 2.0.0 - '@smithy/util-body-length-node': 2.1.0 - '@smithy/util-defaults-mode-browser': 2.0.20 - '@smithy/util-defaults-mode-node': 2.0.26 - '@smithy/util-endpoints': 1.0.5 - '@smithy/util-retry': 2.0.7 - '@smithy/util-utf8': 2.0.2 - tslib: 2.6.2 - transitivePeerDependencies: - - aws-crt - dev: false - /@aws-sdk/client-sso@3.460.0: resolution: {integrity: sha512-p5D9C8LKJs5yoBn5cCs2Wqzrp5YP5BYcP774bhGMFEu/LCIUyWzudwN3+/AObSiq8R8SSvBY2zQD4h+k3NjgTQ==} engines: {node: '>=14.0.0'} @@ -483,19 +438,6 @@ packages: tslib: 2.6.2 dev: false - /@aws-sdk/credential-provider-cognito-identity@3.462.0: - resolution: {integrity: sha512-oTN5PXFidfadOC+Ar1gZrRkvOTDzk4FZCf47Eqf6Spfm/Z58iQ8GuZ99uh98oQJZlcbYx8hCeuffRLQtPZS20Q==} - engines: {node: '>=14.0.0'} - dependencies: - '@aws-sdk/client-cognito-identity': 3.462.0 - '@aws-sdk/types': 3.460.0 - '@smithy/property-provider': 2.0.15 - '@smithy/types': 2.6.0 - tslib: 2.6.2 - transitivePeerDependencies: - - aws-crt - dev: false - /@aws-sdk/credential-provider-env@3.460.0: resolution: {integrity: sha512-WWdaRJFuYRc2Ue9NKDy2NIf8pQRNx/QRVmrsk6EkIID8uWlQIOePk3SWTVV0TZIyPrbfSEaSnJRZoShphJ6PAg==} engines: {node: '>=14.0.0'} @@ -506,21 +448,6 @@ packages: tslib: 2.6.2 dev: false - /@aws-sdk/credential-provider-http@3.460.0: - resolution: {integrity: sha512-tLnuLDsGcBRemj8jxt1MkerjwsQlYdwnlfQXvrYOO8qMrbFP2sEjAx165GeCbsjmY/y0w1UFQEV+xRpFg5dxUw==} - engines: {node: '>=14.0.0'} - dependencies: - '@aws-sdk/types': 3.460.0 - '@smithy/fetch-http-handler': 2.2.7 - '@smithy/node-http-handler': 2.1.10 - '@smithy/property-provider': 2.0.15 - '@smithy/protocol-http': 3.0.10 - '@smithy/smithy-client': 2.1.16 - '@smithy/types': 2.6.0 - '@smithy/util-stream': 2.0.21 - tslib: 2.6.2 - dev: false - /@aws-sdk/credential-provider-ini@3.460.0: resolution: {integrity: sha512-1IEUmyaWzt2M3mONO8QyZtPy0f9ccaEjCo48ZQLgptWxUI+Ohga9gPK0mqu1kTJOjv4JJGACYHzLwEnnpltGlA==} engines: {node: '>=14.0.0'} @@ -594,30 +521,6 @@ packages: tslib: 2.6.2 dev: false - /@aws-sdk/credential-providers@3.462.0: - resolution: {integrity: sha512-M5XBK3NPbuDmZPUPlpiKeS0NxLNq/ihpEP0ZjFKwOi2fToXt3x8LqZ0e6o8NDl4UqjHtgEewu8Wwrl4paTmc1A==} - engines: {node: '>=14.0.0'} - dependencies: - '@aws-sdk/client-cognito-identity': 3.462.0 - '@aws-sdk/client-sso': 3.460.0 - '@aws-sdk/client-sts': 3.462.0 - '@aws-sdk/credential-provider-cognito-identity': 3.462.0 - '@aws-sdk/credential-provider-env': 3.460.0 - '@aws-sdk/credential-provider-http': 3.460.0 - '@aws-sdk/credential-provider-ini': 3.460.0 - '@aws-sdk/credential-provider-node': 3.460.0 - '@aws-sdk/credential-provider-process': 3.460.0 - '@aws-sdk/credential-provider-sso': 3.460.0 - '@aws-sdk/credential-provider-web-identity': 3.460.0 - '@aws-sdk/types': 3.460.0 - '@smithy/credential-provider-imds': 2.1.2 - '@smithy/property-provider': 2.0.15 - '@smithy/types': 2.6.0 - tslib: 2.6.2 - transitivePeerDependencies: - - aws-crt - dev: false - /@aws-sdk/middleware-host-header@3.460.0: resolution: {integrity: sha512-qBeDyuJkEuHe87Xk6unvFO9Zg5j6zM8bQOOZITocTLfu9JN0u5V4GQ/yopvpv+nQHmC/MGr0G7p+kIXMrg/Q2A==} engines: {node: '>=14.0.0'} @@ -4292,6 +4195,10 @@ packages: minimalistic-crypto-utils: 1.0.1 dev: false + /idb@7.1.1: + resolution: {integrity: sha512-gchesWBzyvGHRO9W8tzUWFDycow5gwjvFKfyV9FF32Y7F50yZMp7mP+T2mJIWFx49zicqyC4uefHM17o6xKIVQ==} + dev: false + /ignore@5.2.4: resolution: {integrity: sha512-MAb38BcSbH0eHNBxn7ql2NH/kX33OkB3lZ1BNdh7ENeRChHTYsTvWrMubiIAMNS2llXEEgZ1MUOBtXChP3kaFQ==} engines: {node: '>= 4'} diff --git a/src/Login.tsx b/src/Login.tsx index 54c1593..50da857 100644 --- a/src/Login.tsx +++ b/src/Login.tsx @@ -9,8 +9,10 @@ import { CredentialResponse, GoogleLogin } from '@react-oauth/google' import AppleSignin from 'react-apple-signin-auth' import { randomName } from './utils/indexer' import { useEmailAuth } from "./utils/useEmailAuth.ts"; +import {useSessionHash} from "./utils/useSessionHash.ts"; function Login() { + const { sessionHash } = useSessionHash() const [email, setEmail] = useState('') const inputRef = useRef(null) const isEmailValid = inputRef.current?.validity.valid @@ -145,14 +147,14 @@ function Login() {
- {!emailAuthInProgress && (<> + {!emailAuthInProgress && !!sessionHash && (<> Social Login {import.meta.env.VITE_GOOGLE_CLIENT_ID && ( - + )} {import.meta.env.VITE_APPLE_CLIENT_ID && ( console.error(error)} onSuccess={handleAppleLogin} diff --git a/src/components/views/ListSessionsView.tsx b/src/components/views/ListSessionsView.tsx index 732313d..eab19cc 100644 --- a/src/components/views/ListSessionsView.tsx +++ b/src/components/views/ListSessionsView.tsx @@ -27,7 +27,7 @@ export function ListSessionsView() { useEffect(() => { Promise.all([ sequence.listSessions(), - sequence.getSessionID() + sequence.getSessionId() ]).then(([s, t]) => { setSessions(s) setThisSession(t) diff --git a/src/main.tsx b/src/main.tsx index d790664..cac7c31 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -6,7 +6,7 @@ import {createHashRouter, RouterProvider} from 'react-router-dom' import Login from './Login.tsx' import { GoogleOAuthProvider } from '@react-oauth/google' -import { Sequence } from '@0xsequence/waas' +import { SequenceWaaS } from '@0xsequence/waas' import App from './App.tsx' import { ethers } from 'ethers' import { defaults, ExtendedSequenceConfig } from '@0xsequence/waas' @@ -19,9 +19,9 @@ const SEQUENCE_WAAS_CONFIG_KEY = import.meta.env.VITE_SEQUENCE_WAAS_CONFIG_KEY export const node = new ethers.providers.JsonRpcProvider('https://nodes.sequence.app/polygon') const urlParams = new URLSearchParams(window.location.search) -let projectAccessKey = urlParams.get('projectAccessKey') ?? SEQUENCE_PROJECT_ACCESS_KEY -let waasConfigKey = urlParams.get('waasConfigKey') ?? SEQUENCE_WAAS_CONFIG_KEY -let preset = extendedSequenceConfigFromBase64(urlParams.get('preset') ?? "") ?? defaults.TEST +const projectAccessKey = urlParams.get('projectAccessKey') ?? SEQUENCE_PROJECT_ACCESS_KEY +const waasConfigKey = urlParams.get('waasConfigKey') ?? SEQUENCE_WAAS_CONFIG_KEY +const preset = extendedSequenceConfigFromBase64(urlParams.get('preset') ?? "") ?? defaults.TEST function extendedSequenceConfigFromBase64(config: string): ExtendedSequenceConfig | undefined { if (config === "") { @@ -30,7 +30,7 @@ function extendedSequenceConfigFromBase64(config: string): ExtendedSequenceConfi return JSON.parse(new TextDecoder().decode(base64.decode(config))) as unknown as ExtendedSequenceConfig } -export const sequence = new Sequence({ +export const sequence = new SequenceWaaS({ network: 'polygon', projectAccessKey: projectAccessKey, waasConfigKey: waasConfigKey, diff --git a/src/utils/useEmailAuth.ts b/src/utils/useEmailAuth.ts index aacb962..ee14886 100644 --- a/src/utils/useEmailAuth.ts +++ b/src/utils/useEmailAuth.ts @@ -26,7 +26,8 @@ export function useEmailAuth({ onSuccess }: { onSuccess: (idToken: string) => vo setLoading(true) try { - const { idToken } = await sequence.email.finalizeAuth({ instance, answer, email }) + const sessionHash = await sequence.getSessionHash() + const { idToken } = await sequence.email.finalizeAuth({ instance, answer, email, sessionHash }) onSuccess(idToken) } catch (e: any) { setError(e.message || "Unknown error") diff --git a/src/utils/useSessionHash.ts b/src/utils/useSessionHash.ts new file mode 100644 index 0000000..0bfe87d --- /dev/null +++ b/src/utils/useSessionHash.ts @@ -0,0 +1,26 @@ +import { sequence } from '../main' +import {useEffect, useState} from "react"; + +export function useSessionHash() { + const [sessionHash, setSessionHash] = useState("") + const [error, setError] = useState(undefined) + + useEffect(() => { + const handler = async () => { + try { + setSessionHash(await sequence.getSessionHash()) + } catch (error) { + console.error(error) + setError(error) + } + } + handler() + return sequence.onSessionStateChanged(handler) + }, [setSessionHash, setError]) + + return { + sessionHash, + error, + loading: !!sessionHash, + } +}