-
Notifications
You must be signed in to change notification settings - Fork 9
feat: snap birthday block form on install #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6e80487
c434596
0d7971e
3a99f78
340cc6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { | ||
Form, | ||
Box, | ||
Heading, | ||
Input, | ||
Button, | ||
Text, | ||
Bold, | ||
Divider | ||
} from '@metamask/snaps-sdk/jsx'; | ||
|
||
type BirthdayBlockForm = { customBirthdayBlock: string | null }; | ||
|
||
export type SetBirthdayBlockParams = { latestBlock?: number }; | ||
|
||
export async function setBirthdayBlock({ | ||
latestBlock, | ||
}: SetBirthdayBlockParams): Promise<string | null> { | ||
|
||
const interfaceId = await snap.request({ | ||
method: 'snap_createInterface', | ||
params: { | ||
ui: ( | ||
<Form name="birthday-block-form"> | ||
<Box> | ||
<Heading>Optional syncing block height</Heading> | ||
<Text> | ||
If you alerady created Zcash Web Wallet account with this Metamask | ||
seed you can enter optional birthday block of that Wallet. | ||
</Text> | ||
<Divider /> | ||
<Text> | ||
Syncing | ||
proccess will start from that block. | ||
</Text> | ||
<Divider /> | ||
{!!latestBlock && <Text>Latest block: <Bold>{latestBlock.toString()}</Bold></Text>} | ||
<Input | ||
min={0} | ||
step={1} | ||
type="number" | ||
name="customBirthdayBlock" | ||
placeholder="optional syncing block height" | ||
/> | ||
</Box> | ||
<Button type="submit" name="next"> | ||
Continue to wallet | ||
</Button> | ||
</Form> | ||
), | ||
}, | ||
}); | ||
|
||
const { customBirthdayBlock } = (await snap.request({ | ||
method: 'snap_dialog', | ||
params: { | ||
id: interfaceId, | ||
}, | ||
})) as BirthdayBlockForm; | ||
|
||
|
||
return customBirthdayBlock; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,35 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import React, { useEffect } from 'react'; | ||
import { ZcashYellowPNG, FormTransferSvg, MetaMaskLogoPNG } from '../assets'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import { useWebZjsContext } from '../context/WebzjsContext'; | ||
import { | ||
useInvokeSnap, | ||
useRequestSnap, | ||
useMetaMask, | ||
useWebZjsActions, | ||
} from '../hooks'; | ||
|
||
const Home: React.FC = () => { | ||
const [birthdayHeight, setBirthdayHeight] = useState(0); | ||
const navigate = useNavigate(); | ||
const { state } = useWebZjsContext(); | ||
const { flushDbToStore, addNewAccountFromUfvk } = useWebZjsActions(); | ||
const invokeSnap = useInvokeSnap(); | ||
const { getAccountData, connectWebZjsSnap } = useWebZjsActions(); | ||
const { installedSnap, isFlask } = useMetaMask(); | ||
const requestSnap = useRequestSnap(); | ||
|
||
useEffect(() => { | ||
const fetchBirthday = async () => { | ||
const birthday = await state.webWallet?.get_latest_block(); | ||
setBirthdayHeight(Number(birthday) || 0); | ||
}; | ||
fetchBirthday(); | ||
}, [state]); | ||
|
||
const handleRequestSnapAndGetViewingKey: React.MouseEventHandler< | ||
const handleConnectButton: React.MouseEventHandler< | ||
HTMLButtonElement | ||
> = async (e) => { | ||
e.preventDefault(); | ||
await requestSnap(); | ||
|
||
const viewKey = (await invokeSnap({ method: 'getViewingKey' })) as string; | ||
console.log(viewKey); | ||
|
||
await addNewAccountFromUfvk(viewKey, birthdayHeight); | ||
setBirthdayHeight(0); | ||
|
||
await flushDbToStore(); | ||
await connectWebZjsSnap(); | ||
}; | ||
|
||
useEffect(() => { | ||
if (installedSnap) navigate('/dashboard/account-summary'); | ||
}, [installedSnap, navigate]); | ||
if (installedSnap) { | ||
const homeReload = async () => { | ||
const accountData = await getAccountData(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we get Account data only on the Receive page? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I replaced unifiedAddress instead installedSnap as a flag that allows Maybe checking wallet summary is better for this case |
||
|
||
if (accountData?.unifiedAddress) navigate('/dashboard/account-summary') | ||
} | ||
homeReload(); | ||
}; | ||
}, [installedSnap, navigate, getAccountData, state.activeAccount]); | ||
|
||
return ( | ||
<div className="home-page flex items-start md:items-center justify-center px-4"> | ||
|
@@ -63,7 +49,7 @@ const Home: React.FC = () => { | |
</p> | ||
<button | ||
disabled={!isFlask} | ||
onClick={handleRequestSnapAndGetViewingKey} | ||
onClick={handleConnectButton} | ||
className="flex items-center bg-button-black-gradient hover:bg-button-black-gradient-hover text-white px-6 py-3 rounded-[2rem] cursor-pointer" | ||
> | ||
<span>Connect MetaMask Snap</span> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would also be nice to give a user a hint about the current block height as a reference point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guess I would need "endowment:network-access" and some API providing current block of the chain. Or did I miss some method in wasm returning latest block without initialized wallet?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can pass it from front-end when calling setBirthdayBlock()