forked from metacoin/foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfoundation.go
106 lines (93 loc) · 3.04 KB
/
foundation.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 foundation
import (
"errors"
"os"
"github.com/metacoin/flojson"
)
func RPCCall(command string, args ...interface{}) (interface{}, error) {
// create a new command based on the command string given
// handle errors, and return results to main function
id := 1
var cmd flojson.Cmd
var err error
var ret interface{}
switch command {
case "getblock":
cmd, err = flojson.NewGetBlockCmd(id, args[0].(string))
case "getblockhash":
cmd, err = flojson.NewGetBlockHashCmd(id, args[0].(int))
case "getblockcount":
cmd, err = flojson.NewGetBlockCountCmd(id)
case "getrawtransaction":
cmd, err = flojson.NewGetRawTransactionCmd(id, args[0].(string))
case "getdecoderawtransaction":
cmd, err = flojson.NewGetRawTransactionCmd(id, args[0].(string), 1)
case "decoderawtransaction":
cmd, err = flojson.NewDecodeRawTransactionCmd(id, args[0].(string))
case "getnewaddress":
cmd, err = flojson.NewGetNewAddressCmd(id)
case "getmininginfo":
cmd, err = flojson.NewGetMiningInfoCmd(id)
case "getconnectioncount":
cmd, err = flojson.NewGetConnectionCountCmd(id)
case "getnetworkhashps":
cmd, err = flojson.NewGetNetworkHashPSCmd(id)
case "settxfee":
cmd, err = flojson.NewSetTxFeeCmd(id, args[0].(int64))
case "sendtoaddress":
cmd, err = flojson.NewSendToAddressCmd(id, args[0].(string), args[1].(int64), args[2], args[3], args[4])
case "walletpassphrase":
cmd, err = flojson.NewWalletPassphraseCmd(id, args[0].(string), args[1].(int))
case "signmessage":
cmd, err = flojson.NewSignMessageCmd(id, args[0].(string), args[1].(string))
case "verifymessage":
cmd, err = flojson.NewVerifyMessageCmd(id, args[0].(string), args[1].(string), args[2].(string))
case "validateaddress":
cmd, err = flojson.NewValidateAddressCmd(id, args[0].(string))
case "listtransactions":
cmd, err = flojson.NewListTransactionsCmd(id)
case "getbalance":
cmd, err = flojson.NewGetBalanceCmd(id)
default:
err = errors.New("command " + command + " not found")
}
if err != nil {
return ret, err
}
if cmd != nil {
reply, err := SendCommand(cmd)
if err != nil {
return ret, err
} else {
return reply, nil
}
}
return ret, errors.New("flojson replied with empty NewCmd struct")
}
func SendCommand(cmd flojson.Cmd) (interface{}, error) {
var ret interface{}
ftoken := os.Getenv("F_TOKEN")
if ftoken == "" {
return ret, errors.New("F_TOKEN environment variable not set")
}
fuser := os.Getenv("F_USER")
if ftoken == "" {
return ret, errors.New("F_USER environment variable not set")
}
furi := os.Getenv("F_URI")
if furi == "" || len(furi) < 1 {
//return ret, errors.New("F_URI environment variable not set")
furi = "127.0.0.1:18322"
}
// send command to RPC, get a response in reply
reply, err := flojson.RpcSend(fuser, ftoken, furi, cmd)
if err != nil {
return ret, err
}
if reply.Result != nil {
return reply.Result, nil
} else {
// if for some reason the result is nil, but err isn't nil, return a generic warning message
return ret, errors.New("didn't get a response from the RPC - check auth and permission settings")
}
}