Skip to content

Instantly share code, notes, and snippets.

@worace
Last active February 19, 2016 22:59
Show Gist options
  • Select an option

  • Save worace/07f5e0d8b7d6a069038c to your computer and use it in GitHub Desktop.

Select an option

Save worace/07f5e0d8b7d6a069038c to your computer and use it in GitHub Desktop.
require "digest"
require "hurley"
def mine(target, input)
nonce = 0
message = "#{input}#{nonce}"
while Digest::SHA1.hexdigest(message).hex > target.hex
nonce = nonce + 1
message = "#{input}#{nonce}"
end
nonce
end
def target(difficulty)
"0" * difficulty + "f" * (40 - difficulty)
end
def target_url
"http://git-coin.herokuapp.com/target"
end
def find_me_a_nonce
body = Hurley.get(target_url).body
data = JSON.parse(body)
target = data["target"]
parent = data["parent_hash"]
mine(target, parent)
end
def submission_url
"http://git-coin.herokuapp.com/hash"
end
def submit_nonce_for_coin(nonce, name)
puts Hurley.post(submission_url,
owner: name,
message: nonce.to_s).body
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment