Skip to content

Commit

Permalink
Add handlers for account import, export, CRUD
Browse files Browse the repository at this point in the history
  • Loading branch information
dguenther committed Apr 24, 2024
1 parent b9c7fcf commit b4a6847
Show file tree
Hide file tree
Showing 8 changed files with 278 additions and 58 deletions.
72 changes: 36 additions & 36 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 36 additions & 1 deletion packages/mobile-app/app/(tabs)/transact.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { View, Text, ScrollView, TextInput } from "react-native";
import { useFacade } from "../../data/facades";
import { Button } from "@ironfish/ui";
import { useQueryClient } from "@tanstack/react-query";
import React from "react";
import { AccountFormat } from "@ironfish/sdk";

export default function Transact() {
const facade = useFacade();
Expand All @@ -15,8 +17,24 @@ export default function Transact() {
})
}
});
const importAccount = facade.importAccount.useMutation({
onSuccess: () => {
qc.invalidateQueries({
queryKey: ['getAccounts']
})
}
});
const removeAccount = facade.removeAccount.useMutation({
onSuccess: () => {
qc.invalidateQueries({
queryKey: ['getAccounts']
})
}
});
const exportAccount = facade.exportAccount.useMutation();

const [importAccountText, setImportAccountText] = React.useState('')

return (
<ScrollView>
<Text>Accounts</Text>
Expand All @@ -25,7 +43,15 @@ export default function Transact() {
<Text>{account.name}</Text>
<Button
onClick={async () => {
const otherResult = await exportAccount.mutateAsync({ name: account.name });
await removeAccount.mutateAsync({ name: account.name });
console.log('Removed Account', account.name)
}}
>
Remove Account
</Button>
<Button
onClick={async () => {
const otherResult = await exportAccount.mutateAsync({ name: account.name, format: AccountFormat.Base64Json });
console.log('Exported Account:', otherResult)
}}
>
Expand All @@ -41,6 +67,15 @@ export default function Transact() {
>
Create Account
</Button>
<TextInput value={importAccountText} onChangeText={setImportAccountText} placeholder="Import account"/>
<Button
onClick={async () => {
const otherResult = await importAccount.mutateAsync({ account: importAccountText });
console.log('Imported Account:', otherResult)
}}
>
Import Account
</Button>
</ScrollView>
);
}
68 changes: 62 additions & 6 deletions packages/mobile-app/data/facades/accounts/demoHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { f } from "data-facade";
import { z } from "zod";
import { AccountsMethods } from "./types";
import { AccountFormat } from "@ironfish/sdk";

let ACCOUNTS = [
{ id: 0, name: "alice", viewOnlyAccount: "alice" },
Expand All @@ -14,11 +15,6 @@ async function getAccounts(limit: number) {
}

export const accountsHandlers = f.facade<AccountsMethods>({
getAccounts: f.handler.query(async () => {
const accounts = await getAccounts(ACCOUNTS.length);
console.log("getAccounts", accounts);
return accounts;
}),
createAccount: f.handler
.input(
z.object({
Expand All @@ -39,13 +35,73 @@ export const accountsHandlers = f.facade<AccountsMethods>({
.input(
z.object({
name: z.string(),
format: z.nativeEnum(AccountFormat),
language: z.string().optional(),
viewOnly: z.boolean().optional(),
}),
)
.mutation(async ({ name }) => {
const account = ACCOUNTS.find((a) => a.name === name)
if (account === undefined) {
throw new Error("No accounts found");
throw new Error(`No account found with name ${name}`);
}
return JSON.stringify(account);
}),
getAccount: f.handler.input(
z.object({
name: z.string(),
}),
).query(async ({ name }) => {
const account = ACCOUNTS.find((a) => a.name === name);
if (account === undefined) {
throw new Error(`No account found with name ${name}`);
}
return account
}),
getAccounts: f.handler.query(async () => {
const accounts = await getAccounts(ACCOUNTS.length);
console.log("getAccounts", accounts);
return accounts;
}),
importAccount: f.handler
.input(
z.object({
account: z.string(),
name: z.string(),
}),
)
.mutation(async ({ account, name }) => {
const existingId = ACCOUNTS.at(-1)?.id
if (existingId === undefined) {
throw new Error("No accounts found");
}
const importedAccount = { id: existingId + 1, name, viewOnlyAccount: account }
console.log("createAccount", account);
ACCOUNTS.push(importedAccount);
return importedAccount;
}),
removeAccount: f.handler
.input(
z.object({
name: z.string(),
}),
)
.mutation(async ({ name }) => {
const accountIndex = ACCOUNTS.findIndex((a) => a.name === name);
ACCOUNTS.splice(accountIndex, 1);
}),
renameAccount: f.handler
.input(
z.object({
name: z.string(),
newName: z.string(),
}),
)
.mutation(async ({ name, newName }) => {
const account = ACCOUNTS.find((a) => a.name === name);
if (!account) {
throw new Error("No account found");
}
account.name = newName;
}),
});
57 changes: 51 additions & 6 deletions packages/mobile-app/data/facades/accounts/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import { f } from "data-facade";
import { z } from "zod";
import { AccountsMethods } from "./types";
import { wallet } from "../../wallet";
import { AccountFormat, LanguageKey, LanguageUtils } from "@ironfish/sdk";

export const accountsHandlers = f.facade<AccountsMethods>({
getAccounts: f.handler.query(async () => {
return await wallet.getAccounts();
}),
createAccount: f.handler
.input(
z.object({
Expand All @@ -17,14 +15,61 @@ export const accountsHandlers = f.facade<AccountsMethods>({
const account = await wallet.createAccount(name);
return account;
}),
exportAccount: f.handler
exportAccount: f.handler
.input(
z.object({
name: z.string(),
format: z.nativeEnum(AccountFormat),
language: z.string().optional(),
viewOnly: z.boolean().optional(),
}),
)
.mutation(async ({ name }) => {
const account = await wallet.exportAccount(name);
.mutation(async ({ name, format, language, viewOnly }) => {
if (language && !Object.hasOwn(LanguageUtils.LANGUAGES, language)) {
throw new Error(`Language ${language} is not supported`);
}
const typedLanguage: LanguageKey = language as LanguageKey;

const account = await wallet.exportAccount(name, format, { language: typedLanguage, viewOnly });
return account;
}),
getAccount: f.handler.input(
z.object({
name: z.string(),
}),
).query(async ({ name }) => {
return await wallet.getAccount(name);
}),
getAccounts: f.handler.query(async () => {
return await wallet.getAccounts();
}),
importAccount: f.handler
.input(
z.object({
account: z.string(),
name: z.string().optional(),
}),
)
.mutation(async ({ account, name }) => {
return await wallet.importAccount(account, name);
}),
removeAccount: f.handler
.input(
z.object({
name: z.string(),
}),
)
.mutation(async ({ name }) => {
await wallet.removeAccount(name);
}),
renameAccount: f.handler
.input(
z.object({
name: z.string(),
newName: z.string(),
}),
)
.mutation(async ({ name, newName }) => {
await wallet.renameAccount(name, newName);
}),
});
Loading

0 comments on commit b4a6847

Please sign in to comment.