2018-05-03 10:36:56 +00:00
|
|
|
package status
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2018-12-28 11:37:22 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/account"
|
2019-12-11 13:59:37 +00:00
|
|
|
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2018-05-03 10:36:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PublicAPI represents a set of APIs from the `web3.status` namespace.
|
|
|
|
type PublicAPI struct {
|
|
|
|
s *Service
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAPI creates an instance of the status API.
|
|
|
|
func NewAPI(s *Service) *PublicAPI {
|
|
|
|
return &PublicAPI{s: s}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoginRequest : json request for status_login.
|
|
|
|
type LoginRequest struct {
|
|
|
|
Addr string `json:"address"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoginResponse : json response returned by status_login.
|
|
|
|
type LoginResponse struct {
|
|
|
|
AddressKeyID string `json:"address_key_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Login is an implementation of `status_login` or `web3.status.login` API
|
|
|
|
func (api *PublicAPI) Login(context context.Context, req LoginRequest) (res LoginResponse, err error) {
|
|
|
|
_, accountKey, err := api.s.am.AddressToDecryptedAccount(req.Addr, req.Password)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.AddressKeyID, err = api.s.w.AddKeyPair(accountKey.PrivateKey); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-26 14:45:10 +00:00
|
|
|
loginParams := account.LoginParams{
|
2019-12-11 13:59:37 +00:00
|
|
|
ChatAddress: types.HexToAddress(req.Addr),
|
2019-07-26 14:45:10 +00:00
|
|
|
Password: req.Password,
|
2019-12-11 13:59:37 +00:00
|
|
|
MainAccount: types.HexToAddress(req.Addr),
|
2019-07-26 14:45:10 +00:00
|
|
|
}
|
|
|
|
if err = api.s.am.SelectAccount(loginParams); err != nil {
|
2018-05-03 10:36:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignupRequest : json request for status_signup.
|
|
|
|
type SignupRequest struct {
|
|
|
|
Password string `json:"password"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignupResponse : json response returned by status_signup.
|
|
|
|
type SignupResponse struct {
|
2019-01-18 09:01:14 +00:00
|
|
|
Address string `json:"address"`
|
|
|
|
Pubkey string `json:"pubkey"`
|
|
|
|
WalletAddress string `json:"walletAddress"`
|
|
|
|
WalletPubkey string `json:"walletPubKey"`
|
|
|
|
ChatAddress string `json:"chatAddress"`
|
|
|
|
ChatPubkey string `json:"chatPubkey"`
|
|
|
|
Mnemonic string `json:"mnemonic"`
|
2018-05-03 10:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Signup is an implementation of `status_signup` or `web3.status.signup` API
|
|
|
|
func (api *PublicAPI) Signup(context context.Context, req SignupRequest) (res SignupResponse, err error) {
|
2020-12-09 14:36:40 +00:00
|
|
|
_, accountInfo, mnemonic, err := api.s.am.CreateAccount(req.Password)
|
2019-01-18 09:01:14 +00:00
|
|
|
if err != nil {
|
2018-05-03 10:36:56 +00:00
|
|
|
err = errors.New("could not create the specified account : " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-18 09:01:14 +00:00
|
|
|
res.Address = accountInfo.WalletAddress
|
|
|
|
res.Pubkey = accountInfo.WalletPubKey
|
|
|
|
res.WalletAddress = accountInfo.WalletAddress
|
|
|
|
res.WalletPubkey = accountInfo.WalletPubKey
|
|
|
|
res.ChatAddress = accountInfo.ChatAddress
|
|
|
|
res.ChatPubkey = accountInfo.ChatPubKey
|
|
|
|
res.Mnemonic = mnemonic
|
|
|
|
|
2018-05-03 10:36:56 +00:00
|
|
|
return
|
|
|
|
}
|
2018-12-28 11:37:22 +00:00
|
|
|
|
|
|
|
// CreateAddressResponse : json response returned by status_createaccount
|
|
|
|
type CreateAddressResponse struct {
|
|
|
|
Address string `json:"address"`
|
|
|
|
Pubkey string `json:"pubkey"`
|
|
|
|
Privkey string `json:"privkey"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateAddress is an implementation of `status_createaccount` or `web3.status.createaccount` API
|
|
|
|
func (api *PublicAPI) CreateAddress(context context.Context) (res CreateAddressResponse, err error) {
|
|
|
|
if res.Address, res.Pubkey, res.Privkey, err = account.CreateAddress(); err != nil {
|
|
|
|
err = fmt.Errorf("could not create an address: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|