Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release / 4.19.200 #3164

Merged
merged 10 commits into from
Apr 14, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import {
StakeCredentialParamsType,
CIP36VoteRegistrationFormat,
TxRequiredSignerType,
DatumType,
TxOutputFormat,
} from '@cardano-foundation/ledgerjs-hw-app-cardano';
import { RustModule } from '../../lib/cardanoCrypto/rustLoader';
import { toHexOrBase58 } from '../../lib/storage/bridge/utils';
Expand All @@ -37,6 +39,7 @@ import {
ChainDerivations,
} from '../../../../config/numbersConfig';
import { derivePublicByAddressing } from '../../lib/cardanoCrypto/utils';
import cbor from 'cbor';

// ==================== LEDGER ==================== //
/** Generate a payload for Ledger SignTx */
Expand Down Expand Up @@ -608,7 +611,10 @@ export function toLedgerSignRequest(
ownUtxoAddressMap: AddressMap,
ownStakeAddressMap: AddressMap,
addressedUtxos: Array<CardanoAddressedUtxo>,
rawTxBody: Buffer,
): SignTransactionRequest {
const parsedCbor = cbor.decode(rawTxBody);

function formatInputs(inputs: RustModule.WalletV4.TransactionInputs): Array<TxInput> {
const formatted = [];
for (let i = 0; i < inputs.len(); i++) {
Expand All @@ -627,7 +633,10 @@ export function toLedgerSignRequest(
return formatted.sort(compareInputs);
}

function formatOutput(output: RustModule.WalletV4.TransactionOutput): TxOutput {
function formatOutput(
output: RustModule.WalletV4.TransactionOutput,
isPostAlonzoTransactionOutput: boolean,
): TxOutput {
const addr = output.address();
let destination;

Expand Down Expand Up @@ -741,20 +750,52 @@ export function toLedgerSignRequest(
throw new Error('not expecting to pay to reward address');
}

const amount = output.amount().coin().to_str();
const tokenBundle = toLedgerTokenBundle(output.amount().multiasset());
const outputDataHash = output.data_hash();
const plutusData = output.plutus_data();
const scriptRef = output.script_ref();

if (isPostAlonzoTransactionOutput || scriptRef || plutusData) {
let datum = null;
if (plutusData) {
datum = {
type: DatumType.INLINE,
datumHex: plutusData.to_hex(),
};
} else if (outputDataHash) {
datum = {
type: DatumType.HASH,
datumHashHex: outputDataHash.to_hex(),
};
}
return {
format: TxOutputFormat.MAP_BABBAGE,
amount,
destination,
tokenBundle,
datum,
referenceScriptHex: scriptRef ? scriptRef.to_hex() : null,
};
}

// TODO: Babbage-era output support
return {
amount: output.amount().coin().to_str(),
format: TxOutputFormat.ARRAY_LEGACY,
amount,
destination,
tokenBundle: toLedgerTokenBundle(output.amount().multiasset()),
tokenBundle,
datumHashHex: outputDataHash ? outputDataHash.to_hex() : null,
};
}

const outputs = [];
for (let i = 0; i < txBody.outputs().len(); i++) {
outputs.push(formatOutput(txBody.outputs().get(i)));
outputs.push(
formatOutput(
txBody.outputs().get(i),
parsedCbor.get(1)[i].constructor.name === 'Map',
)
);
}

const additionalWitnessPaths = [];
Expand All @@ -763,11 +804,16 @@ export function toLedgerSignRequest(
if (requiredSigners) {
for (let i = 0; i < requiredSigners.len(); i++) {
const hash = requiredSigners.get(i);
const address = RustModule.WalletV4.EnterpriseAddress.new(
const enterpriseAddress = RustModule.WalletV4.EnterpriseAddress.new(
networkId,
RustModule.WalletV4.StakeCredential.from_keyhash(hash),
).to_address().to_hex();
const ownAddressPath = ownUtxoAddressMap[address];
const stakeAddress = RustModule.WalletV4.RewardAddress.new(
networkId,
RustModule.WalletV4.StakeCredential.from_keyhash(hash),
).to_address().to_hex();
const ownAddressPath = ownUtxoAddressMap[enterpriseAddress] ||
ownStakeAddressMap[stakeAddress];
if (ownAddressPath) {
formattedRequiredSigners.push({
type: TxRequiredSignerType.PATH,
Expand Down Expand Up @@ -831,7 +877,10 @@ export function toLedgerSignRequest(
let formattedCollateralReturn = null;
const collateralReturn = txBody.collateral_return();
if (collateralReturn) {
formattedCollateralReturn = formatOutput(collateralReturn);
formattedCollateralReturn = formatOutput(
collateralReturn,
parsedCbor.get(16).constructor.name === 'Map',
);
}

let formattedReferenceInputs = null;
Expand All @@ -840,8 +889,13 @@ export function toLedgerSignRequest(
formattedReferenceInputs = formatInputs(referenceInputs);
}

let signingMode = TransactionSigningMode.ORDINARY_TRANSACTION;
if (formattedCollateral) {
signingMode = TransactionSigningMode.PLUTUS_TRANSACTION;
}

return {
signingMode: TransactionSigningMode.ORDINARY_TRANSACTION,
signingMode,
tx: {
network: {
networkId,
Expand All @@ -855,12 +909,13 @@ export function toLedgerSignRequest(
withdrawals: formattedWithdrawals,
auxiliaryData: formattedAuxiliaryData,
validityIntervalStart: txBody.validity_start_interval_bignum()?.to_str() ?? null,
mint: txBody.mint()?.to_js_value().map(([policyIdHex, assets]) => ({
policyIdHex,
tokens: Object.keys(assets).map(assetNameHex => (
{ assetNameHex, amount: assets[assetNameHex] }
)),
})) ?? null,
mint: JSON.parse(txBody.mint()?.to_json() ?? 'null')?.map(
([policyIdHex, assets]) => ({
policyIdHex,
tokens: Object.keys(assets).map(assetNameHex => (
{ assetNameHex, amount: assets[assetNameHex] }
)),
})) ?? null,
scriptDataHashHex: txBody.script_data_hash()?.to_hex() ?? null,
collateralInputs: formattedCollateral,
requiredSigners: requiredSigners ? formattedRequiredSigners : null,
Expand Down
12 changes: 8 additions & 4 deletions packages/yoroi-extension/app/connector/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import id from 'react-intl/locale-data/id';
import es from 'react-intl/locale-data/es';
import it from 'react-intl/locale-data/it';
import tr from 'react-intl/locale-data/tr';
import cs from 'react-intl/locale-data/cs';
import sk from 'react-intl/locale-data/sk';
import { observable, autorun, runInAction } from 'mobx';
import { Routes } from './Routes';
import { translations } from '../i18n/translations';
Expand Down Expand Up @@ -46,6 +48,8 @@ addLocaleData([
...es,
...it,
...tr,
...cs,
...sk,
]);

type Props = {|
Expand All @@ -59,19 +63,19 @@ type State = {|

@observer
class App extends Component<Props, State> {
@observable mergedMessages: null | {| [key: string]: string, |} = null;
@observable mergedMessages: null | {| [key: string]: string |} = null;

componentDidMount: () => void = () => {
autorun(async () => {
const _mergedMessages = {
...await translations['en-US'],
...await translations[this.props.stores.profile.currentLocale]
...(await translations['en-US']),
...(await translations[this.props.stores.profile.currentLocale]),
};
runInAction(() => {
this.mergedMessages = _mergedMessages;
});
});
}
};

state: State = {
crashed: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,7 @@ export default class ConnectorStore extends Store<StoresMap, ActionsMap> {
ownUtxoAddressMap,
ownStakeAddressMap,
addressedUtxos,
rawTxBody,
);
} catch {
runInAction(() => {
Expand Down
3 changes: 2 additions & 1 deletion packages/yoroi-extension/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ module.exports = {
'<rootDir>/node_modules/(?!@emurgo/ledger-connect-handler)'
],
setupFiles: [
'jest-canvas-mock'
'jest-canvas-mock',
'./jestSetup',
],
// ignore manifest.test.js file, because it isn't a test
testPathIgnorePatterns: ['manifest.test.js'],
Expand Down
3 changes: 3 additions & 0 deletions packages/yoroi-extension/jestSetup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { TextEncoder, TextDecoder } from 'util';

Object.assign(global, { TextEncoder, TextDecoder });
Loading