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

feat: calculate tx hash before seal #337

Merged
merged 5 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion src/background/controller/provider/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ class ProviderController extends BaseController {
const dataValue = transactionParams.data || '0x';
// console.log('transactionParams ', transactionParams)
let result = await Wallet.dapSendEvmTX(to, gas, value, dataValue);
if (!result.startsWith('0x')) {
if (result && !result.startsWith('0x')) {
result = '0x' + result;
}
return result;
Expand Down
42 changes: 29 additions & 13 deletions src/background/controller/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ethErrors } from 'eth-rpc-errors';
import * as ethUtil from 'ethereumjs-util';
import { getApp } from 'firebase/app';
import { getAuth } from 'firebase/auth/web-extension';
import { encode } from 'rlp';
import web3, { TransactionError } from 'web3';

import {
Expand Down Expand Up @@ -71,7 +72,6 @@ import { getStoragedAccount } from '../utils/getStoragedAccount';

import BaseController from './base';
import provider from './provider';

interface Keyring {
type: string;
getAccounts(): Promise<string[]>;
Expand Down Expand Up @@ -1651,7 +1651,6 @@ export class WalletController extends BaseController {
});
}

console.log('v2data ', transformedArray, address);
const active = await userWalletService.getActiveWallet();
if (!active) {
// userInfoService.addUserId(v2data.data.id);
Expand All @@ -1664,7 +1663,6 @@ export class WalletController extends BaseController {
getUserWallets = async (): Promise<WalletResponse[]> => {
const network = await this.getNetwork();
const wallets = await userWalletService.getUserWallets(network);
console.log('getUserWallets ', wallets);
if (!wallets[0]) {
await this.refreshUserWallets();
const data = await userWalletService.getUserWallets(network);
Expand Down Expand Up @@ -1705,7 +1703,6 @@ export class WalletController extends BaseController {

getCurrentWallet = async () => {
const wallet = await userWalletService.getCurrentWallet();
console.log('getCurrentWallet ', wallet);
if (!wallet.address) {
const network = await this.getNetwork();
await this.refreshUserWallets();
Expand Down Expand Up @@ -2218,24 +2215,43 @@ export class WalletController extends BaseController {
fcl.arg(gasLimit, t.UInt64),
]);

let evmAddress = await this.getEvmAddress();

mixpanelTrack.track('ft_transfer', {
from_address: await this.getEvmAddress(),
from_address: evmAddress,
to_address: to,
amount: parseFloat(amount),
ft_identifier: 'FLOW',
type: 'evm',
});

console.log('result ', result);
const res = await fcl.tx(result).onceSealed();
const transactionExecutedEvent = res.events.find((event) =>
event.type.includes('TransactionExecuted')
);
if (evmAddress.startsWith('0x')) {
evmAddress = evmAddress.substring(2);
}

const addressNonce = await this.getNonce(evmAddress);

if (transactionExecutedEvent) {
const hash = transactionExecutedEvent.data.hash;
const hashHexString = hash.map((num) => parseInt(num).toString(16).padStart(2, '0')).join('');
const keccak256 = (data: Buffer) => {
return ethUtil.keccak256(data);
};

// [nonce, gasPrice, gasLimit, to.addressData, value, data, v, r, s]
const transaction = [
Number(addressNonce), // nonce
0, // Fixed value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use the variable instead of magic number ?

const gasPrice = 0
const transaction = [
      Number(addressNonce), // nonce
      gasPrice, 
      ...
]

gasLimit, // Gas Limit
Buffer.from(to, 'hex'), // To Address
BigInt(amount * 10 ** 18), // Value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BigInt(number)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the amount is already a number

Buffer.from(dataArray), // Call Data
255, // Fixed value
BigInt('0x' + evmAddress), // From Account
5, // SubType
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for 255 and 5.

const directCallTxType = 255
const contractCallSubType = 5

];

const encodedData = encode(transaction);
const hash = keccak256(Buffer.from(encodedData));
const hashHexString = Buffer.from(hash).toString('hex');
if (hashHexString) {
return hashHexString;
} else {
console.log('Transaction Executed event not found');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if hashHexString is nil, we need handle the error response.

Expand Down
1 change: 0 additions & 1 deletion src/background/service/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ class Transaction {
};

setTransaction = (data, network: string) => {
console.log('data ', data);
const txList: TransferItem[] = [];
if (data.transactions && data.transactions.length > 0) {
data.transactions.forEach(async (tx) => {
Expand Down
1 change: 0 additions & 1 deletion src/background/service/userWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,6 @@ class UserWallet {
};

setEvmAddress = (address: string, emoji) => {
console.log('emoji setEvmAddress ', emoji);
if (address.length > 20) {
this.store.evmWallet.address = address;
this.store.evmWallet.name = emoji[9].name;
Expand Down
Loading