Skip to content

我调用了硬件的签名成功签名,但是无法将交易广播出去 #474

Open

Description

@wangfeng7731

const guiji = (): void => {
// let str=getFirstTwoChars('0xd11A69E04F8904c5966650746Ef4513B8C531a1c')

// let dt=usdtToWei(2,str);
// console.log(dt)

// ERC-20 ABI
const erc20Abi = [
"function transfer(address to, uint256 value) public returns (bool)"
];

//ERC-20 合约地址
const erc20ContractAddress = "0x55d398326f99059ff775485246999027b3197955";
const mainnetProvider = ethers.getDefaultProvider('https://bsc-dataseed.binance.org');

// const noc=mainnetProvider.getTransactionCount("0xd11A69E04F8904c5966650746Ef4513B8C531a1c")
// 创建 ERC-20 合约实例
const erc20Contract = new ethers.Contract(erc20ContractAddress, erc20Abi, mainnetProvider);

// 接收地址
const toAddress = "0xd11A69E04F8904c5966650746Ef4513B8C531a1c";

// 转账金额:1000 USDT,假设 USDT 有 18 位小数
const amount = ethers.parseUnits("1", 18);

// 编码交易数据
const encodedData = erc20Contract.interface.encodeFunctionData("transfer", [toAddress, amount]);

console.log(encodedData);
// 1. Create transaction parameters
const txParams = {
to: '0x55d398326f99059fF775485246999027B3197955',
value: '0x0',
data: encodedData,
chainId: 56,
nonce: '0xca3e',
// maxFeePerGas: '0x14',
// maxPriorityFeePerGas: '0x0',
gasLimit: '0xca3e',
gasPrice: "0xb2d05e00"
};
HardwareSDK.evmSignTransaction(data.value.payload[0].connectId, data.value.payload[0].deviceId, {
path: "m/44'/60'/0'/0/10",
transaction: txParams,
chainId: 56
}).then((result11: any) => {
// 在这里获取成功的结果
console.log('成功:', result11.success);

if (result11.success) {
  // 3. Build signed transaction
  const sig = splitSignature(result11.payload);
  // 序列化交易

  const rawTx = serialize({
to: '0x55d398326f99059fF775485246999027B3197955',
value: '0x0',
data: encodedData,
chainId: 56,
nonce: 0,
// maxFeePerGas: '0x14',
// maxPriorityFeePerGas: '0x0',
gasLimit: '0xca3e',
gasPrice: "0xb2d05e00"

}, sig);

  /// 使用 ethers.js 广播交易
  const provider = new ethers.JsonRpcProvider('https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID');
  const wallet = new ethers.Wallet('', provider);
  wallet.sendTransaction({ data: rawTx })

.then((tx) => {
console.log('交易已广播,交易哈希:', tx.hash);
})
.catch((error) => {
console.error('广播失败:', error);
});

} else {
  ElMessage({
    message: h('p', { style: 'line-height: 1; font-size: 14px' }, [
      h('span', { style: 'color: red' }, '地址归集失败,请重新连接设备!')
    ]),
  })
}

})
.catch((error11: any) => {
// 在这里处理错误
console.error('失败:', error11);
ElMessage({
message: h('p', { style: 'line-height: 1; font-size: 14px' }, [
h('span', { style: 'color: red' }, '地址归集失败,请重新连接设备!')
]),
})
});;

}

Activity

  1. wangfeng7731 commented on May 23, 2025

    @wangfeng7731
    Author

    Image

  2. wangfeng7731 commented on May 23, 2025

    @wangfeng7731
    Author

    我想要实现的功能是通过硬件钱包生成ETH网络的地址,通过 HardwareSDK.evmSignTransaction接口获得交易签名,然后广播交易完成转账,转账的网络是BSC;我现在已经正常的唤醒Onekey的硬件,获得了签名数据,但是无法完成广播交易,使用官方示例中的广播也无法正常发送广播交易

  3. wangfeng7731 commented on May 24, 2025

    @wangfeng7731
    Author

    import type { UnsignedTransaction } from '@ethersproject/transactions';
    import { TransactionTypes, serialize } from '@ethersproject/transactions';
    import { keccak256 } from '@ethersproject/keccak256';
    function buildSignedTxFromSignatureEvm({
    tx,
    signature,
    }: {
    tx: UnsignedTransaction;
    signature: {
    v: string | number; // '0x11' , '17', 17
    r: string; // prefix 0x
    s: string; // prefix 0x
    };
    }) {
    const { r, s, v } = signature;
    /**

    • sdk legacy return {v,r,s}; eip1559 return {recoveryParam,r,s}
    • splitSignature auto convert v to recoveryParam
      */
      const sig = splitSignature({
      v: Number(v),
      r,
      s,
      });
      const rawTx = serialize(tx, sig);
      const txid = keccak256(rawTx);
      return {
      rawTx,
      txid,
      };
      }

    // 1. Create transaction parameters
    const txParams = {
    to: '0x7314e0f1c0e28474bdb6be3e2c3e0453255188f8',
    value: '0x0', // Default to 0 if no value
    data: '0x', // Empty data
    chainId: 1,
    nonce: '0x0',
    // For EIP-1559 transaction
    maxFeePerGas: '0x14', // 20
    maxPriorityFeePerGas: '0x0', // 0
    gasLimit: '0x5208', // 21000
    // For legacy transaction
    // gasPrice: '0x14', //
    };

    // 2. Sign the transaction
    const result = await HardwareSDK.evmSignTransaction(connectId, deviceId, {
    path: "m/44'/60'/0'",
    transaction: txParams,
    ...deviceCommonParams
    });

    // 3. Build signed transaction
    const baseTx = {
    to: txParams.to,
    nonce: parseInt(txParams.nonce, 16),
    gasLimit: txParams.gasLimit
    data: txParams.data,
    value: txParams.value,
    chainId: txParams.chainId,
    };

    // Add EIP-1559 specific fields
    const isEIP1559 = txParams?.maxFeePerGas || txParams?.maxPriorityFeePerGas;
    if (isEIP1559) {
    Object.assign(baseTx, {
    type: TransactionTypes.eip1559,
    maxFeePerGas: txParams?.maxFeePerGas
    maxPriorityFeePerGas: txParams?.maxPriorityFeePerGas
    });
    } else {
    Object.assign(baseTx, {
    gasPrice: txParams.gasPrice,
    });
    }

    // 4. Build final transaction with signature
    const { rawTx, txid } = buildSignedTxFromSignatureEvm({
    tx: baseTx,
    signature: result.payload
    });

    这是官方的示例代码,但是里面的splitSignature没有声明,导致无法正常运行

  4. ByteZhang1024 commented on Jun 12, 2025

    @ByteZhang1024
    Contributor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions