Skip to content

Commit 3ac0fc9

Browse files
committed
Merge remote-tracking branch 'zw/master' into causulity-graph
2 parents 57683d4 + 2a48ce0 commit 3ac0fc9

File tree

5 files changed

+51
-40
lines changed

5 files changed

+51
-40
lines changed

event.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package nostr
22

33
import (
4-
"crypto/sha256"
4+
"fmt"
55
"encoding/hex"
66
"strconv"
77

88
"github.com/mailru/easyjson"
9+
"github.com/ethereum/go-ethereum/crypto"
910
)
1011

1112
// Event represents a Nostr event.
@@ -26,19 +27,17 @@ func (evt Event) String() string {
2627

2728
// GetID computes the event ID and returns it as a hex string.
2829
func (evt *Event) GetID() string {
29-
h := sha256.Sum256(evt.Serialize())
30-
return hex.EncodeToString(h[:])
30+
message := evt.Serialize()
31+
prefixedMessage := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(message), message)
32+
h:= crypto.Keccak256Hash([]byte(prefixedMessage))
33+
return hex.EncodeToString(h.Bytes())
3134
}
3235

3336
// CheckID checks if the implied ID matches the given ID more efficiently.
3437
func (evt *Event) CheckID() bool {
35-
if len(evt.ID) != 64 {
36-
return false
37-
}
38-
39-
ser := make([]byte, 0, 100+len(evt.Content)+len(evt.Tags)*80)
40-
ser = serializeEventInto(evt, ser)
41-
h := sha256.Sum256(ser)
38+
message := evt.Serialize()
39+
prefixedMessage := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(message), message)
40+
h:= crypto.Keccak256Hash([]byte(prefixedMessage))
4241

