github.com/status-im/status-go@v1.1.0/mobile/multiaccount.go (about) 1 package statusgo 2 3 import ( 4 "encoding/json" 5 "strings" 6 ) 7 8 // MultiAccountGenerateParams are the params sent to MultiAccountGenerate. 9 type MultiAccountGenerateParams struct { 10 N int `json:"n"` 11 MnemonicPhraseLength int `json:"mnemonicPhraseLength"` 12 Bip39Passphrase string `json:"bip39Passphrase"` 13 } 14 15 // MultiAccountGenerateAndDeriveAddressesParams are the params sent to MultiAccountGenerateAndDeriveAddresses. 16 type MultiAccountGenerateAndDeriveAddressesParams struct { 17 MultiAccountGenerateParams 18 Paths []string `json:"paths"` 19 } 20 21 // MultiAccountDeriveAddressesParams are the params sent to MultiAccountDeriveAddresses. 22 type MultiAccountDeriveAddressesParams struct { 23 AccountID string `json:"accountID"` 24 Paths []string `json:"paths"` 25 } 26 27 // MultiAccountStoreDerivedAccountsParams are the params sent to MultiAccountStoreDerivedAccounts. 28 type MultiAccountStoreDerivedAccountsParams struct { 29 MultiAccountDeriveAddressesParams 30 Password string `json:"password"` 31 } 32 33 // MultiAccountStoreAccountParams are the params sent to MultiAccountStoreAccount. 34 type MultiAccountStoreAccountParams struct { 35 AccountID string `json:"accountID"` 36 Password string `json:"password"` 37 } 38 39 // MultiAccountImportPrivateKeyParams are the params sent to MultiAccountImportPrivateKey. 40 type MultiAccountImportPrivateKeyParams struct { 41 PrivateKey string `json:"privateKey"` 42 } 43 44 // MultiAccountLoadAccountParams are the params sent to MultiAccountLoadAccount. 45 type MultiAccountLoadAccountParams struct { 46 Address string `json:"address"` 47 Password string `json:"password"` 48 } 49 50 // MultiAccountImportMnemonicParams are the params sent to MultiAccountImportMnemonic. 51 type MultiAccountImportMnemonicParams struct { 52 MnemonicPhrase string `json:"mnemonicPhrase"` 53 Bip39Passphrase string `json:"Bip39Passphrase"` 54 Paths []string `json:"paths"` 55 } 56 57 // MultiAccountGenerate generates account in memory without storing them. 58 func MultiAccountGenerate(paramsJSON string) string { 59 var p MultiAccountGenerateParams 60 61 if err := json.Unmarshal([]byte(paramsJSON), &p); err != nil { 62 return makeJSONResponse(err) 63 } 64 65 resp, err := statusBackend.AccountManager().AccountsGenerator().Generate(p.MnemonicPhraseLength, p.N, p.Bip39Passphrase) 66 if err != nil { 67 return makeJSONResponse(err) 68 } 69 70 out, err := json.Marshal(resp) 71 if err != nil { 72 return makeJSONResponse(err) 73 } 74 75 return string(out) 76 } 77 78 // MultiAccountGenerateAndDeriveAddresses combines Generate and DeriveAddresses in one call. 79 func MultiAccountGenerateAndDeriveAddresses(paramsJSON string) string { 80 var p MultiAccountGenerateAndDeriveAddressesParams 81 82 if err := json.Unmarshal([]byte(paramsJSON), &p); err != nil { 83 return makeJSONResponse(err) 84 } 85 86 resp, err := statusBackend.AccountManager().AccountsGenerator().GenerateAndDeriveAddresses(p.MnemonicPhraseLength, p.N, p.Bip39Passphrase, p.Paths) 87 if err != nil { 88 return makeJSONResponse(err) 89 } 90 91 out, err := json.Marshal(resp) 92 if err != nil { 93 return makeJSONResponse(err) 94 } 95 96 return string(out) 97 } 98 99 // MultiAccountDeriveAddresses derive addresses from an account selected by ID, without storing them. 100 func MultiAccountDeriveAddresses(paramsJSON string) string { 101 var p MultiAccountDeriveAddressesParams 102 103 if err := json.Unmarshal([]byte(paramsJSON), &p); err != nil { 104 return makeJSONResponse(err) 105 } 106 107 resp, err := statusBackend.AccountManager().AccountsGenerator().DeriveAddresses(p.AccountID, p.Paths) 108 if err != nil { 109 return makeJSONResponse(err) 110 } 111 112 out, err := json.Marshal(resp) 113 if err != nil { 114 return makeJSONResponse(err) 115 } 116 117 return string(out) 118 } 119 120 // MultiAccountStoreDerivedAccounts derive accounts from the specified key and store them encrypted with the specified password. 121 func MultiAccountStoreDerivedAccounts(paramsJSON string) string { 122 var p MultiAccountStoreDerivedAccountsParams 123 124 if err := json.Unmarshal([]byte(paramsJSON), &p); err != nil { 125 return makeJSONResponse(err) 126 } 127 128 resp, err := statusBackend.AccountManager().AccountsGenerator().StoreDerivedAccounts(p.AccountID, p.Password, p.Paths) 129 if err != nil { 130 return makeJSONResponse(err) 131 } 132 133 out, err := json.Marshal(resp) 134 if err != nil { 135 return makeJSONResponse(err) 136 } 137 138 return string(out) 139 } 140 141 // CreateAccountFromPrivateKey returns an account derived from the private key without storing it 142 func CreateAccountFromPrivateKey(paramsJSON string) string { 143 var p MultiAccountImportPrivateKeyParams 144 145 if err := json.Unmarshal([]byte(paramsJSON), &p); err != nil { 146 return makeJSONResponse(err) 147 } 148 149 resp, err := statusBackend.AccountManager().AccountsGenerator().CreateAccountFromPrivateKey(p.PrivateKey) 150 if err != nil { 151 return makeJSONResponse(err) 152 } 153 154 out, err := json.Marshal(resp) 155 if err != nil { 156 return makeJSONResponse(err) 157 } 158 159 return string(out) 160 } 161 162 // MultiAccountImportPrivateKey imports a raw private key without storing it. 163 func MultiAccountImportPrivateKey(paramsJSON string) string { 164 var p MultiAccountImportPrivateKeyParams 165 166 if err := json.Unmarshal([]byte(paramsJSON), &p); err != nil { 167 return makeJSONResponse(err) 168 } 169 170 resp, err := statusBackend.AccountManager().AccountsGenerator().ImportPrivateKey(p.PrivateKey) 171 if err != nil { 172 return makeJSONResponse(err) 173 } 174 175 out, err := json.Marshal(resp) 176 if err != nil { 177 return makeJSONResponse(err) 178 } 179 180 return string(out) 181 } 182 183 // CreateAccountFromMnemonicAndDeriveAccountsForPaths returns an account derived from the mnemonic phrase and the Bip39Passphrase 184 // and generate derived accounts for the list of paths without storing it 185 func CreateAccountFromMnemonicAndDeriveAccountsForPaths(paramsJSON string) string { 186 var p MultiAccountImportMnemonicParams 187 188 if err := json.Unmarshal([]byte(paramsJSON), &p); err != nil { 189 return makeJSONResponse(err) 190 } 191 192 // remove any duplicate whitespaces 193 mnemonicPhraseNoExtraSpaces := strings.Join(strings.Fields(p.MnemonicPhrase), " ") 194 195 resp, err := statusBackend.AccountManager().AccountsGenerator().CreateAccountFromMnemonicAndDeriveAccountsForPaths(mnemonicPhraseNoExtraSpaces, p.Bip39Passphrase, p.Paths) 196 if err != nil { 197 return makeJSONResponse(err) 198 } 199 200 out, err := json.Marshal(resp) 201 if err != nil { 202 return makeJSONResponse(err) 203 } 204 205 return string(out) 206 } 207 208 // MultiAccountImportMnemonic imports an account derived from the mnemonic phrase and the Bip39Passphrase storing it. 209 func MultiAccountImportMnemonic(paramsJSON string) string { 210 var p MultiAccountImportMnemonicParams 211 212 if err := json.Unmarshal([]byte(paramsJSON), &p); err != nil { 213 return makeJSONResponse(err) 214 } 215 216 // remove any duplicate whitespaces 217 mnemonicPhraseNoExtraSpaces := strings.Join(strings.Fields(p.MnemonicPhrase), " ") 218 219 resp, err := statusBackend.AccountManager().AccountsGenerator().ImportMnemonic(mnemonicPhraseNoExtraSpaces, p.Bip39Passphrase) 220 if err != nil { 221 return makeJSONResponse(err) 222 } 223 224 out, err := json.Marshal(resp) 225 if err != nil { 226 return makeJSONResponse(err) 227 } 228 229 return string(out) 230 } 231 232 // MultiAccountStoreAccount stores the select account. 233 func MultiAccountStoreAccount(paramsJSON string) string { 234 var p MultiAccountStoreAccountParams 235 236 if err := json.Unmarshal([]byte(paramsJSON), &p); err != nil { 237 return makeJSONResponse(err) 238 } 239 240 resp, err := statusBackend.AccountManager().AccountsGenerator().StoreAccount(p.AccountID, p.Password) 241 if err != nil { 242 return makeJSONResponse(err) 243 } 244 245 out, err := json.Marshal(resp) 246 if err != nil { 247 return makeJSONResponse(err) 248 } 249 250 return string(out) 251 } 252 253 // MultiAccountLoadAccount loads in memory the account specified by address unlocking it with password. 254 func MultiAccountLoadAccount(paramsJSON string) string { 255 var p MultiAccountLoadAccountParams 256 257 if err := json.Unmarshal([]byte(paramsJSON), &p); err != nil { 258 return makeJSONResponse(err) 259 } 260 261 resp, err := statusBackend.AccountManager().AccountsGenerator().LoadAccount(p.Address, p.Password) 262 if err != nil { 263 return makeJSONResponse(err) 264 } 265 266 out, err := json.Marshal(resp) 267 if err != nil { 268 return makeJSONResponse(err) 269 } 270 271 return string(out) 272 } 273 274 // MultiAccountReset remove all the multi-account keys from memory. 275 func MultiAccountReset() string { 276 statusBackend.AccountManager().AccountsGenerator().Reset() 277 return makeJSONResponse(nil) 278 }