-
Notifications
You must be signed in to change notification settings - Fork 246
/
api.go
106 lines (87 loc) · 2.95 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
}