status-go-sdk/sdk.go

137 lines
3.1 KiB
Go
Raw Normal View History

2018-04-30 15:57:11 +00:00
package sdk
import (
"encoding/hex"
"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)
}
// SDK is a set of tools to interact with status node
type SDK struct {
RPCClient RPCClient
2018-04-30 15:57:11 +00:00
address string
pubkey string
mnemonic string
2018-04-30 15:57:11 +00:00
userName string
channels []*Channel
minimumPoW float64
2018-04-30 15:57:11 +00:00
}
// New creates a default SDK object
func New(c RPCClient) *SDK {
return &SDK{
RPCClient: c,
minimumPoW: 0.001,
2018-04-30 15:57:11 +00:00
}
}
// Close all channels you're subscribed to
func (c *SDK) Close() {
2018-04-30 15:57:11 +00:00
for _, channel := range c.channels {
channel.Close()
}
}
// Login to status with the given credentials
func (c *SDK) Login(addr, pwd string) error {
res, err := statusLoginRequest(c, addr, pwd)
if err != nil {
2018-04-30 15:57:11 +00:00
return err
}
c.address = res.Result.AddressKeyID
2018-04-30 15:57:11 +00:00
return nil
}
// Signup creates a new account with the given credentials
func (c *SDK) Signup(pwd string) (addr string, pubkey string, mnemonic string, err error) {
res, err := statusSignupRequest(c, pwd)
if err != nil {
return "", "", "", err
2018-04-30 15:57:11 +00:00
}
c.address = res.Result.Address
c.pubkey = res.Result.Pubkey
c.mnemonic = res.Result.Mnemonic
2018-04-30 15:57:11 +00:00
return res.Result.Address, res.Result.Pubkey, res.Result.Mnemonic, err
2018-04-30 15:57:11 +00:00
}
// SignupAndLogin sign up and login on status network
func (c *SDK) SignupAndLogin(password string) (addr string, pubkey string, mnemonic string, err error) {
addr, pubkey, mnemonic, err = c.Signup(password)
if err != nil {
return
2018-04-30 15:57:11 +00:00
}
err = c.Login(addr, password)
return
2018-04-30 15:57:11 +00:00
}
// NewMessageFilterResponse NewMessageFilter json response
type NewMessageFilterResponse struct {
Result string `json:"result"`
}
2018-04-30 15:57:11 +00:00
// JoinPublicChannel joins a status public channel
func (c *SDK) JoinPublicChannel(channelName string) (*Channel, error) {
symkeyResponse, err := shhGenerateSymKeyFromPasswordRequest(c, []string{channelName})
if err != nil {
return nil, err
}
symKey := symkeyResponse.Key
topicID, err := c.calculatePublicChannelTopicID(channelName, symkeyResponse.ID)
if err != nil {
return nil, err
}
2018-04-30 15:57:11 +00:00
return c.Join(channelName, topicID, symKey)
}
// Join joins a status channel
func (c *SDK) Join(channelName, topicID, symKey string) (*Channel, error) {
newMessageFilterResponse, err := newShhMessageFilterFormatRequest(c, []string{topicID}, symKey)
if err != nil {
return nil, err
}
filterID := newMessageFilterResponse.FilterID
2018-04-30 15:57:11 +00:00
ch := &Channel{
conn: c,
name: channelName,
filterID: filterID,
topicID: topicID,
channelKey: symKey,
}
c.channels = append(c.channels, ch)
return ch, nil
2018-04-30 15:57:11 +00:00
}
func (c *SDK) calculatePublicChannelTopicID(name string, symkey int) (topicID string, err error) {
p := "0x" + hex.EncodeToString([]byte(name))
web3ShaResponse, err := web3Sha3Request(c, symkey, []string{p})
if err != nil {
return
}
topicID = web3ShaResponse.Result[0:10]
return
}
func (c *SDK) call(cmd string, res interface{}) error {
log.Println("[ REQUEST ] : " + cmd)
body, err := c.RPCClient.Call(cmd)
if err != nil {
return err
}
log.Println("[ RESPONSE ] : " + body.(string))
return json.Unmarshal([]byte(body.(string)), &res)
}