Skip to content

Commit

Permalink
Merge branch 'develop' into feat/remote-notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljscript authored Dec 19, 2024
2 parents 1c694a1 + d506c77 commit ee25e40
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 72 deletions.
2 changes: 1 addition & 1 deletion apps/wallet-mobile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
"@emurgo/csl-mobile-bridge": "^7.1.0",
"@emurgo/msl-mobile-bridge": "^1.0.4",
"@emurgo/react-native-hid": "5.15.8",
"@emurgo/yoroi-lib": "2.2.2",
"@emurgo/yoroi-lib": "^2.2.3",
"@formatjs/intl-datetimeformat": "^6.7.0",
"@formatjs/intl-getcanonicallocales": "^2.1.0",
"@formatjs/intl-locale": "^3.2.1",
Expand Down
12 changes: 12 additions & 0 deletions apps/wallet-mobile/src/features/Discover/common/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,15 @@ export async function toLedgerSignRequest(
additionalRequiredSigners: Array<string> = [],
): Promise<SignTransactionRequest> {
const parsedCbor = await cbor.decode(rawTxBody)
const tagsState = await csl.hasTransactionSetTag(
await (await csl.FixedTransaction.newFromBodyBytes(await txBody.toBytes())).toBytes(),
)

if (tagsState === csl.TransactionSetsState.MixedSets) {
throw new Error('Transaction with mixed sets cannot be signed by Ledger')
}

const txHasSetTags = tagsState === csl.TransactionSetsState.AllSetsHaveTag

async function formatInputs(inputs: TransactionInputs): Promise<Array<TxInput>> {
const formatted = []
Expand Down Expand Up @@ -533,6 +542,9 @@ export async function toLedgerSignRequest(
referenceInputs: formattedReferenceInputs,
},
additionalWitnessPaths,
options: {
tagCborSets: txHasSetTags,
},
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export const useSignTxWithHW = () => {

const mutation = useMutation<Transaction, Error, {cbor: string; partial?: boolean}>({
mutationFn,
useErrorBoundary: (error) => !isUserRejectedError(error),
useErrorBoundary: (error) => !isUserRejectedError(error) && !error.message.toLowerCase().includes('rejected'),
mutationKey: ['useSignTxWithHW'],
})

Expand Down
22 changes: 13 additions & 9 deletions apps/wallet-mobile/src/yoroi-wallets/cardano/cardano-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import _ from 'lodash'
import {defaultMemoize} from 'reselect'
import {Observable} from 'rxjs'

import {toLedgerSignRequest} from '../../features/Discover/common/ledger'
import {buildPortfolioBalanceManager} from '../../features/Portfolio/common/helpers/build-balance-manager'
import {toBalanceManagerSyncArgs} from '../../features/Portfolio/common/transformers/toBalanceManagerSyncArgs'
import {makeMemosManager, MemosManager} from '../../features/Transactions/common/memos/memosManager'
Expand Down Expand Up @@ -40,7 +41,6 @@ import {Cardano, CardanoMobile} from '../wallets'
import {AccountManager, accountManagerMaker, Addresses} from './account-manager/account-manager'
import * as legacyApi from './api/api'
import {calcLockedDeposit} from './assetUtils'
import {createSwapCancellationLedgerPayload} from './common/signatureUtils'
import {filterAddressesByStakingKey, getDelegationStatus} from './delegationUtils'
import {
doesCardanoAppVersionSupportCIP36,
Expand All @@ -64,7 +64,7 @@ import {
YoroiWallet,
} from './types'
import {yoroiUnsignedTx} from './unsignedTx/unsignedTx'
import {deriveRewardAddressHex, toRecipients} from './utils'
import {deriveRewardAddressHex, getAddressedUtxos, getHexAddressingMap, toRecipients} from './utils'
import {makeUtxoManager, UtxoManager} from './utxoManager/utxoManager'
import {utxosMaker} from './utxoManager/utxos'

Expand Down Expand Up @@ -882,22 +882,26 @@ export const makeCardanoWallet = (networkManager: Network.Manager, implementatio
}

async signSwapCancellationWithLedger(cbor: string, useUSB: boolean, hwDeviceInfo: HW.DeviceInfo): Promise<void> {
const stakeVkeyHash = await this.getStakingKey().then((key) => key.hash())
const payload = await createSwapCancellationLedgerPayload(
cbor,
this,
const tx = await CardanoMobile.Transaction.fromHex(cbor)
const txBody = await tx.body()
const payload = await toLedgerSignRequest(
CardanoMobile,
txBody,
this.networkManager.chainId,
this.networkManager.protocolMagic,
(address: string) => this.getAddressing(address),
stakeVkeyHash,
await getHexAddressingMap(CardanoMobile, this),
await getHexAddressingMap(CardanoMobile, this),
getAddressedUtxos(this),
await txBody.toBytes(),
[],
)

const signedLedgerTx = await signTxWithLedger(payload, hwDeviceInfo, useUSB)

const bytes = await createSignedLedgerTxFromCbor(
CardanoMobile,
cbor,
signedLedgerTx.witnesses,
signedLedgerTx,
implementationConfig.derivations.base.harden.purpose,
this.publicKeyHex,
)
Expand Down
46 changes: 6 additions & 40 deletions apps/wallet-mobile/src/yoroi-wallets/cardano/cip30/cip30-ledger.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import {Transaction, WasmModuleProxy} from '@emurgo/cross-csl-core'
import {has_transaction_set_tag, TransactionSetsState} from '@emurgo/csl-mobile-bridge'
import {Transaction} from '@emurgo/cross-csl-core'
import {createSignedLedgerTxFromCbor} from '@emurgo/yoroi-lib'
import {normalizeToAddress} from '@emurgo/yoroi-lib/dist/internals/utils/addresses'
import {HW, Wallet} from '@yoroi/types'

import {toLedgerSignRequest} from '../../../features/Discover/common/ledger'
import {cardanoConfig} from '../../../features/WalletManager/common/adapters/cardano/cardano-config'
import type {RawUtxo} from '../../types/other'
import {assertHasAllSigners} from '../common/signatureUtils'
import {signTxWithLedger} from '../hw/hw'
import {CardanoTypes, YoroiWallet} from '../types'
import {YoroiWallet} from '../types'
import {getAddressedUtxos, getHexAddressingMap} from '../utils'
import {wrappedCsl} from '../wrappedCsl'

export const cip30LedgerExtensionMaker = (wallet: YoroiWallet, meta: Wallet.Meta) => {
Expand All @@ -26,9 +24,9 @@ class CIP30LedgerExtension {
if (!partial) await assertHasAllSigners(cbor, this.wallet, this.meta)
const txBody = await tx.body()

const transactionSetTag = await has_transaction_set_tag(await tx.toBytes())
const transactionSetTag = await csl.hasTransactionSetTag(await tx.toBytes())

if (transactionSetTag === TransactionSetsState.MixedSets) {
if (transactionSetTag === csl.TransactionSetsState.MixedSets) {
throw new Error('CIP30LedgerExtension.signTx: Mixed transaction sets are not supported when using a HW wallet')
}

Expand All @@ -49,7 +47,7 @@ class CIP30LedgerExtension {
const bytes = await createSignedLedgerTxFromCbor(
csl,
cbor,
signedLedgerTx.witnesses,
signedLedgerTx,
implementationConfig.derivations.base.harden.purpose,
this.wallet.publicKeyHex,
)
Expand All @@ -59,35 +57,3 @@ class CIP30LedgerExtension {
}
}
}

const getHexAddressingMap = async (csl: WasmModuleProxy, wallet: YoroiWallet) => {
const addressedUtxos = wallet.utxos.map(async (utxo: RawUtxo) => {
const addressing = wallet.getAddressing(utxo.receiver)
const hexAddress = await normalizeToAddress(csl, utxo.receiver).then((a) => a?.toHex())

return {addressing, hexAddress}
})

const addressing = await Promise.all(addressedUtxos)
return addressing.reduce<{[addressHex: string]: Array<number>}>((acc, curr) => {
if (!curr.hexAddress) return acc
acc[curr.hexAddress] = curr.addressing.path
return acc
}, {})
}

const getAddressedUtxos = (wallet: YoroiWallet) => {
return wallet.utxos.map((utxo: RawUtxo): CardanoTypes.CardanoAddressedUtxo => {
const addressing = wallet.getAddressing(utxo.receiver)

return {
addressing,
txIndex: utxo.tx_index,
txHash: utxo.tx_hash,
amount: utxo.amount,
receiver: utxo.receiver,
utxoId: utxo.utxo_id,
assets: utxo.assets,
}
})
}
36 changes: 35 additions & 1 deletion apps/wallet-mobile/src/yoroi-wallets/cardano/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable no-empty */
import {WasmModuleProxy} from '@emurgo/cross-csl-core'
import {SendToken} from '@emurgo/yoroi-lib'
import {normalizeToAddress} from '@emurgo/yoroi-lib/dist/internals/utils/addresses'
import {invalid} from '@yoroi/common'
import {Balance, Chain, Portfolio, Wallet} from '@yoroi/types'
import {BigNumber} from 'bignumber.js'
Expand All @@ -13,7 +15,7 @@ import {CardanoMobile} from '../wallets'
import {toAssetNameHex, toPolicyId} from './api/utils'
import {withMinAmounts} from './getMinAmounts'
import {MultiToken} from './MultiToken'
import {CardanoTypes} from './types'
import {CardanoTypes, YoroiWallet} from './types'
import {wrappedCsl as getCSL, wrappedCsl} from './wrappedCsl'

export const deriveRewardAddressHex = async (
Expand Down Expand Up @@ -249,3 +251,35 @@ export const getTransactionUnspentOutput = async ({
release()
}
}

export const getHexAddressingMap = async (csl: WasmModuleProxy, wallet: YoroiWallet) => {
const addressedUtxos = wallet.utxos.map(async (utxo: RawUtxo) => {
const addressing = wallet.getAddressing(utxo.receiver)
const hexAddress = await normalizeToAddress(csl, utxo.receiver).then((a) => a?.toHex())

return {addressing, hexAddress}
})

const addressing = await Promise.all(addressedUtxos)
return addressing.reduce<{[addressHex: string]: Array<number>}>((acc, curr) => {
if (!curr.hexAddress) return acc
acc[curr.hexAddress] = curr.addressing.path
return acc
}, {})
}

export const getAddressedUtxos = (wallet: YoroiWallet) => {
return wallet.allUtxos.map((utxo: RawUtxo): CardanoTypes.CardanoAddressedUtxo => {
const addressing = wallet.getAddressing(utxo.receiver)

return {
addressing,
txIndex: utxo.tx_index,
txHash: utxo.tx_hash,
amount: utxo.amount,
receiver: utxo.receiver,
utxoId: utxo.utxo_id,
assets: utxo.assets,
}
})
}
1 change: 0 additions & 1 deletion packages/swap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@
},
"devDependencies": {
"@commitlint/config-conventional": "^17.0.2",
"@emurgo/yoroi-lib": "^2.0.0",
"@react-native-async-storage/async-storage": "^1.19.3",
"@react-native-community/eslint-config": "^3.0.2",
"@release-it/conventional-changelog": "^5.0.0",
Expand Down
23 changes: 4 additions & 19 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2167,25 +2167,10 @@
"@ledgerhq/logs" "^5.15.0"
rxjs "^6.5.5"

"@emurgo/[email protected]":
version "2.2.2"
resolved "https://registry.yarnpkg.com/@emurgo/yoroi-lib/-/yoroi-lib-2.2.2.tgz#e5575fedb123c36c78a08e28d60149fcb67cde0b"
integrity sha512-XhC+2+F9pa3mUorCKixL9WIFGUjkN/u/MH3tsMHaNKfRwnEaWkXMfQZmN/WAposEi5r5rnwb7I3vRTB94srZow==
dependencies:
"@cardano-foundation/ledgerjs-hw-app-cardano" "^7.1.3"
"@emurgo/cross-csl-core" "6.1.0"
"@noble/hashes" "^1.3.2"
axios "^1.7.5"
axios-cache-interceptor "^1.5.3"
bech32 "^2.0.0"
bignumber.js "^9.0.1"
blake2b "2.1.4"
easy-crc "1.1.0"

"@emurgo/yoroi-lib@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@emurgo/yoroi-lib/-/yoroi-lib-2.0.0.tgz#5f8361a942f9aa8094c842ffece1893cc33f7b63"
integrity sha512-uXeUVHKEE633iBsEGxpbgK/NrXZzBvWRWGk1//rC9ZQsHNhGzSqgr99k4O9b6XjTBxZf7Yndrs8vdpEwEKVFyw==
"@emurgo/yoroi-lib@^2.2.3":
version "2.2.3"
resolved "https://registry.yarnpkg.com/@emurgo/yoroi-lib/-/yoroi-lib-2.2.3.tgz#3ba19fadd837e1ae7b209be13b05c119ee71dfae"
integrity sha512-J72qa93jipD9lXB6TbATZlq/k129afMaCzqlMbnvB6WIvqGXQL9VPnbiLCdDMBGKuIppgNEt3ir9cFgSH09OsQ==
dependencies:
"@cardano-foundation/ledgerjs-hw-app-cardano" "^7.1.3"
"@emurgo/cross-csl-core" "6.1.0"
Expand Down

0 comments on commit ee25e40

Please sign in to comment.