Blockchain.sm 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #!/usr/bin/ruby
  2. # Code translated from:
  3. # https://github.com/Savjee/SavjeeCoin
  4. include SigningKey
  5. import SigningKey::SigningKey
  6. class Transaction(fromAddress, String toAddress, Number amount) {
  7. has timestamp = Date()
  8. has signature = nil
  9. # Creates a SHA256 hash of the transaction
  10. -> calculateHash() -> String {
  11. [fromAddress, toAddress, amount, timestamp].dump.sha256
  12. }
  13. /*
  14. * Signs a transaction with the given signingKey (which is an Elliptic keypair
  15. * object that contains a private key). The signature is then stored inside the
  16. * transaction object and later stored on the blockchain.
  17. */
  18. -> signTransaction(SigningKey signingKey) {
  19. # You can only send a transaction from the wallet that is linked to your
  20. # key. So here we check if the fromAddress matches your publicKey
  21. if (signingKey.getPublic != fromAddress) {
  22. die('You cannot sign transactions for other wallets!')
  23. }
  24. # Calculate the hash of this transaction, sign it with the key
  25. # and store it inside the transaction obect
  26. const hashTx = self.calculateHash
  27. const sig = signingKey.sign(hashTx)
  28. signature = sig
  29. }
  30. /**
  31. * Checks if the signature is valid (transaction has not been tampered with).
  32. * It uses the fromAddress as the public key.
  33. */
  34. -> isValid() -> Bool {
  35. # If the transaction doesn't have a from address we assume it's a
  36. # mining reward and that it's valid. You could verify this in a
  37. # different way (special field for instance)
  38. if (fromAddress == nil) {
  39. return true
  40. }
  41. if (signature == nil) {
  42. die ('No signature in this transaction');
  43. }
  44. SigningKey().keyFromPublic(fromAddress).verify(signature, self.calculateHash)
  45. }
  46. }
  47. class Blockchain::Block (Date timestamp, Array transactions, String previousHash = '') {
  48. has nonce = 0
  49. has hash = nil
  50. method init {
  51. hash = self.calculateHash
  52. }
  53. /**
  54. * Returns the SHA256 of this block (by processing all the data stored
  55. * inside this block)
  56. */
  57. -> calculateHash() -> String {
  58. [previousHash, timestamp, transactions, nonce].dump.sha256
  59. }
  60. /**
  61. * Starts the mining process on the block. It changes the 'nonce' until the hash
  62. * of the block starts with enough zeros (= difficulty)
  63. */
  64. -> mineBlock(Number difficulty) {
  65. var target_prefix = "0"*difficulty
  66. while (hash.first(difficulty) != target_prefix) {
  67. ++nonce
  68. hash = self.calculateHash
  69. }
  70. STDERR.say("Block mined: #{hash}")
  71. }
  72. /**
  73. * Validates all the transactions inside this block (signature + hash) and
  74. * returns true if everything checks out. False if the block is invalid.
  75. */
  76. -> hasValidTransactions() -> Bool {
  77. transactions.all { .isValid }
  78. }
  79. }
  80. class Blockchain (Number difficulty = 2, Array pendingTransactions = [], Number miningReward = 100) {
  81. has chain = []
  82. method init {
  83. chain = [self.createGenesisBlock]
  84. }
  85. -> createGenesisBlock() -> Blockchain::Block {
  86. Blockchain::Block(Date.parse('2021-10-07', '%Y-%m-%d'), [], '0')
  87. }
  88. /**
  89. * Returns the latest block on our chain. Useful when you want to create a
  90. * new Block and you need the hash of the previous Block.
  91. */
  92. -> getLatestBlock() -> Blockchain::Block {
  93. chain.last
  94. }
  95. /**
  96. * Takes all the pending transactions, puts them in a Block and starts the
  97. * mining process. It also adds a transaction to send the mining reward to
  98. * the given address.
  99. */
  100. -> minePendingTransactions(String miningRewardAddress) {
  101. const rewardTx = Transaction(nil, miningRewardAddress, miningReward)
  102. pendingTransactions.push(rewardTx)
  103. const block = Blockchain::Block(Date.now, pendingTransactions, self.getLatestBlock.hash)
  104. block.mineBlock(difficulty)
  105. STDERR.say('Block successfully mined!')
  106. chain.push(block)
  107. pendingTransactions = []
  108. }
  109. /**
  110. * Add a new transaction to the list of pending transactions (to be added
  111. * next time the mining process starts). This verifies that the given
  112. * transaction is properly signed.
  113. */
  114. -> addTransaction(Transaction transaction) {
  115. if (!transaction.fromAddress || !transaction.toAddress) {
  116. die('Transaction must include from and to address')
  117. }
  118. # Verify the transaction
  119. if (!transaction.isValid()) {
  120. die('Cannot add invalid transaction to chain')
  121. }
  122. if (transaction.amount <= 0) {
  123. die('Transaction amount should be higher than 0')
  124. }
  125. # Making sure that the amount sent is not greater than existing balance
  126. if (self.getBalanceOfAddress(transaction.fromAddress) < transaction.amount) {
  127. die('Not enough balance')
  128. }
  129. pendingTransactions.push(transaction)
  130. self
  131. }
  132. /**
  133. * Returns the balance of a given wallet address.
  134. */
  135. -> getBalanceOfAddress(String address) -> Number {
  136. var balance = 0;
  137. chain.each {|block|
  138. block.transactions.each {|trans|
  139. if (trans.fromAddress == address) {
  140. balance -= trans.amount
  141. }
  142. if (trans.toAddress == address) {
  143. balance += trans.amount
  144. }
  145. }
  146. }
  147. return balance
  148. }
  149. /**
  150. * Returns a list of all transactions that happened
  151. * to and from the given wallet address.
  152. */
  153. -> getAllTransactionsForWallet(String address) -> Array {
  154. var txs = []
  155. chain.each {|block|
  156. block.transactions.each {|tx|
  157. if ((tx.fromAddress == address) || (tx.toAddress == address)) {
  158. txs.push(tx)
  159. }
  160. }
  161. }
  162. STDERR.printf("get transactions for wallet count: %s\n", txs.length)
  163. return txs
  164. }
  165. /**
  166. * Loops over all the blocks in the chain and verify if they are properly
  167. * linked together and nobody has tampered with the hashes. By checking
  168. * the blocks it also verifies the (signed) transactions inside of them.
  169. */
  170. -> isChainValid() -> Bool {
  171. # Check if the Genesis block hasn't been tampered with by comparing
  172. # the output of createGenesisBlock with the first block on our chain
  173. if (self.createGenesisBlock.dump != chain.first.dump) {
  174. return false
  175. }
  176. # Check the remaining blocks on the chain to see if there hashes and
  177. # signatures are correct
  178. chain.each_cons(2, {|previousBlock, currentBlock|
  179. if (previousBlock.hash != currentBlock.previousHash) {
  180. return false
  181. }
  182. if (!currentBlock.hasValidTransactions) {
  183. return false
  184. }
  185. if (currentBlock.hash != currentBlock.calculateHash) {
  186. return false
  187. }
  188. })
  189. return true
  190. }
  191. }