Added StartUpPairingClient and refactored client endpoints

This commit is contained in:
Samuel Hawksby-Robinson 2022-08-31 15:01:45 +01:00
parent 9fda3a968e
commit 67088b1ab1
3 changed files with 36 additions and 15 deletions

View File

@ -893,21 +893,9 @@ func GetConnectionStringForBeingBootstrapped(configJSON string) string {
if err != nil { if err != nil {
return makeJSONResponse(err) return makeJSONResponse(err)
} }
return cs return cs
} }
// SetConnectionStringForBootstrappingAnotherDevice starts a server.Receiving server.PairingClient
// 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, configJSON string) string {
if configJSON != "" {
return makeJSONResponse(fmt.Errorf("no config given, PairingPayloadSourceConfig is expected"))
}
}
// GetConnectionStringForBootstrappingAnotherDevice starts a server.Sending server.PairingServer // GetConnectionStringForBootstrappingAnotherDevice starts a server.Sending server.PairingServer
// then generates a server.ConnectionParams. Used when the device is Logged in and therefore has Account keys // then generates a server.ConnectionParams. Used when the device is Logged in and therefore has Account keys
// and the device might not have a camera // and the device might not have a camera
@ -923,18 +911,27 @@ func GetConnectionStringForBootstrappingAnotherDevice(configJSON string) string
if err != nil { if err != nil {
return makeJSONResponse(err) return makeJSONResponse(err)
} }
return cs return cs
} }
// InputQRCodeForBeingBootstrapped starts a server.Sending server.PairingClient // InputConnectionStringForBootstrapping starts a server.PairingClient
// The given server.ConnectionParams string will determine the server.Mode
//
// server.Mode = server.Sending
// 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)
//
// server.Mode = server.Receiving
// Used when the device 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 // Example: A mobile device (device with a camera) receiving account data from
// a device with a screen (mobile or desktop devices) // a device with a screen (mobile or desktop devices)
func InputQRCodeForBeingBootstrapped(cs, configJSON string) string { func InputConnectionStringForBootstrapping(cs, configJSON string) string {
if configJSON != "" { if configJSON != "" {
return makeJSONResponse(fmt.Errorf("no config given, PairingPayloadSourceConfig is expected")) return makeJSONResponse(fmt.Errorf("no config given, PairingPayloadSourceConfig is expected"))
} }
err := server.StartUpPairingClient(statusBackend.GetMultiaccountDB(), cs, configJSON)
return makeJSONResponse(err)
} }

View File

@ -5,6 +5,7 @@ import (
"crypto/ecdsa" "crypto/ecdsa"
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
"encoding/json"
"encoding/pem" "encoding/pem"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
@ -13,6 +14,8 @@ import (
"net/url" "net/url"
"github.com/btcsuite/btcutil/base58" "github.com/btcsuite/btcutil/base58"
"github.com/status-im/status-go/multiaccounts"
) )
type PairingClient struct { type PairingClient struct {
@ -161,3 +164,21 @@ func (c *PairingClient) getChallenge() error {
c.serverChallenge, err = ioutil.ReadAll(resp.Body) c.serverChallenge, err = ioutil.ReadAll(resp.Body)
return err return err
} }
func StartUpPairingClient(db *multiaccounts.Database, cs, configJSON string) error {
var conf PairingPayloadSourceConfig
err := json.Unmarshal([]byte(configJSON), &conf)
if err != nil {
return err
}
ccp := new(ConnectionParams)
err = ccp.FromString(cs)
c, err := NewPairingClient(ccp, &PairingPayloadManagerConfig{db, conf})
if err != nil {
return err
}
return c.PairAccount()
}

View File

@ -129,6 +129,7 @@ func (s *PairingServer) startSendingAccountData() error {
return s.Start() return s.Start()
} }
// MakeFullPairingServer generates a fully configured and randomly seeded PairingServer
func MakeFullPairingServer(db *multiaccounts.Database, mode Mode, storeConfig PairingPayloadSourceConfig) (*PairingServer, error) { func MakeFullPairingServer(db *multiaccounts.Database, mode Mode, storeConfig PairingPayloadSourceConfig) (*PairingServer, error) {
tlsKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) tlsKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil { if err != nil {
@ -171,6 +172,8 @@ func MakeFullPairingServer(db *multiaccounts.Database, mode Mode, storeConfig Pa
}) })
} }
// StartUpPairingServer generates a PairingServer, starts the pairing server in the correct mode
// and returns the ConnectionParams string to allow a PairingClient to make a successful connection.
func StartUpPairingServer(db *multiaccounts.Database, mode Mode, configJSON string) (string, error) { func StartUpPairingServer(db *multiaccounts.Database, mode Mode, configJSON string) (string, error) {
var conf PairingPayloadSourceConfig var conf PairingPayloadSourceConfig
err := json.Unmarshal([]byte(configJSON), &conf) err := json.Unmarshal([]byte(configJSON), &conf)