Skip to content

Commit 1bfb0ce

Browse files
authored
fix_: normalize keycard password for pairing (#6340)
1 parent 0e94479 commit 1bfb0ce

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

server/pairing/payload_management.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package pairing
33
import (
44
"encoding/json"
55
"errors"
6+
"strings"
7+
8+
"github.com/status-im/status-go/common"
69

710
"github.com/golang/protobuf/proto"
811
"go.uber.org/zap"
@@ -79,6 +82,18 @@ func (ppm *AccountPayloadMarshaller) UnmarshalProtobuf(data []byte) error {
7982
if pb.Multiaccount != nil {
8083
ppm.multiaccountFromProtobuf(pb.Multiaccount)
8184
}
85+
86+
// historically it so happened that on a mobile device a password without 0x is used, and on a desktop with it,
87+
// so with local pairing we have a conflict that needs to be resolved
88+
if ppm.multiaccount != nil && ppm.multiaccount.KeycardPairing != "" {
89+
if common.IsMobilePlatform() {
90+
pb.Password = strings.TrimPrefix(pb.Password, "0x")
91+
} else {
92+
if !strings.HasPrefix(pb.Password, "0x") {
93+
pb.Password = "0x" + pb.Password
94+
}
95+
}
96+
}
8297
ppm.password = pb.Password
8398
ppm.chatKey = pb.ChatKey
8499

server/pairing/payload_management_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/stretchr/testify/suite"
1515

1616
"github.com/status-im/status-go/api"
17+
"github.com/status-im/status-go/common"
1718
"github.com/status-im/status-go/common/dbsetup"
1819
"github.com/status-im/status-go/images"
1920
"github.com/status-im/status-go/multiaccounts"
@@ -341,3 +342,82 @@ func (pms *PayloadMarshallerSuite) TestPayloadMarshaller_LockPayload() {
341342
toSend3 := pm.ToSend()
342343
pms.Nil(toSend3)
343344
}
345+
346+
func (pms *PayloadMarshallerSuite) TestKeycardPairingPasswordAdjustments_Unmarshal_OldProto() {
347+
348+
// 1) KeycardPairing == "" => do nothing
349+
pms.T().Run("KeycardPairing is empty => do nothing", func(t *testing.T) {
350+
ap := &AccountPayload{
351+
password: "0xABC",
352+
multiaccount: &multiaccounts.Account{},
353+
keys: make(map[string][]byte),
354+
}
355+
ppm := NewPairingPayloadMarshaller(ap, pms.Logger)
356+
pb, err := ppm.MarshalProtobuf()
357+
require.NoError(t, err)
358+
ppm.password = ""
359+
err = ppm.UnmarshalProtobuf(pb)
360+
require.NoError(t, err)
361+
362+
require.Equal(t, "0xABC", ppm.password)
363+
})
364+
365+
// 2) KeycardPairing != "", IsMobilePlatform() == true => TrimPrefix("0x")
366+
pms.T().Run("IsMobilePlatform => trim prefix 0x", func(t *testing.T) {
367+
ap := &AccountPayload{
368+
password: "0xDEF",
369+
multiaccount: &multiaccounts.Account{},
370+
keys: make(map[string][]byte),
371+
}
372+
ppm := NewPairingPayloadMarshaller(ap, pms.Logger)
373+
ppm.multiaccount.KeycardPairing = "ABCDF"
374+
pb, err := ppm.MarshalProtobuf()
375+
require.NoError(t, err)
376+
ppm.password = ""
377+
common.IsMobilePlatform = func() bool { return true }
378+
379+
err = ppm.UnmarshalProtobuf(pb)
380+
require.NoError(t, err)
381+
382+
require.Equal(t, "DEF", ppm.password)
383+
})
384+
385+
// 3) KeycardPairing != "", IsMobilePlatform() == false, without "0x" => add "0x"
386+
pms.T().Run("No prefix => add 0x", func(t *testing.T) {
387+
ap := &AccountPayload{
388+
password: "1234",
389+
multiaccount: &multiaccounts.Account{},
390+
keys: make(map[string][]byte),
391+
}
392+
ppm := NewPairingPayloadMarshaller(ap, pms.Logger)
393+
common.IsMobilePlatform = func() bool { return false }
394+
ppm.multiaccount.KeycardPairing = "ABCDF"
395+
pb, err := ppm.MarshalProtobuf()
396+
require.NoError(t, err)
397+
ppm.password = ""
398+
err = ppm.UnmarshalProtobuf(pb)
399+
require.NoError(t, err)
400+
401+
require.Equal(t, "0x1234", ppm.password)
402+
})
403+
404+
// 4) KeycardPairing != "", IsMobilePlatform() == false, contains "0x" => do nothing
405+
pms.T().Run("Already has prefix => do nothing", func(t *testing.T) {
406+
ap := &AccountPayload{
407+
password: "0x9999",
408+
multiaccount: &multiaccounts.Account{},
409+
keys: make(map[string][]byte),
410+
}
411+
ppm := NewPairingPayloadMarshaller(ap, pms.Logger)
412+
ppm.multiaccount.KeycardPairing = "ABCDF"
413+
pb, err := ppm.MarshalProtobuf()
414+
require.NoError(t, err)
415+
ppm.password = ""
416+
common.IsMobilePlatform = func() bool { return false }
417+
418+
err = ppm.UnmarshalProtobuf(pb)
419+
require.NoError(t, err)
420+
421+
require.Equal(t, "0x9999", ppm.password)
422+
})
423+
}

0 commit comments

Comments
 (0)