~singpolyma/jmp-pay

ref: 6cd9f94dbdea8bbb5a2a0b68e3e0b3998b2f8ed8 jmp-pay/lib/electrum.rb -rw-r--r-- 836 bytes
6cd9f94dStephen Paul Weber Endpoint that pushes all unknown transactions into Redis 1 year, 11 months ago
                                                                                
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
# frozen_string_literal: true

require "json"
require "net/http"
require "securerandom"

class Electrum
	def initialize(rpc_uri:, rpc_username:, rpc_password:)
		@rpc_uri = URI(rpc_uri)
		@rpc_username = rpc_username
		@rpc_password = rpc_password
	end

	def getaddresshistory(address)
		rpc_call(:getaddresshistory, address: address)["result"]
	end

protected

	def rpc_call(method, params)
		JSON.parse(post_json(
			jsonrpc: "2.0",
			id: SecureRandom.hex,
			method: method.to_s,
			params: params
		).body)
	end

	def post_json(data)
		req = Net::HTTP::Post.new(@rpc_uri, "Content-Type" => "application/json")
		req.basic_auth(@rpc_username, @rpc_password)
		req.body = data.to_json
		Net::HTTP.start(
			@rpc_uri.hostname,
			@rpc_uri.port,
			use_ssl: @rpc_uri.scheme == "https"
		) do |http|
			http.request(req)
		end
	end
end