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 3 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
3 changes: 3 additions & 0 deletions src/background/controller/provider/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ class ProviderController extends BaseController {
const dataValue = transactionParams.data || '0x';
// console.log('transactionParams ', transactionParams)
let result = await Wallet.dapSendEvmTX(to, gas, value, dataValue);
if (!result) {
throw new Error('Transaction hash is null or undefined');
}
if (!result.startsWith('0x')) {
result = '0x' + result;
}
Expand Down
54 changes: 35 additions & 19 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,33 +2215,52 @@ 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 directCallTxType = 255;
const contractCallSubType = 5;
const noceNumber = Number(addressNonce);
const gasPrice = 0;
const transactionValue = BigInt(amount * 10 ** 18);
Copy link
Contributor

Choose a reason for hiding this comment

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

Above we already convert hex to number, we can use it redirectly.
the value*10**18 is the example code, I don't wanna add web3js lib.

   // Convert the hex value to number
      const number = web3.utils.hexToNumber(value);

const transaction = [
noceNumber, // nonce
gasPrice, // Fixed value
gasLimit, // Gas Limit
Buffer.from(to, 'hex'), // To Address
transactionValue, // Value
Buffer.from(dataArray), // Call Data
directCallTxType, // Fixed value
BigInt('0x' + evmAddress), // From Account
contractCallSubType, // SubType
];

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');
return null;
}

// const transaction = await fcl.tx(result).onceSealed();
// console.log('transaction ', transaction);

// return result;
};

getBalance = async (hexEncodedAddress: string): Promise<string> => {
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