/
Client.pm6
182 lines (177 loc) · 5.76 KB
/
Client.pm6
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
unit module Bitcoin::RPC;
our $VERSION = '0.0.2';
use v6;
use WWW;
use JSON::Fast;
use URI::Escape;
use LWP::Simple;
use Base64::Native;
use Bitcoin::Helpers::Config;
# client for querying the public Bitcoin REST API
# https://bitcoin.org/en/developer-reference
class Client is export {
has Str $!url;
has Str $!port;
has Str $!proto;
has Str $!user;
has Str $!password;
has Str $!auth_header;
has Str $!api;
has Bool $.debug is rw = False;
has UInt $!call_counter = 0;
has Config $!config;
submethod BUILD(Str :$url, Bool :$secure) {
$!config = Config.new;
$!url = $url;
$!user = $!config.get-value-for('rpcuser');
$!password = $!config.get-value-for('rpcpassword');
$!port = $!config.get-value-for('rpcport');
$!proto = $secure == True ?? 'https' !! 'http';
$!auth_header = base64-encode($!user ~ ':' ~ $!password, :str);
$!api = $!proto ~ '://' ~ $!url ~ ':' ~ $!port ~ '/';
}
# execute API command
method execute(Str $api, *@params) returns Str {
my Str $call = self!get-command($api);
given $call ne '' {
return LWP::Simple.new.post($!api, self!get-request-headers(),
to-json(self!get-request-parameters($call, @params)));
}
}
method !get-request-parameters(Str $method, *@method_params) {
my %params =
'version' => '1.1',
'method' => $method,
'params' => @method_params,
'id' => $!call_counter++
;
return %params;
}
# componses HTTP call headers
method !get-request-headers() {
my %headers =
'Host' => 'localhost',
'User-Agent' => 'Perl6Client/0.1',
'Authorization' => 'Basic ' ~ $!auth_header,
'Content-Type' => 'application/json'
;
return %headers;
}
# returns API command
method !get-command($name) returns Str {
my %commands = <addmultisigaddress
addmultisigaddress
backupwallet
backupwallet
createrawtransaction
createrawtransaction
decoderawtransaction
decoderawtransaction
dumpprivkey
dumpprivkey
encryptwallet
encryptwallet
getaccount
getaccount
getaccountaddress
getaccountaddress
getaddressesbyaccount
getaddressesbyaccount
getbalance
getbalance
getblock
getblock
getblockbynumber
getblockbynumber
getblockcount
getblockcount
getblockhash
getblockhash
getconnectioncount
getconnectioncount
getdifficulty
getdifficulty
getgenerate
getgenerate
gethashespersec
gethashespersec
getinfo
getinfo
getmemorypool
getmemorypool
getmininginfo
getmininginfo
getnewaddress
getnewaddress
getrawtransaction
getrawtransaction
getreceivedbyaccount
getreceivedbyaccount
getreceivedbyaddress
getreceivedbyaddress
gettransaction
gettransaction
gettxout
gettxout
getwork
getwork
help
help
importprivkey
importprivkey
importaddress
importaddress
keypoolrefill
keypoolrefill
listaccounts
listaccounts
listreceivedbyaccount
listreceivedbyaccount
listreceivedbyaddress
listreceivedbyaddress
listsinceblock
listsinceblock
listtransactions
listtransactions
listunspent
listunspent
move
move
sendfrom
sendfrom
sendmany
sendmany
sendrawtransaction
sendrawtransaction
sendtoaddress
sendtoaddress
setaccount
setaccount
setgenerate
setgenerate
settxfee
settxfee
signmessage
signmessage
signrawtransaction
signrawtransaction
stop
stop
validateaddress
validateaddress
verifymessage
verifymessage
walletlock
walletlock
walletpassphrase
walletpassphrase
walletpassphrasechange
walletpassphrasechange>;
return %commands{$name};
}
}
=begin pod
=TITLE Module for querying the Bitcoin REST interface
=AUTHOR Harris Brakmic
This module helps to make queries against the Bitcoin REST interface.
=end pod