Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ type Address = {
address: string;
};

type MultisigHistoryEntry = {
network: NetworkChoice;
address: string;
};

type SafelyStorage = {
addresses: Address[];
multisig?: string;
network?: NetworkChoice;
profile?: string;
multisigHistory?: MultisigHistoryEntry[];
};

// Config path: ~/.safely/config.json
Expand All @@ -24,6 +30,7 @@ const defaultData: SafelyStorage = {
multisig: undefined,
network: undefined,
profile: undefined,
multisigHistory: [],
};

// Ensure config directory exists
Expand Down Expand Up @@ -157,3 +164,41 @@ export const ensureMultisigAddressExists = createEnsure(
export const ensureNetworkExists = createEnsure(NetworkDefault, 'No network provided');

export const ensureProfileExists = createEnsure(ProfileDefault, 'No profile provided');

// **Multisig History Operations**
export const MultisigHistory = {
async getAll(): Promise<MultisigHistoryEntry[]> {
return readStorage((config) => config.multisigHistory || []);
},

async getForNetwork(network: NetworkChoice): Promise<string[]> {
return readStorage((config) => {
const history = config.multisigHistory || [];
return history.filter((entry) => entry.network === network).map((entry) => entry.address);
});
},

async add(network: NetworkChoice, address: string) {
await updateStorage((config) => {
if (!config.multisigHistory) {
config.multisigHistory = [];
}
// Dedupe: check if this network+address combo already exists
const exists = config.multisigHistory.some(
(entry) => entry.network === network && entry.address === address
);
if (!exists) {
config.multisigHistory.push({ network, address });
}
});
},

async remove(network: NetworkChoice, address: string) {
await updateStorage((config) => {
if (!config.multisigHistory) return;
config.multisigHistory = config.multisigHistory.filter(
(entry) => !(entry.network === network && entry.address === address)
);
});
},
};
142 changes: 92 additions & 50 deletions src/ui/HomeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useState, useEffect, useCallback, useMemo } from 'react';
import { Box, Text, useInput, useApp, render } from 'ink';
import TextInput from 'ink-text-input';
import Spinner from 'ink-spinner';
import { ProfileDefault, MultisigDefault, NetworkDefault } from '../storage.js';
import { ProfileDefault, MultisigDefault, NetworkDefault, MultisigHistory } from '../storage.js';
import { getAllProfiles, ProfileInfo } from '../profiles.js';
import { NETWORK_CHOICES, NetworkChoice } from '../constants.js';
import { validateAddress } from '../validators.js';
Expand All @@ -23,6 +23,7 @@ interface Config {
profiles: ProfileInfo[];
multisigOwners: string[];
profileAddress: string | null;
multisigHistory: string[];
}

