Class: Factom::Client
- Inherits:
-
Object
- Object
- Factom::Client
- Defined in:
- lib/factom-ruby/client.rb
Constant Summary collapse
- PREFIX_FA =
'FA'.freeze
- PREFIX_EC =
'EC'.freeze
- ADDRESS_PREFIX =
{ PREFIX_FA => '5fb1', PREFIX_EC => '592a' }.freeze
Instance Attribute Summary collapse
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
Instance Method Summary collapse
-
#address_to_pubkey(addr) ⇒ Object
to pubkey in hex, 32 bytes.
- #ec_address ⇒ Object
- #ec_public_key ⇒ Object
- #get(path, params = {}, options = {}) ⇒ Object
-
#initialize(endpoint, ec_private_key, version = 'v1') ⇒ Client
constructor
A new instance of Client.
- #pubkey_to_address(prefix, pubkey) ⇒ Object
- #raw_get(path, params = {}, options = {}) ⇒ Object
- #raw_post(path, params = {}, options = {}) ⇒ Object
- #sha256d(bytes) ⇒ Object
- #sign(message) ⇒ Object
-
#signing_key ⇒ Object
ed25519 private key.
Constructor Details
#initialize(endpoint, ec_private_key, version = 'v1') ⇒ Client
Returns a new instance of Client.
202 203 204 205 206 |
# File 'lib/factom-ruby/client.rb', line 202 def initialize(endpoint, ec_private_key, version='v1') @endpoint = endpoint.gsub(/\/\z/, '') @ec_private_key = ec_private_key =~ /\A#{PREFIX_EC}/ ? address_to_pubkey(ec_private_key) : ec_private_key self.instance_eval { extend ::Factom.const_get("API#{version}", false) } end |
Instance Attribute Details
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
193 194 195 |
# File 'lib/factom-ruby/client.rb', line 193 def endpoint @endpoint end |
Instance Method Details
#address_to_pubkey(addr) ⇒ Object
to pubkey in hex, 32 bytes
236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/factom-ruby/client.rb', line 236 def address_to_pubkey(addr) return unless addr.size == 52 prefix = ADDRESS_PREFIX[addr[0,2]] return unless prefix v = Bitcoin.decode_base58(addr) return if v[0,4] != prefix bytes = [v[0, 68]].pack('H*') return if v[68, 8] != sha256d(bytes)[0, 8] v[4, 64] end |
#ec_address ⇒ Object
231 232 233 |
# File 'lib/factom-ruby/client.rb', line 231 def ec_address @ec_address ||= pubkey_to_address ADDRESS_PREFIX[PREFIX_EC], ec_public_key end |
#ec_public_key ⇒ Object
227 228 229 |
# File 'lib/factom-ruby/client.rb', line 227 def ec_public_key @ec_public_key ||= signing_key.verify_key.to_s.unpack('H*').first end |
#get(path, params = {}, options = {}) ⇒ Object
216 217 218 |
# File 'lib/factom-ruby/client.rb', line 216 def get(path, params={}, ={}) JSON.parse raw_get(path, params, ) end |
#pubkey_to_address(prefix, pubkey) ⇒ Object
251 252 253 254 255 256 257 258 |
# File 'lib/factom-ruby/client.rb', line 251 def pubkey_to_address(prefix, pubkey) return unless pubkey.size == 64 # 32 bytes in hex addr = "#{prefix}#{pubkey}" bytes = [addr].pack('H*') Bitcoin.encode_base58 "#{addr}#{sha256d(bytes)[0,8]}" end |
#raw_get(path, params = {}, options = {}) ⇒ Object
208 209 210 211 212 213 214 |
# File 'lib/factom-ruby/client.rb', line 208 def raw_get(path, params={}, ={}) uri = "#{endpoint}#{path}" = {accept: :json}.merge() [:params] = params RestClient.get uri, end |
#raw_post(path, params = {}, options = {}) ⇒ Object
220 221 222 223 224 225 |
# File 'lib/factom-ruby/client.rb', line 220 def raw_post(path, params={}, ={}) uri = "#{endpoint}#{path}" = {accept: :json}.merge() RestClient.post uri, params, end |
#sha256d(bytes) ⇒ Object
260 261 262 |
# File 'lib/factom-ruby/client.rb', line 260 def sha256d(bytes) Digest::SHA256.hexdigest(Digest::SHA256.digest(bytes)) end |
#sign(message) ⇒ Object
269 270 271 272 |
# File 'lib/factom-ruby/client.rb', line 269 def sign() sig = signing_key.sign([].pack('H*')).unpack('H*').first "#{}#{ec_public_key}#{sig}" end |
#signing_key ⇒ Object
ed25519 private key
265 266 267 |
# File 'lib/factom-ruby/client.rb', line 265 def signing_key @signing_key ||= RbNaCl::SigningKey.new([@ec_private_key].pack('H*')) end |