From 40c1a1bda4b6f125c702f5a37ecc48a6ebec24b8 Mon Sep 17 00:00:00 2001 From: Mateusz Franik <47059999+Im-Beast@users.noreply.github.com> Date: Sun, 1 Dec 2024 15:02:20 +0100 Subject: 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 --- impls/monero.ts/src/wallet_manager.ts | 138 ++++++++++++++++++++++++++++++++-- 1 file changed, 130 insertions(+), 8 deletions(-) (limited to 'impls/monero.ts/src/wallet_manager.ts') diff --git a/impls/monero.ts/src/wallet_manager.ts b/impls/monero.ts/src/wallet_manager.ts index b1fafec..45fd0c9 100644 --- a/impls/monero.ts/src/wallet_manager.ts +++ b/impls/monero.ts/src/wallet_manager.ts @@ -1,26 +1,148 @@ -import { getSymbol, Sanitizer } from "./utils.ts"; +import { fns } from "./bindings.ts"; +import { CString } from "./utils.ts"; +import { Wallet, WalletPtr } from "./wallet.ts"; export type WalletManagerPtr = Deno.PointerObject<"walletManager">; export class WalletManager { #ptr: WalletManagerPtr; - sanitizer?: Sanitizer; - constructor(walletManagerPtr: WalletManagerPtr, sanitizer?: Sanitizer) { + constructor(walletManagerPtr: WalletManagerPtr) { this.#ptr = walletManagerPtr; - this.sanitizer = sanitizer; } getPointer(): WalletManagerPtr { return this.#ptr; } - static async new(sanitizer?: Sanitizer) { - const ptr = await getSymbol("WalletManagerFactory_getWalletManager")(); + static async new() { + const ptr = await fns.WalletManagerFactory_getWalletManager(); if (!ptr) { - sanitizer?.(); throw new Error("Failed retrieving wallet manager"); } - return new WalletManager(ptr as WalletManagerPtr, sanitizer); + + return new WalletManager(ptr as WalletManagerPtr); + } + + async setDaemonAddress(address: string): Promise { + return await fns.WalletManager_setDaemonAddress(this.#ptr, CString(address)); + } + + async createWallet(path: string, password: string): Promise { + const walletPtr = await fns.WalletManager_createWallet( + this.#ptr, + CString(path), + CString(password), + CString("English"), + 0, + ); + + const wallet = new Wallet(this, walletPtr as WalletPtr); + await wallet.throwIfError(); + return wallet; + } + + async openWallet(path: string, password: string): Promise { + const walletPtr = await fns.WalletManager_openWallet( + this.#ptr, + CString(path), + CString(password), + 0, + ); + + const wallet = new Wallet(this, walletPtr as WalletPtr); + await wallet.throwIfError(); + return wallet; + } + + async recoverWallet( + path: string, + password: string, + mnemonic: string, + restoreHeight: bigint, + seedOffset: string = "", + ): Promise { + const walletPtr = await fns.WalletManager_recoveryWallet( + this.#ptr, + CString(path), + CString(password), + CString(mnemonic), + 0, + restoreHeight, + 1n, + CString(seedOffset), + ); + + const wallet = new Wallet(this, walletPtr as WalletPtr); + await wallet.throwIfError(); + return wallet; + } + + async recoverFromPolyseed( + path: string, + password: string, + mnemonic: string, + restoreHeight: bigint, + passphrase = "", + ): Promise { + return await this.createFromPolyseed( + path, + password, + mnemonic, + restoreHeight, + passphrase, + false, + ); + } + + async createFromPolyseed( + path: string, + password: string, + mnemonic: string, + restoreHeight: bigint, + passphrase = "", + newWallet = true, + ): Promise { + const walletPtr = await fns.WalletManager_createWalletFromPolyseed( + this.#ptr, + CString(path), + CString(password), + 0, + CString(mnemonic), + CString(passphrase), + newWallet, + restoreHeight, + 1n, + ); + + const wallet = new Wallet(this, walletPtr as WalletPtr); + await wallet.throwIfError(); + return wallet; + } + + async recoverFromKeys( + path: string, + password: string, + restoreHeight: bigint, + address: string, + viewKey: string, + spendKey: string, + ): Promise { + const walletPtr = await fns.WalletManager_createWalletFromKeys( + this.#ptr, + CString(path), + CString(password), + CString("English"), + 0, + restoreHeight, + CString(address), + CString(viewKey), + CString(spendKey), + 0n, + ); + + const wallet = new Wallet(this, walletPtr as WalletPtr); + await wallet.throwIfError(); + return wallet; } } -- cgit v1.2.3