-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
68 lines (59 loc) · 1.74 KB
/
main.go
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"context"
"flag"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"log"
"os"
)
func main() {
nodeURL := os.Getenv("INFURA_URL")
txnHash := flag.String("txnHash", "", "Transaction hash")
flag.Parse()
client, err := ethclient.Dial(nodeURL)
if err != nil {
log.Fatalf("Failed to connect to the Ethereum client: %v", err)
}
txHash := common.HexToHash(*txnHash)
recoverPublicKey(client, txHash)
}
func getSignature(tx *types.Transaction) []byte {
v, r, s := tx.RawSignatureValues()
var recoveryID byte
if v.Sign() == 0 || v.Sign() == 1 {
recoveryID = byte(v.Uint64())
} else {
recoveryID = byte(v.Uint64() - 27)
}
signature := append(r.Bytes(), s.Bytes()...)
signature = append(signature, recoveryID)
log.Printf("Signature: 0x%x", signature)
return signature
}
func getTxHash(tx *types.Transaction) []byte {
signer := types.NewCancunSigner(tx.ChainId())
hash := signer.Hash(tx)
log.Printf("Serialized transaction hash: 0x%x", hash)
return hash.Bytes()
}
func recoverPublicKey(client *ethclient.Client, txHash common.Hash) {
tx, _, err := client.TransactionByHash(context.Background(), txHash)
if err != nil {
log.Fatalf("Failed to fetch transaction: %v", err)
}
signature := getSignature(tx)
hash := getTxHash(tx)
publicKey, err := crypto.Ecrecover(hash, signature)
if err != nil {
log.Fatalf("Failed to recover public key: %v", err)
}
pubKey, err := crypto.UnmarshalPubkey(publicKey)
if err != nil {
log.Fatalf("Failed to unmarshal public key: %v", err)
}
log.Printf("Recovered public key: 0x%x", pubKey)
log.Printf("Recovered address: 0x%x", crypto.PubkeyToAddress(*pubKey))
}