Added and integrated StartUpPairingServer

Also moved Mount() calls into the respective Server/Client send() funcs
This commit is contained in:
Samuel Hawksby-Robinson 2022-08-31 13:47:16 +01:00
parent ea3ae8b213
commit 9fda3a968e
5 changed files with 72 additions and 20 deletions

View File

@ -146,6 +146,10 @@ func (b *GethStatusBackend) UpdateRootDataDir(datadir string) {
b.rootDataDir = datadir
}
func (b *GethStatusBackend) GetMultiaccountDB() *multiaccounts.Database {
return b.multiaccountsDB
}
func (b *GethStatusBackend) OpenAccounts() error {
b.mu.Lock()
defer b.mu.Unlock()

View File

@ -884,15 +884,27 @@ func GenerateImages(filepath string, aX, aY, bX, bY int) string {
// and the device has no camera to read a QR code with
//
// Example: A desktop device (device without camera) receiving account data from mobile (device with camera)
func GetConnectionStringForBeingBootstrapped() string {
func GetConnectionStringForBeingBootstrapped(configJSON string) string {
if configJSON != "" {
return makeJSONResponse(fmt.Errorf("no config given, PairingPayloadSourceConfig is expected"))
}
cs, err := server.StartUpPairingServer(statusBackend.GetMultiaccountDB(), server.Receiving, configJSON)
if err != nil {
return makeJSONResponse(err)
}
return cs
}
// SetConnectionStringForBootstrappingAnotherDevice starts a server.Receiving server.PairingClient
// Used when the devices is Logged in and therefore has Account keys and the has a camera to read a QR code
// Used when the device is Logged in and therefore has Account keys and the has a camera to read a QR code
//
// Example: A mobile (device with camera) sending account data to a desktop device (device without camera)
func SetConnectionStringForBootstrappingAnotherDevice(cs string) string {
func SetConnectionStringForBootstrappingAnotherDevice(cs, configJSON string) string {
if configJSON != "" {
return makeJSONResponse(fmt.Errorf("no config given, PairingPayloadSourceConfig is expected"))
}
}
@ -902,15 +914,27 @@ func SetConnectionStringForBootstrappingAnotherDevice(cs string) string {
//
// Example: A mobile or desktop device (devices that MAY have a camera but MUST have a screen)
// sending account data to a mobile (device with camera)
func GetConnectionStringForBootstrappingAnotherDevice() string {
func GetConnectionStringForBootstrappingAnotherDevice(configJSON string) string {
if configJSON != "" {
return makeJSONResponse(fmt.Errorf("no config given, PairingPayloadSourceConfig is expected"))
}
cs, err := server.StartUpPairingServer(statusBackend.GetMultiaccountDB(), server.Sending, configJSON)
if err != nil {
return makeJSONResponse(err)
}
return cs
}
// InputQRCodeForBeingBootstrapped starts a server.Sending server.PairingClient
// Used when the devices is Logged out or has no Account keys and has a camera to read a QR code
// Used when the device is Logged out or has no Account keys and has a camera to read a QR code
//
// Example: A mobile device (device with a camera) receiving account data from
// a device with a screen (mobile or desktop devices)
func InputQRCodeForBeingBootstrapped(cs string) string {
func InputQRCodeForBeingBootstrapped(cs, configJSON string) string {
if configJSON != "" {
return makeJSONResponse(fmt.Errorf("no config given, PairingPayloadSourceConfig is expected"))
}
}

View File

@ -96,6 +96,11 @@ func (c *PairingClient) PairAccount() error {
}
func (c *PairingClient) sendAccountData() error {
err := c.Mount()
if err != nil {
return err
}
c.baseAddress.Path = pairingReceive
resp, err := c.Post(c.baseAddress.String(), "application/octet-stream", bytes.NewBuffer(c.PayloadManager.ToSend()))
if err != nil {

View File

@ -5,12 +5,14 @@ import (
"crypto/elliptic"
"crypto/rand"
"crypto/tls"
"encoding/json"
"fmt"
"github.com/status-im/status-go/multiaccounts"
"net"
"time"
"github.com/gorilla/sessions"
"github.com/status-im/status-go/multiaccounts"
)
type PairingServer struct {
@ -115,6 +117,11 @@ func (s *PairingServer) startReceivingAccountData() error {
}
func (s *PairingServer) startSendingAccountData() error {
err := s.Mount()
if err != nil {
return err
}
s.SetHandlers(HandlerPatternMap{
pairingSend: challengeMiddleware(s, handlePairingSend(s)),
pairingChallenge: handlePairingChallenge(s),
@ -163,3 +170,28 @@ func MakeFullPairingServer(db *multiaccounts.Database, mode Mode, storeConfig Pa
},
})
}
func StartUpPairingServer(db *multiaccounts.Database, mode Mode, configJSON string) (string, error) {
var conf PairingPayloadSourceConfig
err := json.Unmarshal([]byte(configJSON), &conf)
if err != nil {
return "", err
}
ps, err := MakeFullPairingServer(db, mode, conf)
if err != nil {
return "", err
}
err = ps.StartPairing()
if err != nil {
return "", err
}
cp, err := ps.MakeConnectionParams()
if err != nil {
return "", err
}
return cp.ToString(), nil
}

View File

@ -36,11 +36,6 @@ func (s *PairingServerSuite) TestPairingServer_StartPairing() {
for _, m := range modes {
s.PS.mode = m
if m == Sending {
err := s.PS.Mount()
s.Require().NoError(err)
}
err = s.PS.StartPairing()
s.Require().NoError(err)
@ -76,11 +71,6 @@ func (s *PairingServerSuite) TestPairingServer_StartPairing() {
c.PayloadManager, err = NewMockEncryptOnlyPayloadManager(s.EphemeralAES)
s.Require().NoError(err)
if m == Receiving {
err := c.Mount()
s.Require().NoError(err)
}
err = c.PairAccount()
s.Require().NoError(err)
@ -109,9 +99,6 @@ func (s *PairingServerSuite) sendingSetup() *PairingClient {
s.PS.PayloadManager = pm
s.PS.mode = Sending
err = s.PS.Mount()
s.Require().NoError(err)
err = s.PS.StartPairing()
s.Require().NoError(err)