2019-07-24 18:59:15 +00:00
|
|
|
package statusgo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-01-05 14:43:39 +00:00
|
|
|
"strings"
|
2019-07-24 18:59:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// MultiAccountGenerateParams are the params sent to MultiAccountGenerate.
|
|
|
|
type MultiAccountGenerateParams struct {
|
|
|
|
N int `json:"n"`
|
|
|
|
MnemonicPhraseLength int `json:"mnemonicPhraseLength"`
|
|
|
|
Bip39Passphrase string `json:"bip39Passphrase"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// MultiAccountGenerateAndDeriveAddressesParams are the params sent to MultiAccountGenerateAndDeriveAddresses.
|
|
|
|
type MultiAccountGenerateAndDeriveAddressesParams struct {
|
|
|
|
MultiAccountGenerateParams
|
|
|
|
Paths []string `json:"paths"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// MultiAccountDeriveAddressesParams are the params sent to MultiAccountDeriveAddresses.
|
|
|
|
type MultiAccountDeriveAddressesParams struct {
|
|
|
|
AccountID string `json:"accountID"`
|
|
|
|
Paths []string `json:"paths"`
|
|
|
|
}
|
|
|
|
|
2019-07-26 09:33:38 +00:00
|
|
|
// MultiAccountStoreDerivedAccountsParams are the params sent to MultiAccountStoreDerivedAccounts.
|
|
|
|
type MultiAccountStoreDerivedAccountsParams struct {
|
2019-07-24 18:59:15 +00:00
|
|
|
MultiAccountDeriveAddressesParams
|
|
|
|
Password string `json:"password"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// MultiAccountStoreAccountParams are the params sent to MultiAccountStoreAccount.
|
|
|
|
type MultiAccountStoreAccountParams struct {
|
|
|
|
AccountID string `json:"accountID"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// MultiAccountImportPrivateKeyParams are the params sent to MultiAccountImportPrivateKey.
|
|
|
|
type MultiAccountImportPrivateKeyParams struct {
|
|
|
|
PrivateKey string `json:"privateKey"`
|
|
|
|
}
|
|
|
|
|
2019-07-26 09:33:38 +00:00
|
|
|
// MultiAccountLoadAccountParams are the params sent to MultiAccountLoadAccount.
|
|
|
|
type MultiAccountLoadAccountParams struct {
|
|
|
|
Address string `json:"address"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// MultiAccountImportMnemonicParams are the params sent to MultiAccountImportMnemonic.
|
|
|
|
type MultiAccountImportMnemonicParams struct {
|
2022-10-27 18:29:57 +00:00
|
|
|
MnemonicPhrase string `json:"mnemonicPhrase"`
|
|
|
|
Bip39Passphrase string `json:"Bip39Passphrase"`
|
|
|
|
Paths []string `json:"paths"`
|
2019-07-26 09:33:38 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 18:59:15 +00:00
|
|
|
// MultiAccountGenerate generates account in memory without storing them.
|
|
|
|
func MultiAccountGenerate(paramsJSON string) string {
|
|
|
|
var p MultiAccountGenerateParams
|
|
|
|
|
|
|
|
if err := json.Unmarshal([]byte(paramsJSON), &p); err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := statusBackend.AccountManager().AccountsGenerator().Generate(p.MnemonicPhraseLength, p.N, p.Bip39Passphrase)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := json.Marshal(resp)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MultiAccountGenerateAndDeriveAddresses combines Generate and DeriveAddresses in one call.
|
|
|
|
func MultiAccountGenerateAndDeriveAddresses(paramsJSON string) string {
|
|
|
|
var p MultiAccountGenerateAndDeriveAddressesParams
|
|
|
|
|
|
|
|
if err := json.Unmarshal([]byte(paramsJSON), &p); err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := statusBackend.AccountManager().AccountsGenerator().GenerateAndDeriveAddresses(p.MnemonicPhraseLength, p.N, p.Bip39Passphrase, p.Paths)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := json.Marshal(resp)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MultiAccountDeriveAddresses derive addresses from an account selected by ID, without storing them.
|
|
|
|
func MultiAccountDeriveAddresses(paramsJSON string) string {
|
|
|
|
var p MultiAccountDeriveAddressesParams
|
|
|
|
|
|
|
|
if err := json.Unmarshal([]byte(paramsJSON), &p); err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := statusBackend.AccountManager().AccountsGenerator().DeriveAddresses(p.AccountID, p.Paths)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := json.Marshal(resp)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(out)
|
|
|
|
}
|
|
|
|
|
2019-07-26 09:33:38 +00:00
|
|
|
// MultiAccountStoreDerivedAccounts derive accounts from the specified key and store them encrypted with the specified password.
|
|
|
|
func MultiAccountStoreDerivedAccounts(paramsJSON string) string {
|
|
|
|
var p MultiAccountStoreDerivedAccountsParams
|
2019-07-24 18:59:15 +00:00
|
|
|
|
|
|
|
if err := json.Unmarshal([]byte(paramsJSON), &p); err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := statusBackend.AccountManager().AccountsGenerator().StoreDerivedAccounts(p.AccountID, p.Password, p.Paths)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := json.Marshal(resp)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MultiAccountImportPrivateKey imports a raw private key without storing it.
|
|
|
|
func MultiAccountImportPrivateKey(paramsJSON string) string {
|
|
|
|
var p MultiAccountImportPrivateKeyParams
|
|
|
|
|
|
|
|
if err := json.Unmarshal([]byte(paramsJSON), &p); err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := statusBackend.AccountManager().AccountsGenerator().ImportPrivateKey(p.PrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
2022-08-19 15:13:28 +00:00
|
|
|
out, err := json.Marshal(resp)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(out)
|
|
|
|
}
|
|
|
|
|
2022-10-27 18:29:57 +00:00
|
|
|
// CreateAccountFromMnemonicAndDeriveAccountsForPaths returns an account derived from the mnemonic phrase and the Bip39Passphrase
|
|
|
|
// and generate derived accounts for the list of paths without storing it
|
|
|
|
func CreateAccountFromMnemonicAndDeriveAccountsForPaths(paramsJSON string) string {
|
2022-08-19 15:13:28 +00:00
|
|
|
var p MultiAccountImportMnemonicParams
|
|
|
|
|
|
|
|
if err := json.Unmarshal([]byte(paramsJSON), &p); err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove any duplicate whitespaces
|
|
|
|
mnemonicPhraseNoExtraSpaces := strings.Join(strings.Fields(p.MnemonicPhrase), " ")
|
|
|
|
|
2022-10-27 18:29:57 +00:00
|
|
|
resp, err := statusBackend.AccountManager().AccountsGenerator().CreateAccountFromMnemonicAndDeriveAccountsForPaths(mnemonicPhraseNoExtraSpaces, p.Bip39Passphrase, p.Paths)
|
2022-08-19 15:13:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
2019-07-24 18:59:15 +00:00
|
|
|
out, err := json.Marshal(resp)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(out)
|
|
|
|
}
|
|
|
|
|
2019-07-26 09:33:38 +00:00
|
|
|
// MultiAccountImportMnemonic imports an account derived from the mnemonic phrase and the Bip39Passphrase storing it.
|
|
|
|
func MultiAccountImportMnemonic(paramsJSON string) string {
|
|
|
|
var p MultiAccountImportMnemonicParams
|
|
|
|
|
|
|
|
if err := json.Unmarshal([]byte(paramsJSON), &p); err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
2022-01-05 14:43:39 +00:00
|
|
|
// remove any duplicate whitespaces
|
|
|
|
mnemonicPhraseNoExtraSpaces := strings.Join(strings.Fields(p.MnemonicPhrase), " ")
|
|
|
|
|
|
|
|
resp, err := statusBackend.AccountManager().AccountsGenerator().ImportMnemonic(mnemonicPhraseNoExtraSpaces, p.Bip39Passphrase)
|
2019-07-26 09:33:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := json.Marshal(resp)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(out)
|
|
|
|
}
|
|
|
|
|
2019-07-24 18:59:15 +00:00
|
|
|
// MultiAccountStoreAccount stores the select account.
|
|
|
|
func MultiAccountStoreAccount(paramsJSON string) string {
|
|
|
|
var p MultiAccountStoreAccountParams
|
|
|
|
|
|
|
|
if err := json.Unmarshal([]byte(paramsJSON), &p); err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := statusBackend.AccountManager().AccountsGenerator().StoreAccount(p.AccountID, p.Password)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := json.Marshal(resp)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(out)
|
|
|
|
}
|
2019-07-26 09:33:38 +00:00
|
|
|
|
|
|
|
// MultiAccountLoadAccount loads in memory the account specified by address unlocking it with password.
|
|
|
|
func MultiAccountLoadAccount(paramsJSON string) string {
|
|
|
|
var p MultiAccountLoadAccountParams
|
|
|
|
|
|
|
|
if err := json.Unmarshal([]byte(paramsJSON), &p); err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := statusBackend.AccountManager().AccountsGenerator().LoadAccount(p.Address, p.Password)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := json.Marshal(resp)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MultiAccountReset remove all the multi-account keys from memory.
|
|
|
|
func MultiAccountReset() string {
|
|
|
|
statusBackend.AccountManager().AccountsGenerator().Reset()
|
|
|
|
return makeJSONResponse(nil)
|
|
|
|
}
|