-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction_signature.go
64 lines (53 loc) · 1.43 KB
/
transaction_signature.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
package main
import (
"context"
"fmt"
"log"
"os"
"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"
"github.com/ethereum/go-ethereum/rlp"
)
func transactionSignature() {
var (
ctx = context.Background()
url = "https://eth-mainnet.g.alchemy.com/v2/" + os.Getenv("ALCHEMY_ID")
client, err = ethclient.DialContext(ctx, url)
)
if err != nil {
log.Fatal(err)
}
txHash := common.HexToHash("0xec9db5bfbcd30ad2e3070b626ed4f78abce88687c5d1eb23464242be5edcb537")
tx, _, err := client.TransactionByHash(context.Background(), txHash)
if err != nil {
log.Fatal(err)
}
v, r, s := tx.RawSignatureValues()
R := r.Bytes()
S := s.Bytes()
V := byte(v.Uint64())
sig := make([]byte, 65)
copy(sig[32-len(R):32], R)
copy(sig[64-len(S):64], S)
sig[64] = V
signer := types.NewLondonSigner(tx.ChainId())
hash := signer.Hash(tx)
rawTx, err := rlp.EncodeToBytes(tx)
if err != nil {
panic(err)
}
fmt.Printf("%x\n", rawTx)
publicKeyBytes, err := crypto.Ecrecover(hash.Bytes(), sig)
if err != nil {
log.Fatal(err)
}
fmt.Printf("publicKey = %x\n", publicKeyBytes)
publicKeyECDSA, err := crypto.UnmarshalPubkey(publicKeyBytes)
if err != nil {
log.Fatal(err)
}
address := crypto.PubkeyToAddress(*publicKeyECDSA).Hex()
fmt.Println("address =", address) // 0x5827c0ccf705720cfa395e3fb2dcc449aeef331c
}