Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'net/http'
- require 'uri'
- require 'json'
- class CoinRPC
- class JSONRPCError < RuntimeError; end
- class ConnectionRefusedError < StandardError; end
- def initialize(uri)
- @uri = URI.parse(uri)
- end
- def self.[](currency)
- c = Currency.find_by_code(currency.to_s)
- case c && c.rpc
- when c.code.upcase = 'ETH' then "::CoinRPC::ETH".constantize.new(c.rpc)
- when c.code.upcase = 'XRP' then "::CoinRPC::XRP".constantize.new(c.rpc)
- else "::CoinRPC::BTC".constantize.new(c.rpc)
- end
- end
- def method_missing(name, *args)
- handle name, *args
- end
- def handle
- raise "Not implemented"
- end
- class BTC < self
- def handle(name, *args)
- post_body = { 'method' => name, 'params' => args, 'id' => 'jsonrpc' }.to_json
- resp = JSON.parse( http_post_request(post_body) )
- raise JSONRPCError, resp['error'] if resp['error']
- result = resp['result']
- result.symbolize_keys! if result.is_a? Hash
- result
- end
- def http_post_request(post_body)
- http = Net::HTTP.new(@uri.host, @uri.port)
- request = Net::HTTP::Post.new(@uri.request_uri)
- request.basic_auth @uri.user, @uri.password
- request.content_type = 'application/json'
- request.body = post_body
- http.request(request).body
- rescue Errno::ECONNREFUSED => e
- raise ConnectionRefusedError
- end
- def safe_getbalance
- begin
- getbalance
- rescue
- 'N/A'
- end
- end
- end
- class ETH < self
- def handle(name, *args)
- post_body = {"jsonrpc" => "2.0", 'method' => name, 'params' => args, 'id' => '1' }.to_json
- resp = JSON.parse( http_post_request(post_body) )
- raise JSONRPCError, resp['error'] if resp['error']
- result = resp['result']
- result.symbolize_keys! if result.is_a? Hash
- result
- end
- def http_post_request(post_body)
- http = Net::HTTP.new(@uri.host, @uri.port)
- request = Net::HTTP::Post.new(@uri.request_uri)
- request.basic_auth @uri.user, @uri.password
- request.content_type = 'application/json'
- request.body = post_body
- http.request(request).body
- rescue Errno::ECONNREFUSED => e
- raise ConnectionRefusedError
- end
- def safe_getbalance
- begin
- (open('http://your_server_ip/cgi-bin/total.cgi').read.rstrip.to_f)
- rescue
- 'N/A'
- end
- end
- def getnewaddress(account)
- call 'getnewaddress', account
- end
- def settxfee(fee)
- call 'settxfee', fee
- end
- def getbalance
- call 'getbalance'
- end
- def validateaddress(fund_uid)
- call 'validateaddress', fund_uid
- end
- def listtransactions(account, number)
- call 'listtransactions', account, number
- end
- def gettransaction(txid)
- call 'gettransaction', txid
- end
- def sendtoaddress(fund_uid, amount)
- call 'sendtoaddress', fund_uid, amount
- end
- end
- class XRP < self
- R_B58_DICT = 'rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz'
- def handle(name, *args)
- post_body = {:method => name, :params => args} .to_json
- resp = JSON.parse( http_post_request(post_body) )
- raise JSONRPCError, resp['error'] if resp['error']
- result = resp['result']
- result.symbolize_keys! if result.is_a? Hash
- result
- end
- def http_post_request(post_body)
- http = Net::HTTP.new(@uri.host, @uri.port)
- request = Net::HTTP::Post.new(@uri.request_uri)
- request.basic_auth @uri.user, @uri.password
- request.content_type = 'application/json'
- request.body = post_body
- http.request(request).body
- rescue Errno::ECONNREFUSED
- raise ConnectionRefusedError
- end
- def getnewaddress(args = nil)
- secret = SecureRandom.hex
- pubkey = Digest::SHA512.new.update(secret).hexdigest[0...33]
- pubkey_inner_hash = Digest::SHA256.new.update(pubkey)
- pubkey_outer_hash = Digest::RMD160.new
- pubkey_outer_hash.update(pubkey_inner_hash.digest)
- account_id = pubkey_outer_hash.digest
- address_type_prefix = "\x00"
- payload = address_type_prefix + account_id
- chksum_hash1 = Digest::SHA256.new.update(payload).digest
- chksum_hash2 = Digest::SHA256.new.update(chksum_hash1).digest
- checksum = chksum_hash2[0...4]
- data_to_encode = payload << checksum
- int = data_to_encode.each_byte.reduce(0) { |i, byte| i * 256 + byte }
- address = ''
- while int > 0
- char = R_B58_DICT[int % 58]
- address << char
- int /= 58
- end
- {
- address: (address << R_B58_DICT[0]).reverse,
- secret: secret
- }
- end
- def listtransactions(account, number = 100)
- post_body = {
- method: 'account_tx',
- params: [{
- account: account,
- ledger_index_max: -1,
- ledger_index_min: -1,
- limit: number
- }]
- }.to_json
- resp = JSON.parse(http_post_request(post_body))
- raise JSONRPCError, resp['error'] if resp['error']
- resp['result']['transactions'].map { |t| t['tx'] }
- end
- def gettransaction(txid)
- post_body = {
- method: 'tx',
- params: [
- transaction: txid,
- binary: false
- ]
- }.to_json
- resp = JSON.parse(http_post_request(post_body))
- raise JSONRPCError, resp['error'] if resp['error']
- {
- amount: resp['result']['Amount'].to_d / 1_000_000,
- confirmations: resp['result']['meta']['AffectedNodes'].size,
- timereceived: resp['result']['date'] + 946684800,
- txid: txid,
- details: [{
- account: 'payment',
- address: resp['result']['Destination'],
- amount: resp['result']['Amount'].to_d / 1_000_000,
- category: 'receive'
- }]
- }
- end
- def validateaddress(address = nil)
- p address
- post_body = {
- method: 'ledger_entry',
- params: [
- {
- account_root: address,
- ledger_index: 'validated',
- type: 'account_root'
- }
- ]
- }.to_json
- resp = JSON.parse(http_post_request(post_body))
- raise JSONRPCError, resp['error'] if resp['error']
- {
- isvalid: resp['result']['status'] == 'success',
- ismine: false,
- address: address
- }
- end
- def sendtoaddress(address, amount)
- issuer = FundSource.find_by(uid: Withdraw.last.fund_uid)
- .member.payment_addresses
- .find_by(currency: currency)
- post_body = {
- method: 'sign',
- params: [{
- offline: false,
- secret: issuer['secret'],
- tx_json: {
- Account: issuer['address'],
- Amount: {
- currency: 'XRP',
- issuer: issuer['address'],
- value: amount.to_s
- },
- Destination: address,
- TransactionType: 'Payment'
- },
- fee_mult_max: @tx_fee
- }]
- }.to_json
- resp = JSON.parse(http_post_request(post_body))
- raise JSONRPCError, resp['error'] if resp['error']
- p resp
- end
- def getbalance(account = nil)
- post_body = {
- method: 'account_info',
- params: [
- account: account || Currency.find_by_code('xrp').assets['accounts'].sample['address'],
- strict: true,
- ledger_index: 'validated'
- ]
- }.to_json
- resp = JSON.parse(http_post_request(post_body))
- raise JSONRPCError, resp['error'] if resp['error']
- result = resp['result']['account_data']['Balance'].to_f / 1_000_000
- result
- end
- def safe_getbalance
- getbalance || 'N/A'
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement