forked from safe-global/safe-smart-account
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(onramp-kit): Monerium integration (safe-global#372)
* Allow to pass EthAdapter from outside * Use EthAdapter as parameter * Add auth-kit * Extract refreshToken to outside * Rename to packs and upgrade monerium sdk * Add socket listener connection * Add subscriptions to sockets * Upgrade monerium sdk to support node 16 * Add test
- Loading branch information
Showing
36 changed files
with
5,889 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,3 +60,6 @@ typechain | |
openapi/ | ||
|
||
.idea | ||
|
||
.yalc | ||
yalc.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,14 @@ | ||
import { useEffect, useState, useRef } from 'react' | ||
import { isAddress } from '@ethersproject/address' | ||
import { SafeOnRampKit, StripeSession, StripePack } from '../../../src' | ||
import { Grid, TextField, Button } from '@mui/material' | ||
import { Outlet } from 'react-router-dom' | ||
|
||
import AppBar from './AppBar' | ||
|
||
const isSessionValid = (sessionId: string) => sessionId.length === 28 | ||
import { AuthProvider } from './AuthContext' | ||
|
||
function App() { | ||
const [walletAddress, setWalletAddress] = useState<string>('') | ||
const [sessionId, setSessionId] = useState<string>('') | ||
const [onRampClient, setOnRampClient] = useState<SafeOnRampKit<StripePack>>() | ||
const stripeRootRef = useRef<HTMLDivElement>(null) | ||
|
||
const handleCreateSession = async () => { | ||
if (!isSessionValid(sessionId) && !isAddress(walletAddress)) return | ||
|
||
if (stripeRootRef.current) { | ||
stripeRootRef.current.innerHTML = '' | ||
} | ||
|
||
const sessionData = (await onRampClient?.open({ | ||
element: '#stripe-root', | ||
sessionId: sessionId, | ||
theme: 'light', | ||
defaultOptions: { | ||
transaction_details: { | ||
wallet_address: walletAddress, | ||
supported_destination_networks: ['ethereum', 'polygon'], | ||
supported_destination_currencies: ['usdc'], | ||
lock_wallet_address: true | ||
}, | ||
customer_information: { | ||
email: '[email protected]' | ||
} | ||
} | ||
})) as StripeSession | ||
|
||
onRampClient?.subscribe('onramp_ui_loaded', () => { | ||
console.log('UI loaded') | ||
}) | ||
|
||
onRampClient?.subscribe('onramp_session_updated', (e) => { | ||
console.log('Session Updated', e.payload) | ||
}) | ||
|
||
setWalletAddress(sessionData?.transaction_details?.wallet_address || '') | ||
} | ||
|
||
useEffect(() => { | ||
;(async () => { | ||
const onRampClient = await SafeOnRampKit.init( | ||
new StripePack({ | ||
stripePublicKey: import.meta.env.VITE_STRIPE_PUBLIC_KEY, | ||
onRampBackendUrl: import.meta.env.VITE_SAFE_STRIPE_BACKEND_BASE_URL | ||
}) | ||
) | ||
|
||
setOnRampClient(onRampClient) | ||
})() | ||
}, []) | ||
|
||
return ( | ||
<> | ||
<AuthProvider> | ||
<AppBar /> | ||
<Grid container p={2} height="90vh"> | ||
<Grid item sm={12} md={4} p={2} sx={{ borderRight: `1px solid #303030` }}> | ||
<TextField | ||
id="wallet-address" | ||
label="Wallet address" | ||
placeholder="Enter the address you want to initialize the session with" | ||
variant="outlined" | ||
value={walletAddress} | ||
onChange={(event) => setWalletAddress(event.target.value)} | ||
sx={{ width: '100%' }} | ||
/> | ||
<TextField | ||
id="session-id" | ||
label="Session id" | ||
placeholder="Enter the session id if you have one" | ||
variant="outlined" | ||
value={sessionId} | ||
onChange={(event) => setSessionId(event.target.value)} | ||
sx={{ width: '100%', mt: 2 }} | ||
/> | ||
<br /> | ||
<Button variant="contained" onClick={handleCreateSession} sx={{ mt: 3 }}> | ||
Create session | ||
</Button> | ||
</Grid> | ||
<Grid item sm={12} md={8} p={2}> | ||
<div id="stripe-root" ref={stripeRootRef}></div> | ||
</Grid> | ||
</Grid> | ||
</> | ||
<Outlet /> | ||
</AuthProvider> | ||
) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import React, { createContext, useState, useEffect } from 'react' | ||
import { Web3AuthOptions } from '@web3auth/modal' | ||
import { CHAIN_NAMESPACES, SafeEventEmitterProvider, WALLET_ADAPTERS } from '@web3auth/base' | ||
import { OpenloginAdapter } from '@web3auth/openlogin-adapter' | ||
import { SafeAuthKit, Web3AuthModalPack, SafeAuthSignInData } from '@safe-global/auth-kit' | ||
|
||
type AuthContextProviderProps = { | ||
children: React.ReactNode | ||
} | ||
|
||
type AuthContextType = { | ||
isLoggedIn: boolean | ||
provider?: SafeEventEmitterProvider | ||
data?: SafeAuthSignInData | ||
selectedSafe: string | ||
setSelectedSafe?: (safe: string) => void | ||
logIn?: () => void | ||
logOut?: () => void | ||
} | ||
|
||
export const AuthContext = createContext<AuthContextType>({ | ||
isLoggedIn: false, | ||
selectedSafe: '' | ||
}) | ||
|
||
const AuthProvider = ({ children }: AuthContextProviderProps) => { | ||
const [isLoggedIn, setIsLoggedIn] = useState(false) | ||
const [safeAuth, setSafeAuth] = useState<SafeAuthKit<Web3AuthModalPack>>() | ||
const [safeAuthSignInResponse, setSafeAuthSignInResponse] = useState<SafeAuthSignInData>() | ||
const [provider, setProvider] = useState<SafeEventEmitterProvider | undefined>() | ||
const [selectedSafe, setSelectedSafe] = useState('') | ||
|
||
useEffect(() => { | ||
;(async () => { | ||
const options: Web3AuthOptions = { | ||
clientId: import.meta.env.VITE_WEB3AUTH_CLIENT_ID || '', | ||
web3AuthNetwork: 'testnet', | ||
chainConfig: { | ||
chainNamespace: CHAIN_NAMESPACES.EIP155, | ||
chainId: '0x5', | ||
rpcTarget: 'https://rpc.ankr.com/eth_goerli' | ||
}, | ||
uiConfig: { | ||
theme: 'dark', | ||
loginMethodsOrder: ['google', 'facebook'] | ||
} | ||
} | ||
|
||
const modalConfig = { | ||
[WALLET_ADAPTERS.TORUS_EVM]: { | ||
label: 'torus', | ||
showOnModal: false | ||
}, | ||
[WALLET_ADAPTERS.METAMASK]: { | ||
label: 'metamask', | ||
showOnDesktop: true, | ||
showOnMobile: false | ||
} | ||
} | ||
|
||
const openloginAdapter = new OpenloginAdapter({ | ||
loginSettings: { | ||
mfaLevel: 'mandatory' | ||
}, | ||
adapterSettings: { | ||
uxMode: 'popup', | ||
whiteLabel: { | ||
name: 'Safe' | ||
} | ||
} | ||
}) | ||
|
||
const web3AuthPack = new Web3AuthModalPack(options, [openloginAdapter], modalConfig) | ||
|
||
const safeAuthKit = await SafeAuthKit.init(web3AuthPack, { | ||
txServiceUrl: 'https://safe-transaction-goerli.safe.global' | ||
}) | ||
|
||
const provider = safeAuthKit.getProvider() | ||
|
||
if (provider) { | ||
const response = await safeAuthKit.signIn() | ||
setSafeAuthSignInResponse(response) | ||
setSelectedSafe(response?.safes?.[0] || '') | ||
setProvider(provider as SafeEventEmitterProvider) | ||
|
||
setIsLoggedIn(true) | ||
} | ||
|
||
setSafeAuth(safeAuthKit) | ||
})() | ||
}, []) | ||
|
||
const logIn = async () => { | ||
if (!safeAuth) return | ||
|
||
const response = await safeAuth.signIn() | ||
console.log('SIGN IN RESPONSE: ', response) | ||
|
||
setSafeAuthSignInResponse(response) | ||
setSelectedSafe(response?.safes?.[0] || '') | ||
setProvider(safeAuth.getProvider() as SafeEventEmitterProvider) | ||
setIsLoggedIn(true) | ||
} | ||
|
||
const logOut = async () => { | ||
if (!safeAuth) return | ||
|
||
await safeAuth.signOut() | ||
|
||
setProvider(undefined) | ||
setSafeAuthSignInResponse(undefined) | ||
setIsLoggedIn(false) | ||
} | ||
|
||
return ( | ||
<AuthContext.Provider | ||
value={{ | ||
isLoggedIn, | ||
provider, | ||
data: safeAuthSignInResponse, | ||
logIn, | ||
logOut, | ||
selectedSafe, | ||
setSelectedSafe | ||
}} | ||
> | ||
{children} | ||
</AuthContext.Provider> | ||
) | ||
} | ||
|
||
const useAuth = () => { | ||
const context = React.useContext(AuthContext) | ||
|
||
if (context === undefined) { | ||
throw new Error('useAuth must be used within a AuthContextProvider') | ||
} | ||
|
||
return context | ||
} | ||
|
||
export { AuthProvider, useAuth } |
Oops, something went wrong.