Added string identifier for Connection String

This commit is contained in:
Samuel Hawksby-Robinson 2022-10-05 10:57:12 +01:00
parent d7e57fbb66
commit dde0c71cfc
3 changed files with 14 additions and 5 deletions

View File

@ -1 +1 @@
0.111.2
0.111.3

View File

@ -24,6 +24,10 @@ const (
Sending
)
const (
connectionStringID = "cs"
)
type ConnectionParams struct {
version ConnectionParamVersion
netIP net.IP
@ -47,9 +51,10 @@ func NewConnectionParams(netIP net.IP, port int, publicKey *ecdsa.PublicKey, aes
// ToString generates a string required for generating a secure connection to another Status device.
//
// The returned string will look like below:
// - "2:4FHRnp:H6G:uqnnMwVUfJc2Fkcaojet8F1ufKC3hZdGEt47joyBx9yd:BbnZ7Gc66t54a9kEFCf7FW8SGQuYypwHVeNkRYeNoqV6:2"
// - "cs2:4FHRnp:H6G:uqnnMwVUfJc2Fkcaojet8F1ufKC3hZdGEt47joyBx9yd:BbnZ7Gc66t54a9kEFCf7FW8SGQuYypwHVeNkRYeNoqV6:2"
//
// Format bytes encoded into a base58 string, delimited by ":"
// - string type identifier
// - version
// - net.IP
// - port
@ -64,15 +69,19 @@ func (cp *ConnectionParams) ToString() string {
ek := base58.Encode(cp.aesKey)
m := base58.Encode(new(big.Int).SetInt64(int64(cp.serverMode)).Bytes())
return fmt.Sprintf("%s:%s:%s:%s:%s:%s", v, ip, p, k, ek, m)
return fmt.Sprintf("%s%s:%s:%s:%s:%s:%s", connectionStringID, v, ip, p, k, ek, m)
}
// FromString parses a connection params string required for to securely connect to another Status device.
// This function parses a connection string generated by ToString
func (cp *ConnectionParams) FromString(s string) error {
if s[:2] != connectionStringID {
return fmt.Errorf("connection string doesn't begin with identifier '%s'", connectionStringID)
}
requiredParams := 6
sData := strings.Split(s, ":")
sData := strings.Split(s[2:], ":")
if len(sData) != requiredParams {
return fmt.Errorf("expected data '%s' to have length of '%d', received '%d'", s, requiredParams, len(sData))
}

View File

@ -7,7 +7,7 @@ import (
)
var (
connectionString = "2:4FHRnp:Q4:uqnnMwVUfJc2Fkcaojet8F1ufKC3hZdGEt47joyBx9yd:BbnZ7Gc66t54a9kEFCf7FW8SGQuYypwHVeNkRYeNoqV6:3"
connectionString = "cs2:4FHRnp:Q4:uqnnMwVUfJc2Fkcaojet8F1ufKC3hZdGEt47joyBx9yd:BbnZ7Gc66t54a9kEFCf7FW8SGQuYypwHVeNkRYeNoqV6:3"
)
func TestConnectionParamsSuite(t *testing.T) {