Skip to content

Commit

Permalink
feat: walletd api changes and app updates
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Mar 20, 2024
1 parent 6d41967 commit 50a7717
Show file tree
Hide file tree
Showing 25 changed files with 657 additions and 438 deletions.
3 changes: 2 additions & 1 deletion apps/walletd/components/Node/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export function Node() {
const { openDialog } = useDialog()

const transactionCount = txPool.data
? txPool.data.transactions.length + txPool.data.v2Transactions.length
? (txPool.data.transactions?.length || 0) +
(txPool.data.v2Transactions?.length || 0)
: 0

return (
Expand Down
6 changes: 3 additions & 3 deletions apps/walletd/components/Wallet/WalletActionsMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ export function WalletActionsMenu() {
isSynced={status.isSynced}
/>
<AddressesButton />
{wallet?.type !== 'watch' && (
{wallet?.metadata.type !== 'watch' && (
<Button
size="small"
variant="accent"
onClick={() => {
if (wallet?.type === 'seed') {
if (wallet?.metadata.type === 'seed') {
openDialog('walletSendSeed', {
walletId,
})
} else if (wallet?.type === 'ledger') {
} else if (wallet?.metadata.type === 'ledger') {
openDialog('walletSendLedger', {
walletId,
})
Expand Down
4 changes: 2 additions & 2 deletions apps/walletd/components/Wallet/WalletNavMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ export function WalletNavMenu() {
})
}
className="!p-0"
tip={walletTypes[wallet?.type]?.title}
tip={walletTypes[wallet?.metadata.type]?.title}
>
{walletTypes[wallet?.type]?.icon}
{walletTypes[wallet?.metadata.type]?.icon}
</Button>
</div>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ export function AddressesActionsMenu() {
<Button
variant="accent"
onClick={() => {
if (wallet?.type === 'seed') {
if (wallet?.metadata.type === 'seed') {
openDialog('walletAddressesGenerate', { walletId: id })
return
}
if (wallet?.type === 'watch') {
if (wallet?.metadata.type === 'watch') {
openDialog('walletAddressesAdd', { walletId: id })
return
}
if (wallet?.type === 'ledger') {
if (wallet?.metadata.type === 'ledger') {
openDialog('walletLedgerAddressGenerate', { walletId: id })
return
}
Expand Down
11 changes: 6 additions & 5 deletions apps/walletd/contexts/addresses/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ export function useAddressesMain() {
if (!response.data) {
return null
}
const data: AddressData[] = Object.entries(response.data || {}).map(
([address, meta]) => ({
const data: AddressData[] = response.data.map(
({ address, description, metadata, spendPolicy }) => ({
id: address,
address,
description: meta.description as string,
index: meta.index as number,
publicKey: meta.publicKey as string,
description: description,
index: metadata?.index as number,
publicKey: metadata?.publicKey as string,
spendPolicy: spendPolicy,
walletId,
onClick: () =>
openDialog('addressUpdate', {
Expand Down
1 change: 1 addition & 0 deletions apps/walletd/contexts/addresses/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type AddressData = {
address: string
description?: string
publicKey?: string
spendPolicy?: string
index?: number
walletId: string
}
Expand Down
46 changes: 13 additions & 33 deletions apps/walletd/contexts/events/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,11 @@ import {
useServerFilters,
} from '@siafoundation/design-system'
import {
useResubscribe,
useWalletEvents,
useWalletSubscribe,
useWalletTxPool,
} from '@siafoundation/react-walletd'
import {
createContext,
useCallback,
useContext,
useEffect,
useMemo,
} from 'react'
import { createContext, useCallback, useContext, useMemo } from 'react'
import {
CellContext,
EventData,
Expand Down Expand Up @@ -62,25 +56,12 @@ export function useEventsMain() {
},
})

const walletSub = useWalletSubscribe()
const subscribe = useCallback(async () => {
// do not handle error because the common case of
// already being subscribed returns a 500.
walletSub.post({
params: {
id,
},
const _resubscribe = useResubscribe()
const resubscribe = useCallback(async () => {
_resubscribe.post({
payload: 0,
})
}, [walletSub, id])

// Make sure the wallet is subscribed
useEffect(() => {
if (id) {
subscribe()
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [id])
}, [_resubscribe])

const dataset = useMemo<EventData[] | null>(() => {
if (!responseEvents.data || !responseTxPool.data) {
Expand Down Expand Up @@ -134,27 +115,25 @@ export function useEventsMain() {
if (e.type === 'miner payout') {
amountSc = new BigNumber(e.val.siacoinOutput.siacoinOutput.value)
}
if (e.type === 'foundation subsidy') {
amountSc = new BigNumber(e.val.siacoinOutput.siacoinOutput.value)
}

const id = String(index)
const res: EventData = {
id,
id: e.id,
type: e.type,
timestamp: new Date(e.timestamp).getTime(),
height: e.index.height,
pending: false,
amountSc,
amountSf,
}
if ('fee' in e.val) {
if (e.type === 'transaction') {
res.fee = new BigNumber(e.val.fee)
}
if ('fileContract' in e.val) {
if (e.type === 'contract payout') {
res.contractId = e.val.fileContract.id
}
if ('id' in e.val) {
res.id += e.val.id
res.transactionId = e.val.id
}
return res
})
return [...dataTxPool.reverse(), ...dataEvents]
Expand Down Expand Up @@ -226,6 +205,7 @@ export function useEventsMain() {
removeFilter,
removeLastFilter,
resetFilters,
resubscribe,
offset,
limit,
}
Expand Down
11 changes: 9 additions & 2 deletions apps/walletd/contexts/wallets/columns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ export const columns: WalletsTableColumn[] = [
id: 'type',
label: 'type',
category: 'general',
render: ({ data: { type } }) => {
render: ({
data: {
metadata: { type },
},
}) => {
return (
<Tooltip content={walletTypes[type]?.title}>
<Badge interactive={false} className="flex gap-0.5 items-center">
Expand All @@ -123,9 +127,12 @@ export const columns: WalletsTableColumn[] = [
label: 'status',
category: 'general',
render: ({
data: { type, status, activityAt, unlock, lock },
data,
context: { walletAutoLockEnabled, walletAutoLockTimeout },
}) => {
const { type } = data.metadata
const { status, activityAt } = data.state
const { unlock, lock } = data.actions
if (type === 'seed') {
const sinceActivityMs = new Date().getTime() - activityAt
const remainingMs = Math.max(walletAutoLockTimeout - sinceActivityMs, 0)
Expand Down
32 changes: 18 additions & 14 deletions apps/walletd/contexts/wallets/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
columnsDefaultVisible,
defaultSortField,
sortOptions,
WalletType,
WalletMetadata,
} from './types'
import { columns } from './columns'
import { useRouter } from 'next/router'
Expand Down Expand Up @@ -54,19 +54,23 @@ function useWalletsMain() {
if (!response.data) {
return null
}
const data: WalletData[] = Object.entries(response.data || {}).map(
([id, meta]) => ({
const data: WalletData[] = response.data.map(
({ id, name, description, dateCreated, lastUpdated, metadata }) => ({
id,
name: meta.name as string,
seed: seedCache[id],
status: seedCache[id] ? 'unlocked' : 'locked',
activityAt: walletActivityAt[id],
seedHash: meta.seedHash as string,
description: meta.description as string,
createdAt: (meta.createdAt as number) || 0,
type: meta.type as WalletType,
unlock: () => openDialog('walletUnlock', { walletId: id }),
lock: () => saveWalletSeed(id, undefined),
name,
description,
createdAt: new Date(dateCreated).getTime() || 0,
updatedAt: new Date(lastUpdated).getTime() || 0,
metadata: (metadata || {}) as WalletMetadata,
state: {
seed: seedCache[id],
status: seedCache[id] ? 'unlocked' : 'locked',
activityAt: walletActivityAt[id],
},
actions: {
unlock: () => openDialog('walletUnlock', { walletId: id }),
lock: () => saveWalletSeed(id, undefined),
},
onClick: () =>
router.push({
pathname: routes.wallet.view,
Expand Down Expand Up @@ -145,7 +149,7 @@ function useWalletsMain() {
dataState,
error: response.error,
datasetCount: datasetFiltered?.length || 0,
unlockedCount: dataset?.filter((d) => d.seed).length || 0,
unlockedCount: dataset?.filter((d) => d.state.seed).length || 0,
columns: filteredTableColumns,
dataset: datasetFiltered,
context,
Expand Down
23 changes: 16 additions & 7 deletions apps/walletd/contexts/wallets/types.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
export type WalletType = 'seed' | 'watch' | 'ledger'

export type WalletMetadata = {
type: WalletType
seedHash?: string
}

export type WalletData = {
id: string
name?: string
description?: string
type?: WalletType
seed?: string
activityAt?: number
status: 'unlocked' | 'locked'
seedHash?: string
createdAt?: number
unlock: () => void
lock: () => void
updatedAt?: number
metadata: WalletMetadata
state: {
seed?: string
activityAt?: number
status: 'unlocked' | 'locked'
}
actions: {
unlock: () => void
lock: () => void
}
}

export type TableColumnId =
Expand Down
22 changes: 10 additions & 12 deletions apps/walletd/dialogs/WalletAddNewDialog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { useForm } from 'react-hook-form'
import { useWalletAdd } from '@siafoundation/react-walletd'
import { useDialog } from '../../contexts/dialog'
import { useWallets } from '../../contexts/wallets'
import { v4 as uuidv4 } from 'uuid'
import { walletAddTypes } from '../../config/walletTypes'
import { blake2bHex } from 'blakejs'
import { SeedLayout } from '../SeedLayout'
Expand All @@ -30,13 +29,15 @@ const defaultValues = {
hasCopied: false,
}

type Values = typeof defaultValues

function getFields({
walletNames,
copySeed,
}: {
walletNames: string[]
copySeed: () => void
}): ConfigFields<typeof defaultValues, never> {
}): ConfigFields<Values, never> {
return {
name: {
type: 'text',
Expand Down Expand Up @@ -136,27 +137,24 @@ export function WalletAddNewDialog({ trigger, open, onOpenChange }: Props) {
const fields = getFields({ walletNames, copySeed })

const onSubmit = useCallback(
async (values) => {
const id = uuidv4()
async (values: Values) => {
const { seed } = getWalletWasm().seedFromPhrase(values.mnemonic)
const seedHash = blake2bHex(seed)
const response = await walletAdd.put({
params: {
id,
},
const response = await walletAdd.post({
payload: {
type: 'seed',
seedHash,
name: values.name,
createdAt: new Date().getTime(),
description: values.description,
metadata: {
type: 'seed',
seedHash,
},
},
})
if (response.error) {
triggerErrorToast(response.error)
} else {
openDialog('walletAddressesGenerate', {
walletId: id,
walletId: response.data.id,
})
form.reset(defaultValues)
}
Expand Down
Loading

0 comments on commit 50a7717

Please sign in to comment.