Skip to content

Commit

Permalink
renames
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe committed Apr 19, 2024
1 parent 52a4736 commit e73ab5b
Show file tree
Hide file tree
Showing 27 changed files with 275 additions and 287 deletions.
4 changes: 2 additions & 2 deletions impl/cmd/cli/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ var identityAddCmd = &cobra.Command{
Answer: rrds,
}
// generate put request
putReq, err := dht.CreatePkarrPublishRequest(privKey, msg)
putReq, err := dht.CreateDNSPublishRequest(privKey, msg)
if err != nil {
logrus.WithError(err).Error("failed to create put request")
return err
Expand Down Expand Up @@ -170,7 +170,7 @@ var identityGetCmd = &cobra.Command{
return err
}

msg, err := dht.ParsePkarrGetResponse(*gotResp)
msg, err := dht.ParseDNSGetResponse(*gotResp)
if err != nil {
logrus.WithError(err).Error("failed to parse get response")
return err
Expand Down
2 changes: 1 addition & 1 deletion impl/concurrencytest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func generateDIDPutRequest() (string, []byte, error) {
return "", nil, err
}

bep44Put, err := dht.CreatePkarrPublishRequest(sk, *packet)
bep44Put, err := dht.CreateDNSPublishRequest(sk, *packet)
if err != nil {
return "", nil, err
}
Expand Down
22 changes: 8 additions & 14 deletions impl/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ func (e EnvironmentVariable) String() string {
}

type Config struct {
Log LogConfig `toml:"log"`
ServerConfig ServerConfig `toml:"server"`
DHTConfig DHTServiceConfig `toml:"dht"`
PkarrConfig PkarrServiceConfig `toml:"pkarr"`
Log LogConfig `toml:"log"`
ServerConfig ServerConfig `toml:"server"`
DHTConfig DHTServiceConfig `toml:"dht"`
}

type ServerConfig struct {
Expand All @@ -57,13 +56,10 @@ type ServerConfig struct {
}

type DHTServiceConfig struct {
BootstrapPeers []string `toml:"bootstrap_peers"`
}

type PkarrServiceConfig struct {
RepublishCRON string `toml:"republish_cron"`
CacheTTLSeconds int `toml:"cache_ttl_seconds"`
CacheSizeLimitMB int `toml:"cache_size_limit_mb"`
BootstrapPeers []string `toml:"bootstrap_peers"`
RepublishCRON string `toml:"republish_cron"`
CacheTTLSeconds int `toml:"cache_ttl_seconds"`
CacheSizeLimitMB int `toml:"cache_size_limit_mb"`
}

type LogConfig struct {
Expand All @@ -81,9 +77,7 @@ func GetDefaultConfig() Config {
Telemetry: false,
},
DHTConfig: DHTServiceConfig{
BootstrapPeers: GetDefaultBootstrapPeers(),
},
PkarrConfig: PkarrServiceConfig{
BootstrapPeers: GetDefaultBootstrapPeers(),
RepublishCRON: "0 */3 * * *",
CacheTTLSeconds: 600,
CacheSizeLimitMB: 1000,
Expand Down
2 changes: 1 addition & 1 deletion impl/integrationtest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func generateDIDPutRequest() (string, []byte, error) {
return "", nil, err
}

bep44Put, err := dht.CreatePkarrPublishRequest(sk, *packet)
bep44Put, err := dht.CreateDNSPublishRequest(sk, *packet)
if err != nil {
return "", nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion impl/internal/did/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestClient(t *testing.T) {
assert.NoError(t, err)
assert.NotEmpty(t, packet)

bep44Put, err := dht.CreatePkarrPublishRequest(sk, *packet)
bep44Put, err := dht.CreateDNSPublishRequest(sk, *packet)
assert.NoError(t, err)
assert.NotEmpty(t, bep44Put)

Expand Down
2 changes: 1 addition & 1 deletion impl/internal/did/did.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func (d DHT) ToDNSPacket(doc did.Document, types []TypeIndex, gateways []Authori

keyType := keyTypeForJWK(*vm.PublicKeyJWK)
if keyType < 0 {
return nil, fmt.Errorf("+unsupported key type given alg: %s", vm.PublicKeyJWK.ALG)
return nil, fmt.Errorf("unsupported key type given alg: %s", vm.PublicKeyJWK.ALG)
}

// convert the public key to a base64url encoded string
Expand Down
14 changes: 7 additions & 7 deletions impl/pkg/dht/pkarr.go → impl/pkg/dht/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import (
"github.com/TBD54566975/did-dht-method/internal/dht"
)

// CreatePkarrPublishRequest creates a put request for the given records. Requires a public/private keypair and the records to put.
// The records are expected to be a DNS message packet, such as:
// CreateDNSPublishRequest creates a put request for the given records. Requires a public/private keypair and
// the records to put. The records are expected to be a DNS message packet, such as:
//
// dns.Msg{
// MsgHdr: dns.MsgHdr{
// Id: 0,
// Response: true,
// BEP44Response: true,
// Authoritative: true,
// },
// Answer: dns.RR{
Expand All @@ -30,11 +30,11 @@ import (
// Ttl: 7200,
// },
// Txt: []string{
// "hello pkarr",
// "hello mainline",
// },
// }
// }
func CreatePkarrPublishRequest(privateKey ed25519.PrivateKey, msg dns.Msg) (*bep44.Put, error) {
func CreateDNSPublishRequest(privateKey ed25519.PrivateKey, msg dns.Msg) (*bep44.Put, error) {
packed, err := msg.Pack()
if err != nil {
return nil, util.LoggingErrorMsg(err, "failed to pack records")
Expand All @@ -49,9 +49,9 @@ func CreatePkarrPublishRequest(privateKey ed25519.PrivateKey, msg dns.Msg) (*bep
return put, nil
}

// ParsePkarrGetResponse parses the response from a get request.
// ParseDNSGetResponse parses the response from a get request.
// The response is expected to be a slice of DNS resource records.
func ParsePkarrGetResponse(response dht.FullGetResult) (*dns.Msg, error) {
func ParseDNSGetResponse(response dht.FullGetResult) (*dns.Msg, error) {
var payload string
if err := bencode.Unmarshal(response.V, &payload); err != nil {
return nil, util.LoggingErrorMsg(err, "failed to unmarshal payload value")
Expand Down
12 changes: 6 additions & 6 deletions impl/pkg/dht/pkarr_test.go → impl/pkg/dht/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/TBD54566975/did-dht-method/internal/util"
)

func TestGetPutPkarrDHT(t *testing.T) {
func TestGetPutDNSDHT(t *testing.T) {
dht := NewTestDHT(t)
defer dht.Close()

Expand All @@ -31,7 +31,7 @@ func TestGetPutPkarrDHT(t *testing.T) {
Ttl: 7200,
},
Txt: []string{
"hello pkarr",
"hello mainline",
},
}
msg := dns.Msg{
Expand All @@ -42,7 +42,7 @@ func TestGetPutPkarrDHT(t *testing.T) {
},
Answer: []dns.RR{&txtRecord},
}
put, err := CreatePkarrPublishRequest(privKey, msg)
put, err := CreateDNSPublishRequest(privKey, msg)
require.NoError(t, err)

id, err := dht.Put(context.Background(), *put)
Expand All @@ -53,7 +53,7 @@ func TestGetPutPkarrDHT(t *testing.T) {
require.NoError(t, err)
require.NotEmpty(t, got)

gotMsg, err := ParsePkarrGetResponse(*got)
gotMsg, err := ParseDNSGetResponse(*got)
require.NoError(t, err)
require.NotEmpty(t, gotMsg.Answer)

Expand Down Expand Up @@ -103,7 +103,7 @@ func TestGetPutDIDDHT(t *testing.T) {
didDocPacket, err := didID.ToDNSPacket(*doc, nil, nil)
require.NoError(t, err)

putReq, err := CreatePkarrPublishRequest(privKey, *didDocPacket)
putReq, err := CreateDNSPublishRequest(privKey, *didDocPacket)
require.NoError(t, err)

gotID, err := dht.Put(context.Background(), *putReq)
Expand All @@ -114,7 +114,7 @@ func TestGetPutDIDDHT(t *testing.T) {
require.NoError(t, err)
require.NotEmpty(t, got)

gotMsg, err := ParsePkarrGetResponse(*got)
gotMsg, err := ParseDNSGetResponse(*got)
require.NoError(t, err)
require.NotEmpty(t, gotMsg.Answer)

Expand Down
49 changes: 25 additions & 24 deletions impl/pkg/pkarr/record.go → impl/pkg/dht/record.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pkarr
package dht

import (
"bytes"
Expand All @@ -14,40 +14,41 @@ import (
"github.com/tv42/zbase32"
)

type Response struct {
type BEP44Response struct {
V []byte `validate:"required"`
Seq int64 `validate:"required"`
Sig [64]byte `validate:"required"`
}

// Equals returns true if the response is equal to the other response
func (r Response) Equals(other Response) bool {
func (r BEP44Response) Equals(other BEP44Response) bool {
return r.Seq == other.Seq && bytes.Equal(r.V, other.V) && r.Sig == other.Sig
}

type Record struct {
// BEP44Record represents a record in the DHT
type BEP44Record struct {
Value []byte `json:"v" validate:"required"`
Key [32]byte `json:"k" validate:"required"`
Signature [64]byte `json:"sig" validate:"required"`
SequenceNumber int64 `json:"seq" validate:"required"`
}

// NewRecord returns a new Record with the given key, value, signature, and sequence number
func NewRecord(k []byte, v []byte, sig []byte, seq int64) (*Record, error) {
record := Record{SequenceNumber: seq}
// NewBEP44Record returns a new BEP44Record with the given key, value, signature, and sequence number
func NewBEP44Record(k []byte, v []byte, sig []byte, seq int64) (*BEP44Record, error) {
record := BEP44Record{SequenceNumber: seq}

if len(k) != 32 {
return nil, errors.New("incorrect key length for pkarr record")
return nil, errors.New("incorrect key length for bep44 record")
}
record.Key = [32]byte(k)

if len(v) > 1000 {
return nil, errors.New("pkarr record value too long")
return nil, errors.New("bep44 record value too long")
}
record.Value = v

if len(sig) != 64 {
return nil, errors.New("incorrect sig length for pkarr record")
return nil, errors.New("incorrect sig length for bep44 record")
}
record.Signature = [64]byte(sig)

Expand All @@ -59,15 +60,15 @@ func NewRecord(k []byte, v []byte, sig []byte, seq int64) (*Record, error) {
}

// IsValid returns an error if the request is invalid; also validates the signature
func (r Record) IsValid() error {
func (r BEP44Record) IsValid() error {
if err := util.IsValidStruct(r); err != nil {
return err
}

// validate the signature
bv, err := bencode.Marshal(r.Value)
if err != nil {
return fmt.Errorf("error bencoding pkarr record: %v", err)
return fmt.Errorf("error bencoding bep44 record: %v", err)
}

if !bep44.Verify(r.Key[:], nil, r.SequenceNumber, bv, r.Signature[:]) {
Expand All @@ -76,17 +77,17 @@ func (r Record) IsValid() error {
return nil
}

// Response returns the record as a Response
func (r Record) Response() Response {
return Response{
// Response returns the record as a BEP44Response
func (r BEP44Record) Response() BEP44Response {
return BEP44Response{
V: r.Value,
Seq: r.SequenceNumber,
Sig: r.Signature,
}
}

// BEP44 returns the record as a BEP44 Put message
func (r Record) BEP44() bep44.Put {
// Put returns the record as a bep44.Put message
func (r BEP44Record) Put() bep44.Put {
return bep44.Put{
V: r.Value,
K: &r.Key,
Expand All @@ -96,28 +97,28 @@ func (r Record) BEP44() bep44.Put {
}

// String returns a string representation of the record
func (r Record) String() string {
func (r BEP44Record) String() string {
e := base64.RawURLEncoding
return fmt.Sprintf("pkarr.Record{K=%s V=%s Sig=%s Seq=%d}", zbase32.EncodeToString(r.Key[:]), e.EncodeToString(r.Value), e.EncodeToString(r.Signature[:]), r.SequenceNumber)
return fmt.Sprintf("dht.BEP44Record{K=%s V=%s Sig=%s Seq=%d}", zbase32.EncodeToString(r.Key[:]), e.EncodeToString(r.Value), e.EncodeToString(r.Signature[:]), r.SequenceNumber)
}

// ID returns the base32 encoded key as a string
func (r Record) ID() string {
func (r BEP44Record) ID() string {
return zbase32.EncodeToString(r.Key[:])
}

// Hash returns the SHA256 hash of the record as a string
func (r Record) Hash() (string, error) {
func (r BEP44Record) Hash() (string, error) {
recordBytes, err := json.Marshal(r)
if err != nil {
return "", err
}
return string(sha256.New().Sum(recordBytes)), nil
}

// RecordFromBEP44 returns a Record from a BEP44 Put message
func RecordFromBEP44(putMsg *bep44.Put) Record {
return Record{
// RecordFromBEP44 returns a BEP44Record from a bep44.Put message
func RecordFromBEP44(putMsg *bep44.Put) BEP44Record {
return BEP44Record{
Key: *putMsg.K,
Value: putMsg.V.([]byte),
Signature: putMsg.Sig,
Expand Down
29 changes: 14 additions & 15 deletions impl/pkg/pkarr/record_test.go → impl/pkg/dht/record_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pkarr_test
package dht_test

import (
"strings"
Expand All @@ -9,13 +9,12 @@ import (

"github.com/TBD54566975/did-dht-method/internal/did"
"github.com/TBD54566975/did-dht-method/pkg/dht"
"github.com/TBD54566975/did-dht-method/pkg/pkarr"
)

func TestNewRecord(t *testing.T) {
// validate incorrect key length is rejected
r, err := pkarr.NewRecord([]byte("aaaaaaaaaaa"), nil, nil, 0)
assert.EqualError(t, err, "incorrect key length for pkarr record")
r, err := dht.NewBEP44Record([]byte("aaaaaaaaaaa"), nil, nil, 0)
assert.EqualError(t, err, "incorrect key length for bep44 record")
assert.Nil(t, r)

// create a did doc as a packet to store
Expand All @@ -27,30 +26,30 @@ func TestNewRecord(t *testing.T) {
assert.NoError(t, err)
assert.NotEmpty(t, packet)

putMsg, err := dht.CreatePkarrPublishRequest(sk, *packet)
putMsg, err := dht.CreateDNSPublishRequest(sk, *packet)
require.NoError(t, err)
require.NotEmpty(t, putMsg)

r, err = pkarr.NewRecord(putMsg.K[:], []byte(strings.Repeat("a", 1001)), putMsg.Sig[:], putMsg.Seq)
assert.EqualError(t, err, "pkarr record value too long")
r, err = dht.NewBEP44Record(putMsg.K[:], []byte(strings.Repeat("a", 1001)), putMsg.Sig[:], putMsg.Seq)
assert.EqualError(t, err, "bep44 record value too long")
assert.Nil(t, r)

r, err = pkarr.NewRecord(putMsg.K[:], putMsg.V.([]byte), []byte(strings.Repeat("a", 65)), putMsg.Seq)
assert.EqualError(t, err, "incorrect sig length for pkarr record")
r, err = dht.NewBEP44Record(putMsg.K[:], putMsg.V.([]byte), []byte(strings.Repeat("a", 65)), putMsg.Seq)
assert.EqualError(t, err, "incorrect sig length for bep44 record")
assert.Nil(t, r)

r, err = pkarr.NewRecord(putMsg.K[:], putMsg.V.([]byte), putMsg.Sig[:], 0)
assert.EqualError(t, err, "Key: 'Record.SequenceNumber' Error:Field validation for 'SequenceNumber' failed on the 'required' tag")
r, err = dht.NewBEP44Record(putMsg.K[:], putMsg.V.([]byte), putMsg.Sig[:], 0)
assert.EqualError(t, err, "Key: 'BEP44Record.SequenceNumber' Error:Field validation for 'SequenceNumber' failed on the 'required' tag")
assert.Nil(t, r)

r, err = pkarr.NewRecord(putMsg.K[:], putMsg.V.([]byte), putMsg.Sig[:], 1)
r, err = dht.NewBEP44Record(putMsg.K[:], putMsg.V.([]byte), putMsg.Sig[:], 1)
assert.EqualError(t, err, "signature is invalid")
assert.Nil(t, r)

r, err = pkarr.NewRecord(putMsg.K[:], putMsg.V.([]byte), putMsg.Sig[:], putMsg.Seq)
r, err = dht.NewBEP44Record(putMsg.K[:], putMsg.V.([]byte), putMsg.Sig[:], putMsg.Seq)
assert.NoError(t, err)

bep := r.BEP44()
bep := r.Put()
assert.Equal(t, putMsg.K, bep.K)
assert.Equal(t, putMsg.V, bep.V)
assert.Equal(t, putMsg.Sig, bep.Sig)
Expand All @@ -61,7 +60,7 @@ func TestNewRecord(t *testing.T) {
assert.Equal(t, r.SequenceNumber, resp.Seq)
assert.Equal(t, r.Signature, resp.Sig)

r2 := pkarr.RecordFromBEP44(putMsg)
r2 := dht.RecordFromBEP44(putMsg)
assert.Equal(t, r.Key, r2.Key)
assert.Equal(t, r.Value, r2.Value)
assert.Equal(t, r.Signature, r2.Signature)
Expand Down
Loading

0 comments on commit e73ab5b

Please sign in to comment.