Created
June 16, 2025 17:52
-
-
Save bmeeder22/1414f2aff2c64a1c3b5fc3ce4ee2c6f5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import { | |
| Connection, | |
| PublicKey, | |
| Transaction, | |
| VersionedTransaction, | |
| } from '@solana/web3.js'; | |
| import {useCallback, useEffect, useMemo, useState} from 'react'; | |
| import {MetaKeep} from 'metakeep'; | |
| export function useMetakeepWallet({appId, email}: {appId: string; email: string;}) { | |
| const metakeep = useMemo(() => { | |
| if (!email || !appId) return null; | |
| return new MetaKeep({ | |
| appId, | |
| user: { | |
| email, | |
| }, | |
| }); | |
| }, [appId, email]); | |
| const [publicKey, setPublicKey] = useState<PublicKey | null>(null); | |
| useEffect(() => { | |
| metakeep | |
| ?.getWallet() | |
| .then(res => setPublicKey(new PublicKey(res.wallet.solAddress))); | |
| }, [metakeep]); | |
| const signTransaction = useCallback( | |
| async <T extends Transaction | VersionedTransaction>( | |
| transaction: T | |
| ): Promise<T> => { | |
| if (!metakeep) throw new Error('metakeep is null'); | |
| const metakeepRes: {signature: string} = await metakeep.signTransaction( | |
| transaction, | |
| 'Withdraw USD' | |
| ); | |
| const signature = Buffer.from( | |
| metakeepRes.signature.replace('0x', ''), | |
| 'hex' | |
| ); | |
| if (transaction instanceof Transaction) { | |
| transaction.signatures.push({ | |
| publicKey: publicKey!, | |
| signature, | |
| }); | |
| transaction.signatures = transaction.signatures.filter( | |
| sig => !!sig.signature | |
| ); | |
| } else { | |
| transaction.signatures.push(signature); | |
| } | |
| return transaction; | |
| }, | |
| [metakeep, publicKey] | |
| ); | |
| const signMessage = useCallback( | |
| async (message: Uint8Array) => { | |
| if (!metakeep) throw new Error('metakeep is null'); | |
| const {signature} = await metakeep.signMessage( | |
| Buffer.from(message).toString('utf8'), | |
| 'Sign in' | |
| ); | |
| return Uint8Array.from(Buffer.from(signature.slice(2), 'hex')); | |
| }, | |
| [metakeep] | |
| ); | |
| return { | |
| publicKey, | |
| signMessage, | |
| signTransaction, | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment