Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

fix: fixed issue 56 - error to encode/decode private key. #57

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion key.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func setPrivateKey(opts docopt.Opts) {
return
}

config.PrivateKey = string(keyval)
config.PrivateKey = string(hex.EncodeToString(keyval))
}

func showPublicKey(opts docopt.Opts) {
Expand Down
48 changes: 34 additions & 14 deletions printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"
"errors"
"fmt"
"log"
"strings"
Expand Down Expand Up @@ -35,19 +36,26 @@ func printEvent(evt nostr.Event, nick *string, verbose bool, jsonformat bool) {
}

// Don't print encrypted messages that aren't for me or from me
pubkey := getPubKey(config.PrivateKey)
pubkey := getPubKey(config.PrivateKey)
if evt.Kind == nostr.KindEncryptedDirectMessage {
if (!evt.Tags.ContainsAny("p", nostr.Tag{getPubKey(config.PrivateKey)})) && (evt.PubKey != pubkey) {
return
}
}

// json
if jsonformat {
jevt, _ := json.MarshalIndent(evt, "", "\t")
fmt.Print(string(jevt))
return
}
// json
if jsonformat {
if evt.Kind == nostr.KindEncryptedDirectMessage {
txt, err := decryptMessage((evt))
if err == nil {
evt.SetExtra("content_decrypted", txt)
}
}

jevt, _ := json.MarshalIndent(evt, "", "\t")
fmt.Print(string(jevt))
return
}

var ID string = shorten(evt.ID)
var fromField string = shorten(evt.PubKey)
Expand Down Expand Up @@ -115,14 +123,9 @@ func printEvent(evt nostr.Event, nick *string, verbose bool, jsonformat bool) {
case nostr.KindRecommendServer:
case nostr.KindContactList:
case nostr.KindEncryptedDirectMessage:
sharedSecret, err := nip04.ComputeSharedSecret(config.PrivateKey, evt.PubKey)
if err != nil {
log.Printf("Error computing shared key: %s. \n", err.Error())
return
}
txt, err := nip04.Decrypt(evt.Content, sharedSecret)
txt, err := decryptMessage((evt))
if err != nil {
log.Printf("Error decrypting message: %s. \n", err.Error())
log.Printf("%s\n", err.Error())
return
}
fmt.Print(txt)
Expand All @@ -134,6 +137,23 @@ func printEvent(evt nostr.Event, nick *string, verbose bool, jsonformat bool) {
fmt.Printf("\n")
}

func decryptMessage(evt nostr.Event) (string, error) {
if evt.Kind == nostr.KindEncryptedDirectMessage {
sharedSecret, err := nip04.ComputeSharedSecret(config.PrivateKey, evt.PubKey)
if err != nil {
return "", errors.New(fmt.Sprintf("Error computing shared key: %s.", err.Error()))
}

txt, err := nip04.Decrypt(evt.Content, sharedSecret)
if err != nil {
return "", errors.New(fmt.Sprintf("Error decrypting message: %s.", err.Error()))
}
return txt, nil
} else {
return "", errors.New("The event is not an Encrypted Message.")
}
}

func shorten(id string) string {
if len(id) < 12 {
return id
Expand Down