summaryrefslogtreecommitdiff
path: root/impls/monero.ts/src/pending_transaction.ts
diff options
context:
space:
mode:
authorMateusz Franik <47059999+Im-Beast@users.noreply.github.com>2024-12-01 15:02:20 +0100
committerGitHub <noreply@github.com>2024-12-01 09:02:20 -0500
commit40c1a1bda4b6f125c702f5a37ecc48a6ebec24b8 (patch)
treef5dece80ded669b856ab875d2814fcb9bf0b157b /impls/monero.ts/src/pending_transaction.ts
parentc41c4dad9aa5003a914cfb2c528c76386f952665 (diff)
feat!: monero.ts rewrite, integration tests (#80)
* feat: move spend/view key symbols to the monero.ts implementation * feat: add integration tests for `0001-polyseed.patch` * feat(monero.ts): add support for backgroundSync and closing the wallet * feat: add integration tests for `0002-wallet-background-sync-with-just-the-view-key.patch` * feat!: require users to provide own node url BREAKING CHANGE: Requires users manual call to `Wallet.initWallet` after wallet creation with preferred node url * feat: add background sync test for `0002-wallet-background-sync-with-just-the-view-key.patch` * ci: add integration tests step * feat(monero.ts): support creating and recovering wallet from polyseed * feat: actually test polyseeds in the integration test * chore: remove legacy comments * fix: uncomment getting moneroC * feat(monero.ts): add support for reading wallet's seed * feat: add seed test for `0009-Add-recoverDeterministicWalletFromSpendKey.patch` * chore: slight refactor * feat(monero.ts): add bindings for `setOffline` and `isOffline` * feat: add integration tests for `0012-WIP-UR-functions.patch` * fix: use correct node depending on the coin * fix: prevent segfaults on wownero * feat(monero.ts): add partial bindings for `Coins` and `CoinsInfo` * feat: add integration tests for `0004-coin-control.patch` * fix coin control * clean up console.logs * chore: comment out the entire block * dev: add devcontainer config for deno * fix(monero.ts): invalid PendingTransactionPtr brand * feat(monero.ts): add bindings for retrieving keys and managing transactions * feat: improve `0012-WIP-UR-functions.patch` tests to follow the airgap doc * fix(monero.ts): make UR methods optional so wownero can load properly * remove flaky balance assertions * tests: add a little bit of delay to make 0002 patch test less flake-y * tests: run wallet transaction tests on ci * enable logging to determine why it segfaults on ci * add delay to every syncBlockchain call * its console logging time * even more console.logs * eep * eep more * dont assert that its not frozen * remove console.logs * fix(monero.ts): type typo becoming a default value * feat(monero.ts): add bindings for `createTransactionMultDest` * feat(monero.ts): support returning multiple values whenever necessary * feat(monero.ts): add missing reexports * feat(monero.ts)!: rewrite bindings BREAKING CHANGES!: - Calls to methods no longer automatically throw errors, you should take care of handling errors yourself - This means the whole sanitizer ordeal is gone, no more sanitize arguments etc. - Some misplaced methods have been moved to their "proper" place, e.g. creating Wallet is now possible using WalletManager instance methods, instead of passing WalletManager instance to Wallet's static method - Return types probably changed in places, methods were inconsitent about returning string or empty string and `string | null`, now its always `string | null` - Every available symbol should now be available in `symbols`, even for the things that are not yet implemented, so you can access them in that case * tests: adapt tests to monero.ts changes * tests: reuse dylib in tests --------- Co-authored-by: cyan <cyjan@mrcyjanek.net>
Diffstat (limited to 'impls/monero.ts/src/pending_transaction.ts')
-rw-r--r--impls/monero.ts/src/pending_transaction.ts117
1 files changed, 57 insertions, 60 deletions
diff --git a/impls/monero.ts/src/pending_transaction.ts b/impls/monero.ts/src/pending_transaction.ts
index 169332f..d80b738 100644
--- a/impls/monero.ts/src/pending_transaction.ts
+++ b/impls/monero.ts/src/pending_transaction.ts
@@ -1,87 +1,84 @@
-import { CString, getSymbol, readCString, type Sanitizer } from "./utils.ts";
+import { fns } from "./bindings.ts";
+import { C_SEPARATOR, CString, maybeMultipleStrings, readCString } from "./utils.ts";
-export type PendingTransactionPtr = Deno.PointerObject<"transactionInfo">;
+export type PendingTransactionPtr = Deno.PointerObject<"pendingTransaction">;
-export class PendingTransaction {
- #pendingTxPtr: PendingTransactionPtr;
- sanitizer?: Sanitizer;
+export class PendingTransaction<MultDest extends boolean = false> {
+ #ptr: PendingTransactionPtr;
- constructor(pendingTxPtr: PendingTransactionPtr, sanitizer?: Sanitizer) {
- this.sanitizer = sanitizer;
- this.#pendingTxPtr = pendingTxPtr;
- }
+ #amount!: bigint;
+ #dust!: bigint;
+ #fee!: bigint;
+ #txid!: string | string[] | null;
+ #txCount!: bigint;
- async status(): Promise<number> {
- return await getSymbol("PendingTransaction_status")(this.#pendingTxPtr);
+ constructor(ptr: PendingTransactionPtr) {
+ this.#ptr = ptr;
}
- async errorString(): Promise<string | null> {
- if (!await this.status()) return null;
+ static async new(ptr: PendingTransactionPtr): Promise<PendingTransaction> {
+ const instance = new PendingTransaction(ptr);
- const error = await getSymbol("PendingTransaction_errorString")(this.#pendingTxPtr);
- if (!error) return null;
+ const [amount, dust, fee, txCount, txid] = await Promise.all([
+ fns.PendingTransaction_amount(ptr),
+ fns.PendingTransaction_dust(ptr),
+ fns.PendingTransaction_fee(ptr),
+ fns.PendingTransaction_txCount(ptr),
+ fns.PendingTransaction_txid(ptr, C_SEPARATOR),
+ ]);
- return await readCString(error) || null;
- }
+ instance.#amount = amount;
+ instance.#dust = dust;
+ instance.#fee = fee;
+ instance.#txCount = txCount;
+ instance.#txid = maybeMultipleStrings(await readCString(txid));
- async throwIfError(sanitize = true): Promise<void> {
- const maybeError = await this.errorString();
- if (maybeError) {
- if (sanitize) this.sanitizer?.();
- throw new Error(maybeError);
- }
+ return instance;
}
- async commit(fileName: string, overwrite: boolean, sanitize = true): Promise<boolean> {
- const bool = await getSymbol("PendingTransaction_commit")(
- this.#pendingTxPtr,
- CString(fileName),
- overwrite,
- );
- await this.throwIfError(sanitize);
- return bool;
+ get amount(): bigint {
+ return this.#amount;
}
- async commitUR(maxFragmentLength: number): Promise<string | null> {
- const commitUR = getSymbol("PendingTransaction_commitUR");
-
- if (!commitUR) {
- return null;
- }
+ get dust(): bigint {
+ return this.#dust;
+ }
- const result = await commitUR(
- this.#pendingTxPtr,
- maxFragmentLength,
- );
+ get fee(): bigint {
+ return this.#fee;
+ }
- if (!result) return null;
- await this.throwIfError();
- return await readCString(result) || null;
+ get txCount(): bigint {
+ return this.#txCount;
}
- async amount(): Promise<bigint> {
- return await getSymbol("PendingTransaction_amount")(this.#pendingTxPtr);
+ async commit(fileName: string, overwrite: boolean): Promise<boolean> {
+ return await fns.PendingTransaction_commit(this.#ptr, CString(fileName), overwrite);
}
- async dust(): Promise<bigint> {
- return await getSymbol("PendingTransaction_dust")(this.#pendingTxPtr);
+ async commitUR(maxFragmentLength: number): Promise<string | null> {
+ const commitUR = fns.PendingTransaction_commitUR;
+ if (!commitUR) return null;
+
+ return await readCString(
+ await commitUR(this.#ptr, maxFragmentLength),
+ );
}
- async fee(): Promise<bigint> {
- return await getSymbol("PendingTransaction_fee")(this.#pendingTxPtr);
+ async status(): Promise<number> {
+ return await fns.PendingTransaction_status(this.#ptr);
}
- async txid(separator: string, sanitize = true): Promise<string | null> {
- const result = await getSymbol("PendingTransaction_txid")(
- this.#pendingTxPtr,
- CString(separator),
- );
- if (!result) return null;
- await this.throwIfError(sanitize);
- return await readCString(result) || null;
+ async errorString(): Promise<string | null> {
+ if (!await this.status()) return null;
+ const error = await fns.PendingTransaction_errorString(this.#ptr);
+ return await readCString(error);
}
- async txCount(): Promise<bigint> {
- return await getSymbol("PendingTransaction_txCount")(this.#pendingTxPtr);
+ async throwIfError(): Promise<void> {
+ const maybeError = await this.errorString();
+ if (maybeError) {
+ throw new Error(maybeError);
+ }
}
}