cryptocol 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/cryptocol.rb +66 -0
- metadata +43 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 5ed99d6a6ac967a3e06bc2863c8782153f3206b44e0164e4c620175d0068e1df
|
|
4
|
+
data.tar.gz: c965605551d0ad7115f69f05b15deab1f1dfab707cf69a3288a8c909d5231084
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 8009e1d34b853718c0dd32ccdda8cb6506fb1c29042951c7c61550ae8dd6383c071b7ea913ecc8d36e738b95dc554b9bee046d6f13140057f1718239aac697c1
|
|
7
|
+
data.tar.gz: 48244585898289374c28bf9062254b9275507692f6be2545203b20bb711165d661da13900214918b3ebaac317ad19bd35bdec7951260204c6c0f57c23f15f621
|
data/lib/cryptocol.rb
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
require 'digest'
|
|
3
|
+
|
|
4
|
+
module Cryptocol
|
|
5
|
+
class Blockchain
|
|
6
|
+
def initialize()
|
|
7
|
+
@chain = []
|
|
8
|
+
@current_transactions = []
|
|
9
|
+
# creating the genesis block
|
|
10
|
+
new_block(100, 1)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def new_block(proof, previousHash)
|
|
14
|
+
block = {
|
|
15
|
+
:index => @chain.count + 1,
|
|
16
|
+
:timestamp => Time.now.to_i,
|
|
17
|
+
:transactions => @current_transactions,
|
|
18
|
+
:proof => proof,
|
|
19
|
+
:previous_hash => previousHash ||= self.hash(@chain[-1])
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
# Reset the current transactions
|
|
23
|
+
@transactions = []
|
|
24
|
+
|
|
25
|
+
@chain.push(block)
|
|
26
|
+
|
|
27
|
+
block
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def proof_of_work(last_proof)
|
|
31
|
+
proof = 0
|
|
32
|
+
while !self.valid_proof(last_proof, proof) do
|
|
33
|
+
proof += 1
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
proof
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def new_transaction(sender, recipient, amount)
|
|
40
|
+
@current_transactions.push({
|
|
41
|
+
:sender => sender,
|
|
42
|
+
:recipient => recipient,
|
|
43
|
+
:amount => amount
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
# return the index where the transaction will be added to
|
|
47
|
+
@chain.index last_block
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def last_block
|
|
51
|
+
@chain[-1]
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def self.hash(block)
|
|
55
|
+
block_string = block.to_json
|
|
56
|
+
Digest::SHA256.digest block_string
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def self.valid_proof(last_proof, proof)
|
|
60
|
+
guess = "#{last_proof}#{proof}"
|
|
61
|
+
guess_hash = Digest::SHA256.digest guess
|
|
62
|
+
|
|
63
|
+
guess_hash[-4..-1] == '0000'
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: cryptocol
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Jose Luis Montaña
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2018-04-10 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: The simplest blockchain implementation
|
|
14
|
+
email: ing.jmontana@gmail.com
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- lib/cryptocol.rb
|
|
20
|
+
homepage:
|
|
21
|
+
licenses: []
|
|
22
|
+
metadata: {}
|
|
23
|
+
post_install_message:
|
|
24
|
+
rdoc_options: []
|
|
25
|
+
require_paths:
|
|
26
|
+
- lib
|
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
28
|
+
requirements:
|
|
29
|
+
- - ">="
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: '0'
|
|
32
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
33
|
+
requirements:
|
|
34
|
+
- - ">="
|
|
35
|
+
- !ruby/object:Gem::Version
|
|
36
|
+
version: '0'
|
|
37
|
+
requirements: []
|
|
38
|
+
rubyforge_project:
|
|
39
|
+
rubygems_version: 2.7.6
|
|
40
|
+
signing_key:
|
|
41
|
+
specification_version: 4
|
|
42
|
+
summary: The simplest blockchain implementation
|
|
43
|
+
test_files: []
|