抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

Tx Hash -> Public Key

从一笔链上的交易哈希获取签名的公钥。

code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { ethers } from "ethers";

// provider
const connection = ""; //rpc
const provider = new ethers.providers.JsonRpcProvider(connection);

// Tx-Hash
const txHash = "";

// 获取交易信息
const transaction = await provider.getTransaction(txHash);
// console.log(`transaction => `,transaction); //打印交易详情

// txData 对象内的字段缺一不可
const txData = {
// gasPrice: transaction.gasPrice, // 这个不需要,否则会报错
gasLimit: transaction.gasLimit,
value: transaction.value,
nonce: transaction.nonce,
data: transaction.data,
to: transaction.to,
chainId: transaction.chainId,
type: transaction.type,
maxFeePerGas: transaction.maxFeePerGas,
maxPriorityFeePerGas: transaction.maxPriorityFeePerGas,
}

// 对txData交易对象序列化为其原始二进制格式
const signingData = ethers.utils.serializeTransaction(txData);

// 对序列化后的交易对象进行hash
const msgHash = ethers.utils.keccak256(signingData);

// 获取交易详情中的 r s v
const signature = {r: transaction.r, s: transaction.s, v: transaction.v};

// 获取原生未压缩的公钥,即 0x04 开头的
let rawPublicKey = ethers.utils.recoverPublicKey(msgHash, signature);
console.log(`未压缩公钥:\n ${rawPublicKey}`);

// 把开头的 0x04 字段删掉 ===》 将 `0x04` 替换成 `0x`
rawPublicKey = `0x${rawPublicKey.slice(4)}`;
console.log(`经压缩公钥:\n ${rawPublicKey}`);

// 公钥转钱包地址
let address = ethers.utils.keccak256(rawPublicKey);
console.log(`Address:\n 0x${address.slice(address.length - 40)}`);

package.json:

1
2
3
4
5
6
{
"type": "module",
"dependencies": {
"ethers": "^5.7.2"
}
}

签名原理

评论



政策 · 统计 | 本站使用 Volantis 主题设计