Created
November 14, 2025 09:52
-
-
Save cuiweixie/62f3114d29fe26e4ebe55aee42df0ca3 to your computer and use it in GitHub Desktop.
get enode url from nodekeyhex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "crypto/ecdsa" | |
| "fmt" | |
| "github.com/ethereum/go-ethereum/crypto" | |
| "github.com/ethereum/go-ethereum/p2p/enode" | |
| ) | |
| func main() { | |
| // 你的 nodekeyhex(私钥) | |
| nodekeyHex := "e95afe4502d7b84b03856047e8190a5ba4db55dd16e40945163e5cd9ed620227" | |
| // 1. 加载私钥 | |
| privateKey, err := crypto.HexToECDSA(nodekeyHex) | |
| if err != nil { | |
| panic(err) | |
| } | |
| // 2. 获取公钥 | |
| publicKey := privateKey.Public().(*ecdsa.PublicKey) | |
| nodeID := enode.PubkeyToIDV4(publicKey) | |
| // 3. 获取公钥字节(用于 enode URL) | |
| // enode URL 使用公钥字节(去掉 0x04 前缀),而不是 node ID 哈希 | |
| pubkeyBytes := crypto.FromECDSAPub(publicKey)[1:] // 去掉 0x04 前缀 | |
| // 4. 构建 enode URL | |
| host := "op-geth-seq3" | |
| port := 30303 | |
| enodeURL := fmt.Sprintf("enode://%x@%s:%d", pubkeyBytes, host, port) | |
| fmt.Println("Node ID:", nodeID.String()) | |
| fmt.Println("Enode URL:", enodeURL) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment