diff --git a/components/notifications/NotificationsDrawer.tsx b/components/notifications/NotificationsDrawer.tsx index 3d4ef9c3..39213ee2 100644 --- a/components/notifications/NotificationsDrawer.tsx +++ b/components/notifications/NotificationsDrawer.tsx @@ -19,14 +19,27 @@ import NotificationCookieStore from '@store/notificationCookieStore' import dayjs from 'dayjs' import { useTranslation } from 'next-i18next' import { notify } from 'utils/notifications' +import { + Connection, + PublicKey, + Transaction, + TransactionInstruction, +} from '@solana/web3.js' +import mangoStore from '@store/mangoStore' +import Switch from '@components/forms/Switch' +const MEMO_PROGRAM_ID = new PublicKey( + 'MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr', +) -export const createSolanaMessage = ( +export const createSolanaMessage = async ( wallet: WalletContextState, setCookie: (wallet: string, token: string) => void, + connection: Connection, + usingLedger: boolean, ) => { const payload = new Payload() payload.domain = window.location.host - payload.address = wallet.publicKey!.toString() + payload.address = wallet.publicKey!.toBase58() payload.uri = window.location.origin payload.statement = 'Login to Mango Notifications Admin App' payload.version = '1' @@ -36,39 +49,79 @@ export const createSolanaMessage = ( const messageText = message.prepareMessage() const messageEncoded = new TextEncoder().encode(messageText) + if (usingLedger) { + const tx = new Transaction() - wallet.signMessage!(messageEncoded) - .then(async (resp) => { - const tokenResp = await fetch(`${NOTIFICATION_API}auth`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - ...payload, - signatureString: bs58.encode(resp), - }), - }) - const body = await tokenResp.json() - const token = body.token - const error = body.error - if (error) { - notify({ - type: 'error', - title: 'Error', - description: error, - }) - return - } - setCookie(payload.address, token) + tx.add( + new TransactionInstruction({ + programId: MEMO_PROGRAM_ID, + keys: [], + data: Buffer.from(messageText), + }), + ) + tx.feePayer = wallet.publicKey! + tx.recentBlockhash = (await connection.getLatestBlockhash()).blockhash + + const signedTx = await wallet.signTransaction!(tx) + const serializedTx = signedTx.serialize() + + const tokenResp = await fetch(`${NOTIFICATION_API}auth`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + ...payload, + isLedger: true, + serializedTx: Array.from(serializedTx), + }), }) - .catch((e) => { + const body = await tokenResp.json() + const token = body.token + const error = body.error + if (error) { notify({ type: 'error', title: 'Error', - description: e.message ? e.message : `${e}`, + description: error, }) - }) + return + } + setCookie(payload.address, token) + } else { + wallet.signMessage!(messageEncoded) + .then(async (resp) => { + const tokenResp = await fetch(`${NOTIFICATION_API}auth`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + ...payload, + signatureString: bs58.encode(resp), + }), + }) + const body = await tokenResp.json() + const token = body.token + const error = body.error + if (error) { + notify({ + type: 'error', + title: 'Error', + description: error, + }) + return + } + setCookie(payload.address, token) + }) + .catch((e) => { + notify({ + type: 'error', + title: 'Error', + description: e.message ? e.message : `${e}`, + }) + }) + } } const NotificationsDrawer = ({ @@ -80,11 +133,13 @@ const NotificationsDrawer = ({ }) => { const { t } = useTranslation('notifications') const { data, refetch } = useNotifications() + const connection = mangoStore((s) => s.connection) const wallet = useWallet() const isAuth = useIsAuthorized() const headers = useHeaders() const setCookie = NotificationCookieStore((s) => s.setCookie) const [isRemoving, setIsRemoving] = useState(false) + const [useLedger, setUseLedger] = useState(false) const unseenNotifications = useMemo(() => { if (!data || !data.length) return [] @@ -268,12 +323,24 @@ const NotificationsDrawer = ({

{t('unauth-title')}

-

{t('unauth-desc')}

+

{t('unauth-desc')}

+

{t('im-using-ledger')}

+ setUseLedger(checked)} + />
diff --git a/components/settings/NotificationSettings.tsx b/components/settings/NotificationSettings.tsx index 3e285e8d..52a90126 100644 --- a/components/settings/NotificationSettings.tsx +++ b/components/settings/NotificationSettings.tsx @@ -10,6 +10,8 @@ import { useNotificationSettings } from 'hooks/notifications/useNotificationSett import { useTranslation } from 'next-i18next' import { NOTIFICATION_API } from 'utils/constants' import NotificationCookieStore from '@store/notificationCookieStore' +import mangoStore from '@store/mangoStore' +import { useState } from 'react' const NotificationSettings = () => { const { t } = useTranslation(['common', 'notifications', 'settings']) @@ -19,6 +21,8 @@ const NotificationSettings = () => { const setCookie = NotificationCookieStore((s) => s.setCookie) const headers = useHeaders() const isAuth = useIsAuthorized() + const connection = mangoStore((s) => s.connection) + const [useLedger, setUseLedger] = useState(false) const handleSettingChange = async (key: string, val: boolean) => { if (data) { @@ -60,9 +64,21 @@ const NotificationSettings = () => {

{t('notifications:unauth-desc')}

-
diff --git a/public/locales/en/notifications.json b/public/locales/en/notifications.json index 2632a51a..796cbcd3 100644 --- a/public/locales/en/notifications.json +++ b/public/locales/en/notifications.json @@ -4,6 +4,8 @@ "empty-state-title": "Nothing to see here", "notifications": "Notifications", "sign-message": "Sign Message", + "sign-with-tx": "Sign with tx", + "im-using-ledger": "Im using ledger", "unauth-desc": "Sign with your wallet to start receiving notifications", "unauth-title": "Notifications Inbox" } \ No newline at end of file diff --git a/public/locales/es/notifications.json b/public/locales/es/notifications.json index 2632a51a..796cbcd3 100644 --- a/public/locales/es/notifications.json +++ b/public/locales/es/notifications.json @@ -4,6 +4,8 @@ "empty-state-title": "Nothing to see here", "notifications": "Notifications", "sign-message": "Sign Message", + "sign-with-tx": "Sign with tx", + "im-using-ledger": "Im using ledger", "unauth-desc": "Sign with your wallet to start receiving notifications", "unauth-title": "Notifications Inbox" } \ No newline at end of file diff --git a/public/locales/ru/notifications.json b/public/locales/ru/notifications.json index 2632a51a..796cbcd3 100644 --- a/public/locales/ru/notifications.json +++ b/public/locales/ru/notifications.json @@ -4,6 +4,8 @@ "empty-state-title": "Nothing to see here", "notifications": "Notifications", "sign-message": "Sign Message", + "sign-with-tx": "Sign with tx", + "im-using-ledger": "Im using ledger", "unauth-desc": "Sign with your wallet to start receiving notifications", "unauth-title": "Notifications Inbox" } \ No newline at end of file diff --git a/public/locales/zh/notifications.json b/public/locales/zh/notifications.json index f960c1a8..ce8bfa39 100644 --- a/public/locales/zh/notifications.json +++ b/public/locales/zh/notifications.json @@ -4,6 +4,8 @@ "empty-state-title": "这里没什么", "notifications": "通知", "sign-message": "签署讯息", + "sign-with-tx": "Sign with tx", + "im-using-ledger": "Im using ledger", "unauth-desc": "连接钱包而受到通知", "unauth-title": "通知收件匣" } \ No newline at end of file diff --git a/public/locales/zh_tw/notifications.json b/public/locales/zh_tw/notifications.json index 22a1b1ec..e2962781 100644 --- a/public/locales/zh_tw/notifications.json +++ b/public/locales/zh_tw/notifications.json @@ -4,6 +4,8 @@ "empty-state-title": "這裡沒什麼", "notifications": "通知", "sign-message": "簽署訊息", + "sign-with-tx": "Sign with tx", + "im-using-ledger": "Im using ledger", "unauth-desc": "連接錢包而受到通知", "unauth-title": "通知收件匣" } \ No newline at end of file