4342
const hextable = "0123456789abcdef"
4443

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ require (
1515
github.com/dgraph-io/badger/v4 v4.5.0
1616
github.com/dgraph-io/ristretto v1.0.0
1717
github.com/elnosh/gonuts v0.3.1-0.20250123162555-7c0381a585e3
18+
github.com/ethereum/go-ethereum v1.15.5
1819
github.com/fiatjaf/eventstore v0.16.2
1920
github.com/fiatjaf/khatru v0.17.4
2021
github.com/gomarkdown/markdown v0.0.0-20241205020045-f7e15b2f3e62
@@ -63,6 +64,7 @@ require (
6364
github.com/google/uuid v1.6.0 // indirect
6465
github.com/gorilla/css v1.0.1 // indirect
6566
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
67+
github.com/holiman/uint256 v1.3.2 // indirect
6668
github.com/josharian/intern v1.0.0 // indirect
6769
github.com/klauspost/compress v1.18.0 // indirect
6870
github.com/klauspost/cpuid/v2 v2.2.10 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymF
9898
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
9999
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
100100
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
101+
github.com/ethereum/go-ethereum v1.15.5 h1:Fo2TbBWC61lWVkFw9tsMoHCNX1ndpuaQBRJ8H6xLUPo=
102+
github.com/ethereum/go-ethereum v1.15.5/go.mod h1:1LG2LnMOx2yPRHR/S+xuipXH29vPr6BIH6GElD8N/fo=
101103
github.com/fasthttp/websocket v1.5.12 h1:e4RGPpWW2HTbL3zV0Y/t7g0ub294LkiuXXUuTOUInlE=
102104
github.com/fasthttp/websocket v1.5.12/go.mod h1:I+liyL7/4moHojiOgUOIKEWm9EIxHqxZChS+aMFltyg=
103105
github.com/fiatjaf/eventstore v0.16.2 h1:h4rHwSwPcqAKqWUsAbYWUhDeSgm2Kp+PBkJc3FgBYu4=
@@ -149,6 +151,8 @@ github.com/gorilla/css v1.0.1/go.mod h1:BvnYkspnSzMmwRK+b8/xgNPLiIuNZr6vbZBTPQ2A
149151
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
150152
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
151153
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
154+
github.com/holiman/uint256 v1.3.2 h1:a9EgMPSC1AAaj1SZL5zIQD3WbwTuHrMGOerLjGmM/TA=
155+
github.com/holiman/uint256 v1.3.2/go.mod h1:EOMSn4q6Nyt9P6efbI3bueV4e1b3dGlUCXeiRV4ng7E=
152156
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
153157
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
154158
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=

keys.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"math/big"
99

1010
"github.com/btcsuite/btcd/btcec/v2"
11-
"github.com/btcsuite/btcd/btcec/v2/schnorr"
1211
)
1312

1413
func GeneratePrivateKey() string {
@@ -35,7 +34,7 @@ func GetPublicKey(sk string) (string, error) {
3534
}
3635

3736
_, pk := btcec.PrivKeyFromBytes(b)
38-
return hex.EncodeToString(schnorr.SerializePubKey(pk)), nil
37+
return hex.EncodeToString(pk.SerializeUncompressed()), nil
3938
}
4039

4140
func IsValidPublicKey(pk string) bool {
@@ -44,6 +43,7 @@ func IsValidPublicKey(pk string) bool {
4443
}
4544

4645
v, _ := hex.DecodeString(pk)
47-
_, err := schnorr.ParsePubKey(v)
46+
_, err := btcec.ParsePubKey(v)
47+
4848
return err == nil
4949
}

signature.go

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,51 @@
33
package nostr
44

55
import (
6-
"crypto/sha256"
76
"encoding/hex"
87
"fmt"
8+
"strings"
99

10-
"github.com/btcsuite/btcd/btcec/v2"
11-
"github.com/btcsuite/btcd/btcec/v2/schnorr"
10+
"github.com/ethereum/go-ethereum/crypto"
11+
//"github.com/ethereum/go-ethereum/common/hexutil"
1212
)
1313

1414
// CheckSignature checks if the event signature is valid for the given event.
1515
// It won't look at the ID field, instead it will recompute the id from the entire event body.
1616
// If the signature is invalid bool will be false and err will be set.
1717
func (evt Event) CheckSignature() (bool, error) {
1818
// read and check pubkey
19-
pk, err := hex.DecodeString(evt.PubKey)
20-
if err != nil {
21-
return false, fmt.Errorf("event pubkey '%s' is invalid hex: %w", evt.PubKey, err)
22-
}
23-
24-
pubkey, err := schnorr.ParsePubKey(pk)
25-
if err != nil {
26-
return false, fmt.Errorf("event has invalid pubkey '%s': %w", evt.PubKey, err)
27-
}
19+
address := "0x" + evt.PubKey
2820

2921
// read signature
30-
s, err := hex.DecodeString(evt.Sig)
22+
//sig, err := hexutil.Decode(evt.Sig)
23+
sig, err := hex.DecodeString(evt.Sig)
3124
if err != nil {
3225
return false, fmt.Errorf("signature '%s' is invalid hex: %w", evt.Sig, err)
3326
}
34-
sig, err := schnorr.ParseSignature(s)
35-
if err != nil {
36-
return false, fmt.Errorf("failed to parse signature: %w", err)
27+
if sig[64] >= 27 {
28+
sig[64] -= 27
3729
}
3830

3931
// check signature
40-
hash := sha256.Sum256(evt.Serialize())
41-
return sig.Verify(hash[:], pubkey), nil
32+
message := evt.Serialize()
33+
prefixedMessage := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(message), message)
34+
hash := crypto.Keccak256Hash([]byte(prefixedMessage))
35+
36+
pubKey, err := crypto.SigToPub(hash.Bytes(), sig)
37+
if err != nil {
38+
return false, fmt.Errorf("failed to recover public key: %w", err)
39+
}
40+
41+
recoveredAddr := crypto.PubkeyToAddress(*pubKey).Hex()
42+
43+
return (recoveredAddr == address), nil
4244
}
4345

4446
// Sign signs an event with a given privateKey.
4547
// It sets the event's ID, PubKey, and Sig fields.
4648
// Returns an error if the private key is invalid or if signing fails.
4749
func (evt *Event) Sign(secretKey string) error {
48-
s, err := hex.DecodeString(secretKey)
50+
s, err := crypto.HexToECDSA(secretKey)
4951
if err != nil {
5052
return fmt.Errorf("Sign called with invalid secret key '%s': %w", secretKey, err)
5153
}
@@ -54,18 +56,22 @@ func (evt *Event) Sign(secretKey string) error {
5456
evt.Tags = make(Tags, 0)
5557
}
5658

57-
sk, pk := btcec.PrivKeyFromBytes(s)
58-
pkBytes := pk.SerializeCompressed()
59-
evt.PubKey = hex.EncodeToString(pkBytes[1:])
59+
evt.PubKey = crypto.PubkeyToAddress(s.PublicKey).Hex()
60+
evt.PubKey = strings.TrimPrefix(crypto.PubkeyToAddress(s.PublicKey).Hex(), "0x")
61+
62+
63+
message := evt.Serialize()
64+
prefixedMessage := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(message), message)
65+
h:= crypto.Keccak256Hash([]byte(prefixedMessage))
6066

61-
h := sha256.Sum256(evt.Serialize())
62-
sig, err := schnorr.Sign(sk, h[:], schnorr.FastSign())
67+
sig, err := crypto.Sign(h.Bytes(), s)
6368
if err != nil {
64-
return err
69+
return fmt.Errorf("failed to sign: %w", err)
6570
}
6671

67-
evt.ID = hex.EncodeToString(h[:])
68-
evt.Sig = hex.EncodeToString(sig.Serialize())
72+
evt.ID = hex.EncodeToString(h.Bytes())
73+
//evt.Sig = hexutil.Encode(sig)
74+
evt.Sig = hex.EncodeToString(sig)
6975

7076
return nil
7177
}

0 commit comments

Comments
 (0)