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.ts | 446 ++++++++++++++++++++++-------------------- 1 file changed, 234 insertions(+), 212 deletions(-) (limited to 'impls/monero.ts/src/wallet.ts') diff --git a/impls/monero.ts/src/wallet.ts b/impls/monero.ts/src/wallet.ts index ea25f21..673ccab 100644 --- a/impls/monero.ts/src/wallet.ts +++ b/impls/monero.ts/src/wallet.ts @@ -1,294 +1,268 @@ -import { dylib } from "./bindings.ts"; -import { CString, getSymbol, readCString, Sanitizer } from "./utils.ts"; +import { WalletManager } from "./wallet_manager.ts"; -import { WalletManager, type WalletManagerPtr } from "./wallet_manager.ts"; -import { TransactionHistory, TransactionHistoryPtr } from "./transaction_history.ts"; -import { PendingTransaction } from "./pending_transaction.ts"; -import { PendingTransactionPtr } from "./pending_transaction.ts"; +import { C_SEPARATOR, CString, readCString, SEPARATOR } from "./utils.ts"; +import { PendingTransaction, PendingTransactionPtr } from "./pending_transaction.ts"; +import { UnsignedTransaction, UnsignedTransactionPtr } from "./unsigned_transaction.ts"; +import { Coins, CoinsPtr } from "./coins.ts"; +import { fns } from "./bindings.ts"; export type WalletPtr = Deno.PointerObject<"walletManager">; +interface DaemonInfo { + address?: string; + username?: string; + password?: string; + lightWallet?: boolean; + proxyAddress?: string; +} + export class Wallet { - #walletManagerPtr: WalletManagerPtr; - #walletPtr: WalletPtr; - sanitizer?: Sanitizer; + #walletManager: WalletManager; + #ptr: WalletPtr; - constructor(walletManagerPtr: WalletManager, walletPtr: WalletPtr, sanitizer?: Sanitizer) { - this.#walletPtr = walletPtr; - this.#walletManagerPtr = walletManagerPtr.getPointer(); - this.sanitizer = sanitizer; + constructor(walletManager: WalletManager, ptr: WalletPtr) { + this.#walletManager = walletManager; + this.#ptr = ptr; } - getPointer(): WalletPtr { - return this.#walletPtr; + getPointer() { + return this.#ptr; } - async store(path = ""): Promise { - const bool = await getSymbol("Wallet_store")(this.#walletPtr, CString(path)); - await this.throwIfError(); - return bool; - } + async init(daemonInfo: DaemonInfo, log = false): Promise { + const success = await fns.Wallet_init( + this.#ptr, + CString(daemonInfo.address ?? ""), + 0n, + CString(daemonInfo.username ?? ""), + CString(daemonInfo.password ?? ""), + false, + daemonInfo.lightWallet ?? false, + CString(daemonInfo.proxyAddress ?? ""), + ); + + if (log) { + await fns.Wallet_init3( + this.#ptr, + CString(""), + CString(""), + CString(""), + true, + ); + } - async initWallet(daemonAddress = "http://nodex.monerujo.io:18081"): Promise { - await this.init(); await this.setTrustedDaemon(true); - await this.setDaemonAddress(daemonAddress); await this.startRefresh(); await this.refreshAsync(); - await this.throwIfError(); + + return success; } - async setDaemonAddress(address: string): Promise { - await getSymbol("WalletManager_setDaemonAddress")( - this.#walletManagerPtr, - CString(address), - ); + async setTrustedDaemon(value: boolean): Promise { + return await fns.Wallet_setTrustedDaemon(this.#ptr, value); } async startRefresh(): Promise { - await getSymbol("Wallet_startRefresh")(this.#walletPtr); - await this.throwIfError(); + return await fns.Wallet_startRefresh(this.#ptr); } async refreshAsync(): Promise { - await getSymbol("Wallet_refreshAsync")(this.#walletPtr); - await this.throwIfError(); - } - - async init(): Promise { - const bool = await getSymbol("Wallet_init")( - this.#walletPtr, - CString("http://nodex.monerujo.io:18081"), - 0n, - CString(""), - CString(""), - false, - false, - CString(""), + return await fns.Wallet_refreshAsync(this.#ptr); + } + + async setupBackgroundSync( + backgroundSyncType: number, + walletPassword: string, + backgroundCachePassword: string, + ): Promise { + return await fns.Wallet_setupBackgroundSync( + this.#ptr, + backgroundSyncType, + CString(walletPassword), + CString(backgroundCachePassword), ); - await this.throwIfError(); - return bool; } - async setTrustedDaemon(value: boolean): Promise { - await getSymbol("Wallet_setTrustedDaemon")(this.#walletPtr, value); - } - - static async create( - walletManager: WalletManager, - path: string, - password: string, - sanitizeError = true, - ): Promise { - // We assign holder of the pointer in Wallet constructor - const walletManagerPtr = walletManager.getPointer(); - - const walletPtr = await getSymbol("WalletManager_createWallet")( - walletManagerPtr, - CString(path), - CString(password), - CString("English"), - 0, - ); + async startBackgroundSync(): Promise { + return await fns.Wallet_startBackgroundSync(this.#ptr); + } - const wallet = new Wallet(walletManager, walletPtr as WalletPtr, walletManager.sanitizer); - await wallet.throwIfError(sanitizeError); - await wallet.initWallet(); + async stopBackgroundSync(walletPassword: string): Promise { + return await fns.Wallet_stopBackgroundSync(this.#ptr, CString(walletPassword)); + } - return wallet; + async store(path = ""): Promise { + return await fns.Wallet_store(this.#ptr, CString(path)); } - static async open( - walletManager: WalletManager, - path: string, - password: string, - sanitizeError = true, - ): Promise { - // We assign holder of the pointer in Wallet constructor - const walletManagerPtr = walletManager.getPointer(); + async close(store: boolean): Promise { + return await fns.WalletManager_closeWallet(this.#walletManager.getPointer(), this.#ptr, store); + } - const walletPtr = await getSymbol("WalletManager_openWallet")( - walletManagerPtr, - CString(path), - CString(password), - 0, + async seed(offset = ""): Promise { + return await readCString( + await fns.Wallet_seed(this.#ptr, CString(offset)), ); + } - const wallet = new Wallet(walletManager, walletPtr as WalletPtr, walletManager.sanitizer); - await wallet.throwIfError(sanitizeError); - await wallet.initWallet(); - - return wallet; - } - - static async recover( - walletManager: WalletManager, - path: string, - password: string, - mnemonic: string, - restoreHeight: bigint, - seedOffset: string = "", - sanitizeError = true, - ): Promise { - // We assign holder of the pointer in Wallet constructor - const walletManagerPtr = walletManager.getPointer(); - - const walletPtr = await getSymbol("WalletManager_recoveryWallet")( - walletManagerPtr, - CString(path), - CString(password), - CString(mnemonic), - 0, - restoreHeight, - 1n, - CString(seedOffset), + async address(accountIndex = 0n, addressIndex = 0n): Promise { + return await readCString( + await fns.Wallet_address(this.#ptr, accountIndex, addressIndex), ); + } - const wallet = new Wallet(walletManager, walletPtr as WalletPtr, walletManager.sanitizer); - await wallet.throwIfError(sanitizeError); - await wallet.initWallet(); + async balance(accountIndex = 0): Promise { + return await fns.Wallet_balance(this.#ptr, accountIndex); + } - return wallet; + async unlockedBalance(accountIndex = 0): Promise { + return await fns.Wallet_unlockedBalance(this.#ptr, accountIndex); } - async address(accountIndex = 0n, addressIndex = 0n): Promise { - const address = await getSymbol("Wallet_address")(this.#walletPtr, accountIndex, addressIndex); - if (!address) { - const error = await this.errorString(); - throw new Error(`Failed getting address from a wallet: ${error ?? ""}`); - } - return await readCString(address); + async synchronized(): Promise { + return await fns.Wallet_synchronized(this.#ptr); } - async balance(accountIndex = 0): Promise { - return await getSymbol("Wallet_balance")(this.#walletPtr, accountIndex); + async blockChainHeight(): Promise { + return await fns.Wallet_blockChainHeight(this.#ptr); } - async unlockedBalance(accountIndex = 0): Promise { - return await getSymbol("Wallet_unlockedBalance")(this.#walletPtr, accountIndex); + async daemonBlockChainHeight(): Promise { + return await fns.Wallet_daemonBlockChainHeight(this.#ptr); } - status(): Promise { - return getSymbol("Wallet_status")(this.#walletPtr); + async addSubaddressAccount(label: string): Promise { + return await fns.Wallet_addSubaddressAccount(this.#ptr, CString(label)); } - async errorString(): Promise { - if (!await this.status()) return null; + async numSubaddressAccounts(): Promise { + return await fns.Wallet_numSubaddressAccounts(this.#ptr); + } - const error = await getSymbol("Wallet_errorString")(this.#walletPtr); - if (!error) return null; + async addSubaddress(accountIndex: number, label: string): Promise { + return await fns.Wallet_addSubaddress( + this.#ptr, + accountIndex, + CString(label), + ); + } - return await readCString(error) || null; + async numSubaddresses(accountIndex: number): Promise { + return await fns.Wallet_numSubaddresses( + this.#ptr, + accountIndex, + ); } - async throwIfError(sanitize = true): Promise { - const maybeError = await this.errorString(); - if (maybeError) { - if (sanitize) this.sanitizer?.(); - throw new Error(maybeError); - } + async getSubaddressLabel(accountIndex: number, addressIndex: number): Promise { + return await readCString( + await fns.Wallet_getSubaddressLabel(this.#ptr, accountIndex, addressIndex), + ); } - async synchronized(): Promise { - const synchronized = await getSymbol("Wallet_synchronized")(this.#walletPtr); - await this.throwIfError(); - return synchronized; + async setSubaddressLabel(accountIndex: number, addressIndex: number, label: string): Promise { + return await fns.Wallet_setSubaddressLabel(this.#ptr, accountIndex, addressIndex, CString(label)); } - async blockChainHeight(): Promise { - const height = await getSymbol("Wallet_blockChainHeight")(this.#walletPtr); - await this.throwIfError(); - return height; + async isOffline(): Promise { + return await fns.Wallet_isOffline(this.#ptr); } - async daemonBlockChainHeight(): Promise { - const height = await getSymbol("Wallet_daemonBlockChainHeight")(this.#walletPtr); - await this.throwIfError(); - return height; + async setOffline(offline: boolean): Promise { + return await fns.Wallet_setOffline(this.#ptr, offline); } - async managerBlockChainHeight(): Promise { - const height = await getSymbol("WalletManager_blockchainHeight")(this.#walletManagerPtr); - await this.throwIfError(); - return height; + async publicViewKey(): Promise { + return await readCString(await fns.Wallet_publicViewKey(this.#ptr)); } - async managerTargetBlockChainHeight(): Promise { - const height = await getSymbol("WalletManager_blockchainTargetHeight")(this.#walletManagerPtr); - await this.throwIfError(); - return height; + async secretViewKey(): Promise { + return await readCString(await fns.Wallet_secretViewKey(this.#ptr)); } - async addSubaddressAccount(label: string): Promise { - await getSymbol("Wallet_addSubaddressAccount")( - this.#walletPtr, - CString(label), - ); - await this.throwIfError(); + async publicSpendKey(): Promise { + return await readCString(await fns.Wallet_publicSpendKey(this.#ptr)); } - async numSubaddressAccounts(): Promise { - const accountsLen = await getSymbol("Wallet_numSubaddressAccounts")(this.#walletPtr); - await this.throwIfError(); - return accountsLen; + async secretSpendKey(): Promise { + return await readCString(await fns.Wallet_secretSpendKey(this.#ptr)); } - async addSubaddress(accountIndex: number, label: string): Promise { - await getSymbol("Wallet_addSubaddress")( - this.#walletPtr, - accountIndex, - CString(label), - ); - await this.throwIfError(); + async exportOutputs(fileName: string, all: boolean): Promise { + return await fns.Wallet_exportOutputs(this.#ptr, CString(fileName), all); } - async numSubaddresses(accountIndex: number): Promise { - const address = await getSymbol("Wallet_numSubaddresses")( - this.#walletPtr, - accountIndex, + async exportOutputsUR(maxFragmentLength: bigint, all: boolean): Promise { + const exportOutputsUR = fns.Wallet_exportOutputsUR; + if (!exportOutputsUR) return null; + + return await readCString( + await exportOutputsUR(this.#ptr, maxFragmentLength, all), ); - await this.throwIfError(); - return address; } - async getSubaddressLabel(accountIndex: number, addressIndex: number): Promise { - const label = await getSymbol("Wallet_getSubaddressLabel")(this.#walletPtr, accountIndex, addressIndex); - if (!label) { - const error = await this.errorString(); - throw new Error(`Failed getting subaddress label from a wallet: ${error ?? ""}`); - } - return await readCString(label); + async importOutputs(fileName: string): Promise { + return await fns.Wallet_importOutputs(this.#ptr, CString(fileName)); } - async setSubaddressLabel(accountIndex: number, addressIndex: number, label: string): Promise { - await getSymbol("Wallet_setSubaddressLabel")( - this.#walletPtr, - accountIndex, - addressIndex, - CString(label), + async importOutputsUR(input: string): Promise { + const importOutputsUR = fns.Wallet_importOutputsUR; + if (!importOutputsUR) return null; + + return await importOutputsUR(this.#ptr, CString(input)); + } + + async exportKeyImages(fileName: string, all: boolean): Promise { + return await fns.Wallet_exportKeyImages(this.#ptr, CString(fileName), all); + } + + async exportKeyImagesUR(maxFragmentLength: bigint, all: boolean): Promise { + const exportKeyImagesUR = fns.Wallet_exportKeyImagesUR; + if (!exportKeyImagesUR) return null; + + return await readCString( + await exportKeyImagesUR(this.#ptr, maxFragmentLength, all), ); - await this.throwIfError(); } - async getHistory(): Promise { - const transactionHistoryPointer = await getSymbol("Wallet_history")(this.#walletPtr); - await this.throwIfError(); - return new TransactionHistory(transactionHistoryPointer as TransactionHistoryPtr); + async importKeyImages(fileName: string): Promise { + return await fns.Wallet_importKeyImages(this.#ptr, CString(fileName)); + } + + async importKeyImagesUR(input: string): Promise { + const importKeyImagesUR = fns.Wallet_importKeyImagesUR; + if (!importKeyImagesUR) return null; + + return await importKeyImagesUR(this.#ptr, CString(input)); + } + + async loadUnsignedTx(fileName: string): Promise { + const pendingTxPtr = await fns.Wallet_loadUnsignedTx(this.#ptr, CString(fileName)); + return UnsignedTransaction.new(pendingTxPtr as UnsignedTransactionPtr); + } + + async loadUnsignedTxUR(input: string): Promise { + const loadUnsignedTxUR = fns.Wallet_loadUnsignedTxUR; + if (!loadUnsignedTxUR) return null; + + const pendingTxPtr = await loadUnsignedTxUR(this.#ptr, CString(input)); + if (await this.status()) { + throw this.errorString(); + } + return UnsignedTransaction.new(pendingTxPtr as UnsignedTransactionPtr); } async createTransaction( destinationAddress: string, amount: bigint, - pendingTransactionPriority = 0 | 1 | 2 | 3, + pendingTransactionPriority: 0 | 1 | 2 | 3, subaddressAccount: number, - sanitize = true, prefferedInputs = "", mixinCount = 0, paymentId = "", - separator = ",", - ): Promise { - const pendingTxPtr = await getSymbol("Wallet_createTransaction")( - this.#walletPtr, + ): Promise { + const pendingTxPtr = await fns.Wallet_createTransaction( + this.#ptr, CString(destinationAddress), CString(paymentId), amount, @@ -296,13 +270,61 @@ export class Wallet { pendingTransactionPriority, subaddressAccount, CString(prefferedInputs), - CString(separator), + C_SEPARATOR, + ); + + if (!pendingTxPtr) return null; + return PendingTransaction.new(pendingTxPtr as PendingTransactionPtr); + } + + async createTransactionMultDest( + destinationAddresses: string[], + amounts: bigint[], + amountSweepAll: boolean, + pendingTransactionPriority: 0 | 1 | 2 | 3, + subaddressAccount: number, + preferredInputs: string[] = [], + mixinCount = 0, + paymentId = "", + ): Promise { + const pendingTxPtr = await fns.Wallet_createTransactionMultDest( + this.#ptr, + CString(destinationAddresses.join(SEPARATOR)), + C_SEPARATOR, + CString(paymentId), + amountSweepAll, + CString(amounts.join(SEPARATOR)), + C_SEPARATOR, + mixinCount, + pendingTransactionPriority, + subaddressAccount, + CString(preferredInputs.join(SEPARATOR)), + C_SEPARATOR, ); - await this.throwIfError(sanitize); - return new PendingTransaction(pendingTxPtr as PendingTransactionPtr); + return PendingTransaction.new(pendingTxPtr as PendingTransactionPtr); + } + + async coins(): Promise { + const coinsPtr = await fns.Wallet_coins(this.#ptr); + if (!coinsPtr) return null; + + return new Coins(coinsPtr as CoinsPtr); + } + + async status(): Promise { + return await fns.Wallet_status(this.#ptr); + } + + async errorString(): Promise { + if (!await this.status()) return null; + const error = await fns.Wallet_errorString(this.#ptr); + return await readCString(error); } - async amountFromString(amount: string): Promise { - return await getSymbol("Wallet_amountFromString")(CString(amount)); + async throwIfError(): Promise { + const maybeError = await this.errorString(); + if (maybeError) { + throw new Error(maybeError); + } } } -- cgit v1.2.3 From 85770ea6f1dab106754675f40e1334e575e03646 Mon Sep 17 00:00:00 2001 From: Mateusz Franik <47059999+Im-Beast@users.noreply.github.com> Date: Mon, 30 Dec 2024 10:39:28 +0100 Subject: tests: run integration and regression tests on other platforms (#93) * tests: add script to download test dependencies from fallback mirrors * tests: use the new download_deps script, run tests on macos * ci: download proper artifact for macos * chore: make download_deps script download everything to dir named `monero_c` when ran directly * tests: await downloading deps * tests download proper monero_c version in prepareMoneroC * tests: fix typos * tests: add file data for more targets * tests: print why retrieving tags failed * chore: change mirror url endpoint from `monero_c` to `download_mirror` * tests: use cached releases endpoint to prevent ratelimits * ci: remove brew@1.76 dependency * tests: fix macos dylib path * feat!(monero.ts): make `createTransactionMultDest` optionally return `null` * feat(monero.ts): make `Wallet_reconnectDevice` symbol optional * tests: don't try to extract file if out already exists * tests: remove unnecesary directory rm calls * ci: set regression tests to use canary --- .github/workflows/full_check.yaml | 67 ++++++++++- .gitignore | 4 +- docs/Writerside/topics/macOS.md | 12 +- impls/monero.ts/src/symbols.ts | 2 + impls/monero.ts/src/wallet.ts | 6 +- tests/compare.ts | 17 +-- tests/deno.lock | 32 ++---- tests/download_deps.ts | 235 ++++++++++++++++++++++++++++++++++++++ tests/integration.test.ts | 28 +---- tests/regression.test.ts | 9 +- tests/utils.ts | 164 ++++++++++++++++++-------- 11 files changed, 451 insertions(+), 125 deletions(-) create mode 100644 tests/download_deps.ts (limited to 'impls/monero.ts/src/wallet.ts') diff --git a/.github/workflows/full_check.yaml b/.github/workflows/full_check.yaml index 2f73515..e083dd6 100644 --- a/.github/workflows/full_check.yaml +++ b/.github/workflows/full_check.yaml @@ -271,7 +271,7 @@ jobs: xcode-version: '15.4' - name: install dependencies run: | - brew install ccache unbound boost@1.76 zmq autoconf automake libtool && brew link boost@1.76 + brew install ccache unbound zmq autoconf automake libtool - name: Patch sources run: | git config --global --add safe.directory '*' @@ -404,7 +404,8 @@ jobs: cd impls/monero.ts deno run --unstable-ffi --allow-ffi checksum.ts - regression_check: + regression_tests_linux: + name: linux regression tests strategy: fail-fast: false matrix: @@ -431,7 +432,35 @@ jobs: - name: Run regression tests run: COIN="${{ matrix.coin }}" deno test -A tests/regression.test.ts - integration_check: + regression_tests_macos: + name: macos regression tests + strategy: + matrix: + coin: [monero, wownero] + needs: [ + lib_macos + ] + runs-on: macos-14 + steps: + - uses: denoland/setup-deno@v2 + with: + deno-version: canary + + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + submodules: recursive + + - uses: actions/download-artifact@v4 + with: + name: macos ${{ matrix.coin }} + path: release/${{ matrix.coin }} + + - name: Run regression tests + run: COIN="${{ matrix.coin }}" deno test -A tests/regression.test.ts + + integration_tests_linux: + name: linux integration tests strategy: matrix: coin: [monero, wownero] @@ -462,6 +491,38 @@ jobs: SECRET_WALLET_RESTORE_HEIGHT: ${{ secrets.SECRET_WALLET_RESTORE_HEIGHT }} + integration_tests_macos: + name: macos integration tests + strategy: + matrix: + coin: [monero, wownero] + needs: [ + lib_macos + ] + runs-on: macos-14 + steps: + - uses: denoland/setup-deno@v2 + with: + deno-version: v2.x + + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + submodules: recursive + + - uses: actions/download-artifact@v4 + with: + name: macos ${{ matrix.coin }} + path: release/${{ matrix.coin }} + + - name: Run integration tests + run: COIN="${{ matrix.coin }}" deno test -A tests/integration.test.ts + env: + SECRET_WALLET_PASSWORD: ${{ secrets.SECRET_WALLET_PASSWORD }} + SECRET_WALLET_MNEMONIC: ${{ secrets.SECRET_WALLET_MNEMONIC }} + SECRET_WALLET_RESTORE_HEIGHT: ${{ secrets.SECRET_WALLET_RESTORE_HEIGHT }} + + comment_pr: name: comment on pr runs-on: ubuntu-latest diff --git a/.gitignore b/.gitignore index 9159f7d..7dc5983 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,4 @@ release/ build/ -tests/monero-cli -tests/wownero-cli -tests/libs +tests/dependencies tests/wallets diff --git a/docs/Writerside/topics/macOS.md b/docs/Writerside/topics/macOS.md index 367893b..52b6be8 100644 --- a/docs/Writerside/topics/macOS.md +++ b/docs/Writerside/topics/macOS.md @@ -11,17 +11,15 @@ Building on linux has been tested on $ # install xcode 15.4 (or current latest) -$ brew install ccache unbound boost@1.76 zmq autoconf automake libtool -$ brew link boost@1.76 +$ brew install ccache unbound zmq autoconf automake libtool $ # install xcode 15.4 (or current latest) -$ brew install ccache unbound boost@1.76 zmq autoconf automake libtool -$ brew link boost@1.76 -$ arch -x86_64 brew install ccache unbound boost@1.76 zmq autoconf automake libtool -$ arch -x86_64 brew link boost@1.76 +$ brew install ccache unbound zmq autoconf automake libtool +$ arch -x86_64 brew install ccache unbound zmq autoconf automake libtool +$ arch -x86_64 brew link @@ -83,4 +81,4 @@ $ ./build_single.sh monero aarch64-apple-darwin-11 -j$(nproc) ### Creating fat library -[Check cake_wallet solution](https://github.com/cake-tech/cake_wallet/blob/main/scripts/macos/build_monero_all.sh) \ No newline at end of file +[Check cake_wallet solution](https://github.com/cake-tech/cake_wallet/blob/main/scripts/macos/build_monero_all.sh) diff --git a/impls/monero.ts/src/symbols.ts b/impls/monero.ts/src/symbols.ts index 2c34a6e..91d95b2 100644 --- a/impls/monero.ts/src/symbols.ts +++ b/impls/monero.ts/src/symbols.ts @@ -1548,6 +1548,7 @@ export const moneroSymbols = { ], }, MONERO_Wallet_createTransactionMultDest: { + optional: true, nonblocking: true, result: "pointer", parameters: [ @@ -1956,6 +1957,7 @@ export const moneroSymbols = { ], }, MONERO_Wallet_reconnectDevice: { + optional: true, nonblocking: true, result: "bool", parameters: ["pointer"] as [ diff --git a/impls/monero.ts/src/wallet.ts b/impls/monero.ts/src/wallet.ts index 673ccab..92832da 100644 --- a/impls/monero.ts/src/wallet.ts +++ b/impls/monero.ts/src/wallet.ts @@ -286,8 +286,8 @@ export class Wallet { preferredInputs: string[] = [], mixinCount = 0, paymentId = "", - ): Promise { - const pendingTxPtr = await fns.Wallet_createTransactionMultDest( + ): Promise { + const pendingTxPtr = await fns.Wallet_createTransactionMultDest?.( this.#ptr, CString(destinationAddresses.join(SEPARATOR)), C_SEPARATOR, @@ -301,6 +301,8 @@ export class Wallet { CString(preferredInputs.join(SEPARATOR)), C_SEPARATOR, ); + + if (!pendingTxPtr) return null; return PendingTransaction.new(pendingTxPtr as PendingTransactionPtr); } diff --git a/tests/compare.ts b/tests/compare.ts index 2fd27b8..8c13fc5 100755 --- a/tests/compare.ts +++ b/tests/compare.ts @@ -1,24 +1,13 @@ import { assertEquals } from "jsr:@std/assert"; -import { - loadMoneroDylib, - loadWowneroDylib, - moneroSymbols, - WalletManager, - wowneroSymbols, -} from "../impls/monero.ts/mod.ts"; +import { WalletManager } from "../impls/monero.ts/mod.ts"; +import { loadDylib } from "./utils.ts"; const coin = Deno.args[0] as "monero" | "wownero"; const version = Deno.args[1]; const walletInfo = JSON.parse(Deno.args[2]); -if (coin === "monero") { - const dylib = Deno.dlopen(`tests/libs/${version}/monero_libwallet2_api_c.so`, moneroSymbols); - loadMoneroDylib(dylib); -} else { - const dylib = Deno.dlopen(`tests/libs/${version}/wownero_libwallet2_api_c.so`, wowneroSymbols); - loadWowneroDylib(dylib); -} +loadDylib(coin, version); const walletManager = await WalletManager.new(); const wallet = await walletManager.openWallet(walletInfo.path, walletInfo.password); diff --git a/tests/deno.lock b/tests/deno.lock index b67d77d..5ed1a7a 100644 --- a/tests/deno.lock +++ b/tests/deno.lock @@ -9,14 +9,12 @@ "jsr:@std/bytes@0.221": "0.221.0", "jsr:@std/fmt@0.221": "0.221.0", "jsr:@std/fmt@1": "1.0.2", - "jsr:@std/fs@1": "1.0.4", + "jsr:@std/fs@1": "1.0.5", "jsr:@std/io@0.221": "0.221.0", - "jsr:@std/path@1": "1.0.6", - "jsr:@std/path@1.0.8": "1.0.8", - "jsr:@std/path@^1.0.6": "1.0.6", - "jsr:@std/streams@0.221": "0.221.0", - "jsr:@std/streams@^1.0.7": "1.0.7", - "jsr:@std/tar@*": "0.1.2" + "jsr:@std/path@*": "1.0.8", + "jsr:@std/path@1": "1.0.8", + "jsr:@std/path@^1.0.7": "1.0.8", + "jsr:@std/streams@0.221": "0.221.0" }, "jsr": { "@david/dax@0.42.0": { @@ -28,7 +26,7 @@ "jsr:@std/fs", "jsr:@std/io", "jsr:@std/path@1", - "jsr:@std/streams@0.221" + "jsr:@std/streams" ] }, "@david/path@0.2.0": { @@ -56,10 +54,10 @@ "@std/fmt@1.0.2": { "integrity": "87e9dfcdd3ca7c066e0c3c657c1f987c82888eb8103a3a3baa62684ffeb0f7a7" }, - "@std/fs@1.0.4": { - "integrity": "2907d32d8d1d9e540588fd5fe0ec21ee638134bd51df327ad4e443aaef07123c", + "@std/fs@1.0.5": { + "integrity": "41806ad6823d0b5f275f9849a2640d87e4ef67c51ee1b8fb02426f55e02fd44e", "dependencies": [ - "jsr:@std/path@^1.0.6" + "jsr:@std/path@^1.0.7" ] }, "@std/io@0.221.0": { @@ -69,9 +67,6 @@ "jsr:@std/bytes" ] }, - "@std/path@1.0.6": { - "integrity": "ab2c55f902b380cf28e0eec501b4906e4c1960d13f00e11cfbcd21de15f18fed" - }, "@std/path@1.0.8": { "integrity": "548fa456bb6a04d3c1a1e7477986b6cffbce95102d0bb447c67c4ee70e0364be" }, @@ -80,15 +75,6 @@ "dependencies": [ "jsr:@std/io" ] - }, - "@std/streams@1.0.7": { - "integrity": "1a93917ca0c58c01b2bfb93647189229b1702677f169b6fb61ad6241cd2e499b" - }, - "@std/tar@0.1.2": { - "integrity": "98183102395decd6268253996177804f818580ef547a25b81da0e7cc334db708", - "dependencies": [ - "jsr:@std/streams@^1.0.7" - ] } } } diff --git a/tests/download_deps.ts b/tests/download_deps.ts new file mode 100644 index 0000000..808640e --- /dev/null +++ b/tests/download_deps.ts @@ -0,0 +1,235 @@ +import { join, resolve } from "jsr:@std/path"; +import { Coin, getMoneroCTags } from "./utils.ts"; + +export type Target = `${typeof Deno["build"]["os"]}_${typeof Deno["build"]["arch"]}`; + +interface FileInfo { + overrideMirrors?: string[]; + name: string; + sha256?: string; +} + +interface DownloadInfo { + mirrors: string[]; + file: + | FileInfo + | { [os in Target]?: FileInfo }; + outDir?: string; +} + +export function getFileInfo( + downloadInfo: DownloadInfo, + target: Target = `${Deno.build.os}_${Deno.build.arch}`, +): FileInfo { + const fileInfo = "name" in downloadInfo.file ? downloadInfo.file : downloadInfo.file[target]; + if (!fileInfo) { + throw new Error(`No fileInfo set for target: ${target}`); + } + return fileInfo; +} + +async function sha256(buffer: Uint8Array): Promise { + const hashed = new Uint8Array(await crypto.subtle.digest("SHA-256", buffer)); + return Array.from(hashed).map((i) => i.toString(16).padStart(2, "0")).join(""); +} + +export async function downloadDependencies(...infos: DownloadInfo[]): Promise { + return await downloadFiles("./tests/dependencies", `${Deno.build.os}_${Deno.build.arch}`, ...infos); +} + +export async function downloadFiles(outDir: string, target: Target, ...infos: DownloadInfo[]): Promise { + try { + await Deno.mkdir(outDir, { recursive: true }); + } catch (error) { + if (!(error instanceof Deno.errors.AlreadyExists)) { + throw error; + } + } + + for (const info of infos) { + const fileInfo = getFileInfo(info, target); + const fileName = fileInfo.name; + const filePath = join(outDir, info.outDir ?? "", fileName); + + file_might_exist: try { + const fileBuffer = await Deno.readFile(filePath); + + // File exists, make sure checksum matches + if (fileInfo.sha256) { + const fileChecksum = await sha256(fileBuffer); + if (fileChecksum !== fileInfo.sha256) { + console.log( + `File ${fileName} already exists, but checksum is mismatched (${fileChecksum} != ${fileInfo.sha256}), redownloading`, + ); + await Deno.remove(filePath); + break file_might_exist; + } + } + + console.log(`File ${fileName} already exists, skipping`); + continue; + } catch { /**/ } + + let buffer: Uint8Array | undefined; + + for (const mirror of fileInfo.overrideMirrors ?? info.mirrors) { + const url = `${mirror}/${fileName}`; + + const response = await fetch(url); + if (!response.ok) { + console.warn(`Could not reach file ${fileName} on mirror: ${mirror}`); + await response.body?.cancel(); + continue; + } + + const responseBuffer = await response.bytes(); + + if (fileInfo.sha256) { + const responseChecksum = await sha256(responseBuffer); + if (responseChecksum !== fileInfo.sha256) { + console.warn( + `Checksum mismatch on file ${fileName} on mirror: ${mirror} (${responseChecksum} != ${fileInfo.sha256})`, + ); + continue; + } + } + + buffer = responseBuffer; + } + + if (!buffer) { + throw new Error(`None of the mirrors for ${fileName} are available`); + } + + await Deno.mkdir(resolve(filePath, ".."), { + recursive: true, + }).catch(() => {}); + + await Deno.writeFile(filePath, buffer); + console.info("Downloaded file", fileInfo.name); + } +} + +export const wowneroCliInfo: DownloadInfo = { + mirrors: [ + "https://static.mrcyjanek.net/download_mirror/", + "https://codeberg.org/wownero/wownero/releases/download/v0.11.2.0/", + ], + file: { + linux_aarch64: { + name: "wownero-aarch64-linux-gnu-59db3fe8d.tar.bz2", + sha256: "07ce678302c07a6e79d90be65cbda243d843d414fbadb30f972d6c226575cfa7", + }, + linux_x86_64: { + name: "wownero-x86_64-linux-gnu-59db3fe8d.tar.bz2", + sha256: "03880967c70cc86558d962b8a281868c3934238ea457a36174ba72b99d70107e", + }, + + darwin_aarch64: { + name: "wownero-aarch64-apple-darwin11-59db3fe8d.tar.bz2", + sha256: "25ff454a92b1cf036df5f28cdd2c63dcaf4b03da7da9403087371f868827c957", + }, + darwin_x86_64: { + name: "wownero-x86_64-apple-darwin11-59db3fe8d.tar.bz2", + sha256: "7e9b6a84a560ed7a9ed7117c6f07fb228d77a06afac863d0ea1dbf833c4eddf6", + }, + + windows_x86_64: { + name: "wownero-x86_64-w64-mingw32-59db3fe8d.zip", + sha256: "7e0ed84afa51e3b403d635c706042859094eb6850de21c9e82cb0a104425510e", + }, + + android_aarch64: { + overrideMirrors: [ + "https://static.mrcyjanek.net/download_mirror/", + "https://codeberg.org/wownero/wownero/releases/download/v0.11.1.0/", + ], + name: "wownero-aarch64-linux-android-v0.11.1.0.tar.bz2", + sha256: "236188f8d8e7fad2ff35973f8c2417afffa8855d1a57b4c682fff5b199ea40f5", + }, + }, +}; + +export const moneroCliInfo: DownloadInfo = { + mirrors: [ + "https://static.mrcyjanek.net/download_mirror/", + "https://downloads.getmonero.org/cli/", + ], + file: { + linux_aarch64: { + name: "monero-linux-armv8-v0.18.3.4.tar.bz2", + sha256: "33ca2f0055529d225b61314c56370e35606b40edad61c91c859f873ed67a1ea7", + }, + linux_x86_64: { + name: "monero-linux-x64-v0.18.3.4.tar.bz2", + sha256: "51ba03928d189c1c11b5379cab17dd9ae8d2230056dc05c872d0f8dba4a87f1d", + }, + + darwin_aarch64: { + name: "monero-mac-armv8-v0.18.3.4.tar.bz2", + sha256: "44520cb3a05c2518ca9aeae1b2e3080fe2bba1e3596d014ceff1090dfcba8ab4", + }, + darwin_x86_64: { + name: "monero-mac-x64-v0.18.3.4.tar.bz2", + sha256: "32c449f562216d3d83154e708471236d07db7477d6b67f1936a0a85a5005f2b8", + }, + + windows_x86_64: { + name: "monero-win-x64-v0.18.3.4.zip", + sha256: "54a66db6c892b2a0999754841f4ca68511741b88ea3ab20c7cd504a027f465f5", + }, + + android_aarch64: { + name: "monero-android-armv8-v0.18.3.4.tar.bz2", + sha256: "d9c9249d1408822ce36b346c6b9fb6b896cda16714d62117fb1c588a5201763c", + }, + }, +}; + +export const dylibInfos: Record = { + monero: [], + wownero: [], +}; + +for (const tag of await getMoneroCTags()) { + for (const coin of ["monero", "wownero"] as const) { + dylibInfos[coin].push({ + mirrors: [ + `https://static.mrcyjanek.net/download_mirror/libs/${tag}/`, + `https://github.com/MrCyjaneK/monero_c/releases/download/${tag}/`, + ], + file: { + linux_aarch64: { name: `${coin}_aarch64-linux-gnu_libwallet2_api_c.so.xz` }, + linux_x86_64: { name: `${coin}_x86_64-linux-gnu_libwallet2_api_c.so.xz` }, + darwin_aarch64: { name: `${coin}_aarch64-apple-darwin11_libwallet2_api_c.dylib.xz` }, + darwin_x86_64: { name: `${coin}_x86_64-apple-darwin11_libwallet2_api_c.dylib.xz` }, + windows_x86_64: { name: `${coin}_x86_64-w64-mingw32_libwallet2_api_c.dll.xz` }, + android_aarch64: { name: `${coin}_aarch64-linux-android_libwallet2_api_c.so.xz` }, + }, + outDir: `libs/${tag}`, + }); + } +} + +// Download files to the download_mirror folder +// (used on mirror to keep files up to date) +if (import.meta.main) { + const supportedTargets: Target[] = [ + "linux_x86_64", + "linux_aarch64", + "darwin_x86_64", + "darwin_aarch64", + "windows_x86_64", + "android_aarch64", + ]; + + for (const target of supportedTargets) { + await downloadFiles( + "./download_mirror", + target, + moneroCliInfo, + wowneroCliInfo, + ...Object.values(dylibInfos).flat(), + ); + } +} diff --git a/tests/integration.test.ts b/tests/integration.test.ts index 1a65009..100bd43 100644 --- a/tests/integration.test.ts +++ b/tests/integration.test.ts @@ -1,16 +1,7 @@ -import { - CoinsInfo, - type Dylib, - loadMoneroDylib, - loadWowneroDylib, - moneroSymbols, - Wallet, - WalletManager, - wowneroSymbols, -} from "../impls/monero.ts/mod.ts"; +import { CoinsInfo, Wallet, WalletManager } from "../impls/monero.ts/mod.ts"; import { assert, assertEquals } from "jsr:@std/assert"; -import { $, downloadCli, getMoneroC } from "./utils.ts"; +import { $, loadDylib, prepareCli, prepareMoneroC } from "./utils.ts"; const coin = Deno.env.get("COIN"); if (coin !== "monero" && coin !== "wownero") { @@ -53,7 +44,7 @@ const DESTINATION_ADDRESS = coin === "monero" ? MONERO_DESTINATION_ADDRESS : WOW const BILLION = 10n ** 9n; -await getMoneroC(coin, "next"); +await prepareMoneroC(coin, "next"); interface WalletInfo { name: string; @@ -74,14 +65,7 @@ async function clearWallets() { await Deno.mkdir("tests/wallets/"); } -let dylib: Dylib; -if (coin === "monero") { - dylib = Deno.dlopen(`tests/libs/next/monero_libwallet2_api_c.so`, moneroSymbols); - loadMoneroDylib(dylib); -} else { - dylib = Deno.dlopen(`tests/libs/next/wownero_libwallet2_api_c.so`, wowneroSymbols); - loadWowneroDylib(dylib); -} +loadDylib(coin, "next"); Deno.test("0001-polyseed.patch", async (t) => { const WALLETS: Record<"monero" | "wownero", WalletInfo[]> = { @@ -487,7 +471,7 @@ Deno.test("0004-coin-control.patch", { Deno.test("0009-Add-recoverDeterministicWalletFromSpendKey.patch", async () => { await Promise.all([ - downloadCli(coin), + prepareCli(coin), clearWallets(), ]); @@ -498,7 +482,7 @@ Deno.test("0009-Add-recoverDeterministicWalletFromSpendKey.patch", async () => { await Deno.remove("./tests/wallets/stoat"); - const cliPath = `./tests/${coin}-cli/${coin}-wallet-cli`; + const cliPath = `./tests/dependencies/${coin}-cli/${coin}-wallet-cli`; const moneroCliSeed = (await $.raw`${cliPath} --wallet-file ./tests/wallets/stoat --password gornostay --command seed` .stdinText(`gornostay\n`) .lines()).slice(-3).join(" "); diff --git a/tests/regression.test.ts b/tests/regression.test.ts index 82a9f95..797720f 100755 --- a/tests/regression.test.ts +++ b/tests/regression.test.ts @@ -1,4 +1,4 @@ -import { $, createWalletViaCli, downloadCli, getMoneroC, getMoneroCTags } from "./utils.ts"; +import { $, createWalletViaCli, getMoneroCTags, prepareCli, prepareMoneroC } from "./utils.ts"; const coin = Deno.env.get("COIN"); if (coin !== "monero" && coin !== "wownero") { @@ -11,7 +11,7 @@ Deno.test(`Regression tests (${coin})`, async (t) => { const tags = await getMoneroCTags(); const latestTag = tags[0]; - await Promise.all([getMoneroC(coin, "next"), await getMoneroC(coin, latestTag), downloadCli(coin)]); + await Promise.all([prepareMoneroC(coin, "next"), await prepareMoneroC(coin, latestTag), prepareCli(coin)]); await t.step("Simple (next, latest, next)", async () => { const walletInfo = await createWalletViaCli(coin, "dog", "sobaka"); @@ -27,13 +27,10 @@ Deno.test(`Regression tests (${coin})`, async (t) => { const walletInfo = await createWalletViaCli(coin, "cat", "koshka"); for (const version of tags.toReversed()) { - if (version !== "next" && version !== tags[0]) await getMoneroC(coin, version); + if (version !== "next" && version !== tags[0]) await prepareMoneroC(coin, version); await $`deno run -A ./tests/compare.ts ${coin} ${version} ${JSON.stringify(walletInfo)}`; } - - await Deno.remove("./tests/wallets", { recursive: true }).catch(() => {}); }); await Deno.remove("./tests/wallets", { recursive: true }).catch(() => {}); - await Deno.remove("./tests/libs", { recursive: true }).catch(() => {}); }); diff --git a/tests/utils.ts b/tests/utils.ts index cd05232..86501a8 100755 --- a/tests/utils.ts +++ b/tests/utils.ts @@ -1,4 +1,18 @@ import { build$, CommandBuilder } from "jsr:@david/dax"; +import { dirname, join } from "jsr:@std/path"; +import { + downloadDependencies, + dylibInfos, + getFileInfo, + moneroCliInfo, + Target, + wowneroCliInfo, +} from "./download_deps.ts"; +import { loadMoneroDylib, loadWowneroDylib, moneroSymbols, wowneroSymbols } from "../impls/monero.ts/mod.ts"; + +export type Coin = "monero" | "wownero"; + +const target = `${Deno.build.os}_${Deno.build.arch}` as const; export const $ = build$({ commandBuilder: new CommandBuilder() @@ -8,34 +22,83 @@ export const $ = build$({ .stderr("inherit"), }); -type Coin = "monero" | "wownero"; +export const dylibNames = (coin: Coin, version: MoneroCVersion) => ({ + linux_x86_64: `${coin}_x86_64-linux-gnu_libwallet2_api_c.so`, + darwin_aarch64: version === "next" + ? `${coin}_aarch64-apple-darwin_libwallet2_api_c.dylib` + : `${coin}_aarch64-apple-darwin11_libwallet2_api_c.dylib`, + windows_x86_64: `${coin}_x86_64-w64-mingw32_libwallet2_api_c.dll`, +} as Partial>); + +export const moneroTsDylibNames = (coin: Coin) => ({ + linux_x86_64: `${coin}_libwallet2_api_c.so`, + darwin_aarch64: `${coin}_aarch64-apple-darwin11_libwallet2_api_c.dylib`, + windows_x86_64: `${coin}_libwallet2_api_c.dll`, +} as Partial>); + +export function loadDylib(coin: Coin, version: MoneroCVersion) { + const dylibName = moneroTsDylibNames(coin)[target]!; + + if (coin === "monero") { + const dylib = Deno.dlopen(`tests/dependencies/libs/${version}/${dylibName}`, moneroSymbols); + loadMoneroDylib(dylib); + return dylib; + } else { + const dylib = Deno.dlopen(`tests/dependencies/libs/${version}/${dylibName}`, wowneroSymbols); + loadWowneroDylib(dylib); + return dylib; + } +} -export async function downloadMoneroCli() { - const MONERO_CLI_FILE_NAME = "monero-linux-x64-v0.18.3.4"; - const MONERO_WALLET_CLI_URL = `https://downloads.getmonero.org/cli/${MONERO_CLI_FILE_NAME}.tar.bz2`; +async function exists(path: string): Promise { + try { + await Deno.stat(path); + return true; + } catch (error) { + if (error instanceof Deno.errors.NotFound) { + return false; + } + throw error; + } +} - await $`wget -q -o /dev/null ${MONERO_WALLET_CLI_URL}`; - await $ - .raw`tar -xf ${MONERO_CLI_FILE_NAME}.tar.bz2 --one-top-level=monero-cli --strip-components=1 -C tests`; - await $.raw`rm ${MONERO_CLI_FILE_NAME}.tar.bz2`; +export async function extract(path: string, out: string) { + const outDir = out.endsWith("/") ? out : dirname(out); + await Deno.mkdir(outDir, { recursive: true }); + + if (path.endsWith(".tar.bz2")) { + let args = `-C ${dirname(out)}`; + if (outDir === out) { + args = `-C ${out} --strip-components=1`; + } + await $.raw`tar -xf ${path} ${args}`; + } else if (path.endsWith(".zip")) { + await $.raw`unzip ${path} -nu -d ${outDir}`; + } else if (path.endsWith(".xz")) { + await $.raw`xz -kd ${path}`; + await Deno.rename(path.slice(0, -3), out); + } else { + throw new Error("Unsupported archive file for:" + path); + } } -export async function downloadWowneroCli() { - const WOWNERO_CLI_FILE_NAME = "wownero-x86_64-linux-gnu-59db3fe8d"; - const WOWNERO_WALLET_CLI_URL = - `https://codeberg.org/wownero/wownero/releases/download/v0.11.2.0/wownero-x86_64-linux-gnu-59db3fe8d.tar.bz2`; +export async function prepareMoneroCli() { + await downloadDependencies(moneroCliInfo); + const path = join("./tests/dependencies", moneroCliInfo.outDir ?? "", getFileInfo(moneroCliInfo).name); + await extract(path, "./tests/dependencies/monero-cli/"); +} - await $`wget -q -o /dev/null ${WOWNERO_WALLET_CLI_URL}`; - await $ - .raw`tar -xf ${WOWNERO_CLI_FILE_NAME}.tar.bz2 --one-top-level=wownero-cli --strip-components=1 -C tests`; - await $.raw`rm ${WOWNERO_CLI_FILE_NAME}.tar.bz2`; +export async function prepareWowneroCli() { + await downloadDependencies(wowneroCliInfo); + const path = join("./tests/dependencies", wowneroCliInfo.outDir ?? "", getFileInfo(wowneroCliInfo).name); + await extract(path, "./tests/dependencies/wownero-cli/"); } -export function downloadCli(coin: Coin) { +export function prepareCli(coin: Coin) { if (coin === "wownero") { - return downloadWowneroCli(); + return prepareWowneroCli(); } - return downloadMoneroCli(); + return prepareMoneroCli(); } interface WalletInfo { @@ -54,7 +117,7 @@ export async function createWalletViaCli( password: string, ): Promise { const path = `./tests/wallets/${name}`; - const cliPath = `./tests/${coin}-cli/${coin}-wallet-cli`; + const cliPath = `./tests/dependencies/${coin}-cli/${coin}-wallet-cli`; await $ .raw`${cliPath} --generate-new-wallet ${path} --password ${password} --mnemonic-language English --command exit` @@ -97,35 +160,46 @@ export async function createWalletViaCli( export type MoneroCVersion = "next" | (string & {}); export async function getMoneroCTags(): Promise { - return (( - await (await fetch( - "https://api.github.com/repos/MrCyjanek/monero_c/releases", - )).json() - ) as { tag_name: string }[]) - .map(({ tag_name }) => tag_name); + const response = await fetch("https://static.mrcyjanek.net/monero_c/release.php"); + + if (!response.ok) { + throw new Error(`Could not receive monero_c release tags: ${await response.text()}`); + } + + const json = await response.json() as { tag_name: string }[]; + return json.map(({ tag_name }) => tag_name); } -export async function getMoneroC(coin: Coin, version: MoneroCVersion) { - const dylibName = `${coin}_x86_64-linux-gnu_libwallet2_api_c.so`; - const endpointDylibName = `${coin}_libwallet2_api_c.so`; + +export async function prepareMoneroC(coin: Coin, version: MoneroCVersion) { + const dylibName = dylibNames(coin, version)[target]; + const moneroTsDylibName = moneroTsDylibNames(coin)[target]; + + if (!dylibName || !moneroTsDylibName) { + throw new Error(`Missing dylib name value for target: ${target}`); + } + const releaseDylibName = dylibName.slice(`${coin}_`.length); if (version === "next") { - await $.raw`xz -kd release/${coin}/${releaseDylibName}.xz`; - await $`mkdir -p tests/libs/next`; - await $`mv release/${coin}/${releaseDylibName} tests/libs/next/${endpointDylibName}`; + const outFileDir = `./tests/dependencies/libs/${version}/${moneroTsDylibName}`; + + if (await exists(outFileDir)) { + return; + } + + await extract(`./release/${coin}/${releaseDylibName}.xz`, outFileDir); } else { - const downloadUrl = `https://github.com/MrCyjaneK/monero_c/releases/download/${version}/${dylibName}.xz`; - - const file = await Deno.open(`./tests/${dylibName}.xz`, { - create: true, - write: true, - }); - file.write(await (await fetch(downloadUrl)).bytes()); - file.close(); - - await $.raw`xz -d ./tests/${dylibName}.xz`; - await $.raw`mkdir -p ./tests/libs/${version}`; - await $ - .raw`mv ./tests/${dylibName} ./tests/libs/${version}/${endpointDylibName}`; + const outFileDir = `./tests/dependencies/libs/${version}/${moneroTsDylibName}`; + + if (await exists(outFileDir)) { + return; + } + + const downloadInfo = dylibInfos[coin].find((info) => info.outDir?.endsWith(version)); + if (downloadInfo) { + await downloadDependencies(downloadInfo); + } + + await extract(`./tests/dependencies/libs/${version}/${dylibName}.xz`, outFileDir); } } -- cgit v1.2.3