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

eutxo lib and API usage update #3774

Draft
wants to merge 4 commits into
base: production
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -584,8 +584,6 @@ export async function newAdaUnsignedTxFromUtxo(
memPriceTo: 1000,
stepPriceFrom: 721,
stepPriceTo: 10000000,
// todo remove this after updating the eUtxo lib
coinsPerUtxoWord: Number(protocolParams.coinsPerUtxoByte) * 8 + 2,
};

const utxoSet = new LibUtxoSet(
Expand All @@ -594,7 +592,7 @@ export async function newAdaUnsignedTxFromUtxo(
)
);

const txBuilder = await TxBuilder.new(defaultNetworkConfig, utxoSet);
const txBuilder = await TxBuilder.new(defaultNetworkConfig);

// When both hash and datum are present - datum is added as extra witness
const extraWitnessDatumsPresent =
Expand Down Expand Up @@ -687,6 +685,21 @@ export async function newAdaUnsignedTxFromUtxo(
await txBuilder.calcScriptDataHash('default');
}

try {
await txBuilder.selectInputsFrom(utxoSet);
} catch (error) {
if (error instanceof LibNotEnoughMoneyToSendError) {
throw new NotEnoughMoneyToSendError();
}
if (error instanceof LibOverflowError) {
throw new AssetOverflowError();
}
if (String(error).includes('less than the minimum UTXO value')) {
throw new CannotSendBelowMinimumValueError();
}
throw error;
}

await txBuilder.addChangeAndFee(changeAddress);

let unsignedTx;
Expand Down Expand Up @@ -879,8 +892,6 @@ async function newAdaUnsignedTxFromUtxoForConnector(
memPriceTo: 1000,
stepPriceFrom: 721,
stepPriceTo: 10000000,
// todo remove this after updating the eUtxo lib
coinsPerUtxoWord: Number(protocolParams.coinsPerUtxoByte) * 8 + 2,
};

const utxoSet = new LibUtxoSet(
Expand All @@ -889,7 +900,7 @@ async function newAdaUnsignedTxFromUtxoForConnector(
)
);

const txBuilder = await TxBuilder.new(defaultNetworkConfig, utxoSet);
const txBuilder = await TxBuilder.new(defaultNetworkConfig);

