status-go/services/status/api.go

107 lines
2.9 KiB
Go

package status
import (
"context"
"errors"
"fmt"
"github.com/status-im/status-go/account"
"github.com/status-im/status-go/eth-node/types"
)
// 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
}
loginParams := account.LoginParams{
ChatAddress: types.HexToAddress(req.Addr),
Password: req.Password,
MainAccount: types.HexToAddress(req.Addr),
}
if err = api.s.am.SelectAccount(loginParams); err != nil {
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 {
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"`
}
// 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) {
_, accountInfo, mnemonic, err := api.s.am.CreateAccount(req.Password)
if err != nil {
err = errors.New("could not create the specified account : " + err.Error())
return
}
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
return
}
// 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
}