forked from jangorecki/shinyPay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitcoind.R
36 lines (34 loc) · 1.89 KB
/
bitcoind.R
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
# json-rpc to bitcoind
bitcoind.rpc <- function(host = getOption("rpchost","127.0.0.1"),
user = getOption("rpcuser"),
password = getOption("rpcpassword"),
port = getOption("rpcport","8332"),
id = NA_integer_, method, params = list()){
stopifnot(is.character(user), is.character(password), is.character(method))
rpcurl <- paste0("http://",user,":",password,"@",host,":",port)
req <- httr::POST(rpcurl, body = jsonlite::toJSON(list(jsonrpc = "1.0", id = id, method = method, params = params), auto_unbox=TRUE))
if(httr::http_status(req)$category != "success"){
message(jsonlite::fromJSON(httr::content(req, "text"))$error$message)
httr::stop_for_status(req)
}
jsonlite::fromJSON(httr::content(req, "text"))
}
# wrappers
getinfo <- function() bitcoind.rpc(method = "getinfo")$result
getnewaddress <- function() bitcoind.rpc(method = "getnewaddress")$result
getreceivedbyaddress <- function(address, minconf=1L) bitcoind.rpc(method = "getreceivedbyaddress", params = list(address, minconf))$result
# helpers
makepaymenturi <- function(address, amount) paste0("bitcoin:",address,"?amount=",amount)
plotQR <- function(to_encode){
mar <- par(mar=c(0,0,0,0))
on.exit(par(mar))
if(requireNamespace("qrcode", quietly = TRUE)){
suppressPackageStartupMessages(require("raster")) # cannot image without attaching raster package
suppressPackageStartupMessages(require("qrcode")) # cannot generate QRcode without attaching qrcode package
image(qrcode_gen(to_encode, plotQRcode = FALSE, dataOutput = TRUE),
asp=1, col=c("white", "black"), axes=FALSE, xlab="", ylab="")
} else {
plot(c(0, 1), c(0, 1), ann=FALSE, bty="n", type="n", xaxt="n", yaxt="n")
text(x = 0.5, y = 0.5, "To display QR Code install qrcode package", col = "black")
}
}