// Copyright 2023 @soul-wallet/extension-base // SPDX-License-Identifier: Apache-2.0 import BigN from 'bignumber.js'; import BNEther from 'bn.js'; import { ethers } from 'ethers'; import { SignedTransaction } from 'web3-core'; import { hexStripPrefix, numberToHex } from '@polkadot/util'; const hexToNumberString = (s: string): string => { const temp = parseInt(s, 16); if (isNaN(temp)) { return '0'; } else { return temp.toString(); } }; export class Transaction { readonly nonce: string; readonly gasPrice: string; readonly gas: string; readonly to: string; readonly value: string; readonly data: string; readonly ethereumChainId: string; readonly isSafe: boolean; constructor (nonce: string, gasPrice: string, gas: string, to: string, value: string, data: string, ethereumChainId: string) { this.nonce = hexToNumberString(nonce); this.gasPrice = hexToNumberString(gasPrice); this.gas = hexToNumberString(gas); this.to = to; this.value = hexToNumberString(value); this.data = data || ''; this.ethereumChainId = parseInt(ethereumChainId, 16).toString(); this.isSafe = true; } } export const anyNumberToBN = (value?: string | number | BNEther): BigN => { if (typeof value === 'string' || typeof value === 'number') { return new BigN(value); } else if (typeof value === 'undefined') { return new BigN(0); } else { return new BigN(value.toNumber()); } }; export const createTransactionFromRLP = (rlp: string): Transaction | null => { try { const transaction = ethers.Transaction.from(rlp); const nonce = transaction.nonce.toString(16); const gasPrice = transaction.gasPrice?.toString(16) || ''; const gas = transaction.gasLimit.toString(16); const to = transaction.to || ''; const value = transaction.value.toString(16); const data = transaction.data; const ethereumChainId = transaction.chainId.toString(16); return new Transaction(nonce, gasPrice, gas, to, value, data, ethereumChainId); } catch (e) { console.log((e as Error).message); return null; } }; export const signatureToHex = (sig: SignedTransaction): string => { const v = parseInt(sig.v); const r = hexStripPrefix(sig.r); const s = hexStripPrefix(sig.s); const hexR = r.length % 2 === 1 ? `0${r}` : r; const hexS = s.length % 2 === 1 ? `0${s}` : s; const hexV = hexStripPrefix(numberToHex(v)); return hexR + hexS + hexV; };