Skip to content

Hamdan85/cryptoruby

Repository files navigation

Gem Version Maintainability Dependency Status Build Status

Cryptoruby

How about to have your own blockchain? This gems allow your application to have it...

Installation

Add this line to your application's Gemfile:

gem 'cryptoruby'

And then execute:

$ bundle

Or install it yourself as:

$ gem install cryptoruby

Usage

In order to use this gem you'll need to initialize your block first:

blockchain = Cryptoruby::Blockchain.new

This will create your blockchain and initiate with and empty first block. The Genesis one.

After that, all youll need to do is to add some blocks to it:

data = {amount: 3000}
blockchain.add_block(data) # or
blockchain << data

You see... data can be anything. You can store strings, jsons, arrays, even base64 files.

Then, after adding blocks, you'll need to check if your chain is still valid right?

blockchain.is_valid? # returns true or false

Try changing something in the blocks and validating again... this works man... for real!

Ah... almost forgot... you can set yours blockchain's difficult too... To do so, all you need to do is to change (or instantiate from the beginning) the difficult parameter

blockchain = Cryptoruby::Blockchain.new(3) 

The difficult is default to 0, but you can set it for whatever you want. In this example, it was set to 3 zeroes in the start of the hash.

And, in the end, you'll be able to export everything. How About That?

blockchain.export

Full Usage

blockchain = Cryptoruby::Blockchain.new(2) # Difficult of 2 initial zeros
#<Cryptoruby::Blockchain:0x007fcb340b5cc8 @blocks=[#<Cryptoruby::Blockchain::Block:0x007fcb340b57f0 @index=0, @previous_hash=nil, @data="Genesis Block", @difficult=2, @blockchain=#<Cryptoruby::Blockchain:0x007fcb340b5cc8 ...>, @timestamp=1513486966, @nonce=6995, @hash="000449121a75ece0e52adbc12cf60bde1f9fe3f7c627f1163c412d58cfc2a41fddcf275814fc8acdbf685b993b07fc3f8bb0d2664294af418a47198c5b7eb90f">], @index=0, @difficult=2>
blockchain << { name: 'last night', amount_in_cents: 35000 }
[#<Cryptoruby::Blockchain::Block:0x007fcb340b57f0 @index=0, @previous_hash=nil, @data="Genesis Block", @difficult=2, @blockchain=#<Cryptoruby::Blockchain:0x007fcb340b5cc8 @blocks=[...], @index=1, @difficult=2>, @timestamp=1513486966, @nonce=6995, @hash="000449121a75ece0e52adbc12cf60bde1f9fe3f7c627f1163c412d58cfc2a41fddcf275814fc8acdbf685b993b07fc3f8bb0d2664294af418a47198c5b7eb90f">, #<Cryptoruby::Blockchain::Block:0x007fcb358bf080 @index=1, @previous_hash="000449121a75ece0e52adbc12cf60bde1f9fe3f7c627f1163c412d58cfc2a41fddcf275814fc8acdbf685b993b07fc3f8bb0d2664294af418a47198c5b7eb90f", @data={:name=>"last night", :amount_in_cents=>35000}, @difficult=2, @blockchain=#<Cryptoruby::Blockchain:0x007fcb340b5cc8 @blocks=[...], @index=1, @difficult=2>, @timestamp=1513486969, @nonce=4565, @hash="0008767c32faa0bdf72229f1b3d5e55fe590ea7b9dc6791804a831ac4229e074ddc58b1ee9369d7af16d0e89f4f3627df626483a248b720337620f4c26d9e2e7">]
blockchain.export
{
    :difficult => 2,
       :blocks => [
        [0] {
                    :index => 0,
            :previous_hash => nil,
                     :data => "Genesis Block",
                :timestamp => 1513486966,
                     :hash => "000449121a75ece0e52adbc12cf60bde1f9fe3f7c627f1163c412d58cfc2a41fddcf275814fc8acdbf685b993b07fc3f8bb0d2664294af418a47198c5b7eb90f",
                :difficult => 2,
                    :nonce => 6995
        },
        [1] {
                    :index => 1,
            :previous_hash => "000449121a75ece0e52adbc12cf60bde1f9fe3f7c627f1163c412d58cfc2a41fddcf275814fc8acdbf685b993b07fc3f8bb0d2664294af418a47198c5b7eb90f",
                     :data => {
                           :name => "last night",
                :amount_in_cents => 35000
            },
                :timestamp => 1513486969,
                     :hash => "0008767c32faa0bdf72229f1b3d5e55fe590ea7b9dc6791804a831ac4229e074ddc58b1ee9369d7af16d0e89f4f3627df626483a248b720337620f4c26d9e2e7",
                :difficult => 2,
                    :nonce => 4565
        }
    ]
}

Hope you like it!!

Development

It'll be awesome if you help me to get it better. After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/hamdan85/cryptoruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Cryptoruby project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

About

Ruby's Blockchain

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published