Skip to content

Commit

Permalink
pkarr service
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe committed Oct 18, 2023
1 parent 96daeb2 commit df3b6df
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 52 deletions.
4 changes: 2 additions & 2 deletions impl/cmd/cli/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ var identityAddCmd = &cobra.Command{
}

// put the identity into the dht
id, err := d.Put(context.Background(), pubKey, *putReq)
id, err := d.Put(context.Background(), *putReq)
if err != nil {
logrus.WithError(err).Error("failed to put identity into dht")
return err
Expand Down Expand Up @@ -169,7 +169,7 @@ var identityGetCmd = &cobra.Command{
return err
}

msg, err := dht.ParsePKARRGetResponse(gotRR)
msg, err := dht.ParsePKARRGetResponse(*gotRR)
if err != nil {
logrus.WithError(err).Error("failed to parse get response")
return err
Expand Down
2 changes: 1 addition & 1 deletion impl/docs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 94 additions & 0 deletions impl/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,23 @@ definitions:
description: Status is always equal to `OK`.
type: string
type: object
pkg_server.GetPKARRResponse:
properties:
seq:
type: integer
sig:
items:
type: integer
type: array
v:
items:
type: integer
type: array
required:
- seq
- sig
- v
type: object
pkg_server.ListDIDsResponse:
properties:
dids:
Expand All @@ -158,6 +175,23 @@ definitions:
type: object
pkg_server.PublishDIDRequest:
type: object
pkg_server.PublishPKARRRequest:
properties:
seq:
type: integer
sig:
items:
type: integer
type: array
v:
items:
type: integer
type: array
required:
- seq
- sig
- v
type: object
host: '{{.Server.APIHost}}'
info:
contact:
Expand All @@ -171,6 +205,66 @@ info:
title: DID DHT Service API
version: '{{.SVN}}'
paths:
/{id}:
get:
consumes:
- application/json
description: Read a PKARR record from the DHT
parameters:
- description: ID
in: path
name: id
required: true
type: string
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/pkg_server.GetPKARRResponse'
"400":
description: Bad request
schema:
type: string
"404":
description: Not found
schema:
type: string
"500":
description: Internal server error
schema:
type: string
summary: Read a PKARR record from the DHT
tags:
- PKARR
put:
consumes:
- application/json
description: Publishes a PKARR to the DHT
parameters:
- description: Publish PKARR Request
in: body
name: request
required: true
schema:
$ref: '#/definitions/pkg_server.PublishPKARRRequest'
produces:
- application/json
responses:
"202":
description: Accepted
"400":
description: Bad request
schema:
type: string
"500":
description: Internal server error
schema:
type: string
summary: Publish a PKARR to the DHT
tags:
- PKARR
/health:
get:
consumes:
Expand Down
41 changes: 18 additions & 23 deletions impl/pkg/dht/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package dht

import (
"context"
"crypto/ed25519"

errutil "github.com/TBD54566975/ssi-sdk/util"
"github.com/anacrolix/dht/v2"
"github.com/anacrolix/dht/v2/bep44"
"github.com/anacrolix/dht/v2/exts/getput"
"github.com/anacrolix/torrent/bencode"
"github.com/anacrolix/torrent/types/infohash"
"github.com/sirupsen/logrus"

Expand All @@ -30,35 +29,31 @@ func NewDHT(bootstrapPeers []string) (*DHT, error) {
return &DHT{Server: s}, nil
}

// Put puts the given BEP-44 value into the DHT and returns its z32-encoded key.
func (d *DHT) Put(ctx context.Context, request bep44.Put) (string, error) {
t, err := getput.Put(ctx, request.Target(), d.Server, nil, func(int64) bep44.Put {
return request
})
if err != nil {
return "", errutil.LoggingNewErrorf("failed to put key into dht, tried %d nodes, got %d responses", t.NumAddrsTried, t.NumResponses)
}
return util.Z32Encode(request.K[:]), nil
}

// Get returns the BEP-44 value for the given key from the DHT.
// The key is a z32-encoded string, such as "yj47pezutnpw9pyudeeai8cx8z8d6wg35genrkoqf9k3rmfzy58o".
func (d *DHT) Get(ctx context.Context, key string) ([]byte, error) {
func (d *DHT) Get(ctx context.Context, key string) (*bep44.Put, error) {
z32Decoded, err := util.Z32Decode(key)
if err != nil {
logrus.WithError(err).Error("failed to decode key")
return nil, err
}
res, t, err := getput.Get(ctx, infohash.HashBytes(z32Decoded), d.Server, nil, nil)
if err != nil {
logrus.WithError(err).Errorf("failed to get key<%s> from dht; tried %d nodes, got %d responses", key, t.NumAddrsTried, t.NumResponses)
return nil, err
}
var payload string
if err = bencode.Unmarshal(res.V, &payload); err != nil {
logrus.WithError(err).Error("failed to unmarshal payload value")
return nil, err
}
return []byte(payload), nil
}

// Put puts the given BEP-44 value into the DHT and returns its z32-encoded key.
func (d *DHT) Put(ctx context.Context, key ed25519.PublicKey, request bep44.Put) (string, error) {
t, err := getput.Put(ctx, request.Target(), d.Server, nil, func(int64) bep44.Put {
return request
})
if err != nil {
logrus.WithError(err).Errorf("failed to put key into dht, tried %d nodes, got %d responses", t.NumAddrsTried, t.NumResponses)
return "", err
return nil, errutil.LoggingNewErrorf("failed to get key<%s> from dht; tried %d nodes, got %d responses", key, t.NumAddrsTried, t.NumResponses)
}
return util.Z32Encode(key), nil
return &bep44.Put{
V: res.V,
Seq: res.Seq,
}, nil
}
2 changes: 1 addition & 1 deletion impl/pkg/dht/dht_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestGetPutDHT(t *testing.T) {
}
put.Sign(privKey)

id, err := d.Put(context.Background(), pubKey, *put)
id, err := d.Put(context.Background(), *put)
require.NoError(t, err)
require.NotEmpty(t, id)

Expand Down
Loading

0 comments on commit df3b6df

Please sign in to comment.