module Factom::APIv1
Constants
- DENOMINATION_ENTRY_CREDIT
- DENOMINATION_FACTOID
- GetBalanceFailed
- VERSION
Public Instance Methods
block(keymr)
click to toggle source
# File lib/factom-ruby/client.rb, line 27 def block(keymr) get "/v1/directory-block-by-keymr/#{keymr}" end
chain_head(id)
click to toggle source
# File lib/factom-ruby/client.rb, line 31 def chain_head(id) json = get "/v1/chain-head/#{id}" json['ChainHead'] end
commit_chain(chain_names, content)
click to toggle source
Params: chain_names - chain name combination, must be unique globally. It’s
first entry's external ids actually.
content - content of first entry
# File lib/factom-ruby/client.rb, line 81 def commit_chain(chain_names, content) params = { 'CommitChainMsg' => get_chain_commit(chain_names, content) } raw_post "/v1/commit-chain/", params.to_json, content_type: :json end
commit_entry(chain_id, ext_ids, content)
click to toggle source
# File lib/factom-ruby/client.rb, line 94 def commit_entry(chain_id, ext_ids, content) params = { 'CommitEntryMsg' => get_entry_commit(chain_id, ext_ids, content) } # TODO: will factom make response return json, for a better world? raw_post "/v1/commit-entry/", params.to_json, content_type: :json end
ec_balance(pubkey=ec_public_key)
click to toggle source
# File lib/factom-ruby/client.rb, line 67 def ec_balance(pubkey=ec_public_key) json = get "/v1/entry-credit-balance/#{pubkey}" raise GetBalanceFailed unless json['Success'] json['Response'] end
ec_balance_in_decimal(pubkey=ec_public_key)
click to toggle source
# File lib/factom-ruby/client.rb, line 73 def ec_balance_in_decimal(pubkey=ec_public_key) BigDecimal.new(ec_balance(pubkey)) / DENOMINATION_ENTRY_CREDIT end
entry(hash)
click to toggle source
# File lib/factom-ruby/client.rb, line 44 def entry(hash) decode_entry get "/v1/entry-by-hash/#{hash}" end
entry_block(keymr)
click to toggle source
# File lib/factom-ruby/client.rb, line 36 def entry_block(keymr) get "/v1/entry-block-by-keymr/#{keymr}" end
fa_balance(pubkey)
click to toggle source
# File lib/factom-ruby/client.rb, line 57 def fa_balance(pubkey) json = get "/v1/factoid-balance/#{pubkey}" raise GetBalanceFailed unless json['Success'] json['Response'] end
fa_balance_in_decimal(pubkey)
click to toggle source
# File lib/factom-ruby/client.rb, line 63 def fa_balance_in_decimal(pubkey) BigDecimal.new(fa_balance(pubkey)) / DENOMINATION_FACTOID end
fee()
click to toggle source
# File lib/factom-ruby/client.rb, line 48 def fee json = get "/v1/factoid-get-fee/" json['Fee'] end
head()
click to toggle source
# File lib/factom-ruby/client.rb, line 22 def head json = get "/v1/directory-block-head/" json['KeyMR'] end
height()
click to toggle source
# File lib/factom-ruby/client.rb, line 17 def height json = get "/v1/directory-block-height/" json['Height'] end
properties()
click to toggle source
# File lib/factom-ruby/client.rb, line 53 def properties get "/v1/properties/" end
raw_data(hash)
click to toggle source
# File lib/factom-ruby/client.rb, line 40 def raw_data(hash) get "/v1/get-raw-data/#{hash}" end
reveal_chain(chain_names, content)
click to toggle source
# File lib/factom-ruby/client.rb, line 86 def reveal_chain(chain_names, content) chain_id = get_chain_id chain_names ext_ids = chain_names params = { 'Entry' => build_entry(chain_id, ext_ids, content) } raw_post "/v1/reveal-chain/", params.to_json, content_type: :json end
reveal_entry(chain_id, ext_ids, content)
click to toggle source
# File lib/factom-ruby/client.rb, line 100 def reveal_entry(chain_id, ext_ids, content) params = { 'Entry' => build_entry(chain_id, ext_ids, content) } # TODO: the same, replace raw_post with post raw_post "/v1/reveal-entry/", params.to_json, content_type: :json end
Private Instance Methods
build_entry(chain_id, ext_ids, content)
click to toggle source
# File lib/factom-ruby/client.rb, line 154 def build_entry(chain_id, ext_ids, content) len = 0 ext_ids_hex = [] content_hex = content.unpack('H*').first ext_ids.each do |id| len += id.size + 2 ext_ids_hex.push uint16_to_hex(id.size) ext_ids_hex.push id.unpack('H*').first end "#{VERSION}#{chain_id}#{uint16_to_hex(len)}#{ext_ids_hex.join}#{content_hex}" end
calculate_fee(entry)
click to toggle source
# File lib/factom-ruby/client.rb, line 174 def calculate_fee(entry) fee = entry.size / 2 # count of entry bytes fee -= 35 # header doesn't count fee = (fee+1023)/1024 # round up and divide, rate = 1 EC/KiB # fee only occupy 1 byte in commit message # but the hard limit is 10kB actually, much less than 255 raise "entry is too large!" if fee > 255 fee end
decode_entry(json)
click to toggle source
# File lib/factom-ruby/client.rb, line 168 def decode_entry(json) json['ExtIDs'] = json['ExtIDs'].map {|bin| [bin].pack('H*') } json['Content'] = [ json['Content'] ].pack('H*') json end
get_chain_commit(chain_names, content)
click to toggle source
# File lib/factom-ruby/client.rb, line 108 def get_chain_commit(chain_names, content) timestamp = (Time.now.to_f*1000).floor ts = [ timestamp ].pack('Q>').unpack('H*').first chain_id = get_chain_id chain_names chain_id_hash = get_chain_id_hash chain_id first_entry = build_entry chain_id, chain_names, content first_entry_hash = get_entry_hash first_entry weld = get_weld chain_id, first_entry_hash fee = [ calculate_fee(first_entry)+10 ].pack('C').unpack('H*').first sign "#{VERSION}#{ts[4..-1]}#{chain_id_hash}#{weld}#{first_entry_hash}#{fee}" end
get_chain_id(chain_names)
click to toggle source
# File lib/factom-ruby/client.rb, line 128 def get_chain_id(chain_names) pre_id = chain_names.map {|name| Digest::SHA256.digest(name) }.join Digest::SHA256.hexdigest(pre_id) end
get_chain_id_hash(chain_id)
click to toggle source
# File lib/factom-ruby/client.rb, line 124 def get_chain_id_hash(chain_id) sha256d [chain_id].pack("H*") end
get_entry_commit(chain_id, ext_ids, content)
click to toggle source
# File lib/factom-ruby/client.rb, line 137 def get_entry_commit(chain_id, ext_ids, content) timestamp = (Time.now.to_f*1000).floor ts = [ timestamp ].pack('Q>').unpack('H*').first entry = build_entry(chain_id, ext_ids, content) entry_hash = get_entry_hash entry fee = [ calculate_fee(entry) ].pack('C').unpack('H*').first sign "#{VERSION}#{ts[4..-1]}#{entry_hash}#{fee}" end
get_entry_hash(entry)
click to toggle source
# File lib/factom-ruby/client.rb, line 149 def get_entry_hash(entry) sha512 = Digest::SHA512.hexdigest([entry].pack('H*')) + entry Digest::SHA256.hexdigest [sha512].pack('H*') end
get_weld(chain_id, entry_hash)
click to toggle source
# File lib/factom-ruby/client.rb, line 133 def get_weld(chain_id, entry_hash) sha256d [entry_hash+chain_id].pack("H*") end
uint16_to_hex(i)
click to toggle source
# File lib/factom-ruby/client.rb, line 186 def uint16_to_hex(i) [i].pack('n').unpack('H*').first end