Skip to content

Commit

Permalink
Tweak nodekeys according to node location
Browse files Browse the repository at this point in the history
  • Loading branch information
wizeguyy committed Sep 25, 2023
1 parent 9be7a33 commit 08235be
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package utils

import (
"crypto/ecdsa"
"crypto/elliptic"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -718,10 +719,19 @@ func setNodeKey(ctx *cli.Context, cfg *p2p.Config) {
case file != "" && hex != "":
Fatalf("Options %q and %q are mutually exclusive", NodeKeyFileFlag.Name, NodeKeyHexFlag.Name)
case file != "":
curve := elliptic.P256()
order := curve.Params().P
// Load the node private key from the file
if key, err = crypto.LoadECDSA(file); err != nil {
Fatalf("Option %q: %v", NodeKeyFileFlag.Name, err)
}
cfg.PrivateKey = key
pkey := big.NewInt(0).Mod(key.D, order)
// Tweak the private key to be unique for each location
locationTweak := big.NewInt(0).SetBytes(crypto.Keccak256([]byte(common.NodeLocation.Name())))
locationTweak.Mod(locationTweak, order)
tweakedKey := pkey.Mul(pkey, locationTweak)
key.D = tweakedKey
key.PublicKey.X, key.PublicKey.Y = curve.ScalarBaseMult(tweakedKey.Bytes())
case hex != "":
if key, err = crypto.HexToECDSA(hex); err != nil {
Fatalf("Option %q: %v", NodeKeyHexFlag.Name, err)
Expand Down

0 comments on commit 08235be

Please sign in to comment.