await txBuilder.addRequiredInputs(
await Promise.all(
Expand Down Expand Up @@ -997,6 +1008,21 @@ async function newAdaUnsignedTxFromUtxoForConnector(
}
);

try {
await txBuilder.selectInputsFrom(utxoSet);
} catch (error) {
if (error instanceof LibNotEnoughMoneyToSendError) {
throw new NotEnoughMoneyToSendError();
}
if (error instanceof LibOverflowError) {
throw new AssetOverflowError();
}
if (String(error).includes('less than the minimum UTXO value')) {
throw new CannotSendBelowMinimumValueError();
}
throw error;
}

await txBuilder.addChangeAndFee(changeAddress);

const unsignedTx = await txBuilder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import type { CardanoTxRequest } from '../../../../../app/api/ada';
import type { NFTMetadata } from '../../../../../app/api/ada/lib/storage/database/primitives/tables';
import { getProtocolParameters } from '../yoroi/protocolParameters';
import { hexToBytes } from '../../../../../app/coreUtils';
import { transactionHexToHash } from '../../../../../app/api/ada/lib/cardanoCrypto/utils';

declare var chrome;

Expand Down Expand Up @@ -388,28 +389,23 @@ const Handlers = Object.freeze({
[ string /* tx hex */ ],
string,
>(async ({ wallet, message }) => {
const txBuffer = hexToBytes(message.params[0]);
const txHex = message.params[0];
await connectorSendTxCardano(
wallet,
txBuffer,
txHex,
new LocalStorageApi(),
);
const tx = RustModule.WalletV4.Transaction.from_bytes(
txBuffer
);
const id = RustModule.WalletV4.hash_transaction(tx.body()).to_hex();
try {
await connectorRecordSubmittedCardanoTransaction(
wallet,
tx,
txHex,
);
} catch (error) {
// eslint-disable-next-line no-console
console.error('error recording submitted tx', error);
}
tx.free();

return { ok: id };
return { ok: transactionHexToHash(txHex) };
}),

'ping': NewHandler.basic<void, boolean>(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { RustModule } from '../../../../../app/api/ada/lib/cardanoCrypto/rustLoa
import { emitUpdateToSubscriptions } from '../../subscriptionManager';
import LocalStorageApi from '../../../../../app/api/localStorage/index';
import { getCardanoStateFetcher } from '../../utils';
import { hexToBytes } from '../../../../../app/coreUtils';
import { bytesToHex, hexToBytes } from '../../../../../app/coreUtils';
import { getWalletsState } from '../utils';
import type { CardanoAddressedUtxo } from '../../../../../app/api/ada/transactions/types';

Expand Down Expand Up @@ -141,7 +141,7 @@ export const SignAndBroadcastTransaction: HandlerType<
try {
await connectorRecordSubmittedCardanoTransaction(
publicDeriver,
Scope.WalletV4.Transaction.from_hex(signedTxHex)
signedTxHex,
);
} catch (_error) {
// ignore
Expand Down Expand Up @@ -198,11 +198,11 @@ export const BroadcastTransaction: HandlerType<
});
try {
for (let i = 0; i < txs.length; i++) {
await RustModule.WasmScope(Scope => connectorRecordSubmittedCardanoTransaction(
await connectorRecordSubmittedCardanoTransaction(
publicDeriver,
Scope.WalletV4.Transaction.from_bytes(txs[i].encodedTx),
bytesToHex(txs[i].encodedTx),
addressedUtxoArray[i]
));
);
}
} catch (_error) {
// ignore
Expand Down
18 changes: 10 additions & 8 deletions packages/yoroi-extension/chrome/extension/connector/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -865,14 +865,14 @@ export async function connectorCreateCardanoTx(

export async function connectorSendTxCardano(
wallet: IPublicDeriver</* ConceptualWallet */>,
signedTx: Buffer,
signedTxHex: string,
localStorage: LocalStorageApi,
): Promise<void> {
await sendTx({
body: {
network: wallet.getParent().getNetworkInfo(),
id: transactionHexToHash(bytesToHex(signedTx)),
encodedTx: signedTx,
id: transactionHexToHash(signedTxHex),
encodedTx: bytesToHex(signedTxHex),
},
lastLaunchVersion: await localStorage.getLastLaunchVersion() ?? '',
currentLocale: await localStorage.getUserLocale() ?? '',
Expand All @@ -888,7 +888,7 @@ export async function connectorSendTxCardano(

export async function connectorRecordSubmittedCardanoTransaction(
publicDeriver: PublicDeriver<>,
tx: RustModule.WalletV4.Transaction,
sourceTxHex: string,
addressedUtxos?: ?Array<CardanoAddressedUtxo>,
) {
const withUtxos = asGetAllUtxos(publicDeriver);
Expand Down Expand Up @@ -916,18 +916,22 @@ export async function connectorRecordSubmittedCardanoTransaction(
submittedTxs,
);

const txId = transactionHexToHash(tx.to_hex());
const txId = transactionHexToHash(sourceTxHex);
const tx = RustModule.WalletV4.Transaction.from_hex(sourceTxHex);
const defaultToken = publicDeriver.getParent().defaultToken;
const defaults = {
defaultNetworkId: defaultToken.NetworkId,
defaultIdentifier: defaultToken.Identifier
};

const amount = new MultiToken([], defaults);
const fee = new MultiToken([], defaults);
const addresses = { from: [], to: [] };
let isIntraWallet = true;
const txBody = tx.body();

const fee = new MultiToken([], defaults);
fee.joinAddMutable(fee.createDefaultEntry(new BigNumber(txBody.fee().to_str())));

const usedUtxos = [];
for (const input of iterateLenGet(txBody.inputs())) {
const txHash = input.transaction_id().to_hex();
Expand Down Expand Up @@ -960,7 +964,6 @@ export async function connectorRecordSubmittedCardanoTransaction(
if (allAddresses.has(utxo.receiver)) {
amount.joinSubtractMutable(value);
}
fee.joinAddMutable(value);
}
for (const output of iterateLenGet(txBody.outputs())) {
const value = multiTokenFromCardanoValue(output.amount(), defaults);
Expand All @@ -975,7 +978,6 @@ export async function connectorRecordSubmittedCardanoTransaction(
} else {
isIntraWallet = false;
}
fee.joinSubtractMutable(value);
}

const withdrawalsData = iterateLenGetMap(txBody.withdrawals())
Expand Down
Loading
Loading