Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
client
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
elixxir
client
Commits
8f3ac4f6
Commit
8f3ac4f6
authored
May 25, 2018
by
Rick Carback
Browse files
Options
Downloads
Patches
Plain Diff
Added initial userDiscovery implementation.
parent
f2ceb92d
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
bots/README.md
+1
-0
1 addition, 0 deletions
bots/README.md
bots/userDiscovery.go
+91
-0
91 additions, 0 deletions
bots/userDiscovery.go
with
92 additions
and
0 deletions
bots/README.md
0 → 100644
+
1
−
0
View file @
8f3ac4f6
Use this module to write code for working with different cMix bots
This diff is collapsed.
Click to expand it.
bots/userDiscovery.go
0 → 100644
+
91
−
0
View file @
8f3ac4f6
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2018 Privategrity Corporation /
// /
// All rights reserved. /
////////////////////////////////////////////////////////////////////////////////
// Package bot functions for working with the user discovery bot (UDB)
package
bot
import
(
"encoding/base64"
"fmt"
"gitlab.com/privategrity/client/io"
"gitlab.com/privategrity/crypto/hash"
)
// UdbID is the ID of the user discovery bot, which is always 13
const
udbID
=
uint64
(
13
)
// Register sends a registration message to the UDB. It does this by sending 2
// PUSHKEY messages to the UDB, then calling UDB's REGISTER command.
// If any of the commands fail, it returns an error.
func
Register
(
valueType
,
value
string
,
publicKey
[]
byte
)
error
{
keyFP
:=
fingerprint
(
publicKey
)
// check if key already exists and push one if it doesn't
if
!
keyExists
(
udbID
,
keyFP
)
{
err
:=
pushKey
(
udbID
,
keyFP
,
publicKey
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"Could not PUSHKEY: %s"
,
err
.
Error
())
}
}
// Send register command
regResult
:=
sendCommand
(
udbID
,
fmt
.
Sprintf
(
"REGISTER %s %s %s"
,
valueType
,
value
,
keyFP
))
if
regResult
!=
"REGISTRATION COMPLETE"
{
return
fmt
.
Errorf
(
"Registration failed: %s"
,
regResult
)
}
return
nil
}
// Returns a list of users matching the search criteria, or nil if
// none were found.
func
Search
(
valueType
,
value
string
)
[]
string
{
return
nil
}
func
pushKey
(
udbID
uint64
,
keyFP
string
,
publicKey
[]
byte
)
error
{
publicKeyParts
:=
make
([]
string
,
2
)
publicKeyParts
[
0
]
=
base64
.
StdEncoding
.
EncodeToString
(
publicKey
[
:
128
])
publicKeyParts
[
1
]
=
base64
.
StdEncoding
.
EncodeToString
(
publicKey
[
128
:
])
cmd
:=
"PUSHKEY %s %d %s"
expected
:=
fmt
.
Sprintf
(
"PUSHKEY COMPLETE %s"
,
keyFP
)
sendCommand
(
udbID
,
fmt
.
Sprintf
(
cmd
,
keyFP
,
0
,
publicKeyParts
[
0
]))
r
:=
sendCommand
(
udbID
,
fmt
.
Sprintf
(
cmd
,
keyFP
,
128
,
publicKeyParts
[
1
]))
if
r
!=
expected
{
return
fmt
.
Errorf
(
"PUSHKEY Failed: %s"
,
r
)
}
return
nil
}
func
keyExists
(
udbID
uint64
,
keyFP
string
)
bool
{
cmd
:=
fmt
.
Sprintf
(
"GETKEY %s"
,
keyFP
)
expected
:=
fmt
.
Sprintf
(
"GETKEY %s NOTFOUND"
,
keyFP
)
getKeyResponse
:=
sendCommand
(
udbID
,
cmd
)
return
getKeyResponse
!=
expected
}
// fingerprint generates the same fingerprint that the udb should generate
// TODO: Maybe move this helper to crypto module?
func
fingerprint
(
publicKey
[]
byte
)
string
{
h
,
_
:=
hash
.
NewCMixHash
()
// why does this return an err and not panic?
h
.
Write
(
publicKey
)
return
base64
.
StdEncoding
.
EncodeToString
(
h
.
Sum
(
nil
))
}
// sendCommand sends a command to the udb. This can block forever, but
// only does so if the send command succeeds. Our assumption is that
// we will eventually receive a response from the server. Callers
// to registration that need timeouts should implement it themselves.
func
sendCommand
(
botID
uint64
,
command
string
)
string
{
listener
:=
io
.
Listen
(
botID
)
defer
io
.
StopListening
(
listener
)
err
:=
io
.
SendMessage
(
botID
,
command
)
if
err
!=
nil
{
return
err
.
Error
()
}
response
:=
<-
listener
return
response
.
GetPayload
()
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment