|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "crypto/sha256" |
| 6 | + "encoding/binary" |
| 7 | + "encoding/hex" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "github.com/btcsuite/btcd/btcec" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + EventSetMetadata uint8 = 0 |
| 15 | + EventTextNote uint8 = 1 |
| 16 | + EventDelete uint8 = 2 |
| 17 | + EventRecommendServer uint8 = 3 |
| 18 | +) |
| 19 | + |
| 20 | +type Event struct { |
| 21 | + Pubkey string `db:"pubkey"` |
| 22 | + Time uint32 `db:"time"` |
| 23 | + |
| 24 | + Kind uint8 `db:"kind"` |
| 25 | + // - set_metadata |
| 26 | + // - text_note |
| 27 | + // - delete |
| 28 | + |
| 29 | + Content string `db:"content"` |
| 30 | + Signature string `db:"signature"` |
| 31 | +} |
| 32 | + |
| 33 | +// Serialize outputs a byte array that can be hashed/signed to identify/authenticate |
| 34 | +// this event. An error will be returned if anything is malformed. |
| 35 | +func (evt *Event) Serialize() ([]byte, error) { |
| 36 | + b := bytes.Buffer{} |
| 37 | + |
| 38 | + // version: 0 |
| 39 | + b.Write([]byte{0}) |
| 40 | + |
| 41 | + // pubkey |
| 42 | + pubkeyb, err := hex.DecodeString(evt.Pubkey) |
| 43 | + if err != nil { |
| 44 | + return nil, err |
| 45 | + } |
| 46 | + pubkey, err := btcec.ParsePubKey(pubkeyb, btcec.S256()) |
| 47 | + if err != nil { |
| 48 | + return nil, fmt.Errorf("error parsing pubkey: %w", err) |
| 49 | + } |
| 50 | + if evt.Pubkey != hex.EncodeToString(pubkey.SerializeCompressed()) { |
| 51 | + return nil, fmt.Errorf("pubkey is not serialized in compressed format") |
| 52 | + } |
| 53 | + if _, err = b.Write(pubkeyb); err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + |
| 57 | + // time |
| 58 | + var timeb [4]byte |
| 59 | + binary.BigEndian.PutUint32(timeb[:], evt.Time) |
| 60 | + if _, err := b.Write(timeb[:]); err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + |
| 64 | + // kind |
| 65 | + var kindb [1]byte |
| 66 | + kindb[0] = evt.Kind |
| 67 | + if _, err := b.Write(kindb[:]); err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + |
| 71 | + // content |
| 72 | + if _, err = b.Write([]byte(evt.Content)); err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + |
| 76 | + return b.Bytes(), nil |
| 77 | +} |
| 78 | + |
| 79 | +// CheckSignature checks if the signature is valid for the serialized event. |
| 80 | +// It will call Serialize() and return an error if that raises an error or if |
| 81 | +// the signature itself is invalid. |
| 82 | +func (evt Event) CheckSignature() (bool, error) { |
| 83 | + serialized, err := evt.Serialize() |
| 84 | + if err != nil { |
| 85 | + return false, fmt.Errorf("serialization error: %w", err) |
| 86 | + } |
| 87 | + |
| 88 | + // validity of these is checked by Serialize() |
| 89 | + pubkeyb, _ := hex.DecodeString(evt.Pubkey) |
| 90 | + pubkey, _ := btcec.ParsePubKey(pubkeyb, btcec.S256()) |
| 91 | + |
| 92 | + bsig, err := hex.DecodeString(evt.Signature) |
| 93 | + if err != nil { |
| 94 | + return false, fmt.Errorf("signature is invalid hex: %w", err) |
| 95 | + } |
| 96 | + signature, err := btcec.ParseDERSignature(bsig, btcec.S256()) |
| 97 | + if err != nil { |
| 98 | + return false, fmt.Errorf("failed to parse DER signature: %w", err) |
| 99 | + } |
| 100 | + |
| 101 | + hash := sha256.Sum256(serialized) |
| 102 | + return signature.Verify(hash[:], pubkey), nil |
| 103 | +} |
0 commit comments