throw error if account already exist when doing local pairing on receiver side (#3091)

This commit is contained in:
frank 2023-02-01 20:28:32 +08:00 committed by GitHub
parent d44fa42cbf
commit 4a970683d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 5 deletions

View File

@ -3,6 +3,7 @@ package pairing
import (
"crypto/rand"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
@ -20,6 +21,10 @@ import (
"github.com/status-im/status-go/protocol/protobuf"
)
var (
ErrKeyFileAlreadyExists = errors.New("key file already exists")
)
// PayloadManager is the interface for PayloadManagers and wraps the basic functions for fulfilling payload management
type PayloadManager interface {
// Mount Loads the payload into the PayloadManager's state
@ -475,10 +480,16 @@ func (apr *AccountPayloadRepository) storeKeys(keyStorePath string) error {
return fmt.Errorf("no known Key UID")
}
keyStorePath = filepath.Join(keyStorePath, apr.multiaccount.KeyUID)
err := os.MkdirAll(keyStorePath, 0777)
if err != nil {
_, err := os.Stat(keyStorePath)
if os.IsNotExist(err) {
err := os.MkdirAll(keyStorePath, 0777)
if err != nil {
return err
}
} else if err != nil {
return err
} else {
return ErrKeyFileAlreadyExists
}
}

View File

@ -70,7 +70,7 @@ func makeKeystores(t *testing.T) (string, string, func()) {
keyStoreDir = filepath.Join(keyStoreDir, "keystore", keyUID)
// TODO test case where the keystore dir does not yet exist because the device is new
emptyKeyStoreDir = filepath.Join(emptyKeyStoreDir, "keystore", keyUID)
emptyKeyStoreDir = filepath.Join(emptyKeyStoreDir, "keystore")
err = os.MkdirAll(keyStoreDir, 0777)
require.NoError(t, err)
@ -301,7 +301,7 @@ func (pms *PayloadMarshallerSuite) TestPayloadMarshaller_StorePayloads() {
pms.Require().NoError(err)
// TEST PairingPayloadRepository 2 StoreToSource()
keys := getFiles(pms.T(), pms.config2.KeystorePath)
keys := getFiles(pms.T(), filepath.Join(pms.config2.KeystorePath, pms.config2.KeyUID))
pms.Require().Len(keys, 2)
pms.Require().Len(keys[utils.GetAccount1PKFile()], 489)
@ -326,6 +326,9 @@ func (pms *PayloadMarshallerSuite) TestPayloadMarshaller_StorePayloads() {
pms.Require().Exactly(expected.Name, acc.Name)
pms.Require().Exactly(expected.Timestamp, acc.Timestamp)
pms.Require().Len(acc.Images, 2)
err = ppr2.StoreToSource()
pms.Require().ErrorIs(err, ErrKeyFileAlreadyExists)
}
func (pms *PayloadMarshallerSuite) TestPayloadMarshaller_LockPayload() {