2017-09-26 13:44:26 +00:00
|
|
|
package account
|
2016-07-27 11:47:41 +00:00
|
|
|
|
2016-06-15 19:50:35 +00:00
|
|
|
import (
|
2023-03-10 12:53:40 +00:00
|
|
|
"context"
|
2019-01-24 15:44:46 +00:00
|
|
|
"crypto/ecdsa"
|
2017-10-10 09:38:49 +00:00
|
|
|
"encoding/json"
|
2016-06-20 15:21:45 +00:00
|
|
|
"errors"
|
2016-06-15 19:50:35 +00:00
|
|
|
"fmt"
|
2017-05-06 21:53:18 +00:00
|
|
|
"io/ioutil"
|
2017-05-15 21:49:22 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2023-02-11 11:14:20 +00:00
|
|
|
"strings"
|
2018-04-20 15:39:53 +00:00
|
|
|
"sync"
|
2023-03-10 12:53:40 +00:00
|
|
|
"time"
|
2016-06-15 19:50:35 +00:00
|
|
|
|
2021-06-30 11:40:54 +00:00
|
|
|
"github.com/google/uuid"
|
test_: Code Migration from status-cli-tests
author shashankshampi <shashank.sanket1995@gmail.com> 1729780155 +0530
committer shashankshampi <shashank.sanket1995@gmail.com> 1730274350 +0530
test: Code Migration from status-cli-tests
fix_: functional tests (#5979)
* fix_: generate on test-functional
* chore(test)_: fix functional test assertion
---------
Co-authored-by: Siddarth Kumar <siddarthkay@gmail.com>
feat(accounts)_: cherry-pick Persist acceptance of Terms of Use & Privacy policy (#5766) (#5977)
* feat(accounts)_: Persist acceptance of Terms of Use & Privacy policy (#5766)
The original GH issue https://github.com/status-im/status-mobile/issues/21113
came from a request from the Legal team. We must show to Status v1 users the new
terms (Terms of Use & Privacy Policy) right after they upgrade to Status v2
from the stores.
The solution we use is to create a flag in the accounts table, named
hasAcceptedTerms. The flag will be set to true on the first account ever
created in v2 and we provide a native call in mobile/status.go#AcceptTerms,
which allows the client to persist the user's choice in case they are upgrading
(from v1 -> v2, or from a v2 older than this PR).
This solution is not the best because we should store the setting in a separate
table, not in the accounts table.
Related Mobile PR https://github.com/status-im/status-mobile/pull/21124
* fix(test)_: Compare addresses using uppercased strings
---------
Co-authored-by: Icaro Motta <icaro.ldm@gmail.com>
test_: restore account (#5960)
feat_: `LogOnPanic` linter (#5969)
* feat_: LogOnPanic linter
* fix_: add missing defer LogOnPanic
* chore_: make vendor
* fix_: tests, address pr comments
* fix_: address pr comments
fix(ci)_: remove workspace and tmp dir
This ensures we do not encounter weird errors like:
```
+ ln -s /home/jenkins/workspace/go_prs_linux_x86_64_main_PR-5907 /home/jenkins/workspace/go_prs_linux_x86_64_main_PR-5907@tmp/go/src/github.com/status-im/status-go
ln: failed to create symbolic link '/home/jenkins/workspace/go_prs_linux_x86_64_main_PR-5907@tmp/go/src/github.com/status-im/status-go': File exists
script returned exit code 1
```
Signed-off-by: Jakub Sokołowski <jakub@status.im>
chore_: enable windows and macos CI build (#5840)
- Added support for Windows and macOS in CI pipelines
- Added missing dependencies for Windows and x86-64-darwin
- Resolved macOS SDK version compatibility for darwin-x86_64
The `mkShell` override was necessary to ensure compatibility with the newer
macOS SDK (version 11.0) for x86_64. The default SDK (10.12) was causing build failures
because of the missing libs and frameworks. OverrideSDK creates a mapping from
the default SDK in all package categories to the requested SDK (11.0).
fix(contacts)_: fix trust status not being saved to cache when changed (#5965)
Fixes https://github.com/status-im/status-desktop/issues/16392
cleanup
added logger and cleanup
review comments changes
fix_: functional tests (#5979)
* fix_: generate on test-functional
* chore(test)_: fix functional test assertion
---------
Co-authored-by: Siddarth Kumar <siddarthkay@gmail.com>
feat(accounts)_: cherry-pick Persist acceptance of Terms of Use & Privacy policy (#5766) (#5977)
* feat(accounts)_: Persist acceptance of Terms of Use & Privacy policy (#5766)
The original GH issue https://github.com/status-im/status-mobile/issues/21113
came from a request from the Legal team. We must show to Status v1 users the new
terms (Terms of Use & Privacy Policy) right after they upgrade to Status v2
from the stores.
The solution we use is to create a flag in the accounts table, named
hasAcceptedTerms. The flag will be set to true on the first account ever
created in v2 and we provide a native call in mobile/status.go#AcceptTerms,
which allows the client to persist the user's choice in case they are upgrading
(from v1 -> v2, or from a v2 older than this PR).
This solution is not the best because we should store the setting in a separate
table, not in the accounts table.
Related Mobile PR https://github.com/status-im/status-mobile/pull/21124
* fix(test)_: Compare addresses using uppercased strings
---------
Co-authored-by: Icaro Motta <icaro.ldm@gmail.com>
test_: restore account (#5960)
feat_: `LogOnPanic` linter (#5969)
* feat_: LogOnPanic linter
* fix_: add missing defer LogOnPanic
* chore_: make vendor
* fix_: tests, address pr comments
* fix_: address pr comments
chore_: enable windows and macos CI build (#5840)
- Added support for Windows and macOS in CI pipelines
- Added missing dependencies for Windows and x86-64-darwin
- Resolved macOS SDK version compatibility for darwin-x86_64
The `mkShell` override was necessary to ensure compatibility with the newer
macOS SDK (version 11.0) for x86_64. The default SDK (10.12) was causing build failures
because of the missing libs and frameworks. OverrideSDK creates a mapping from
the default SDK in all package categories to the requested SDK (11.0).
fix(contacts)_: fix trust status not being saved to cache when changed (#5965)
Fixes https://github.com/status-im/status-desktop/issues/16392
test_: remove port bind
chore(wallet)_: move route execution code to separate module
chore_: replace geth logger with zap logger (#5962)
closes: #6002
feat(telemetry)_: add metrics for message reliability (#5899)
* feat(telemetry)_: track message reliability
Add metrics for dial errors, missed messages,
missed relevant messages, and confirmed delivery.
* fix_: handle error from json marshal
chore_: use zap logger as request logger
iterates: status-im/status-desktop#16536
test_: unique project per run
test_: use docker compose v2, more concrete project name
fix(codecov)_: ignore folders without tests
Otherwise Codecov reports incorrect numbers when making changes.
https://docs.codecov.com/docs/ignoring-paths
Signed-off-by: Jakub Sokołowski <jakub@status.im>
test_: verify schema of signals during init; fix schema verification warnings (#5947)
fix_: update defaultGorushURL (#6011)
fix(tests)_: use non-standard port to avoid conflicts
We have observed `nimbus-eth2` build failures reporting this port:
```json
{
"lvl": "NTC",
"ts": "2024-10-28 13:51:32.308+00:00",
"msg": "REST HTTP server could not be started",
"topics": "beacnde",
"address": "127.0.0.1:5432",
"reason": "(98) Address already in use"
}
```
https://ci.status.im/job/nimbus-eth2/job/platforms/job/linux/job/x86_64/job/main/job/PR-6683/3/
Signed-off-by: Jakub Sokołowski <jakub@status.im>
fix_: create request logger ad-hoc in tests
Fixes `TestCall` failing when run concurrently.
chore_: configure codecov (#6005)
* chore_: configure codecov
* fix_: after_n_builds
2024-10-24 14:29:15 +00:00
|
|
|
"go.uber.org/zap"
|
2019-01-24 15:44:46 +00:00
|
|
|
|
2021-06-23 09:21:21 +00:00
|
|
|
gethkeystore "github.com/ethereum/go-ethereum/accounts/keystore"
|
|
|
|
gethcommon "github.com/ethereum/go-ethereum/common"
|
2023-03-10 12:53:40 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2019-07-24 18:59:15 +00:00
|
|
|
"github.com/status-im/status-go/account/generator"
|
2019-12-11 13:59:37 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
2019-12-19 18:27:27 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/keystore"
|
2019-11-23 17:57:05 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2016-09-11 11:44:14 +00:00
|
|
|
"github.com/status-im/status-go/extkeys"
|
2023-03-10 12:53:40 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/accounts"
|
|
|
|
"github.com/status-im/status-go/params"
|
|
|
|
"github.com/status-im/status-go/rpc"
|
2016-06-15 19:50:35 +00:00
|
|
|
)
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
// errors
|
2016-06-15 19:50:35 +00:00
|
|
|
var (
|
2018-03-22 12:31:12 +00:00
|
|
|
ErrAddressToAccountMappingFailure = errors.New("cannot retrieve a valid account for a given address")
|
|
|
|
ErrAccountToKeyMappingFailure = errors.New("cannot retrieve a valid key for a given account")
|
|
|
|
ErrNoAccountSelected = errors.New("no account has been selected, please login")
|
|
|
|
ErrInvalidMasterKeyCreated = errors.New("can not create master extended key")
|
2019-06-26 22:28:16 +00:00
|
|
|
ErrOnboardingNotStarted = errors.New("onboarding must be started before choosing an account")
|
|
|
|
ErrOnboardingAccountNotFound = errors.New("cannot find onboarding account with the given id")
|
2019-08-20 15:38:40 +00:00
|
|
|
ErrAccountKeyStoreMissing = errors.New("account key store is not set")
|
2023-03-10 12:53:40 +00:00
|
|
|
ErrInvalidPersonalSignAccount = errors.New("invalid account as only the selected one can generate a signature")
|
2016-06-15 19:50:35 +00:00
|
|
|
)
|
|
|
|
|
2022-07-06 16:12:49 +00:00
|
|
|
type ErrCannotLocateKeyFile struct {
|
|
|
|
Msg string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e ErrCannotLocateKeyFile) Error() string {
|
|
|
|
return e.Msg
|
|
|
|
}
|
|
|
|
|
2019-12-11 13:59:37 +00:00
|
|
|
var zeroAddress = types.Address{}
|
2019-07-26 14:45:10 +00:00
|
|
|
|
2023-03-10 12:53:40 +00:00
|
|
|
type SignParams struct {
|
|
|
|
Data interface{} `json:"data"`
|
|
|
|
Address string `json:"account"`
|
2023-10-20 06:21:41 +00:00
|
|
|
Password string `json:"password,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sp *SignParams) Validate(checkPassword bool) error {
|
|
|
|
if len(sp.Address) != 2*types.AddressLength+2 {
|
|
|
|
return errors.New("address has to be provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
if sp.Data == "" {
|
|
|
|
return errors.New("data has to be provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
if checkPassword && sp.Password == "" {
|
|
|
|
return errors.New("password has to be provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2023-03-10 12:53:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RecoverParams struct {
|
|
|
|
Message string `json:"message"`
|
|
|
|
Signature string `json:"signature"`
|
|
|
|
}
|
|
|
|
|
2023-06-21 12:13:31 +00:00
|
|
|
// Manager represents account manager interface
|
|
|
|
type Manager interface {
|
2023-06-16 07:10:32 +00:00
|
|
|
GetVerifiedWalletAccount(db *accounts.Database, address, password string) (*SelectedExtKey, error)
|
|
|
|
Sign(rpcParams SignParams, verifiedAccount *SelectedExtKey) (result types.HexBytes, err error)
|
2023-06-21 12:13:31 +00:00
|
|
|
CanRecover(rpcParams RecoverParams, revealedAddress types.Address) (bool, error)
|
2023-06-28 19:45:36 +00:00
|
|
|
DeleteAccount(address types.Address) error
|
2023-06-16 07:10:32 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 12:13:31 +00:00
|
|
|
// DefaultManager represents default account manager implementation
|
|
|
|
type DefaultManager struct {
|
2023-03-10 12:53:40 +00:00
|
|
|
mu sync.RWMutex
|
|
|
|
rpcClient *rpc.Client
|
|
|
|
rpcTimeout time.Duration
|
|
|
|
Keydir string
|
|
|
|
keystore types.KeyStore
|
2019-06-26 22:28:16 +00:00
|
|
|
|
2019-07-24 18:59:15 +00:00
|
|
|
accountsGenerator *generator.Generator
|
|
|
|
onboarding *Onboarding
|
2019-06-26 22:28:16 +00:00
|
|
|
|
2019-07-26 14:45:10 +00:00
|
|
|
selectedChatAccount *SelectedExtKey // account that was processed during the last call to SelectAccount()
|
2019-12-11 13:59:37 +00:00
|
|
|
mainAccountAddress types.Address
|
|
|
|
watchAddresses []types.Address
|
test_: Code Migration from status-cli-tests
author shashankshampi <shashank.sanket1995@gmail.com> 1729780155 +0530
committer shashankshampi <shashank.sanket1995@gmail.com> 1730274350 +0530
test: Code Migration from status-cli-tests
fix_: functional tests (#5979)
* fix_: generate on test-functional
* chore(test)_: fix functional test assertion
---------
Co-authored-by: Siddarth Kumar <siddarthkay@gmail.com>
feat(accounts)_: cherry-pick Persist acceptance of Terms of Use & Privacy policy (#5766) (#5977)
* feat(accounts)_: Persist acceptance of Terms of Use & Privacy policy (#5766)
The original GH issue https://github.com/status-im/status-mobile/issues/21113
came from a request from the Legal team. We must show to Status v1 users the new
terms (Terms of Use & Privacy Policy) right after they upgrade to Status v2
from the stores.
The solution we use is to create a flag in the accounts table, named
hasAcceptedTerms. The flag will be set to true on the first account ever
created in v2 and we provide a native call in mobile/status.go#AcceptTerms,
which allows the client to persist the user's choice in case they are upgrading
(from v1 -> v2, or from a v2 older than this PR).
This solution is not the best because we should store the setting in a separate
table, not in the accounts table.
Related Mobile PR https://github.com/status-im/status-mobile/pull/21124
* fix(test)_: Compare addresses using uppercased strings
---------
Co-authored-by: Icaro Motta <icaro.ldm@gmail.com>
test_: restore account (#5960)
feat_: `LogOnPanic` linter (#5969)
* feat_: LogOnPanic linter
* fix_: add missing defer LogOnPanic
* chore_: make vendor
* fix_: tests, address pr comments
* fix_: address pr comments
fix(ci)_: remove workspace and tmp dir
This ensures we do not encounter weird errors like:
```
+ ln -s /home/jenkins/workspace/go_prs_linux_x86_64_main_PR-5907 /home/jenkins/workspace/go_prs_linux_x86_64_main_PR-5907@tmp/go/src/github.com/status-im/status-go
ln: failed to create symbolic link '/home/jenkins/workspace/go_prs_linux_x86_64_main_PR-5907@tmp/go/src/github.com/status-im/status-go': File exists
script returned exit code 1
```
Signed-off-by: Jakub Sokołowski <jakub@status.im>
chore_: enable windows and macos CI build (#5840)
- Added support for Windows and macOS in CI pipelines
- Added missing dependencies for Windows and x86-64-darwin
- Resolved macOS SDK version compatibility for darwin-x86_64
The `mkShell` override was necessary to ensure compatibility with the newer
macOS SDK (version 11.0) for x86_64. The default SDK (10.12) was causing build failures
because of the missing libs and frameworks. OverrideSDK creates a mapping from
the default SDK in all package categories to the requested SDK (11.0).
fix(contacts)_: fix trust status not being saved to cache when changed (#5965)
Fixes https://github.com/status-im/status-desktop/issues/16392
cleanup
added logger and cleanup
review comments changes
fix_: functional tests (#5979)
* fix_: generate on test-functional
* chore(test)_: fix functional test assertion
---------
Co-authored-by: Siddarth Kumar <siddarthkay@gmail.com>
feat(accounts)_: cherry-pick Persist acceptance of Terms of Use & Privacy policy (#5766) (#5977)
* feat(accounts)_: Persist acceptance of Terms of Use & Privacy policy (#5766)
The original GH issue https://github.com/status-im/status-mobile/issues/21113
came from a request from the Legal team. We must show to Status v1 users the new
terms (Terms of Use & Privacy Policy) right after they upgrade to Status v2
from the stores.
The solution we use is to create a flag in the accounts table, named
hasAcceptedTerms. The flag will be set to true on the first account ever
created in v2 and we provide a native call in mobile/status.go#AcceptTerms,
which allows the client to persist the user's choice in case they are upgrading
(from v1 -> v2, or from a v2 older than this PR).
This solution is not the best because we should store the setting in a separate
table, not in the accounts table.
Related Mobile PR https://github.com/status-im/status-mobile/pull/21124
* fix(test)_: Compare addresses using uppercased strings
---------
Co-authored-by: Icaro Motta <icaro.ldm@gmail.com>
test_: restore account (#5960)
feat_: `LogOnPanic` linter (#5969)
* feat_: LogOnPanic linter
* fix_: add missing defer LogOnPanic
* chore_: make vendor
* fix_: tests, address pr comments
* fix_: address pr comments
chore_: enable windows and macos CI build (#5840)
- Added support for Windows and macOS in CI pipelines
- Added missing dependencies for Windows and x86-64-darwin
- Resolved macOS SDK version compatibility for darwin-x86_64
The `mkShell` override was necessary to ensure compatibility with the newer
macOS SDK (version 11.0) for x86_64. The default SDK (10.12) was causing build failures
because of the missing libs and frameworks. OverrideSDK creates a mapping from
the default SDK in all package categories to the requested SDK (11.0).
fix(contacts)_: fix trust status not being saved to cache when changed (#5965)
Fixes https://github.com/status-im/status-desktop/issues/16392
test_: remove port bind
chore(wallet)_: move route execution code to separate module
chore_: replace geth logger with zap logger (#5962)
closes: #6002
feat(telemetry)_: add metrics for message reliability (#5899)
* feat(telemetry)_: track message reliability
Add metrics for dial errors, missed messages,
missed relevant messages, and confirmed delivery.
* fix_: handle error from json marshal
chore_: use zap logger as request logger
iterates: status-im/status-desktop#16536
test_: unique project per run
test_: use docker compose v2, more concrete project name
fix(codecov)_: ignore folders without tests
Otherwise Codecov reports incorrect numbers when making changes.
https://docs.codecov.com/docs/ignoring-paths
Signed-off-by: Jakub Sokołowski <jakub@status.im>
test_: verify schema of signals during init; fix schema verification warnings (#5947)
fix_: update defaultGorushURL (#6011)
fix(tests)_: use non-standard port to avoid conflicts
We have observed `nimbus-eth2` build failures reporting this port:
```json
{
"lvl": "NTC",
"ts": "2024-10-28 13:51:32.308+00:00",
"msg": "REST HTTP server could not be started",
"topics": "beacnde",
"address": "127.0.0.1:5432",
"reason": "(98) Address already in use"
}
```
https://ci.status.im/job/nimbus-eth2/job/platforms/job/linux/job/x86_64/job/main/job/PR-6683/3/
Signed-off-by: Jakub Sokołowski <jakub@status.im>
fix_: create request logger ad-hoc in tests
Fixes `TestCall` failing when run concurrently.
chore_: configure codecov (#6005)
* chore_: configure codecov
* fix_: after_n_builds
2024-10-24 14:29:15 +00:00
|
|
|
|
|
|
|
logger *zap.Logger
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2019-12-19 18:27:27 +00:00
|
|
|
// GetKeystore is only used in tests
|
2023-06-21 12:13:31 +00:00
|
|
|
func (m *DefaultManager) GetKeystore() types.KeyStore {
|
2019-08-20 15:38:40 +00:00
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
return m.keystore
|
|
|
|
}
|
2019-07-24 18:59:15 +00:00
|
|
|
|
|
|
|
// AccountsGenerator returns accountsGenerator.
|
2023-06-21 12:13:31 +00:00
|
|
|
func (m *DefaultManager) AccountsGenerator() *generator.Generator {
|
2019-07-24 18:59:15 +00:00
|
|
|
return m.accountsGenerator
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2016-09-27 13:51:08 +00:00
|
|
|
// CreateAccount creates an internal geth account
|
2016-08-23 21:32:04 +00:00
|
|
|
// BIP44-compatible keys are generated: CKD#1 is stored as account key, CKD#2 stored as sub-account root
|
|
|
|
// Public key of CKD#1 is returned, with CKD#2 securely encoded into account key file (to be used for
|
|
|
|
// sub-account derivations)
|
2023-06-21 12:13:31 +00:00
|
|
|
func (m *DefaultManager) CreateAccount(password string) (generator.GeneratedAccountInfo, Info, string, error) {
|
2020-12-10 13:38:58 +00:00
|
|
|
var mkInfo generator.GeneratedAccountInfo
|
2019-01-18 09:01:14 +00:00
|
|
|
info := Info{}
|
2020-12-09 14:03:43 +00:00
|
|
|
|
2016-08-23 21:32:04 +00:00
|
|
|
// generate mnemonic phrase
|
2018-05-14 17:13:56 +00:00
|
|
|
mn := extkeys.NewMnemonic()
|
2019-01-18 09:01:14 +00:00
|
|
|
mnemonic, err := mn.MnemonicPhrase(extkeys.EntropyStrength128, extkeys.EnglishLanguage)
|
2016-08-23 21:32:04 +00:00
|
|
|
if err != nil {
|
2020-12-09 14:03:43 +00:00
|
|
|
return mkInfo, info, "", fmt.Errorf("can not create mnemonic seed: %v", err)
|
2016-08-23 21:32:04 +00:00
|
|
|
}
|
|
|
|
|
2018-05-14 17:13:56 +00:00
|
|
|
// Generate extended master key (see BIP32)
|
|
|
|
// We call extkeys.NewMaster with a seed generated with the 12 mnemonic words
|
|
|
|
// but without using the optional password as an extra entropy as described in BIP39.
|
|
|
|
// Future ideas/iterations in Status can add an an advanced options
|
|
|
|
// for expert users, to be able to add a passphrase to the generation of the seed.
|
|
|
|
extKey, err := extkeys.NewMaster(mn.MnemonicSeed(mnemonic, ""))
|
2016-08-23 21:32:04 +00:00
|
|
|
if err != nil {
|
2020-12-09 14:03:43 +00:00
|
|
|
return mkInfo, info, "", fmt.Errorf("can not create master extended key: %v", err)
|
2016-08-23 21:32:04 +00:00
|
|
|
}
|
|
|
|
|
2020-12-15 15:06:59 +00:00
|
|
|
acc := generator.NewAccount(nil, extKey)
|
|
|
|
mkInfo = acc.ToGeneratedAccountInfo("", mnemonic)
|
2020-12-09 14:03:43 +00:00
|
|
|
|
2016-08-23 21:32:04 +00:00
|
|
|
// import created key into account keystore
|
2019-01-18 09:01:14 +00:00
|
|
|
info.WalletAddress, info.WalletPubKey, err = m.importExtendedKey(extkeys.KeyPurposeWallet, extKey, password)
|
2016-08-23 21:32:04 +00:00
|
|
|
if err != nil {
|
2020-12-09 14:03:43 +00:00
|
|
|
return mkInfo, info, "", err
|
2016-08-23 21:32:04 +00:00
|
|
|
}
|
2016-06-29 11:32:04 +00:00
|
|
|
|
2019-01-18 09:01:14 +00:00
|
|
|
info.ChatAddress = info.WalletAddress
|
|
|
|
info.ChatPubKey = info.WalletPubKey
|
|
|
|
|
2020-12-09 14:03:43 +00:00
|
|
|
return mkInfo, info, mnemonic, nil
|
2016-08-18 00:15:58 +00:00
|
|
|
}
|
|
|
|
|
2016-09-27 13:51:08 +00:00
|
|
|
// RecoverAccount re-creates master key using given details.
|
2016-08-23 21:32:04 +00:00
|
|
|
// Once master key is re-generated, it is inserted into keystore (if not already there).
|
2023-06-21 12:13:31 +00:00
|
|
|
func (m *DefaultManager) RecoverAccount(password, mnemonic string) (Info, error) {
|
2019-01-18 09:01:14 +00:00
|
|
|
info := Info{}
|
2016-08-23 21:32:04 +00:00
|
|
|
// re-create extended key (see BIP32)
|
2018-05-14 17:13:56 +00:00
|
|
|
mn := extkeys.NewMnemonic()
|
|
|
|
extKey, err := extkeys.NewMaster(mn.MnemonicSeed(mnemonic, ""))
|
2016-08-23 21:32:04 +00:00
|
|
|
if err != nil {
|
2019-01-18 09:01:14 +00:00
|
|
|
return info, ErrInvalidMasterKeyCreated
|
2016-08-23 21:32:04 +00:00
|
|
|
}
|
2016-08-18 00:15:58 +00:00
|
|
|
|
2016-08-23 21:32:04 +00:00
|
|
|
// import re-created key into account keystore
|
2019-01-18 09:01:14 +00:00
|
|
|
info.WalletAddress, info.WalletPubKey, err = m.importExtendedKey(extkeys.KeyPurposeWallet, extKey, password)
|
2016-08-23 21:32:04 +00:00
|
|
|
if err != nil {
|
2019-01-18 09:01:14 +00:00
|
|
|
return info, err
|
2016-08-18 00:15:58 +00:00
|
|
|
}
|
2016-06-15 19:50:35 +00:00
|
|
|
|
2019-01-18 09:01:14 +00:00
|
|
|
info.ChatAddress = info.WalletAddress
|
|
|
|
info.ChatPubKey = info.WalletPubKey
|
|
|
|
|
|
|
|
return info, nil
|
2016-06-15 19:50:35 +00:00
|
|
|
}
|
2016-06-20 15:21:45 +00:00
|
|
|
|
2017-05-06 21:53:18 +00:00
|
|
|
// VerifyAccountPassword tries to decrypt a given account key file, with a provided password.
|
|
|
|
// If no error is returned, then account is considered verified.
|
2023-06-21 12:13:31 +00:00
|
|
|
func (m *DefaultManager) VerifyAccountPassword(keyStoreDir, address, password string) (*types.Key, error) {
|
2017-05-15 21:49:22 +00:00
|
|
|
var err error
|
2017-10-10 09:38:49 +00:00
|
|
|
var foundKeyFile []byte
|
2017-05-15 21:49:22 +00:00
|
|
|
|
2019-12-11 13:59:37 +00:00
|
|
|
addressObj := types.BytesToAddress(types.FromHex(address))
|
2017-05-15 21:49:22 +00:00
|
|
|
checkAccountKey := func(path string, fileInfo os.FileInfo) error {
|
2017-10-10 09:38:49 +00:00
|
|
|
if len(foundKeyFile) > 0 || fileInfo.IsDir() {
|
2017-05-15 21:49:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-20 09:06:22 +00:00
|
|
|
rawKeyFile, e := ioutil.ReadFile(path)
|
|
|
|
if e != nil {
|
|
|
|
return fmt.Errorf("invalid account key file: %v", e)
|
2017-05-15 21:49:22 +00:00
|
|
|
}
|
2017-10-10 09:38:49 +00:00
|
|
|
|
|
|
|
var accountKey struct {
|
|
|
|
Address string `json:"address"`
|
|
|
|
}
|
2017-10-20 09:06:22 +00:00
|
|
|
if e := json.Unmarshal(rawKeyFile, &accountKey); e != nil {
|
|
|
|
return fmt.Errorf("failed to read key file: %s", e)
|
2017-10-10 09:38:49 +00:00
|
|
|
}
|
2019-12-11 13:59:37 +00:00
|
|
|
if types.HexToAddress("0x"+accountKey.Address).Hex() == addressObj.Hex() {
|
2017-10-10 09:38:49 +00:00
|
|
|
foundKeyFile = rawKeyFile
|
2017-05-15 21:49:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// locate key within key store directory (address should be within the file)
|
|
|
|
err = filepath.Walk(keyStoreDir, func(path string, fileInfo os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return checkAccountKey(path, fileInfo)
|
|
|
|
})
|
2017-05-06 21:53:18 +00:00
|
|
|
if err != nil {
|
2017-05-15 21:49:22 +00:00
|
|
|
return nil, fmt.Errorf("cannot traverse key store folder: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-10-10 09:38:49 +00:00
|
|
|
if len(foundKeyFile) == 0 {
|
2022-07-06 16:12:49 +00:00
|
|
|
return nil, &ErrCannotLocateKeyFile{fmt.Sprintf("cannot locate account for address: %s", addressObj.Hex())}
|
2017-05-06 21:53:18 +00:00
|
|
|
}
|
|
|
|
|
2017-10-10 09:38:49 +00:00
|
|
|
key, err := keystore.DecryptKey(foundKeyFile, password)
|
2017-05-06 21:53:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// avoid swap attack
|
2019-12-19 18:27:27 +00:00
|
|
|
if key.Address != addressObj {
|
2017-10-10 09:38:49 +00:00
|
|
|
return nil, fmt.Errorf("account mismatch: have %s, want %s", key.Address.Hex(), addressObj.Hex())
|
2017-05-06 21:53:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
|
2016-09-27 13:51:08 +00:00
|
|
|
// SelectAccount selects current account, by verifying that address has corresponding account which can be decrypted
|
2018-03-22 12:31:12 +00:00
|
|
|
// using provided password. Once verification is done, all previous identities are removed).
|
2023-06-21 12:13:31 +00:00
|
|
|
func (m *DefaultManager) SelectAccount(loginParams LoginParams) error {
|
2018-04-20 15:39:53 +00:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
2019-07-24 18:59:15 +00:00
|
|
|
m.accountsGenerator.Reset()
|
|
|
|
|
2019-07-26 14:45:10 +00:00
|
|
|
selectedChatAccount, err := m.unlockExtendedKey(loginParams.ChatAddress.String(), loginParams.Password)
|
2016-09-27 13:51:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-26 14:45:10 +00:00
|
|
|
m.watchAddresses = loginParams.WatchAddresses
|
|
|
|
m.mainAccountAddress = loginParams.MainAccount
|
2019-01-18 09:01:14 +00:00
|
|
|
m.selectedChatAccount = selectedChatAccount
|
2016-08-21 06:45:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-06-21 18:29:38 +00:00
|
|
|
|
2023-06-21 12:13:31 +00:00
|
|
|
func (m *DefaultManager) SetAccountAddresses(main types.Address, secondary ...types.Address) {
|
2019-12-11 13:59:37 +00:00
|
|
|
m.watchAddresses = []types.Address{main}
|
2019-09-02 19:03:15 +00:00
|
|
|
m.watchAddresses = append(m.watchAddresses, secondary...)
|
|
|
|
m.mainAccountAddress = main
|
|
|
|
}
|
|
|
|
|
2019-01-24 15:44:46 +00:00
|
|
|
// SetChatAccount initializes selectedChatAccount with privKey
|
2023-06-21 12:13:31 +00:00
|
|
|
func (m *DefaultManager) SetChatAccount(privKey *ecdsa.PrivateKey) error {
|
2019-01-24 15:44:46 +00:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
|
|
|
address := crypto.PubkeyToAddress(privKey.PublicKey)
|
2021-06-30 11:40:54 +00:00
|
|
|
id, err := uuid.NewRandom()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-19 18:27:27 +00:00
|
|
|
key := &types.Key{
|
2020-02-10 11:22:37 +00:00
|
|
|
ID: id,
|
2019-12-19 18:27:27 +00:00
|
|
|
Address: address,
|
2019-01-24 15:44:46 +00:00
|
|
|
PrivateKey: privKey,
|
|
|
|
}
|
|
|
|
|
|
|
|
m.selectedChatAccount = &SelectedExtKey{
|
|
|
|
Address: address,
|
|
|
|
AccountKey: key,
|
|
|
|
}
|
2021-06-30 11:40:54 +00:00
|
|
|
return nil
|
2019-01-24 15:44:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 16:12:49 +00:00
|
|
|
// MainAccountAddress returns main account address set during login
|
2023-06-21 12:13:31 +00:00
|
|
|
func (m *DefaultManager) MainAccountAddress() (types.Address, error) {
|
2019-01-09 08:47:06 +00:00
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
|
2019-07-26 14:45:10 +00:00
|
|
|
if m.mainAccountAddress == zeroAddress {
|
|
|
|
return zeroAddress, ErrNoAccountSelected
|
2019-01-09 08:47:06 +00:00
|
|
|
}
|
2019-07-26 14:45:10 +00:00
|
|
|
|
|
|
|
return m.mainAccountAddress, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WatchAddresses returns currently selected watch addresses.
|
2023-06-21 12:13:31 +00:00
|
|
|
func (m *DefaultManager) WatchAddresses() []types.Address {
|
2019-07-26 14:45:10 +00:00
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
|
|
|
|
return m.watchAddresses
|
2019-01-09 08:47:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SelectedChatAccount returns currently selected chat account
|
2023-06-21 12:13:31 +00:00
|
|
|
func (m *DefaultManager) SelectedChatAccount() (*SelectedExtKey, error) {
|
2018-04-20 15:39:53 +00:00
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
|
2019-01-09 08:47:06 +00:00
|
|
|
if m.selectedChatAccount == nil {
|
2017-05-16 12:09:52 +00:00
|
|
|
return nil, ErrNoAccountSelected
|
|
|
|
}
|
2019-01-09 08:47:06 +00:00
|
|
|
return m.selectedChatAccount, nil
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
2017-01-24 18:42:55 +00:00
|
|
|
|
2019-07-26 14:45:10 +00:00
|
|
|
// Logout clears selected accounts.
|
2023-06-21 12:13:31 +00:00
|
|
|
func (m *DefaultManager) Logout() {
|
2018-04-20 15:39:53 +00:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2016-08-23 21:32:04 +00:00
|
|
|
|
2019-07-24 18:59:15 +00:00
|
|
|
m.accountsGenerator.Reset()
|
2019-07-26 14:45:10 +00:00
|
|
|
m.mainAccountAddress = zeroAddress
|
|
|
|
m.watchAddresses = nil
|
2019-01-09 08:47:06 +00:00
|
|
|
m.selectedChatAccount = nil
|
2016-08-29 00:31:16 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 18:59:15 +00:00
|
|
|
// ImportAccount imports the account specified with privateKey.
|
2023-06-21 12:13:31 +00:00
|
|
|
func (m *DefaultManager) ImportAccount(privateKey *ecdsa.PrivateKey, password string) (types.Address, error) {
|
2019-08-20 15:38:40 +00:00
|
|
|
if m.keystore == nil {
|
2019-12-11 13:59:37 +00:00
|
|
|
return types.Address{}, ErrAccountKeyStoreMissing
|
2019-07-24 18:59:15 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
account, err := m.keystore.ImportECDSA(privateKey, password)
|
2019-07-24 18:59:15 +00:00
|
|
|
|
2019-12-19 18:27:27 +00:00
|
|
|
return account.Address, err
|
2019-07-24 18:59:15 +00:00
|
|
|
}
|
|
|
|
|
2019-12-19 18:27:27 +00:00
|
|
|
// ImportSingleExtendedKey imports an extended key setting it in both the PrivateKey and ExtendedKey fields
|
|
|
|
// of the Key struct.
|
|
|
|
// ImportExtendedKey is used in older version of Status where PrivateKey is set to be the BIP44 key at index 0,
|
|
|
|
// and ExtendedKey is the extended key of the BIP44 key at index 1.
|
2023-06-21 12:13:31 +00:00
|
|
|
func (m *DefaultManager) ImportSingleExtendedKey(extKey *extkeys.ExtendedKey, password string) (address, pubKey string, err error) {
|
2019-08-20 15:38:40 +00:00
|
|
|
if m.keystore == nil {
|
|
|
|
return "", "", ErrAccountKeyStoreMissing
|
2019-07-24 18:59:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// imports extended key, create key file (if necessary)
|
2019-08-20 15:38:40 +00:00
|
|
|
account, err := m.keystore.ImportSingleExtendedKey(extKey, password)
|
2019-07-24 18:59:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
address = account.Address.Hex()
|
|
|
|
|
|
|
|
// obtain public key to return
|
2019-08-20 15:38:40 +00:00
|
|
|
account, key, err := m.keystore.AccountDecryptedKey(account, password)
|
2019-07-24 18:59:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return address, "", err
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
pubKey = types.EncodeHex(crypto.FromECDSAPub(&key.PrivateKey.PublicKey))
|
2019-07-24 18:59:15 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-23 21:32:04 +00:00
|
|
|
// importExtendedKey processes incoming extended key, extracts required info and creates corresponding account key.
|
|
|
|
// Once account key is formed, that key is put (if not already) into keystore i.e. key is *encoded* into key file.
|
2023-06-21 12:13:31 +00:00
|
|
|
func (m *DefaultManager) importExtendedKey(keyPurpose extkeys.KeyPurpose, extKey *extkeys.ExtendedKey, password string) (address, pubKey string, err error) {
|
2019-08-20 15:38:40 +00:00
|
|
|
if m.keystore == nil {
|
|
|
|
return "", "", ErrAccountKeyStoreMissing
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2016-08-23 21:32:04 +00:00
|
|
|
// imports extended key, create key file (if necessary)
|
2019-08-20 15:38:40 +00:00
|
|
|
account, err := m.keystore.ImportExtendedKeyForPurpose(keyPurpose, extKey, password)
|
2016-08-23 21:32:04 +00:00
|
|
|
if err != nil {
|
2016-09-11 11:44:14 +00:00
|
|
|
return "", "", err
|
2016-08-23 21:32:04 +00:00
|
|
|
}
|
2017-10-10 09:38:49 +00:00
|
|
|
address = account.Address.Hex()
|
2016-08-23 21:32:04 +00:00
|
|
|
|
|
|
|
// obtain public key to return
|
2019-08-20 15:38:40 +00:00
|
|
|
account, key, err := m.keystore.AccountDecryptedKey(account, password)
|
2016-08-23 21:32:04 +00:00
|
|
|
if err != nil {
|
2016-09-11 11:44:14 +00:00
|
|
|
return address, "", err
|
2016-08-23 21:32:04 +00:00
|
|
|
}
|
2019-11-23 17:57:05 +00:00
|
|
|
pubKey = types.EncodeHex(crypto.FromECDSAPub(&key.PrivateKey.PublicKey))
|
2016-08-23 21:32:04 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2016-09-27 13:51:08 +00:00
|
|
|
|
2017-09-25 16:04:40 +00:00
|
|
|
// Accounts returns list of addresses for selected account, including
|
|
|
|
// subaccounts.
|
2023-06-21 12:13:31 +00:00
|
|
|
func (m *DefaultManager) Accounts() ([]types.Address, error) {
|
2018-04-20 15:39:53 +00:00
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
2019-12-11 13:59:37 +00:00
|
|
|
addresses := make([]types.Address, 0)
|
2019-07-26 14:45:10 +00:00
|
|
|
if m.mainAccountAddress != zeroAddress {
|
|
|
|
addresses = append(addresses, m.mainAccountAddress)
|
2017-09-25 16:04:40 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 14:45:10 +00:00
|
|
|
return addresses, nil
|
2017-09-25 16:04:40 +00:00
|
|
|
}
|
2016-09-27 13:51:08 +00:00
|
|
|
|
2019-06-26 22:28:16 +00:00
|
|
|
// StartOnboarding starts the onboarding process generating accountsCount accounts and returns a slice of OnboardingAccount.
|
2023-06-21 12:13:31 +00:00
|
|
|
func (m *DefaultManager) StartOnboarding(accountsCount, mnemonicPhraseLength int) ([]*OnboardingAccount, error) {
|
2019-06-26 22:28:16 +00:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
|
|
|
onboarding, err := NewOnboarding(accountsCount, mnemonicPhraseLength)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.onboarding = onboarding
|
|
|
|
|
|
|
|
return m.onboarding.Accounts(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveOnboarding reset the current onboarding struct setting it to nil and deleting the accounts from memory.
|
2023-06-21 12:13:31 +00:00
|
|
|
func (m *DefaultManager) RemoveOnboarding() {
|
2019-06-26 22:28:16 +00:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
|
|
|
m.onboarding = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ImportOnboardingAccount imports the account specified by id and encrypts it with password.
|
2023-06-21 12:13:31 +00:00
|
|
|
func (m *DefaultManager) ImportOnboardingAccount(id string, password string) (Info, string, error) {
|
2019-06-26 22:28:16 +00:00
|
|
|
var info Info
|
|
|
|
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
|
|
|
if m.onboarding == nil {
|
|
|
|
return info, "", ErrOnboardingNotStarted
|
|
|
|
}
|
|
|
|
|
|
|
|
acc, err := m.onboarding.Account(id)
|
|
|
|
if err != nil {
|
|
|
|
return info, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err = m.RecoverAccount(password, acc.mnemonic)
|
|
|
|
if err != nil {
|
|
|
|
return info, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.onboarding = nil
|
|
|
|
|
|
|
|
return info, acc.mnemonic, nil
|
|
|
|
}
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
// AddressToDecryptedAccount tries to load decrypted key for a given account.
|
|
|
|
// The running node, has a keystore directory which is loaded on start. Key file
|
|
|
|
// for a given address is expected to be in that directory prior to node start.
|
2023-06-21 12:13:31 +00:00
|
|
|
func (m *DefaultManager) AddressToDecryptedAccount(address, password string) (types.Account, *types.Key, error) {
|
2019-08-20 15:38:40 +00:00
|
|
|
if m.keystore == nil {
|
2019-12-19 18:27:27 +00:00
|
|
|
return types.Account{}, nil, ErrAccountKeyStoreMissing
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 12:31:12 +00:00
|
|
|
account, err := ParseAccountString(address)
|
2017-05-16 12:09:52 +00:00
|
|
|
if err != nil {
|
2019-12-19 18:27:27 +00:00
|
|
|
return types.Account{}, nil, ErrAddressToAccountMappingFailure
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2019-12-19 18:27:27 +00:00
|
|
|
account, key, err := m.keystore.AccountDecryptedKey(account, password)
|
2019-01-18 09:01:14 +00:00
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("%s: %s", ErrAccountToKeyMappingFailure, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return account, key, err
|
|
|
|
}
|
|
|
|
|
2023-06-21 12:13:31 +00:00
|
|
|
func (m *DefaultManager) unlockExtendedKey(address, password string) (*SelectedExtKey, error) {
|
2019-01-18 09:01:14 +00:00
|
|
|
account, accountKey, err := m.AddressToDecryptedAccount(address, password)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
selectedExtendedKey := &SelectedExtKey{
|
2019-12-19 18:27:27 +00:00
|
|
|
Address: account.Address,
|
2019-07-26 14:45:10 +00:00
|
|
|
AccountKey: accountKey,
|
2019-01-18 09:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return selectedExtendedKey, nil
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
2020-06-22 12:03:28 +00:00
|
|
|
|
2023-06-21 12:13:31 +00:00
|
|
|
func (m *DefaultManager) MigrateKeyStoreDir(oldDir, newDir string, addresses []string) error {
|
2020-06-22 12:03:28 +00:00
|
|
|
paths := []string{}
|
|
|
|
|
|
|
|
addressesMap := map[string]struct{}{}
|
|
|
|
for _, address := range addresses {
|
|
|
|
addressesMap[address] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkFile := func(path string, fileInfo os.FileInfo) error {
|
|
|
|
if fileInfo.IsDir() || filepath.Dir(path) != oldDir {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rawKeyFile, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid account key file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var accountKey struct {
|
|
|
|
Address string `json:"address"`
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(rawKeyFile, &accountKey); err != nil {
|
|
|
|
return fmt.Errorf("failed to read key file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
address := types.HexToAddress("0x" + accountKey.Address).Hex()
|
|
|
|
if _, ok := addressesMap[address]; ok {
|
|
|
|
paths = append(paths, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := filepath.Walk(oldDir, func(path string, fileInfo os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return checkFile(path, fileInfo)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot traverse key store folder: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, path := range paths {
|
|
|
|
_, fileName := filepath.Split(path)
|
|
|
|
newPath := filepath.Join(newDir, fileName)
|
|
|
|
err := os.Rename(path, newPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-06-23 09:21:21 +00:00
|
|
|
|
2023-06-21 12:13:31 +00:00
|
|
|
func (m *DefaultManager) ReEncryptKey(rawKey []byte, pass string, newPass string) (reEncryptedKey []byte, e error) {
|
2021-06-23 09:21:21 +00:00
|
|
|
cryptoJSON, e := keystore.RawKeyToCryptoJSON(rawKey)
|
|
|
|
if e != nil {
|
|
|
|
return reEncryptedKey, fmt.Errorf("convert to crypto json error: %v", e)
|
|
|
|
}
|
|
|
|
|
|
|
|
decryptedKey, e := keystore.DecryptKey(rawKey, pass)
|
|
|
|
if e != nil {
|
|
|
|
return reEncryptedKey, fmt.Errorf("decryption error: %v", e)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cryptoJSON.KDFParams["n"] == nil || cryptoJSON.KDFParams["p"] == nil {
|
|
|
|
return reEncryptedKey, fmt.Errorf("Unable to determine `n` or `p`: %v", e)
|
|
|
|
}
|
|
|
|
n := int(cryptoJSON.KDFParams["n"].(float64))
|
|
|
|
p := int(cryptoJSON.KDFParams["p"].(float64))
|
|
|
|
|
|
|
|
gethKey := gethkeystore.Key{
|
|
|
|
Id: decryptedKey.ID,
|
|
|
|
Address: gethcommon.Address(decryptedKey.Address),
|
|
|
|
PrivateKey: decryptedKey.PrivateKey,
|
|
|
|
ExtendedKey: decryptedKey.ExtendedKey,
|
|
|
|
SubAccountIndex: decryptedKey.SubAccountIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
return gethkeystore.EncryptKey(&gethKey, newPass, n, p)
|
|
|
|
}
|
|
|
|
|
2023-06-21 12:13:31 +00:00
|
|
|
func (m *DefaultManager) ReEncryptKeyStoreDir(keyDirPath, oldPass, newPass string) error {
|
2021-06-23 09:21:21 +00:00
|
|
|
rencryptFileAtPath := func(tempKeyDirPath, path string, fileInfo os.FileInfo) error {
|
|
|
|
if fileInfo.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rawKeyFile, e := ioutil.ReadFile(path)
|
|
|
|
if e != nil {
|
|
|
|
return fmt.Errorf("invalid account key file: %v", e)
|
|
|
|
}
|
|
|
|
|
|
|
|
reEncryptedKey, e := m.ReEncryptKey(rawKeyFile, oldPass, newPass)
|
|
|
|
if e != nil {
|
|
|
|
return fmt.Errorf("unable to re-encrypt key file: %v, path: %s, name: %s", e, path, fileInfo.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
tempWritePath := filepath.Join(tempKeyDirPath, fileInfo.Name())
|
|
|
|
e = ioutil.WriteFile(tempWritePath, reEncryptedKey, fileInfo.Mode().Perm())
|
|
|
|
if e != nil {
|
|
|
|
return fmt.Errorf("unable write key file: %v", e)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-11 11:14:20 +00:00
|
|
|
keyDirPath = strings.TrimSuffix(keyDirPath, "/")
|
2023-04-20 20:57:16 +00:00
|
|
|
keyDirPath = strings.TrimSuffix(keyDirPath, "\\")
|
2021-06-23 09:21:21 +00:00
|
|
|
keyParent, keyDirName := filepath.Split(keyDirPath)
|
|
|
|
|
|
|
|
// backupKeyDirName used to store existing keys before final write
|
|
|
|
backupKeyDirName := keyDirName + "-backup"
|
|
|
|
// tempKeyDirName used to put re-encrypted keys
|
|
|
|
tempKeyDirName := keyDirName + "-re-encrypted"
|
|
|
|
backupKeyDirPath := filepath.Join(keyParent, backupKeyDirName)
|
|
|
|
tempKeyDirPath := filepath.Join(keyParent, tempKeyDirName)
|
|
|
|
|
|
|
|
// create temp key dir
|
|
|
|
err := os.MkdirAll(tempKeyDirPath, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("mkdirall error: %v, tempKeyDirPath: %s", err, tempKeyDirPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = filepath.Walk(keyDirPath, func(path string, fileInfo os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
os.RemoveAll(tempKeyDirPath)
|
|
|
|
return fmt.Errorf("walk callback error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return rencryptFileAtPath(tempKeyDirPath, path, fileInfo)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
os.RemoveAll(tempKeyDirPath)
|
|
|
|
return fmt.Errorf("walk error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// move existing keys
|
|
|
|
err = os.Rename(keyDirPath, backupKeyDirPath)
|
|
|
|
if err != nil {
|
|
|
|
os.RemoveAll(tempKeyDirPath)
|
|
|
|
return fmt.Errorf("unable to rename keyDirPath to backupKeyDirPath: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// move tempKeyDirPath to keyDirPath
|
|
|
|
err = os.Rename(tempKeyDirPath, keyDirPath)
|
|
|
|
if err != nil {
|
|
|
|
// if this happens, then the app is probably bricked, because the keystore won't exist anymore
|
|
|
|
// try to restore from backup
|
|
|
|
_ = os.Rename(backupKeyDirPath, keyDirPath)
|
|
|
|
return fmt.Errorf("unable to rename tempKeyDirPath to keyDirPath: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove temp and backup folders and their contents
|
|
|
|
err = os.RemoveAll(tempKeyDirPath)
|
|
|
|
if err != nil {
|
|
|
|
// the re-encryption is complete so we don't throw
|
test_: Code Migration from status-cli-tests
author shashankshampi <shashank.sanket1995@gmail.com> 1729780155 +0530
committer shashankshampi <shashank.sanket1995@gmail.com> 1730274350 +0530
test: Code Migration from status-cli-tests
fix_: functional tests (#5979)
* fix_: generate on test-functional
* chore(test)_: fix functional test assertion
---------
Co-authored-by: Siddarth Kumar <siddarthkay@gmail.com>
feat(accounts)_: cherry-pick Persist acceptance of Terms of Use & Privacy policy (#5766) (#5977)
* feat(accounts)_: Persist acceptance of Terms of Use & Privacy policy (#5766)
The original GH issue https://github.com/status-im/status-mobile/issues/21113
came from a request from the Legal team. We must show to Status v1 users the new
terms (Terms of Use & Privacy Policy) right after they upgrade to Status v2
from the stores.
The solution we use is to create a flag in the accounts table, named
hasAcceptedTerms. The flag will be set to true on the first account ever
created in v2 and we provide a native call in mobile/status.go#AcceptTerms,
which allows the client to persist the user's choice in case they are upgrading
(from v1 -> v2, or from a v2 older than this PR).
This solution is not the best because we should store the setting in a separate
table, not in the accounts table.
Related Mobile PR https://github.com/status-im/status-mobile/pull/21124
* fix(test)_: Compare addresses using uppercased strings
---------
Co-authored-by: Icaro Motta <icaro.ldm@gmail.com>
test_: restore account (#5960)
feat_: `LogOnPanic` linter (#5969)
* feat_: LogOnPanic linter
* fix_: add missing defer LogOnPanic
* chore_: make vendor
* fix_: tests, address pr comments
* fix_: address pr comments
fix(ci)_: remove workspace and tmp dir
This ensures we do not encounter weird errors like:
```
+ ln -s /home/jenkins/workspace/go_prs_linux_x86_64_main_PR-5907 /home/jenkins/workspace/go_prs_linux_x86_64_main_PR-5907@tmp/go/src/github.com/status-im/status-go
ln: failed to create symbolic link '/home/jenkins/workspace/go_prs_linux_x86_64_main_PR-5907@tmp/go/src/github.com/status-im/status-go': File exists
script returned exit code 1
```
Signed-off-by: Jakub Sokołowski <jakub@status.im>
chore_: enable windows and macos CI build (#5840)
- Added support for Windows and macOS in CI pipelines
- Added missing dependencies for Windows and x86-64-darwin
- Resolved macOS SDK version compatibility for darwin-x86_64
The `mkShell` override was necessary to ensure compatibility with the newer
macOS SDK (version 11.0) for x86_64. The default SDK (10.12) was causing build failures
because of the missing libs and frameworks. OverrideSDK creates a mapping from
the default SDK in all package categories to the requested SDK (11.0).
fix(contacts)_: fix trust status not being saved to cache when changed (#5965)
Fixes https://github.com/status-im/status-desktop/issues/16392
cleanup
added logger and cleanup
review comments changes
fix_: functional tests (#5979)
* fix_: generate on test-functional
* chore(test)_: fix functional test assertion
---------
Co-authored-by: Siddarth Kumar <siddarthkay@gmail.com>
feat(accounts)_: cherry-pick Persist acceptance of Terms of Use & Privacy policy (#5766) (#5977)
* feat(accounts)_: Persist acceptance of Terms of Use & Privacy policy (#5766)
The original GH issue https://github.com/status-im/status-mobile/issues/21113
came from a request from the Legal team. We must show to Status v1 users the new
terms (Terms of Use & Privacy Policy) right after they upgrade to Status v2
from the stores.
The solution we use is to create a flag in the accounts table, named
hasAcceptedTerms. The flag will be set to true on the first account ever
created in v2 and we provide a native call in mobile/status.go#AcceptTerms,
which allows the client to persist the user's choice in case they are upgrading
(from v1 -> v2, or from a v2 older than this PR).
This solution is not the best because we should store the setting in a separate
table, not in the accounts table.
Related Mobile PR https://github.com/status-im/status-mobile/pull/21124
* fix(test)_: Compare addresses using uppercased strings
---------
Co-authored-by: Icaro Motta <icaro.ldm@gmail.com>
test_: restore account (#5960)
feat_: `LogOnPanic` linter (#5969)
* feat_: LogOnPanic linter
* fix_: add missing defer LogOnPanic
* chore_: make vendor
* fix_: tests, address pr comments
* fix_: address pr comments
chore_: enable windows and macos CI build (#5840)
- Added support for Windows and macOS in CI pipelines
- Added missing dependencies for Windows and x86-64-darwin
- Resolved macOS SDK version compatibility for darwin-x86_64
The `mkShell` override was necessary to ensure compatibility with the newer
macOS SDK (version 11.0) for x86_64. The default SDK (10.12) was causing build failures
because of the missing libs and frameworks. OverrideSDK creates a mapping from
the default SDK in all package categories to the requested SDK (11.0).
fix(contacts)_: fix trust status not being saved to cache when changed (#5965)
Fixes https://github.com/status-im/status-desktop/issues/16392
test_: remove port bind
chore(wallet)_: move route execution code to separate module
chore_: replace geth logger with zap logger (#5962)
closes: #6002
feat(telemetry)_: add metrics for message reliability (#5899)
* feat(telemetry)_: track message reliability
Add metrics for dial errors, missed messages,
missed relevant messages, and confirmed delivery.
* fix_: handle error from json marshal
chore_: use zap logger as request logger
iterates: status-im/status-desktop#16536
test_: unique project per run
test_: use docker compose v2, more concrete project name
fix(codecov)_: ignore folders without tests
Otherwise Codecov reports incorrect numbers when making changes.
https://docs.codecov.com/docs/ignoring-paths
Signed-off-by: Jakub Sokołowski <jakub@status.im>
test_: verify schema of signals during init; fix schema verification warnings (#5947)
fix_: update defaultGorushURL (#6011)
fix(tests)_: use non-standard port to avoid conflicts
We have observed `nimbus-eth2` build failures reporting this port:
```json
{
"lvl": "NTC",
"ts": "2024-10-28 13:51:32.308+00:00",
"msg": "REST HTTP server could not be started",
"topics": "beacnde",
"address": "127.0.0.1:5432",
"reason": "(98) Address already in use"
}
```
https://ci.status.im/job/nimbus-eth2/job/platforms/job/linux/job/x86_64/job/main/job/PR-6683/3/
Signed-off-by: Jakub Sokołowski <jakub@status.im>
fix_: create request logger ad-hoc in tests
Fixes `TestCall` failing when run concurrently.
chore_: configure codecov (#6005)
* chore_: configure codecov
* fix_: after_n_builds
2024-10-24 14:29:15 +00:00
|
|
|
m.logger.Error("unable to delete tempKeyDirPath, manual cleanup required")
|
2021-06-23 09:21:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = os.RemoveAll(backupKeyDirPath)
|
|
|
|
if err != nil {
|
|
|
|
// the re-encryption is complete so we don't throw
|
test_: Code Migration from status-cli-tests
author shashankshampi <shashank.sanket1995@gmail.com> 1729780155 +0530
committer shashankshampi <shashank.sanket1995@gmail.com> 1730274350 +0530
test: Code Migration from status-cli-tests
fix_: functional tests (#5979)
* fix_: generate on test-functional
* chore(test)_: fix functional test assertion
---------
Co-authored-by: Siddarth Kumar <siddarthkay@gmail.com>
feat(accounts)_: cherry-pick Persist acceptance of Terms of Use & Privacy policy (#5766) (#5977)
* feat(accounts)_: Persist acceptance of Terms of Use & Privacy policy (#5766)
The original GH issue https://github.com/status-im/status-mobile/issues/21113
came from a request from the Legal team. We must show to Status v1 users the new
terms (Terms of Use & Privacy Policy) right after they upgrade to Status v2
from the stores.
The solution we use is to create a flag in the accounts table, named
hasAcceptedTerms. The flag will be set to true on the first account ever
created in v2 and we provide a native call in mobile/status.go#AcceptTerms,
which allows the client to persist the user's choice in case they are upgrading
(from v1 -> v2, or from a v2 older than this PR).
This solution is not the best because we should store the setting in a separate
table, not in the accounts table.
Related Mobile PR https://github.com/status-im/status-mobile/pull/21124
* fix(test)_: Compare addresses using uppercased strings
---------
Co-authored-by: Icaro Motta <icaro.ldm@gmail.com>
test_: restore account (#5960)
feat_: `LogOnPanic` linter (#5969)
* feat_: LogOnPanic linter
* fix_: add missing defer LogOnPanic
* chore_: make vendor
* fix_: tests, address pr comments
* fix_: address pr comments
fix(ci)_: remove workspace and tmp dir
This ensures we do not encounter weird errors like:
```
+ ln -s /home/jenkins/workspace/go_prs_linux_x86_64_main_PR-5907 /home/jenkins/workspace/go_prs_linux_x86_64_main_PR-5907@tmp/go/src/github.com/status-im/status-go
ln: failed to create symbolic link '/home/jenkins/workspace/go_prs_linux_x86_64_main_PR-5907@tmp/go/src/github.com/status-im/status-go': File exists
script returned exit code 1
```
Signed-off-by: Jakub Sokołowski <jakub@status.im>
chore_: enable windows and macos CI build (#5840)
- Added support for Windows and macOS in CI pipelines
- Added missing dependencies for Windows and x86-64-darwin
- Resolved macOS SDK version compatibility for darwin-x86_64
The `mkShell` override was necessary to ensure compatibility with the newer
macOS SDK (version 11.0) for x86_64. The default SDK (10.12) was causing build failures
because of the missing libs and frameworks. OverrideSDK creates a mapping from
the default SDK in all package categories to the requested SDK (11.0).
fix(contacts)_: fix trust status not being saved to cache when changed (#5965)
Fixes https://github.com/status-im/status-desktop/issues/16392
cleanup
added logger and cleanup
review comments changes
fix_: functional tests (#5979)
* fix_: generate on test-functional
* chore(test)_: fix functional test assertion
---------
Co-authored-by: Siddarth Kumar <siddarthkay@gmail.com>
feat(accounts)_: cherry-pick Persist acceptance of Terms of Use & Privacy policy (#5766) (#5977)
* feat(accounts)_: Persist acceptance of Terms of Use & Privacy policy (#5766)
The original GH issue https://github.com/status-im/status-mobile/issues/21113
came from a request from the Legal team. We must show to Status v1 users the new
terms (Terms of Use & Privacy Policy) right after they upgrade to Status v2
from the stores.
The solution we use is to create a flag in the accounts table, named
hasAcceptedTerms. The flag will be set to true on the first account ever
created in v2 and we provide a native call in mobile/status.go#AcceptTerms,
which allows the client to persist the user's choice in case they are upgrading
(from v1 -> v2, or from a v2 older than this PR).
This solution is not the best because we should store the setting in a separate
table, not in the accounts table.
Related Mobile PR https://github.com/status-im/status-mobile/pull/21124
* fix(test)_: Compare addresses using uppercased strings
---------
Co-authored-by: Icaro Motta <icaro.ldm@gmail.com>
test_: restore account (#5960)
feat_: `LogOnPanic` linter (#5969)
* feat_: LogOnPanic linter
* fix_: add missing defer LogOnPanic
* chore_: make vendor
* fix_: tests, address pr comments
* fix_: address pr comments
chore_: enable windows and macos CI build (#5840)
- Added support for Windows and macOS in CI pipelines
- Added missing dependencies for Windows and x86-64-darwin
- Resolved macOS SDK version compatibility for darwin-x86_64
The `mkShell` override was necessary to ensure compatibility with the newer
macOS SDK (version 11.0) for x86_64. The default SDK (10.12) was causing build failures
because of the missing libs and frameworks. OverrideSDK creates a mapping from
the default SDK in all package categories to the requested SDK (11.0).
fix(contacts)_: fix trust status not being saved to cache when changed (#5965)
Fixes https://github.com/status-im/status-desktop/issues/16392
test_: remove port bind
chore(wallet)_: move route execution code to separate module
chore_: replace geth logger with zap logger (#5962)
closes: #6002
feat(telemetry)_: add metrics for message reliability (#5899)
* feat(telemetry)_: track message reliability
Add metrics for dial errors, missed messages,
missed relevant messages, and confirmed delivery.
* fix_: handle error from json marshal
chore_: use zap logger as request logger
iterates: status-im/status-desktop#16536
test_: unique project per run
test_: use docker compose v2, more concrete project name
fix(codecov)_: ignore folders without tests
Otherwise Codecov reports incorrect numbers when making changes.
https://docs.codecov.com/docs/ignoring-paths
Signed-off-by: Jakub Sokołowski <jakub@status.im>
test_: verify schema of signals during init; fix schema verification warnings (#5947)
fix_: update defaultGorushURL (#6011)
fix(tests)_: use non-standard port to avoid conflicts
We have observed `nimbus-eth2` build failures reporting this port:
```json
{
"lvl": "NTC",
"ts": "2024-10-28 13:51:32.308+00:00",
"msg": "REST HTTP server could not be started",
"topics": "beacnde",
"address": "127.0.0.1:5432",
"reason": "(98) Address already in use"
}
```
https://ci.status.im/job/nimbus-eth2/job/platforms/job/linux/job/x86_64/job/main/job/PR-6683/3/
Signed-off-by: Jakub Sokołowski <jakub@status.im>
fix_: create request logger ad-hoc in tests
Fixes `TestCall` failing when run concurrently.
chore_: configure codecov (#6005)
* chore_: configure codecov
* fix_: after_n_builds
2024-10-24 14:29:15 +00:00
|
|
|
m.logger.Error("unable to delete backupKeyDirPath, manual cleanup required")
|
2021-06-23 09:21:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-12 11:06:58 +00:00
|
|
|
|
2023-06-21 12:13:31 +00:00
|
|
|
func (m *DefaultManager) DeleteAccount(address types.Address) error {
|
2023-05-05 07:43:05 +00:00
|
|
|
return m.keystore.Delete(types.Account{Address: address})
|
2022-05-12 11:06:58 +00:00
|
|
|
}
|
2023-03-10 12:53:40 +00:00
|
|
|
|
2023-06-21 12:13:31 +00:00
|
|
|
func (m *DefaultManager) GetVerifiedWalletAccount(db *accounts.Database, address, password string) (*SelectedExtKey, error) {
|
2023-03-10 12:53:40 +00:00
|
|
|
exists, err := db.AddressExists(types.HexToAddress(address))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
return nil, errors.New("account doesn't exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := m.VerifyAccountPassword(m.Keydir, address, password)
|
|
|
|
if _, ok := err.(*ErrCannotLocateKeyFile); ok {
|
|
|
|
key, err = m.generatePartialAccountKey(db, address, password)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &SelectedExtKey{
|
|
|
|
Address: key.Address,
|
|
|
|
AccountKey: key,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-06-21 12:13:31 +00:00
|
|
|
func (m *DefaultManager) generatePartialAccountKey(db *accounts.Database, address string, password string) (*types.Key, error) {
|
2023-03-10 12:53:40 +00:00
|
|
|
dbPath, err := db.GetPath(types.HexToAddress(address))
|
|
|
|
path := "m/" + dbPath[strings.LastIndex(dbPath, "/")+1:]
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rootAddress, err := db.GetWalletRootAddress()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
info, err := m.AccountsGenerator().LoadAccount(rootAddress.Hex(), password)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
masterID := info.ID
|
|
|
|
|
|
|
|
accInfosMap, err := m.AccountsGenerator().StoreDerivedAccounts(masterID, password, []string{path})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, key, err := m.AddressToDecryptedAccount(accInfosMap[path].Address, password)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
|
2023-06-21 12:13:31 +00:00
|
|
|
func (m *DefaultManager) Recover(rpcParams RecoverParams) (addr types.Address, err error) {
|
2023-03-10 12:53:40 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), m.rpcTimeout)
|
|
|
|
defer cancel()
|
|
|
|
var gethAddr gethcommon.Address
|
|
|
|
err = m.rpcClient.CallContextIgnoringLocalHandlers(
|
|
|
|
ctx,
|
|
|
|
&gethAddr,
|
|
|
|
m.rpcClient.UpstreamChainID,
|
|
|
|
params.PersonalRecoverMethodName,
|
|
|
|
rpcParams.Message, rpcParams.Signature)
|
|
|
|
addr = types.Address(gethAddr)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-21 12:13:31 +00:00
|
|
|
func (m *DefaultManager) CanRecover(rpcParams RecoverParams, revealedAddress types.Address) (bool, error) {
|
|
|
|
recovered, err := m.Recover(rpcParams)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return recovered == revealedAddress, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DefaultManager) Sign(rpcParams SignParams, verifiedAccount *SelectedExtKey) (result types.HexBytes, err error) {
|
2023-03-10 12:53:40 +00:00
|
|
|
if !strings.EqualFold(rpcParams.Address, verifiedAccount.Address.Hex()) {
|
|
|
|
err = ErrInvalidPersonalSignAccount
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), m.rpcTimeout)
|
|
|
|
defer cancel()
|
|
|
|
var gethResult hexutil.Bytes
|
|
|
|
err = m.rpcClient.CallContextIgnoringLocalHandlers(
|
|
|
|
ctx,
|
|
|
|
&gethResult,
|
|
|
|
m.rpcClient.UpstreamChainID,
|
|
|
|
params.PersonalSignMethodName,
|
|
|
|
rpcParams.Data, rpcParams.Address, rpcParams.Password)
|
|
|
|
result = types.HexBytes(gethResult)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|