Skip to content

Commit

Permalink
restructure protocol package (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
asmogo authored Nov 17, 2024
1 parent cf28dd5 commit ba1e856
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
10 changes: 6 additions & 4 deletions protocol/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func IsDomainName(name string) bool { //nolint:cyclop
split := strings.Split(name, ".")

// Need a TLD and a domain.
if len(split) < 2 { //nolint:gomnd
if len(split) < 2 { //nolint:mnd
return false
}
l := len(name)
Expand Down Expand Up @@ -325,8 +325,10 @@ func IsDomainName(name string) bool { //nolint:cyclop
}

// RemoveScheme removes the scheme from a URL string.
// If the URL string includes a scheme (e.g., "http://"), the scheme will be removed and the remaining string will be returned.
// If the URL string includes a default scheme (e.g., "//"), the default scheme will be removed and the remaining string will be returned.
// If the URL string includes a scheme (e.g., "http://"),
// the scheme will be removed and the remaining string will be returned.
// If the URL string includes a default scheme (e.g., "//"),
// the default scheme will be removed and the remaining string will be returned.
// If the URL string does not include a scheme, the original string will be returned unchanged.
func RemoveScheme(s string) string {
if strings.Contains(s, "://") {
Expand Down Expand Up @@ -374,7 +376,7 @@ func removeScheme(s string) string {

// index returns the starting index of the first occurrence of the specified scheme in the given string.
// If the scheme is not found, it returns -1.
// The returned index is incremented by the length of the scheme to obtain the starting position of the remaining string.
// The returned int is incremented by the length of the scheme to obtain the starting position of the remaining string.
func index(s, scheme string) int {
return strings.Index(s, scheme) + len(scheme)
}
14 changes: 8 additions & 6 deletions protocol/domain_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package protocol
package protocol_test

import (
"net/url"
"reflect"
"testing"

"github.com/asmogo/nws/protocol"
)

type args struct {
Expand All @@ -13,7 +15,7 @@ type args struct {
type parseTest struct {
name string
args args
want *URL
want *protocol.URL
wantErr bool
}

Expand All @@ -23,7 +25,7 @@ func TestParse(t *testing.T) {
testCopy := test
t.Run(testCopy.name, func(t *testing.T) {
t.Parallel()
got, err := Parse(testCopy.args.s)
got, err := protocol.Parse(testCopy.args.s)
if (err != nil) != testCopy.wantErr {
t.Errorf("Parse() error = %v, wantErr %v", err, testCopy.wantErr)
return
Expand All @@ -37,9 +39,9 @@ func TestParse(t *testing.T) {

func createParseTests() []parseTest {
return []parseTest{
{name: "1", args: args{s: "http://D1Q78S3J78NIURJFEDQ74BJQCLH6AP35CKN66R3FELI0.9B7NTQSU4PBM2JJQJ0CMGHUENQON4GB28RLGQCH3D3NK2AQVFE70.nostr"}, want: &URL{IsDomain: true, TLD: "nostr", Name: "9b7ntqsu4pbm2jjqj0cmghuenqon4gb28rlgqch3d3nk2aqvfe70", SubName: "d1q78s3j78niurjfedq74bjqclh6ap35ckn66r3feli0", URL: &url.URL{Host: "d1q78s3j78niurjfedq74bjqclh6ap35ckn66r3feli0.9b7ntqsu4pbm2jjqj0cmghuenqon4gb28rlgqch3d3nk2aqvfe70.nostr", Scheme: "http"}}, wantErr: false}, //nolint:lll
{name: "1", args: args{s: "http://d1q78s3j78niurjfedq74bjqclh6ap35ckn66r3feli0.9b7ntqsu4pbm2jjqj0cmghuenqon4gb28rlgqch3d3nk2aqvfe70.nostr"}, want: &URL{IsDomain: true, TLD: "nostr", Name: "9b7ntqsu4pbm2jjqj0cmghuenqon4gb28rlgqch3d3nk2aqvfe70", SubName: "d1q78s3j78niurjfedq74bjqclh6ap35ckn66r3feli0", URL: &url.URL{Host: "d1q78s3j78niurjfedq74bjqclh6ap35ckn66r3feli0.9b7ntqsu4pbm2jjqj0cmghuenqon4gb28rlgqch3d3nk2aqvfe70.nostr", Scheme: "http"}}, wantErr: false}, //nolint:lll
{name: "1", args: args{s: "https://d1q78s3j78niurjfedq74bjqclh6ap35ckn66r3feli0.9b7ntqsu4pbm2jjqj0cmghuenqon4gb28rlgqch3d3nk2aqvfe70.nostr"}, want: &URL{IsDomain: true, TLD: "nostr", Name: "9b7ntqsu4pbm2jjqj0cmghuenqon4gb28rlgqch3d3nk2aqvfe70", SubName: "d1q78s3j78niurjfedq74bjqclh6ap35ckn66r3feli0", URL: &url.URL{Host: "d1q78s3j78niurjfedq74bjqclh6ap35ckn66r3feli0.9b7ntqsu4pbm2jjqj0cmghuenqon4gb28rlgqch3d3nk2aqvfe70.nostr", Scheme: "https"}}, wantErr: false}, //nolint:lll
{name: "1", args: args{s: "http://D1Q78S3J78NIURJFEDQ74BJQCLH6AP35CKN66R3FELI0.9B7NTQSU4PBM2JJQJ0CMGHUENQON4GB28RLGQCH3D3NK2AQVFE70.nostr"}, want: &protocol.URL{IsDomain: true, TLD: "nostr", Name: "9b7ntqsu4pbm2jjqj0cmghuenqon4gb28rlgqch3d3nk2aqvfe70", SubName: "d1q78s3j78niurjfedq74bjqclh6ap35ckn66r3feli0", URL: &url.URL{Host: "d1q78s3j78niurjfedq74bjqclh6ap35ckn66r3feli0.9b7ntqsu4pbm2jjqj0cmghuenqon4gb28rlgqch3d3nk2aqvfe70.nostr", Scheme: "http"}}, wantErr: false}, //nolint:lll
{name: "1", args: args{s: "http://d1q78s3j78niurjfedq74bjqclh6ap35ckn66r3feli0.9b7ntqsu4pbm2jjqj0cmghuenqon4gb28rlgqch3d3nk2aqvfe70.nostr"}, want: &protocol.URL{IsDomain: true, TLD: "nostr", Name: "9b7ntqsu4pbm2jjqj0cmghuenqon4gb28rlgqch3d3nk2aqvfe70", SubName: "d1q78s3j78niurjfedq74bjqclh6ap35ckn66r3feli0", URL: &url.URL{Host: "d1q78s3j78niurjfedq74bjqclh6ap35ckn66r3feli0.9b7ntqsu4pbm2jjqj0cmghuenqon4gb28rlgqch3d3nk2aqvfe70.nostr", Scheme: "http"}}, wantErr: false}, //nolint:lll
{name: "1", args: args{s: "https://d1q78s3j78niurjfedq74bjqclh6ap35ckn66r3feli0.9b7ntqsu4pbm2jjqj0cmghuenqon4gb28rlgqch3d3nk2aqvfe70.nostr"}, want: &protocol.URL{IsDomain: true, TLD: "nostr", Name: "9b7ntqsu4pbm2jjqj0cmghuenqon4gb28rlgqch3d3nk2aqvfe70", SubName: "d1q78s3j78niurjfedq74bjqclh6ap35ckn66r3feli0", URL: &url.URL{Host: "d1q78s3j78niurjfedq74bjqclh6ap35ckn66r3feli0.9b7ntqsu4pbm2jjqj0cmghuenqon4gb28rlgqch3d3nk2aqvfe70.nostr", Scheme: "https"}}, wantErr: false}, //nolint:lll

}
}
14 changes: 7 additions & 7 deletions protocol/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package protocol

import (
"encoding/json"
"github.com/google/uuid"

"fmt"

"github.com/google/uuid"
)

type MessageType string
Expand All @@ -16,11 +16,11 @@ const (
)

type Message struct {
Key uuid.UUID `json:"key,omitempty"`
Type MessageType `json:"type,omitempty"`
Data []byte `json:"data,omitempty"`
Destination string `json:"destination,omitempty"`
EntryPublicAddress string `json:"entryPublicAddress,omitempty"`
Key uuid.UUID `json:"key,omitempty"` // unique identifier for the message
Type MessageType `json:"type,omitempty"` // type of message
Data []byte `json:"data,omitempty"` // data to be sent
Destination string `json:"destination,omitempty"` // destination to send the message
EntryPublicAddress string `json:"entryPublicAddress,omitempty"` // public ip address of the entry node (used for reverse connect)
}

type MessageOption func(*Message)
Expand Down
11 changes: 8 additions & 3 deletions protocol/nip44.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@ package protocol

import (
"encoding/hex"
"fmt"
)

const (
padding = "02"
)

func GetEncryptionKeys(privateKey, publicKey string) ([]byte, []byte, error) {
targetPublicKeyBytes, err := hex.DecodeString("02" + publicKey)
targetPublicKeyBytes, err := hex.DecodeString(padding + publicKey)
if err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf("failed to decode public key: %w", err)
}
privateKeyBytes, err := hex.DecodeString(privateKey)
if err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf("failed to decode private key: %w", err)
}
return privateKeyBytes, targetPublicKeyBytes, nil
}

0 comments on commit ba1e856

Please sign in to comment.