Beispiel #1
0
func payAccount(authtoken, paytoken []byte, serverKnown string, cacert []byte) (server string, lastcounter uint64, err error) {
	var ServerT string
	var ok bool
	method := "AccountServer.LoadAccount"
	url := DefaultAccountServer
	if server != "" {
		url = server
	}
	url = "https://" + url + ":" + RPCPort + "/account"
	client, err := DefaultClientFactory(url, cacert)
	if err != nil {
		return "", 0, err
	}
	authtokenEnc := base64.StdEncoding.EncodeToString(authtoken)
	paytokenEnc := base64.StdEncoding.EncodeToString(paytoken)
	data, err := client.JSONRPCRequest(method, struct {
		AuthToken string
		PayToken  string
	}{
		AuthToken: authtokenEnc,
		PayToken:  paytokenEnc,
	})
	if err != nil {
		LastCounter, err := walletauth.IsReplay(err)
		return "", LastCounter, err
	}
	if _, ok = data["Server"]; !ok {
		return "", 0, ErrProto
	}
	if ServerT, ok = data["Server"].(string); !ok {
		return "", 0, ErrProto
	}
	return ServerT, 0, nil
}
Beispiel #2
0
// delAccount delets an account
func delAccount(authtoken []byte, server string, cacert []byte) (bool, uint64, error) {
	var ok, ResultT bool
	method := "AccountServer.DeleteAccount"
	url := "https://" + server + ":" + RPCPort + "/account"
	client, err := DefaultClientFactory(url, cacert)
	if err != nil {
		return false, 0, err
	}
	authtokenEnc := base64.StdEncoding.EncodeToString(authtoken)
	data, err := client.JSONRPCRequest(method, struct{ AuthToken string }{AuthToken: authtokenEnc})
	if err != nil {
		LastCounter, err := walletauth.IsReplay(err)
		return false, LastCounter, err
	}
	if _, ok = data["Result"]; !ok {
		return false, 0, ErrProto
	}
	if ResultT, ok = data["Result"].(bool); !ok {
		return false, 0, ErrProto
	}
	return ResultT, 0, nil
}
Beispiel #3
0
func (wc WalletClient) getToken(usage string, authtoken []byte) (token, params, pubKeyUsed []byte, LastCounter uint64, err error) {
	method := "WalletServer.GetToken"
	client, err := wc.ClientFactory(ServiceURL, wc.ServiceGuardCA)
	if err != nil {
		return nil, nil, nil, 0, err
	}
	authtokenEnc := base64.StdEncoding.EncodeToString(authtoken)
	data, err := client.JSONRPCRequest(method, struct{ AuthToken, Usage string }{AuthToken: authtokenEnc, Usage: usage})
	if err != nil {
		LastCounter, err := walletauth.IsReplay(err)
		return nil, nil, nil, LastCounter, err
	}
	if _, ok := data["Token"]; !ok {
		return nil, nil, nil, 0, ErrParams
	}
	if _, ok := data["Token"].(string); !ok {
		return nil, nil, nil, 0, ErrParams
	}
	if _, ok := data["Params"]; !ok {
		return nil, nil, nil, 0, ErrParams
	}
	if _, ok := data["Params"].(string); !ok {
		return nil, nil, nil, 0, ErrParams
	}
	token, err = base64.StdEncoding.DecodeString(data["Token"].(string))
	if err != nil {
		return nil, nil, nil, 0, ErrParams
	}
	params, err = base64.StdEncoding.DecodeString(data["Params"].(string))
	if err != nil {
		return nil, nil, nil, 0, ErrParams
	}
	pubKeyUsed, err = base64.StdEncoding.DecodeString(data["PubKeyUsed"].(string))
	if err != nil {
		return nil, nil, nil, 0, ErrParams
	}

	return token, params, pubKeyUsed, 0, nil
}
Beispiel #4
0
// getBalance is the real GetBalance call to return the client's balance from the wallet service
func (wc WalletClient) getBalance(authtoken []byte) (SubscriptionTokens, PrepayTokens, LastSubscribeLoad, LastCounter uint64, err error) {
	var ok bool
	var SubscriptionTokensT, PrepayTokensT, LastSubscribeLoadT float64
	method := "WalletServer.GetBalance"
	client, err := wc.ClientFactory(ServiceURL, wc.ServiceGuardCA)
	if err != nil {
		return 0, 0, 0, 0, err
	}
	authtokenEnc := base64.StdEncoding.EncodeToString(authtoken)
	data, err := client.JSONRPCRequest(method, struct{ AuthToken string }{AuthToken: authtokenEnc})
	if err != nil {
		LastCounter, err := walletauth.IsReplay(err)
		return 0, 0, 0, LastCounter, err
	}
	if _, ok := data["SubscriptionTokens"]; !ok {
		return 0, 0, 0, 0, ErrParams
	}

	if SubscriptionTokensT, ok = data["SubscriptionTokens"].(float64); !ok {
		return 0, 0, 0, 0, ErrParams
	}
	SubscriptionTokens = uint64(SubscriptionTokensT)
	if _, ok := data["PrepayTokens"]; !ok {
		return 0, 0, 0, 0, ErrParams
	}
	if PrepayTokensT, ok = data["PrepayTokens"].(float64); !ok {
		return 0, 0, 0, 0, ErrParams
	}
	PrepayTokens = uint64(PrepayTokensT)
	if _, ok := data["LastSubscribeLoad"]; !ok {
		return 0, 0, 0, 0, ErrParams
	}
	if LastSubscribeLoadT, ok = data["LastSubscribeLoad"].(float64); !ok {
		return 0, 0, 0, 0, ErrParams
	}
	LastSubscribeLoad = uint64(LastSubscribeLoadT)
	return SubscriptionTokens, PrepayTokens, LastSubscribeLoad, 0, nil
}
Beispiel #5
0
func fetchmessage(messageID, authtoken []byte, server string, cacert []byte) (message []byte, lastcounter uint64, err error) {
	postVal := url.Values{}
	postVal.Set("messageid", hex.EncodeToString(messageID))
	postVal.Set("authtoken", base64.StdEncoding.EncodeToString(authtoken))
	body, err := HTTPSPost(postVal, "https://"+server+":"+RPCPort+"/message", cacert)
	if err != nil {
		return nil, 0, err
	}
	if len(body) < 7 {
		return nil, 0, ErrProto
	}
	if string(body[0:6]) == "ERROR:" {
		errStr := body[7:]
		err := fmt.Errorf("%s", errStr)
		LastCounter, err := walletauth.IsReplay(err)
		return nil, LastCounter, err
	}
	message, err = ReadMail(body)
	if err != nil {
		return nil, 0, err
	}
	return message, 0, nil
}
Beispiel #6
0
// accountStat real call
func accountStat(authtoken []byte, server string, cacert []byte) (int64, uint64, error) {
	var ok bool
	var LoadTimeT float64
	method := "AccountServer.AccountStat"
	url := "https://" + server + ":" + RPCPort + "/account"
	client, err := DefaultClientFactory(url, cacert)
	if err != nil {
		return 0, 0, err
	}
	authtokenEnc := base64.StdEncoding.EncodeToString(authtoken)
	data, err := client.JSONRPCRequest(method, struct{ AuthToken string }{AuthToken: authtokenEnc})
	if err != nil {
		LastCounter, err := walletauth.IsReplay(err)
		return 0, LastCounter, err
	}
	if _, ok = data["LoadTime"]; !ok {
		return 0, 0, ErrProto
	}
	if LoadTimeT, ok = data["LoadTime"].(float64); !ok {
		return 0, 0, ErrProto
	}
	return int64(LoadTimeT), 0, nil
}
Beispiel #7
0
// listMessages real call
func listMessages(authtoken []byte, lastMessageTime int64, server string, cacert []byte) ([]MessageMeta, uint64, error) {
	var ok bool
	var messages []MessageMeta
	method := "AccountServer.ListMessages"
	url := "https://" + server + ":" + RPCPort + "/account"
	client, err := DefaultClientFactory(url, cacert)
	if err != nil {
		return nil, 0, err
	}
	authtokenEnc := base64.StdEncoding.EncodeToString(authtoken)
	data, err := client.JSONRPCRequest(method, struct {
		AuthToken       string
		LastReceiveTime int64
	}{
		AuthToken:       authtokenEnc,
		LastReceiveTime: lastMessageTime,
	})
	if err != nil {
		LastCounter, err := walletauth.IsReplay(err)
		return nil, LastCounter, err
	}
	if _, ok = data["Messages"]; !ok {
		return nil, 0, ErrProto
	}
	messagesI, ok := data["Messages"].([]interface{})
	if !ok {
		return nil, 0, ErrProto
	}
	if len(messagesI) > 0 {
		for _, e := range messagesI {
			x := e.(map[string]interface{})

			ReceiveTime, ok := x["ReceiveTime"].(float64)
			if !ok {
				err = ErrProto
				break
			}
			ReceiveTimeNano, ok := x["ReceiveTimeNano"].(float64)
			if !ok {
				err = ErrProto
				break
			}
			ReadTime, ok := x["ReadTime"].(float64)
			if !ok {
				err = ErrProto
				break
			}
			uks, ok := x["UserKey"].(string)
			if !ok {
				err = ErrProto
				break
			}
			uk, err := hex.DecodeString(uks)
			if err != nil {
				break
			}
			ukh := new([ed25519.PublicKeySize]byte)
			copy(ukh[:], uk)

			mis, ok := x["MessageID"].(string)
			if !ok {
				err = ErrProto
				break
			}
			mi, err := hex.DecodeString(mis)
			if err != nil {
				break
			}
			messages = append(messages, MessageMeta{
				MessageID:       mi,
				ReceiveTime:     int64(ReceiveTime),
				ReceiveTimeNano: int64(ReceiveTimeNano),
				ReadTime:        int64(ReadTime),
				UserKey:         *ukh,
			})
		}
		if err != nil {
			return nil, 0, err
		}
	}
	return messages, 0, nil
}