-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
200 lines (171 loc) · 4.87 KB
/
main.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"errors"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/rpcclient"
"github.com/linkpoolio/bridges/bridge"
)
// Bitcoin is the main object
type Bitcoin struct{}
///////////////////////////////
// getblockcount
///////////////////////////////
// Parameters
// None
///////////////////////////////
// Description
// Returns the number of blocks in the longest block chain.
///////////////////////////////
func getBlockCount(client *rpcclient.Client) (int64, error) {
return client.GetBlockCount()
}
///////////////////////////////
// getdifficulty
///////////////////////////////
// Parameters
// None
///////////////////////////////
// Description
// Returns the proof-of-work difficulty as a multiple of the minimum difficulty.
///////////////////////////////
// getDifficulty returns the proof-of-work difficulty as a multiple of the minimum difficulty
func getDifficulty(client *rpcclient.Client) (float64, error) {
return client.GetDifficulty()
}
///////////////////////////////
// getrawtransaction
///////////////////////////////
// Parameters
// 1. transaction hash (string, required) - the hash of the transaction
///////////////////////////////
// Description
// Returns information about a transaction given its hash.
///////////////////////////////
func getRawTransaction(client *rpcclient.Client, txID *chainhash.Hash, verbose bool) (*btcjson.TxRawResult, error) {
result, err := client.GetRawTransactionVerbose(txID)
if err != nil {
return &btcjson.TxRawResult{}, err
}
return result, nil
}
///////////////////////////////
// getblock
///////////////////////////////
// Parameters
// 1. block_num (string, required) - the number of the block
///////////////////////////////
// Description
// Returns information about a transaction given its hash.
///////////////////////////////
func getBlock(client *rpcclient.Client, blockNum int64, verbose bool, verboseTx bool) (*btcjson.GetBlockVerboseResult, error) {
// First get block hash of the given block num
blockHash, err := client.GetBlockHash(blockNum)
if err != nil {
return &btcjson.GetBlockVerboseResult{}, err
}
// Get the block result JSON object
result, err := client.GetBlockVerboseTx(blockHash)
if err != nil {
return &btcjson.GetBlockVerboseResult{}, err
}
return result, nil
}
// Run is called on each HTTP request
func (btc *Bitcoin) Run(h *bridge.Helper) (interface{}, error) {
//Return object
obj := make(map[string]interface{})
// Load the certificate for the TLS connection which is automatically
// generated by btcd when it starts the RPC server and doesn't already
// have one.
certs, err := ioutil.ReadFile(os.Getenv("BTCD_RPC_CERT"))
if err != nil {
log.Fatal(err)
}
// Create a new RPC client using websockets. Since this example is
// not long-lived, the connection will be closed as soon as the program
// exits.
connCfg := &rpcclient.ConnConfig{
Host: os.Getenv("BTCD_RPC_HOST"),
Endpoint: "ws",
User: os.Getenv("BTCD_RPC_USER"),
Pass: os.Getenv("BTCD_RPC_PASS"),
Certificates: certs,
}
// Create a btcd client object
client, err := rpcclient.New(connCfg, nil)
if err != nil {
log.Println(err)
return nil, errors.New("Unable to connect to btcd instance")
}
defer client.Shutdown()
// Check rpc_command converting it to lowercase to make it case-insensitive
switch command := strings.ToLower(h.GetParam("rpc_command")); command {
case "getblockcount":
{
blockCount, err := getBlockCount(client)
if err != nil {
return nil, err
}
obj["block_count"] = blockCount
}
case "getdifficulty":
{
difficulty, err := getDifficulty(client)
if err != nil {
return nil, err
}
obj["difficulty"] = difficulty
}
case "getrawtransaction":
{
txHash, err := chainhash.NewHashFromStr(h.GetParam("tx_id"))
if err != nil {
return nil, errors.New("Invalid tx_id specified")
}
// Hard-coded verbose result
txResult, err := getRawTransaction(client, txHash, true)
if err != nil {
return nil, err
}
obj["tx"] = txResult
}
case "getblock":
{
// Hard-coded verbose result
blockResult, err := getBlock(client, h.GetIntParam("block_num"), true, true)
if err != nil {
return nil, err
}
obj["block"] = blockResult
}
default:
return nil, errors.New("Invalid or unsupported rpc_command specified")
}
return obj, nil
}
// Opts is called on startup
func (btc *Bitcoin) Opts() *bridge.Opts {
return &bridge.Opts{
Name: "Bitcoin",
Lambda: true,
}
}
// Handler is used for GCP compatibility
func Handler(w http.ResponseWriter, r *http.Request) {
bridge.NewServer(&Bitcoin{}).Handler(w, r)
}
func main() {
// Start the HTTP server
port, err := strconv.Atoi(os.Getenv("LISTEN_PORT"))
if err != nil {
port = 8080
}
bridge.NewServer(&Bitcoin{}).Start(port)
}