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

swap hw #3661

Open
wants to merge 23 commits into
base: develop
Choose a base branch
from
Open

swap hw #3661

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 73 additions & 52 deletions packages/yoroi-extension/app/api/ada/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ import {
signTransaction as shelleySignTransaction,
} from './transactions/shelley/transactions';
import { generateAdaMnemonic, generateWalletRootKey, } from './lib/cardanoCrypto/cryptoWallet';
import { cip8Sign, v4PublicToV2, makeCip8Key, buildCoseSign1FromSignature } from './lib/cardanoCrypto/utils';
import { buildCoseSign1FromSignature, cip8Sign, makeCip8Key, v4PublicToV2 } from './lib/cardanoCrypto/utils';
import { isValidBip39Mnemonic, } from './lib/cardanoCrypto/wallet';
import type { CardanoSignTransaction } from 'trezor-connect-flow';
import { createTrezorSignTxPayload, } from './transactions/shelley/trezorTx';
import { createLedgerSignTxPayload, } from './transactions/shelley/ledgerTx';
import { createTrezorSignTxPayload, toTrezorSignRequest, } from './transactions/shelley/trezorTx';
import { createLedgerSignTxPayload, toLedgerSignRequest, } from './transactions/shelley/ledgerTx';
import {
GenericApiError,
IncorrectWalletPasswordError,
Expand Down Expand Up @@ -121,8 +121,6 @@ import type {
MultiAssetSupplyFunc,
RemoteUnspentOutput,
SendFunc,
SignedRequest,
SignedResponse,
TokenInfoFunc,
UtxoData,
} from './lib/state-fetch/types';
Expand All @@ -131,10 +129,10 @@ import type { AddressRowWithPath, } from './lib/storage/bridge/traitUtils';
import { getAllAddressesForDisplay, getAllAddressesForWallet, } from './lib/storage/bridge/traitUtils';
import {
asAddressedUtxo,
cardanoMinAdaRequiredFromAssets,
convertAdaTransactionsToExportRows,
multiTokenFromCardanoValue,
multiTokenFromRemote,
cardanoMinAdaRequiredFromAssets,
} from './transactions/utils';
import type { TransactionExportRow } from '../export';

Expand All @@ -150,14 +148,15 @@ import type {
} from '../common/types';
import {
getCardanoHaskellBaseConfig,
getCardanoHaskellBaseConfigCombined,
getNetworkById,
} from './lib/storage/database/prepackaged/networks';
import { toSenderUtxos } from './transactions/transfer/utils';
import type { DefaultTokenEntry } from '../common/lib/MultiToken';
import { MultiToken } from '../common/lib/MultiToken';
import { getReceiveAddress } from '../../stores/stateless/addressStores';
import { generateRegistrationMetadata } from './lib/cardanoCrypto/catalyst';
import { bytesToHex, hexToBytes, hexToUtf } from '../../coreUtils';
import { bytesToHex, fail, hexToBytes, hexToUtf } from '../../coreUtils';
import type { PersistedSubmittedTransaction } from '../localStorage';
import type WalletTransaction from '../../domain/WalletTransaction';
import { derivePrivateByAddressing, derivePublicByAddressing } from './lib/cardanoCrypto/deriveByAddressing';
Expand All @@ -180,9 +179,6 @@ export type GetAllAddressesForDisplayRequest = {|
type: CoreAddressT,
|};
export type GetAllAddressesForDisplayResponse = Array<AddressDetails>;
export type GetAllAddressesForDisplayFunc = (
request: GetAllAddressesForDisplayRequest
) => Promise<GetAllAddressesForDisplayResponse>;

// getChainAddressesForDisplay

Expand All @@ -192,9 +188,6 @@ export type GetChainAddressesForDisplayRequest = {|
type: CoreAddressT,
|};
export type GetChainAddressesForDisplayResponse = Array<AddressDetails>;
export type GetChainAddressesForDisplayFunc = (
request: GetChainAddressesForDisplayRequest
) => Promise<GetChainAddressesForDisplayResponse>;

// refreshTransactions

Expand Down Expand Up @@ -238,28 +231,10 @@ export type SignAndBroadcastFunc = (

// createTrezorSignTxData

export type CreateTrezorSignTxDataRequest = {|
signRequest: HaskellShelleyTxSignRequest,
network: $ReadOnly<NetworkRow>,
|};
export type CreateTrezorSignTxDataResponse = {|
// https://github.com/trezor/connect/blob/develop/docs/methods/cardanoSignTransaction.md
trezorSignTxPayload: $Exact<CardanoSignTransaction>,
|};
export type CreateTrezorSignTxDataFunc = (
request: CreateTrezorSignTxDataRequest
) => Promise<CreateTrezorSignTxDataResponse>;

// broadcastTrezorSignedTx

export type BroadcastTrezorSignedTxRequest = {|
signedTxRequest: SignedRequest,
sendTx: SendFunc,
|};
export type BroadcastTrezorSignedTxResponse = SignedResponse;
export type BroadcastTrezorSignedTxFunc = (
request: BroadcastTrezorSignedTxRequest
) => Promise<BroadcastTrezorSignedTxResponse>;

// createLedgerSignTxData

Expand All @@ -272,20 +247,16 @@ export type CreateLedgerSignTxDataRequest = {|
export type CreateLedgerSignTxDataResponse = {|
ledgerSignTxPayload: SignTransactionRequest,
|};
export type CreateLedgerSignTxDataFunc = (
request: CreateLedgerSignTxDataRequest
) => Promise<CreateLedgerSignTxDataResponse>;

// broadcastLedgerSignedTx
// createHwSignTxData

export type BroadcastLedgerSignedTxRequest = {|
signedTxRequest: SignedRequest,
sendTx: SendFunc,
export type CreateHWSignTxDataRequestFromRawTx = {|
txBodyHex: string,
network: $ReadOnly<NetworkRow>,
addressingMap: string => (void | $PropertyType<Addressing, 'addressing'>),
senderUtxos: Array<CardanoAddressedUtxo>,
additionalRequiredSigners?: Array<string>,
|};
export type BroadcastLedgerSignedTxResponse = SignedResponse;
export type BroadcastLedgerSignedTxFunc = (
request: BroadcastLedgerSignedTxRequest
) => Promise<BroadcastLedgerSignedTxResponse>;

// createUnsignedTx

Expand Down Expand Up @@ -777,17 +748,18 @@ export default class AdaApi {
}
}

async createTrezorSignTxData(
request: CreateTrezorSignTxDataRequest
): Promise<CreateTrezorSignTxDataResponse> {
createTrezorSignTxData(request: {|
signRequest: HaskellShelleyTxSignRequest,
network: $ReadOnly<NetworkRow>,
|}): CreateTrezorSignTxDataResponse {
try {
Logger.debug(`${nameof(AdaApi)}::${nameof(this.createTrezorSignTxData)} called`);

const config = getCardanoHaskellBaseConfig(
request.network
).reduce((acc, next) => Object.assign(acc, next), {});

const trezorSignTxPayload = await createTrezorSignTxPayload(
const trezorSignTxPayload = createTrezorSignTxPayload(
request.signRequest,
config.ByronNetworkId,
Number.parseInt(config.ChainNetworkId, 10),
Expand All @@ -803,17 +775,17 @@ export default class AdaApi {
}
}

async createLedgerSignTxData(
createLedgerSignTxData(
request: CreateLedgerSignTxDataRequest
): Promise<CreateLedgerSignTxDataResponse> {
): CreateLedgerSignTxDataResponse {
try {
Logger.debug(`${nameof(AdaApi)}::${nameof(this.createLedgerSignTxData)} called`);

const config = getCardanoHaskellBaseConfig(
request.network
).reduce((acc, next) => Object.assign(acc, next), {});

const ledgerSignTxPayload = await createLedgerSignTxPayload({
const ledgerSignTxPayload = createLedgerSignTxPayload({
signRequest: request.signRequest,
byronNetworkMagic: config.ByronNetworkId,
networkId: Number.parseInt(config.ChainNetworkId, 10),
Expand All @@ -833,6 +805,57 @@ export default class AdaApi {
}
}

createHwSignTxDataFromRawTx(
hw: 'ledger' | 'trezor',
request: CreateHWSignTxDataRequestFromRawTx
): (
{| hw: 'ledger', result: CreateLedgerSignTxDataResponse |}
| {| hw: 'trezor', result: CreateTrezorSignTxDataResponse |}
) {
try {
Logger.debug(`${nameof(AdaApi)}::${nameof(this.createHwSignTxDataFromRawTx)} called`);

const config = getCardanoHaskellBaseConfigCombined(request.network);
const protocolMagic = config.ByronNetworkId ?? fail('Missing ByronNetworkId in network config!');

const addressMap = s => request.addressingMap(s)?.path;

if (hw === 'ledger') {

const ledgerSignTxPayload = toLedgerSignRequest(
request.txBodyHex,
Number(config.ChainNetworkId),
protocolMagic,
addressMap,
request.senderUtxos,
request.additionalRequiredSigners ?? [],
);

Logger.debug(`${nameof(AdaApi)}::${nameof(this.createHwSignTxDataFromRawTx)} success: ` + stringifyData(ledgerSignTxPayload));
return { hw, result: { ledgerSignTxPayload } };
}
if (hw === 'trezor') {

const trezorSignTxPayload = toTrezorSignRequest(
request.txBodyHex,
Number(config.ChainNetworkId),
protocolMagic,
addressMap,
request.senderUtxos,
);
Logger.debug(`${nameof(AdaApi)}::${nameof(this.createHwSignTxDataFromRawTx)} success: ` + stringifyData(trezorSignTxPayload));
return { hw, result: { trezorSignTxPayload } };
}

throw new Error('Now supported HW type: ' + hw);

} catch (error) {
Logger.error(`${nameof(AdaApi)}::${nameof(this.createHwSignTxDataFromRawTx)} error: ` + stringifyError(error));
if (error instanceof LocalizableError) throw error;
throw new GenericApiError();
}
}

async createUnsignedTxForUtxos(
request: CreateUnsignedTxForUtxosRequest
): Promise<CreateUnsignedTxForUtxosResponse> {
Expand Down Expand Up @@ -1652,8 +1675,6 @@ export default class AdaApi {
neededHashes: new Set(),
wits: new Set(),
},
trezorTCatalystRegistrationTxSignData: undefined,
ledgerNanoCatalystRegistrationTxSignData: undefined,
});
} catch (error) {
Logger.error(`${nameof(AdaApi)}::${nameof(this.createSimpleTx)} error: ` + stringifyError(error));
Expand Down Expand Up @@ -2187,7 +2208,7 @@ export default class AdaApi {
);
}

// fixme: refactor this tmp function
// <TODO:TMP>
async _addressedUtxosWithSubmittedTxs(
originalUtxos: Array<CardanoAddressedUtxo>,
publicDeriverId: number,
Expand Down
14 changes: 14 additions & 0 deletions packages/yoroi-extension/app/api/ada/lib/cardanoCrypto/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ export function transactionHexToWitnessSet(txHex: string): string {
bytesToHex(Module.WalletV4.FixedTransaction.from_hex(txHex).raw_witness_set()));
}

export function transactionBodyHexToTransaction(txBodyHex: string): string {
return RustModule.WasmScope(Module =>
Module.WalletV4.FixedTransaction.new(
hexToBytes(txBodyHex),
Module.WalletV4.TransactionWitnessSet.new().to_bytes(),
true,
).to_hex());
}

export function transactionHexToBodyHex(txHex: string): string {
return RustModule.WasmScope(Module =>
bytesToHex(Module.WalletV4.FixedTransaction.from_hex(txHex).raw_body()));
}

export function transactionHexToHash(txHex: string): string {
return RustModule.WasmScope(Module =>
Module.WalletV4.hash_transaction(Module.WalletV4.FixedTransaction.from_hex(txHex).body()).to_hex());
Expand Down
Loading
Loading