-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from openfort-xyz/unity-bridge
fix: linter
- Loading branch information
Showing
35 changed files
with
1,076 additions
and
307 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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
46 changes: 33 additions & 13 deletions
46
examples/apps/auth-sample/src/components/NFT/MintNFTButton.tsx
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
51 changes: 51 additions & 0 deletions
51
examples/apps/auth-sample/src/components/OAuth/LinkOAuthButton.tsx
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,51 @@ | ||
import React, {useMemo, useState} from 'react'; | ||
import {useOpenfort} from '../../hooks/useOpenfort'; | ||
import {AuthPlayerResponse, OAuthProvider} from '@openfort/openfort-js'; | ||
import Loading from '../Loading'; | ||
import openfort from '../../utils/openfortConfig'; | ||
import {getURL} from '../../utils/getUrl'; | ||
|
||
const LinkOAuthButton: React.FC<{ | ||
provider: OAuthProvider; | ||
user: AuthPlayerResponse | null; | ||
}> = ({provider, user}) => { | ||
const {state} = useOpenfort(); | ||
const [loading, setLoading] = useState(false); | ||
const handleLinkOAuth = async () => { | ||
try { | ||
setLoading(true); | ||
const accessToken = openfort.getAccessToken() as string; | ||
const {url} = await openfort.initLinkOAuth({ | ||
authToken: accessToken, | ||
provider: provider, | ||
options: { | ||
redirectTo: getURL() + '/login', | ||
}, | ||
}); | ||
setLoading(false); | ||
window.location.href = url; | ||
} catch (err) { | ||
console.error('Failed to sign message:', err); | ||
alert('Failed to sign message. Please try again.'); | ||
} | ||
}; | ||
|
||
const isLinked = useMemo(() => { | ||
if (!user) return false; | ||
return user.linkedAccounts.some((account) => account.provider === provider); | ||
}, [user]); | ||
|
||
return ( | ||
<div> | ||
<button | ||
onClick={handleLinkOAuth} | ||
disabled={isLinked} | ||
className={`mt-2 w-44 px-4 py-2 bg-black text-white font-semibold rounded-lg shadow-md hover:bg-gray-800 disabled:bg-gray-400 disabled:cursor-not-allowed focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-opacity-50`} | ||
> | ||
{loading ? <Loading /> : `${isLinked ? 'Linked' : 'Link'} ${provider}`} | ||
</button> | ||
</div> | ||
); | ||
}; | ||
|
||
export default LinkOAuthButton; |
129 changes: 129 additions & 0 deletions
129
examples/apps/auth-sample/src/components/Sessions/CreateSessionButton.tsx
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,129 @@ | ||
import React, {useCallback, useState} from 'react'; | ||
import {useOpenfort} from '../../hooks/useOpenfort'; | ||
import {EmbeddedState} from '@openfort/openfort-js'; | ||
import Loading from '../Loading'; | ||
import openfort from '../../utils/openfortConfig'; | ||
import {ethers} from 'ethers'; | ||
import MintNFTSessionButton from './MintNFTButtoSession'; | ||
|
||
const sessionMethods = [ | ||
{id: '1hour', title: '1 Hour'}, | ||
{id: '1day', title: '1 Day'}, | ||
{id: '1month', title: '1 Month'}, | ||
]; | ||
|
||
const CreateSessionButton: React.FC<{ | ||
handleSetMessage: (message: string) => void; | ||
}> = ({handleSetMessage}) => { | ||
const {state, signMessage} = useOpenfort(); | ||
const [loading, setLoading] = useState(false); | ||
const [sessionKey, setSessionKey] = useState<string | null>(null); | ||
|
||
const createSession = useCallback(async (): Promise<{ | ||
address: string; | ||
privateKey: string; | ||
} | null> => { | ||
const sessionKey = ethers.Wallet.createRandom(); | ||
const sessionResponse = await fetch(`/api/protected-create-session`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${openfort.getAccessToken()}`, | ||
}, | ||
body: JSON.stringify({ | ||
sessionDuration: document.querySelector( | ||
'input[name="session-method"]:checked' | ||
)?.id, | ||
sessionAddress: sessionKey.address, | ||
}), | ||
}); | ||
|
||
if (!sessionResponse.ok) { | ||
alert('Failed to create session: ' + sessionResponse.status); | ||
return null; | ||
} | ||
const sessionResponseJSON = await sessionResponse.json(); | ||
|
||
if (sessionResponseJSON.data?.nextAction) { | ||
const signature = await signMessage( | ||
sessionResponseJSON.data?.nextAction.payload.userOperationHash, | ||
{ | ||
hashMessage: true, | ||
arrayifyMessage: true, | ||
} | ||
); | ||
if (signature?.error) { | ||
throw new Error(`Failed to sign message. ${signature?.error}`); | ||
} | ||
const response = await openfort.sendRegisterSessionRequest( | ||
sessionResponseJSON.data.id, | ||
signature.data as string | ||
); | ||
if (!response?.isActive) { | ||
throw new Error('Session key registration failed'); | ||
} | ||
setSessionKey(sessionKey.privateKey); | ||
return {address: sessionKey.address, privateKey: sessionKey.privateKey}; | ||
} else { | ||
return null; | ||
} | ||
}, []); | ||
|
||
const handleCreateSession = async () => { | ||
setLoading(true); | ||
const session = await createSession(); | ||
setLoading(false); | ||
if (session) { | ||
handleSetMessage( | ||
`Session key registered successfully:\n Address: ${session.address}\n Private Key: ${session.privateKey}` | ||
); | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<div> | ||
<fieldset> | ||
<legend className="font-medium leading-6 text-black"> | ||
Session duration | ||
</legend> | ||
<p className="mt-1 text-sm leading-6 text-gray-600"> | ||
How long should the session last? | ||
</p> | ||
<div className="mt-3 space-y-1"> | ||
{sessionMethods.map((sessionMethod) => ( | ||
<div key={sessionMethod.id} className="flex items-center"> | ||
<input | ||
id={sessionMethod.id} | ||
name="session-method" | ||
type="radio" | ||
defaultChecked={sessionMethod.id === '1day'} | ||
className="h-4 w-4 border-gray-300 text-blue-600 focus:ring-blue-600" | ||
/> | ||
<label | ||
htmlFor={sessionMethod.id} | ||
className="ml-3 block text-sm font-medium leading-6 text-gray-900" | ||
> | ||
{sessionMethod.title} | ||
</label> | ||
</div> | ||
))} | ||
</div> | ||
</fieldset> | ||
<button | ||
onClick={handleCreateSession} | ||
disabled={state !== EmbeddedState.READY} | ||
className={`mt-4 w-44 px-4 py-2 bg-black text-white font-semibold rounded-lg shadow-md hover:bg-gray-800 disabled:bg-gray-400 disabled:cursor-not-allowed focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-opacity-50`} | ||
> | ||
{loading ? <Loading /> : 'Create session'} | ||
</button> | ||
</div> | ||
<MintNFTSessionButton | ||
handleSetMessage={handleSetMessage} | ||
sessionKey={sessionKey} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
export default CreateSessionButton; |
79 changes: 79 additions & 0 deletions
79
examples/apps/auth-sample/src/components/Sessions/MintNFTButtoSession.tsx
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,79 @@ | ||
import React, {useCallback, useState} from 'react'; | ||
import Loading from '../Loading'; | ||
import openfort from '../../utils/openfortConfig'; | ||
import {ethers} from 'ethers'; | ||
import {arrayify} from 'ethers/lib/utils'; | ||
|
||
const MintNFTSessionButton: React.FC<{ | ||
handleSetMessage: (message: string) => void; | ||
sessionKey: string | null; | ||
}> = ({handleSetMessage, sessionKey}) => { | ||
const [loading, setLoading] = useState(false); | ||
|
||
const mintNFT = useCallback(async (): Promise<string | null> => { | ||
if (!sessionKey) { | ||
return null; | ||
} | ||
const collectResponse = await fetch(`/api/protected-collect`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${openfort.getAccessToken()}`, | ||
}, | ||
}); | ||
|
||
if (!collectResponse.ok) { | ||
alert('Failed to mint NFT status: ' + collectResponse.status); | ||
return null; | ||
} | ||
const collectResponseJSON = await collectResponse.json(); | ||
|
||
if (collectResponseJSON.data?.nextAction) { | ||
const message = arrayify( | ||
collectResponseJSON.data.nextAction.payload.userOperationHash | ||
); | ||
const sessionSigner = new ethers.Wallet(sessionKey); | ||
const signature = await sessionSigner?.signMessage(message); | ||
if (!signature) { | ||
throw new Error('Failed to sign message with session key'); | ||
} | ||
|
||
const response = await openfort.sendSignatureTransactionIntentRequest( | ||
collectResponseJSON.data.id, | ||
null, | ||
signature | ||
); | ||
return response?.response?.transactionHash ?? null; | ||
} else { | ||
return collectResponseJSON.response?.transactionHash; | ||
} | ||
}, [sessionKey]); | ||
|
||
const handleMintNFT = async () => { | ||
setLoading(true); | ||
const transactionHash = await mintNFT(); | ||
setLoading(false); | ||
if (transactionHash) { | ||
handleSetMessage(`https://www.oklink.com/amoy/tx/${transactionHash}`); | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<button | ||
onClick={handleMintNFT} | ||
disabled={!sessionKey} | ||
className={`mt-4 w-32 px-4 py-2 bg-black text-white font-semibold rounded-lg shadow-md hover:bg-gray-800 disabled:bg-gray-400 disabled:cursor-not-allowed focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-opacity-50`} | ||
> | ||
{loading ? <Loading /> : 'Mint NFT'} | ||
</button> | ||
{!sessionKey && ( | ||
<p className="text-red-400 text-xs mt-2"> | ||
Create a session before minting an NFT signed with a session key. | ||
</p> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default MintNFTSessionButton; |
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
Oops, something went wrong.