Skip to content

Commit

Permalink
make npub format pubkey to be specified at -p option and removed -n o…
Browse files Browse the repository at this point in the history
…ption because it is needless now.
  • Loading branch information
ryogrid committed May 3, 2024
1 parent c3c8f7b commit 3adae88
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 29 deletions.
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ Usage:
Available Commands:
help Help about any command.
server Startup server.
genkey Generate key pair.
genkey Generate new key pair.
Flags:
-h, --help Help for NostrP2P
Expand All @@ -170,7 +170,17 @@ Flags:
```
# Examples
## Generate Key Pair
- Under construction
```bash
./nostrp2p genkey
Secret Key:
...
Secret Key (In Hex Representation):
...
Public Key:
...
Public key (In Hex Representation):
...
```

## Server Launch
```bash
Expand Down Expand Up @@ -232,13 +242,12 @@ Flags:
- [x] Repost
- [x] Server Side Signature Validation
- [x] Restriction of Memory Usage on Server
- [x] Data persistence with embedded DB
- [x] Data persistence on Server with embedded DB
- [ ] Follower Multicast Mode
- In this mode, posted messages are not broadcasted and server of follower should register to followee's server
- If broadcasting design reaches limit of scalability, this mode will be activated
- [ ] Optimization of Transfered Data Size between Servers
- Changing serialization format from MessagePack to Protocol Buffers

- [ ] Optimization of Data Size between Client and Server
- Changing serialization format from JSON Text to Protocol Buffers
- Format conversion must be needed both side
- [ ] Developing More Scalable Overlay Transport
- Scalability of Overlay NW with Mesh library should be around 100 nodes at most
54 changes: 33 additions & 21 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import (
"net"
"os"
"strconv"
"strings"
)

var listenAddrPort = "127.0.0.1:20000"
var bootPeerAddrPort = ""
var publicKey = ""
var nickname = ""
var writable = true
var debug = false
var isEnabledSSL = false
Expand Down Expand Up @@ -50,7 +50,7 @@ var serverCmd = &cobra.Command{
peers.Set(bootPeerAddrPort)
}

logger := log.New(os.Stderr, nickname+"> ", log.LstdFlags)
logger := log.New(os.Stderr, "> ", log.LstdFlags)

