Skip to content

Instantly share code, notes, and snippets.

@krtschmr
Created September 27, 2017 05:44
Show Gist options
  • Save krtschmr/b8e8b40cf7de6b863de78bd1908961fc to your computer and use it in GitHub Desktop.
Save krtschmr/b8e8b40cf7de6b863de78bd1908961fc to your computer and use it in GitHub Desktop.
Monero::Wallet to access Monero-Wallet-RPC via ruby
# gem "money"
Money::Currency.register({ :priority => 1, :iso_code => "xmr", :iso_numeric => "846", :name => "Monero", :symbol => "XMR", :subunit => "", :subunit_to_unit => 1000000000000, :decimal_mark => ".", :thousands_separator => ""})
class XMR
def self.new(amount); Money.new(amount, :xmr); end
def to_s; Money.new(amount, :xmr).format; end
end
class Monero::Wallet
attr_accessor :host, :port, :username, :password
def initialize(args={})
self.host = args.fetch(:host, "192.168.1.226")
self.port = args.fetch(:port, "18081")
self.username = args.fetch(:username, "username")
self.password = args.fetch(:password, "password")
end
def getbalance
curl_request("getbalance")
end
def getaddress
curl_request("getaddress")["address"]
end
def getheight
curl_request("getheight")["height"]
end
def query_key(type)
raise ArgumentError unless ["mnemonic", "view_key"].include?(type.to_s)
curl_request("query_key", {key_type: type})["key"]
end
def make_integrated_address(payment_id = "")
curl_request("make_integrated_address", {payment_id: payment_id})
end
def balance
XMR.new(getbalance["balance"])
end
def unlocked_balance
XMR.new(getbalance["unlocked_balance"])
end
private
def curl_request(method, params=nil)
params_string = ""
if params
# TODO multi params looping
k,v = params.first
params_string = '"'+k.to_s+'": "'+v.to_s+'"'
end
data = '{"jsonrpc":"2.0","id":"0","method": "'+method+'", "params": { '+params_string+' } }'
args = " -s"
args << " -u #{username}:#{password} --digest"
args << " -X POST #{base_uri}/json_rpc"
args << " -d '#{data}'"
args << " -H 'Content-Type: application/json'"
json = JSON.parse(`curl #{args}`)
json["result"]
end
def base_uri
"http://#{host}:#{port}"
end
end
@dl3br
Copy link

dl3br commented Sep 27, 2017

hey,i have something similar in typescript

https://github.com/dlebrecht/rx-monero-wallet/blob/master/lib/wallet.ts

cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment