Last active
February 19, 2016 22:59
-
-
Save worace/07f5e0d8b7d6a069038c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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