2018-04-30 15:57:11 +00:00
|
|
|
package sdk
|
|
|
|
|
|
|
|
import (
|
2018-05-01 07:46:34 +00:00
|
|
|
"encoding/json"
|
|
|
|
"log"
|
2018-04-30 15:57:11 +00:00
|
|
|
)
|
|
|
|
|
2018-05-04 14:12:36 +00:00
|
|
|
// RPCClient is a client to manage all rpc calls
|
|
|
|
type RPCClient interface {
|
|
|
|
Call(request interface{}) (response interface{}, err error)
|
|
|
|
}
|
|
|
|
|
2018-04-30 17:07:36 +00:00
|
|
|
// SDK is a set of tools to interact with status node
|
|
|
|
type SDK struct {
|
|
|
|
RPCClient RPCClient
|
2018-05-07 10:03:45 +00:00
|
|
|
accounts []*Account
|
2018-05-01 07:46:34 +00:00
|
|
|
minimumPoW float64
|
2018-04-30 15:57:11 +00:00
|
|
|
}
|
|
|
|
|
2018-04-30 17:07:36 +00:00
|
|
|
// New creates a default SDK object
|
2018-05-04 13:49:23 +00:00
|
|
|
func New(c RPCClient) *SDK {
|
2018-04-30 17:07:36 +00:00
|
|
|
return &SDK{
|
2018-05-04 13:49:23 +00:00
|
|
|
RPCClient: c,
|
2018-05-01 07:46:34 +00:00
|
|
|
minimumPoW: 0.001,
|
2018-04-30 15:57:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-30 17:07:36 +00:00
|
|
|
// Login to status with the given credentials
|
2018-05-07 10:03:45 +00:00
|
|
|
func (c *SDK) Login(addr, pwd string) (a *Account, err error) {
|
2018-05-03 13:24:38 +00:00
|
|
|
res, err := statusLoginRequest(c, addr, pwd)
|
|
|
|
if err != nil {
|
2018-05-07 10:03:45 +00:00
|
|
|
return a, err
|
2018-04-30 15:57:11 +00:00
|
|
|
}
|
2018-05-07 10:03:45 +00:00
|
|
|
return &Account{
|
|
|
|
Address: res.Result.AddressKeyID,
|
|
|
|
}, err
|
2018-04-30 15:57:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Signup creates a new account with the given credentials
|
2018-05-07 10:03:45 +00:00
|
|
|
func (c *SDK) Signup(pwd string) (a *Account, err error) {
|
2018-05-03 13:24:38 +00:00
|
|
|
res, err := statusSignupRequest(c, pwd)
|
|
|
|
if err != nil {
|
2018-05-07 10:03:45 +00:00
|
|
|
return a, err
|
2018-04-30 15:57:11 +00:00
|
|
|
}
|
2018-05-07 10:03:45 +00:00
|
|
|
return &Account{
|
|
|
|
Address: res.Result.Address,
|
|
|
|
PubKey: res.Result.Pubkey,
|
|
|
|
Mnemonic: res.Result.Mnemonic,
|
|
|
|
}, err
|
2018-04-30 15:57:11 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-01 07:46:34 +00:00
|
|
|
// SignupAndLogin sign up and login on status network
|
2018-05-07 10:03:45 +00:00
|
|
|
func (c *SDK) SignupAndLogin(password string) (a *Account, err error) {
|
|
|
|
a, err = c.Signup(password)
|
2018-05-01 07:46:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
2018-04-30 15:57:11 +00:00
|
|
|
}
|
2018-05-07 10:03:45 +00:00
|
|
|
la, err := c.Login(a.Address, password)
|
|
|
|
a.Address = la.Address
|
2018-05-01 07:46:34 +00:00
|
|
|
return
|
2018-04-30 15:57:11 +00:00
|
|
|
}
|
|
|
|
|
2018-05-01 07:46:34 +00:00
|
|
|
// NewMessageFilterResponse NewMessageFilter json response
|
|
|
|
type NewMessageFilterResponse struct {
|
|
|
|
Result string `json:"result"`
|
|
|
|
}
|
2018-04-30 15:57:11 +00:00
|
|
|
|
2018-05-01 07:46:34 +00:00
|
|
|
func (c *SDK) call(cmd string, res interface{}) error {
|
2018-05-03 13:24:38 +00:00
|
|
|
log.Println("[ REQUEST ] : " + cmd)
|
2018-05-01 07:46:34 +00:00
|
|
|
body, err := c.RPCClient.Call(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Println("[ RESPONSE ] : " + body.(string))
|
|
|
|
|
|
|
|
return json.Unmarshal([]byte(body.(string)), &res)
|
2018-04-30 17:07:36 +00:00
|
|
|
}
|