host, portStr, err := net.SplitHostPort(listenAddrPort)
if err != nil {
Expand All @@ -63,16 +63,29 @@ var serverCmd = &cobra.Command{

fmt.Println("public key: ", publicKey)

pubKeyBytes, err := hex.DecodeString(publicKey)
if err != nil {
logger.Fatalf("public key: %s: %v", publicKey, err)
var hexPubKeyStr string
var pubKeyBytes []byte
var err2 error
if strings.HasPrefix(publicKey, "npub") {
_, tmpPubKeyVal, err2_ := nip19.Decode(publicKey)
if err2_ != nil {
logger.Fatalf("public key: %s: %v", publicKey, err)
}
hexPubKeyStr = tmpPubKeyVal.(string)
pubKeyBytes, err2 = hex.DecodeString(hexPubKeyStr)
} else {
hexPubKeyStr = publicKey
pubKeyBytes, err2 = hex.DecodeString(publicKey)
if err2 != nil {
logger.Fatalf("public key: %s: %v", publicKey, err)
}
}

var tmpArr [32]byte
copy(tmpArr[:], pubKeyBytes[:32])
glo_val.SelfPubkeyStr = publicKey
glo_val.SelfPubkeyStr = hexPubKeyStr
glo_val.SelfPubkey = &tmpArr
glo_val.SelfPubkey64bit = np2p_util.GetUint64FromHexPubKeyStr(publicKey)
glo_val.SelfPubkey64bit = np2p_util.GetUint64FromHexPubKeyStr(hexPubKeyStr)

if isEnabledSSL {
glo_val.IsEnabledSSL = true
Expand All @@ -83,7 +96,7 @@ var serverCmd = &cobra.Command{
np2p_util.InitializeRandGen(-1 * int64(glo_val.SelfPubkey64bit))

// mesh library's peer ID is 6 bytes uint64 (MeshTransport only restriction)
peerId := np2p_util.Get6ByteUint64FromHexPubKeyStr(publicKey)
peerId := np2p_util.Get6ByteUint64FromHexPubKeyStr(hexPubKeyStr)
peer := core.NewPeer(peerId, logger)

fmt.Println(fmt.Sprintf("%x", *glo_val.SelfPubkey), fmt.Sprintf("%x", peerId))
Expand All @@ -97,7 +110,7 @@ var serverCmd = &cobra.Command{
ConnLimit: 64,
PeerDiscovery: true,
TrustedSubnets: []*net.IPNet{},
}, mesh.PeerName(peerId), nickname, mesh.NullOverlay{}, log.New(ioutil.Discard, "", 0))
}, mesh.PeerName(peerId), "", mesh.NullOverlay{}, log.New(ioutil.Discard, "", 0))

if err != nil {
logger.Fatalf("Could not create router: %v", err)
Expand Down Expand Up @@ -128,10 +141,6 @@ var serverCmd = &cobra.Command{
router.Stop()
}()

//// if log file exist, load it
//// note: Recovery is neededn only when DataManager implementation is OnMemoryDataManager
//core.NewRecoveryManager(peer.MessageMan).Recover()

apiServ := api_server.NewApiServer(peer)
go apiServ.LaunchAPIServer(host + ":" + strconv.Itoa(port+1))

Expand All @@ -156,6 +165,9 @@ var genkeyCmd = &cobra.Command{
fmt.Println(npub)
fmt.Println("Public key (In Hex Representation): ")
fmt.Println(pk)
fmt.Println()
fmt.Println("Please keep the secret key secret.")
fmt.Println("The key is used only at NostrP2P client as your identity.")
},
}

Expand Down Expand Up @@ -192,14 +204,14 @@ func init() {
)
serverCmd.MarkFlagRequired("public-key")

serverCmd.Flags().StringVarP(
&nickname,
"Your nickname on nostrp2p (required)",
"n",
"",
"Port to forward",
)
serverCmd.MarkFlagRequired("nickname")
//serverCmd.Flags().StringVarP(
// &nickname,
// "Your nickname on nostrp2p (required)",
// "n",
// "",
// "Port to forward",
//)
//serverCmd.MarkFlagRequired("nickname")

serverCmd.Flags().BoolVarP(
&writable,
Expand Down
4 changes: 3 additions & 1 deletion core/nuts_db_data_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/nutsdb/nutsdb"
"github.com/ryogrid/nostrp2p/glo_val"
"github.com/ryogrid/nostrp2p/np2p_const"
"github.com/ryogrid/nostrp2p/np2p_util"
"github.com/ryogrid/nostrp2p/schema"
"log"
Expand Down Expand Up @@ -63,7 +64,8 @@ func NewNutsDBDataManager() DataManager {
}
opt := nutsdb.DefaultOptions
opt.EntryIdxMode = nutsdb.HintKeyValAndRAMIdxMode
opt.HintKeyAndRAMIdxCacheSize = 300 * 1024 * 1024 // 300MB
// memory usage limit for caching buffer
opt.HintKeyAndRAMIdxCacheSize = np2p_const.MemoryUsageLimitForDBBuffer
db, err := nutsdb.Open(
opt,
nutsdb.WithDir(dbFilePath),
Expand Down
3 changes: 2 additions & 1 deletion np2p_const/np2p_const.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ const EventIdSize = 32
const SignatureSize = 64
const ResendCcheckInterval = time.Minute * 1
const ResendTimeBaseMin = 5
const ResendMaxTimes = 10 // Max time is 5*2^10 = 5120 minutes = about 3.5 days
const ResendMaxTimes = 10 // Max time is 5*2^10 = 5120 minutes = about 3.5 days
const MemoryUsageLimitForDBBuffer = 50 * 1024 * 1024 // 50MB

0 comments on commit 3adae88

Please sign in to comment.