interface MenuState {
Expand All @@ -44,7 +45,8 @@ const HomeView: React.FC<HomeViewProps> = ({ onNavigate }) => {
const [config, setConfig] = useState<Config>({
profiles: [],
multisigOwners: [],
profileAddress: null
profileAddress: null,
multisigHistory: []
});
const [menu, setMenu] = useState<MenuState>({
selectedIndex: 0,
Expand All @@ -64,13 +66,15 @@ const HomeView: React.FC<HomeViewProps> = ({ onNavigate }) => {
ProfileDefault.get()
]);
const owners = network && multisig ? await fetchMultisigOwners(network, multisig) : [];
const multisigHistory = network ? await MultisigHistory.getForNetwork(network) : [];
setConfig({
network,
multisig,
profile,
profiles: getAllProfiles(),
multisigOwners: owners,
profileAddress: null
profileAddress: null,
multisigHistory
});
};
load();
Expand Down Expand Up @@ -153,13 +157,21 @@ const HomeView: React.FC<HomeViewProps> = ({ onNavigate }) => {
newConfig.profile = undefined;
newConfig.multisigOwners = [];
newConfig.profileAddress = null;
// Load multisig history for new network
const multisigHistory = await MultisigHistory.getForNetwork(updates.network);
newConfig.multisigHistory = multisigHistory;
}
if ('multisig' in updates && updates.multisig) {
await MultisigDefault.set(updates.multisig);
// Fetch owners for new multisig
if (config.network) {
const owners = await fetchMultisigOwners(config.network, updates.multisig);
newConfig.multisigOwners = owners;
// Add to history
await MultisigHistory.add(config.network, updates.multisig);
// Refresh history
const multisigHistory = await MultisigHistory.getForNetwork(config.network);
newConfig.multisigHistory = multisigHistory;
}
}
if ('profile' in updates && updates.profile) {
Expand Down Expand Up @@ -189,7 +201,11 @@ const HomeView: React.FC<HomeViewProps> = ({ onNavigate }) => {
const idx = filteredProfiles.findIndex(p => p.name === config.profile);
updates.subIndex = Math.max(0, idx);
} else if (item === 'multisig') {
updates.multisigInput = config.multisig || '';
updates.multisigInput = '';
updates.isValidating = false;
updates.multisigError = '';
// Start with input field selected (subIndex = history.length means input field)
updates.subIndex = config.multisigHistory.length;
}

setMenu(m => ({ ...m, ...updates }));
Expand Down Expand Up @@ -233,39 +249,49 @@ const HomeView: React.FC<HomeViewProps> = ({ onNavigate }) => {
} else if (expandedItem === 'multisig') {
if (key.escape) {
collapseMenu();
} else if (key.upArrow) {
setMenu(m => ({ ...m, subIndex: Math.max(0, m.subIndex - 1) }));
} else if (key.downArrow) {
setMenu(m => ({ ...m, subIndex: Math.min(config.multisigHistory.length, m.subIndex + 1) }));
} else if (key.return) {
// Validate and save multisig address
(async () => {
try {
validateAddress(menu.multisigInput);
setMenu(m => ({ ...m, isValidating: true, multisigError: '' }));

// Check if address has MultisigAccount resource
const aptos = initAptos(config.network!);
// If on input field (subIndex == history.length), validate and save
if (subIndex === config.multisigHistory.length) {
(async () => {
try {
const resource = await aptos.getAccountResource<MultisigResource>({
accountAddress: menu.multisigInput,
resourceType: '0x1::multisig_account::MultisigAccount'
});
// Successfully got the resource, it's a valid multisig
await updateConfig({ multisig: menu.multisigInput, multisigOwners: resource.owners });
collapseMenu();
} catch (resourceError) {
// Resource doesn't exist or error fetching
validateAddress(menu.multisigInput);
setMenu(m => ({ ...m, isValidating: true, multisigError: '' }));

// Check if address has MultisigAccount resource
const aptos = initAptos(config.network!);
try {
const resource = await aptos.getAccountResource<MultisigResource>({
accountAddress: menu.multisigInput,
resourceType: '0x1::multisig_account::MultisigAccount'
});
// Successfully got the resource, it's a valid multisig
await updateConfig({ multisig: menu.multisigInput, multisigOwners: resource.owners });
collapseMenu();
} catch (resourceError) {
// Resource doesn't exist or error fetching
setMenu(m => ({
...m,
multisigError: 'Address is not a multisig account',
isValidating: false
}));
}
} catch (error) {
setMenu(m => ({
...m,
multisigError: 'Address is not a multisig account',
multisigError: String(error).replace('Error: ', ''),
isValidating: false
}));
}
} catch (error) {
setMenu(m => ({
...m,
multisigError: String(error).replace('Error: ', ''),
isValidating: false
}));
}
})();
})();
} else if (config.multisigHistory[subIndex]) {
// Select from history
updateConfig({ multisig: config.multisigHistory[subIndex] });
collapseMenu();
}
}
} else {
// Main menu navigation
Expand Down Expand Up @@ -340,6 +366,9 @@ const HomeView: React.FC<HomeViewProps> = ({ onNavigate }) => {
isSelected={selectedIndex === 1}
isValidating={menu.isValidating}
onInputChange={value => setMenu(m => ({ ...m, multisigInput: value }))}
multisigHistory={config.multisigHistory}
currentMultisig={config.multisig}
subIndex={subIndex}
/>
) : (
<MenuItem
Expand Down Expand Up @@ -450,27 +479,40 @@ const MultisigExpanded: React.FC<{
isSelected: boolean;
isValidating: boolean;
onInputChange: (value: string) => void;
}> = ({ input, error, isSelected, isValidating, onInputChange }) => (
<>
<Text>{isSelected ? '▼' : ' '} Multisig:</Text>
<Box paddingLeft={2}>
<Text>Address: </Text>
{isValidating ? (
<Text color="cyan">
<Spinner type="dots" /> Validating...
multisigHistory: string[];
currentMultisig?: string;
subIndex: number;
}> = ({ input, error, isSelected, isValidating, onInputChange, multisigHistory, currentMultisig, subIndex }) => {
const isInputSelected = subIndex === multisigHistory.length;

return (
<>
<Text>{isSelected ? '▼' : ' '} Multisig:</Text>
{multisigHistory.map((address, index) => (
<Text key={address}>
{' '}{index === subIndex ? '▶' : ' '} {address.slice(0, 10)}...{address.slice(-6)}
{address === currentMultisig && <Text color="green"> ✓</Text>}
</Text>
) : (
<TextInput
value={input}
onChange={onInputChange}
placeholder="0x..."
/>
)}
</Box>
{error && <Text color="red"> ✗ {error}</Text>}
{!isValidating && <Text dimColor> [Enter] Save | [Esc] Cancel</Text>}
</>
);
))}
<Box paddingLeft={2}>
<Text>{isInputSelected ? '▶' : ' '} New: </Text>
{isValidating ? (
<Text color="cyan">
<Spinner type="dots" /> Validating...
</Text>
) : (
<TextInput
value={input}
onChange={onInputChange}
placeholder="0x..."
focus={isInputSelected}
/>
)}
</Box>
{error && <Text color="red"> ✗ {error}</Text>}
</>
);
};

// Component: Profile Expanded
const ProfileExpanded: React.FC<{
Expand Down
Loading