module Openname

A toolkit for the Openname distributed identity & naming system

Constants

DEFAULT_ENDPOINT
OPENNAME_REGEX

github.com/openname/openname-specifications#usernames

SCHEMA_VERSION
USERAGENT
VERSION

Public Class Methods

auth(username, password) click to toggle source

Send basic authentication Openname.auth(nil,nil) disables basic auth Disabled by default

# File lib/openname.rb, line 41
def self.auth(username, password)
    @@username = username
    @@password = password
end
endpoint() click to toggle source

Current endpoint used by the library

# File lib/openname.rb, line 28
def self.endpoint
    if @@endpoint.nil?
        return DEFAULT_ENDPOINT
    else
        return @@endpoint
    end
end
endpoint=(url) click to toggle source

Set endpoint to url if url is nil, DEFAULT_ENDPOINT is used as the endpoint

# File lib/openname.rb, line 80
def self.endpoint=(url)
    @@endpoint = url
end
get(openname) click to toggle source

Return a User representing the given openname

# File lib/openname.rb, line 135
def self.get(openname)
    User.from_json(self.get_json(openname),openname)
end
get_bitcoin_address(openname_or_address) click to toggle source

Takes either a bitcoin address or a openname Returns the bitcoin address associated with the openname or passes through address provided

# File lib/openname.rb, line 142
def self.get_bitcoin_address(openname_or_address)
    return openname_or_address if Bitcoin.valid_address?(openname_or_address)
    raise ArgumentError.new("#{openname_or_address} is not a valid Openname or Bitcoin address") if !self.valid?(openname_or_address)
    user = get(openname_or_address)
    raise NameError.new("Openname user #{openname_or_address} does not have a Bitcoin address") if !Bitcoin.valid_address?(user.bitcoin_address)
    return user.bitcoin_address
end
get_json(openname) click to toggle source

Retrieve JSON data stored in Openname record

# File lib/openname.rb, line 95
def self.get_json(openname)
    raise ArgumentError.new("#{openname} is not a valid Openname") if !self.valid?(openname)
    uri = URI(self.endpoint + "/#{openname.downcase}#{self.suffix}")
    http = Net::HTTP.new(uri.host,uri.port)
    http.use_ssl = uri.scheme == "https" ? true : false
    req = Net::HTTP::Get.new(uri.path, {'User-Agent' => USERAGENT})
    req.basic_auth(@@username, @@password) if @@username && @@password
    res = http.request(req)
    case res.code.to_s
    when "404" then raise NameError.new("Openname \"#{openname}\" does not exist")
    when "200" then
        json_body = JSON.parse(res.body)
        if(json_body["status"] && json_body["status"] == "reserved")
            NameError.new("Openname \"#{openname}\" does not exist. It is reserved.")
        else

            # Current ONS resolver always returns 200
            # so we need to manually detect names that don't exist
            if (json_body["error"])
                raise NameError.new("Openname \"#{openname}\" does not exist")
            end

            # Current ONS resolver wraps profile
            # so as to also return proof verification
            # results. openname-ruby ignores verifications
            if (profile_only? && json_body["profile"])
                json_body["profile"]
            else
                json_body
            end
        end
    else
        error = JSON.parse(res.body)
        raise RuntimeError.new("Openname endpoint returned error: #{error["error"]}")
    end

end
profile_only(profile_only) click to toggle source

Profile only if profile_only is true, Openname profile will not be wrapped in { “profile”: {} }

# File lib/openname.rb, line 51
def self.profile_only(profile_only)
    @@profile_only = profile_only
end
profile_only?() click to toggle source
# File lib/openname.rb, line 55
def self.profile_only?
    @@profile_only
end
suffix() click to toggle source

Suffix appended to openname on each endpoint request

# File lib/openname.rb, line 69
def self.suffix
    if @@suffix.nil?
        return ""
    else
        return @@suffix
    end
end
suffix=(suffix) click to toggle source

Set suffix appended to openname to suffix if suffix is nil, no suffix will be appended

# File lib/openname.rb, line 63
def self.suffix=(suffix)
    @@suffix = suffix
end
valid?(openname) click to toggle source

Check if the given openname is in proper format Does not downcase input

# File lib/openname.rb, line 89
def self.valid?(openname)
    Openname::OPENNAME_REGEX.match(openname).nil? ? false : true
end

Protected Class Methods

check_schema_version(json_result) click to toggle source
# File lib/openname.rb, line 246
def self.check_schema_version(json_result)
    if json_result["v"] != SCHEMA_VERSION
        warn "Openname gem only supports Openname schema version #{SCHEMA_VERSION}"
    end
end