Skip to content

Commit

Permalink
Finish creating transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
dguenther committed Sep 23, 2024
1 parent e77f460 commit 79c2fac
Show file tree
Hide file tree
Showing 7 changed files with 263 additions and 81 deletions.
30 changes: 28 additions & 2 deletions packages/mobile-app/app/account-settings/account-name.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,45 @@
import { StatusBar } from "expo-status-bar";
import { Button, StyleSheet, Text, TextInput, View } from "react-native";
import { useRouter } from "expo-router";
import { useFacade } from "../../data/facades";
import { useState } from "react";

export default function AccountName() {
const router = useRouter();
const facade = useFacade();

const [newName, setNewName] = useState("");

const activeAccount = facade.getAccount.useQuery({});

const renameAccount = facade.renameAccount.useMutation();

return (
<View style={styles.container}>
<Button title="Back" onPress={() => router.dismiss()} />

<View>
<Text>Account Name</Text>
<TextInput placeholder="Account 1" />
<Text>Current name: {activeAccount.data?.name}</Text>
<TextInput
placeholder="New Name"
value={newName}
onChangeText={setNewName}
/>
</View>
<Button title="Save" />
<Button
title="Save"
onPress={async () => {
if (!activeAccount.data) {
return;
}
await renameAccount.mutateAsync({
name: activeAccount.data?.name,
newName: newName,
});
router.dismissAll();
}}
/>
<StatusBar style="auto" />
</View>
);
Expand Down
68 changes: 62 additions & 6 deletions packages/mobile-app/data/wallet/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ interface NotesTable {
// If storage is an issue, we could fall back to re-decrypting the notes
// to get the full note.
note: Uint8Array;
// The index of the note within the transaction. Notes can be reused, so combining
// the transaction hash and the index provides a unique identifier for a note (aka outpoint).
noteTransactionIndex: number;
// Note index in the merkle tree. Could also be derived from the note size on the
// transaction, but stored here for convenience.
// Can be null for:
Expand Down Expand Up @@ -269,6 +272,10 @@ export class WalletDb {
.addColumn("type", SQLiteType.String, (col) =>
col.notNull().check(sql`type IN ("receive", "send")`),
)
.addUniqueConstraint("accountTransactions_accountId_hash", [
"accountId",
"transactionHash",
])
.execute();
console.log("created account transactions");
},
Expand All @@ -291,6 +298,9 @@ export class WalletDb {
col.notNull().check(sql`network IN ("mainnet", "testnet")`),
)
.addColumn("note", SQLiteType.Blob, (col) => col.notNull())
.addColumn("noteTransactionIndex", SQLiteType.Integer, (col) =>
col.notNull(),
)
.addColumn("position", SQLiteType.Integer)
.addColumn("assetId", SQLiteType.Blob, (col) => col.notNull())
.addColumn("sender", SQLiteType.Blob, (col) => col.notNull())
Expand All @@ -302,6 +312,10 @@ export class WalletDb {
.addColumn("memo", SQLiteType.Blob, (col) => col.notNull())
.addColumn("nullifier", SQLiteType.Blob)
.addColumn("nullifierTransactionHash", SQLiteType.Blob)
.addUniqueConstraint(
"notes_accountId_transactionHash_noteTransactionIndex",
["accountId", "transactionHash", "noteTransactionIndex"],
)
.execute();

await db.schema
Expand Down Expand Up @@ -522,7 +536,9 @@ export class WalletDb {
return await this.db
.updateTable("accounts")
.where("accounts.name", "=", name)
.set("accounts.name", newName)
.set({
name: newName,
})
.executeTakeFirst();
}

Expand Down Expand Up @@ -831,9 +847,9 @@ export class WalletDb {
timestamp: Date;
expirationSequence: number;
fee: string;
ownerNotes: { note: Note }[];
ownerNotes: { note: Note; noteTransactionIndex: number }[];
foundNullifiers: Uint8Array[];
spenderNotes: { note: Note }[];
spenderNotes: { note: Note; noteTransactionIndex: number }[];
}) {
await this.db.transaction().execute(async (db) => {
const balanceDeltas = new BalanceDeltas();
Expand Down Expand Up @@ -895,6 +911,7 @@ export class WalletDb {
network: values.network,
transactionHash: values.hash,
note: new Uint8Array(note.note.serialize()),
noteTransactionIndex: note.noteTransactionIndex,
assetId: new Uint8Array(note.note.assetId()),
owner: Uint8ArrayUtils.fromHex(note.note.owner()),
sender: Uint8ArrayUtils.fromHex(note.note.sender()),
Expand Down Expand Up @@ -924,6 +941,7 @@ export class WalletDb {
network: values.network,
transactionHash: values.hash,
note: new Uint8Array(note.note.serialize()),
noteTransactionIndex: note.noteTransactionIndex,
assetId: new Uint8Array(note.note.assetId()),
owner: Uint8ArrayUtils.fromHex(note.note.owner()),
sender: Uint8ArrayUtils.fromHex(note.note.sender()),
Expand Down Expand Up @@ -958,9 +976,14 @@ export class WalletDb {
blockHash: Uint8Array;
transactions: {
hash: Uint8Array;
ownerNotes: { position: number | null; note: Note; nullifier: string }[];
ownerNotes: {
position: number | null;
note: Note;
nullifier: string;
noteTransactionIndex: number;
}[];
foundNullifiers: Uint8Array[];
spenderNotes: { note: Note }[];
spenderNotes: { note: Note; noteTransactionIndex: number }[];
timestamp: Date;
}[];
}) {
Expand Down Expand Up @@ -1068,15 +1091,25 @@ export class WalletDb {
)
.executeTakeFirstOrThrow();

await db
const r = await db
.insertInto("accountTransactions")
.values({
accountId: values.accountId,
transactionHash: txn.hash,
type: transactionType,
})
// Transaction might already be pending
.onConflict((oc) =>
oc.columns(["accountId", "transactionHash"]).doNothing(),
)
.executeTakeFirstOrThrow();

if (r.insertId) {
console.log("new");
} else {
console.log("update");
}

for (const note of txn.ownerNotes) {
await db
.insertInto("notes")
Expand All @@ -1085,6 +1118,7 @@ export class WalletDb {
network: values.network,
transactionHash: txn.hash,
note: new Uint8Array(note.note.serialize()),
noteTransactionIndex: note.noteTransactionIndex,
assetId: new Uint8Array(note.note.assetId()),
owner: Uint8ArrayUtils.fromHex(note.note.owner()),
sender: Uint8ArrayUtils.fromHex(note.note.sender()),
Expand All @@ -1095,6 +1129,19 @@ export class WalletDb {
nullifier: Uint8ArrayUtils.fromHex(note.nullifier),
nullifierTransactionHash: null,
})
.onConflict((oc) =>
oc
.columns([
"accountId",
"transactionHash",
"noteTransactionIndex",
])
.doUpdateSet({
position: note.position,
nullifier: Uint8ArrayUtils.fromHex(note.nullifier),
nullifierTransactionHash: null,
}),
)
.executeTakeFirst();
}

Expand All @@ -1114,14 +1161,18 @@ export class WalletDb {
network: values.network,
transactionHash: txn.hash,
note: new Uint8Array(note.note.serialize()),
noteTransactionIndex: note.noteTransactionIndex,
assetId: new Uint8Array(note.note.assetId()),
owner: Uint8ArrayUtils.fromHex(note.note.owner()),
sender: Uint8ArrayUtils.fromHex(note.note.sender()),
value: note.note.value().toString(),
valueNum: Number(note.note.value()),
memo: new Uint8Array(note.note.memo()),
position: null,
nullifier: null,
nullifierTransactionHash: null,
})
.onConflict((oc) => oc.doNothing())
.executeTakeFirstOrThrow();
}

Expand Down Expand Up @@ -1167,6 +1218,11 @@ export class WalletDb {
transactionHash: txn.hash,
value: delta[1].toString(),
})
.onConflict((oc) =>
oc
.columns(["accountId", "transactionHash", "assetId"])
.doNothing(),
)
.executeTakeFirstOrThrow();
}
}
Expand Down
Loading

0 comments on commit 79c2fac

Please sign in to comment.