From a402226a77db0a4207c28852c94a5752dc86deb6 Mon Sep 17 00:00:00 2001 From: Michal Date: Sat, 25 May 2024 08:45:13 +0100 Subject: [PATCH] WIP --- .../src/yoroi-wallets/cardano/cip30.ts | 26 +++++---- packages/dapp-connector/src/resolver.ts | 56 ++++++++++++------- 2 files changed, 49 insertions(+), 33 deletions(-) diff --git a/apps/wallet-mobile/src/yoroi-wallets/cardano/cip30.ts b/apps/wallet-mobile/src/yoroi-wallets/cardano/cip30.ts index 63d3aa4827..b01aa4e74a 100644 --- a/apps/wallet-mobile/src/yoroi-wallets/cardano/cip30.ts +++ b/apps/wallet-mobile/src/yoroi-wallets/cardano/cip30.ts @@ -25,20 +25,22 @@ export const cip30ExtensionMaker = (wallet: YoroiWallet) => { const getCSL = () => wrappedCsl() -const recreateValue = async (value: CSL.Value) => { - return CardanoMobile.Value.fromHex(await value.toHex()) +const copy = async Promise}>( + creator: {fromHex: (hex: string) => Promise}, + value: T, +): Promise => { + return creator.fromHex(await value.toHex()) } -const recreateMultiple = async (items: T[], recreate: (item: T) => Promise) => { - return Promise.all(items.map(recreate)) +const copyMultiple = async Promise}>( + items: T[], + creator: {fromHex: (hex: string) => Promise}, +) => { + return Promise.all(items.map((item) => copy(creator, item))) } const recreateTransactionUnspentOutput = async (utxo: CSL.TransactionUnspentOutput) => { - return CardanoMobile.TransactionUnspentOutput.fromHex(await utxo.toHex()) -} - -const recreateWitnessSet = async (witnessSet: CSL.TransactionWitnessSet) => { - return CardanoMobile.TransactionWitnessSet.fromHex(await witnessSet.toHex()) + return copy(CardanoMobile.TransactionUnspentOutput, utxo) } class CIP30Extension { @@ -48,7 +50,7 @@ class CIP30Extension { const {csl, release} = getCSL() try { const value = await _getBalance(csl, tokenId, this.wallet.utxos, this.wallet.primaryTokenInfo.id) - return recreateValue(value) + return copy(CardanoMobile.Value, value) } finally { release() } @@ -111,7 +113,7 @@ class CIP30Extension { const multipleUtxosCollateral = await _drawCollateralInMultipleUtxos(csl, this.wallet, asQuantity(valueNum)) if (multipleUtxosCollateral && multipleUtxosCollateral.length > 0) { - return recreateMultiple(multipleUtxosCollateral, recreateTransactionUnspentOutput) + return copyMultiple(multipleUtxosCollateral, CardanoMobile.TransactionUnspentOutput) } return null @@ -146,7 +148,7 @@ class CIP30Extension { const keys = await Promise.all(signers.map(async (signer) => createRawTxSigningKey(rootKey, signer))) const signedTxBytes = await signRawTransaction(csl, cbor, keys) const signedTx = await csl.Transaction.fromBytes(signedTxBytes) - return recreateWitnessSet(await signedTx.witnessSet()) + return copy(CardanoMobile.TransactionWitnessSet, await signedTx.witnessSet()) } finally { release() } diff --git a/packages/dapp-connector/src/resolver.ts b/packages/dapp-connector/src/resolver.ts index 1bd34665ed..a76f994597 100644 --- a/packages/dapp-connector/src/resolver.ts +++ b/packages/dapp-connector/src/resolver.ts @@ -103,7 +103,20 @@ export const resolver: Resolver = { : undefined const result = await context.wallet.getCollateral(value) - if (result === null || (result.length === 0 && typeof value === 'string')) return null + if (result === null) { + // offer reorganisation + const balance = await context.wallet.getBalance('*') + const coin = await balance.coin() + // check min collateral value + if ((await coin.toStr()) !== '0') { + const utxos = await context.wallet.getUtxos(value ?? '1000000') + if (utxos === null || utxos.length === 0) { + return null + } + } + + return null + } return Promise.all(result.map((u) => u.toHex())) }, @@ -212,27 +225,28 @@ const handleMethod = async ( supportedExtensions: trustedContext.supportedExtensions, } - if (method === 'cardano_enable') { - return resolver.enable(params, context) - } - - if (method === 'cardano_is_enabled') { - return resolver.isEnabled(params, context) - } - - if (method === LOG_MESSAGE_EVENT) { - return resolver.logMessage(params, context) - } - - if (method.startsWith('api.')) { - const methodParts = method.split('.') - if (methodParts.length !== 2) throw new Error(`Invalid method ${method}`) - const apiMethod = methodParts[1] - if (!isKeyOf(apiMethod, resolver.api)) throw new Error(`Unknown method ${method}`) - return resolver.api[apiMethod](params, context) - } + if (!method) throw new Error('Method is required') + const isValidMethod = isKeyOf(method, methods) + if (!isValidMethod) throw new Error(`Unknown method '${method}'`) + return methods[method](params, context) +} - throw new Error(`Unknown method '${method}' with params ${JSON.stringify(params)}`) +const methods = { + 'cardano_enable': resolver.enable, + 'cardano_is_enabled': resolver.isEnabled, + 'log_message': resolver.logMessage, + 'api.getBalance': resolver.api.getBalance, + 'api.getChangeAddress': resolver.api.getChangeAddress, + 'api.getNetworkId': resolver.api.getNetworkId, + 'api.getRewardAddresses': resolver.api.getRewardAddresses, + 'api.getUsedAddresses': resolver.api.getUsedAddresses, + 'api.getExtensions': resolver.api.getExtensions, + 'api.getUnusedAddresses': resolver.api.getUnusedAddresses, + 'api.getUtxos': resolver.api.getUtxos, + 'api.getCollateral': resolver.api.getCollateral, + 'api.submitTx': resolver.api.submitTx, + 'api.signTx': resolver.api.signTx, + 'api.signData': resolver.api.signData, } export const resolverHandleEvent = async (