daily pastebin goal
30%
SHARE
TWEET

Untitled

a guest Mar 24th, 2019 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import UIKit
  2. import PlaygroundSupport
  3.  
  4. /*
  5.  A block is a record which contains some data. Blocks come together to form a blockchain.
  6.  
  7.  Here, we will explore the parts of a block! A block consists of data, nonce and a hash. A hash is a string of letters and numbers. The hash can be thought of as a fingerprint of the block. When any data in the block is changed, the hash also changes. In this way, the hash can be used not only to identify blocks but also to detect changes in their data.
  8.  
  9.  Change the data variable of the block and see how the hash (red text) changes¡
  10.  */
  11.  
  12. let data = "WWDC 2019 begins on June 3, 2019!"
  13.  
  14. /* A block can only be used if it is valid. A block is valid when the first four digits of it's hash are zeroes. This is where the "nonce" comes in. The nonce is a number which also part of the block. The value of nonce is adjusted until the hash has fours zeroes. The process of adjusting and testing every nonce is called mining.
  15.  
  16.  Change the boolean below to true to also mine for the correct nonce and validate your block!"
  17.  */
  18. var mineHash = true
  19.  
  20. /*
  21.  As you can see, we have now mined for a valid nonce (blue numbers) and our hash also starts with four zeroes!
  22.  */
  23.  
  24.  
  25. /*
  26.  DISCLAIMER: The program doesn't actually generate a true hash of the data and is only a simulation.
  27.  */
  28.  
  29.  
  30.  
  31.  
  32.  
  33. /*#########################################################*/
  34. let view = UIView(frame: CGRect(x: 128, y: 128, width: 512, height: 512))
  35. PlaygroundPage.current.liveView = view
  36. view.backgroundColor = .white
  37.  
  38. var hash = "0"
  39. var nonce = 0
  40.  
  41. func randomString(length: Int) -> String {
  42.     let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  43.     return String((0..<length).map{ _ in letters.randomElement()! })
  44. }
  45.  
  46. func generateTrueHash() {
  47.     var random = "0"
  48.     random = randomString(length: 60)
  49.     hash = "0000" + random
  50. }
  51.  
  52. func generateFalseHash() {
  53.     hash = randomString(length: 60)
  54. }
  55.  
  56. func generateNonce() {
  57.     if mineHash == true {
  58.         nonce = Int.random(in: 0..<99999)
  59.     }else {nonce = 0}
  60. }
  61.  
  62. if mineHash == true {
  63.     generateTrueHash()
  64.     generateNonce()
  65. } else {
  66.     generateFalseHash()
  67.     generateNonce()
  68. }
  69.  
  70. let dataLabel = UILabel()
  71. dataLabel.frame = CGRect(x: 10, y: 50, width: 512, height: 100)
  72. dataLabel.text = data
  73. dataLabel.textColor = .black
  74. view.addSubview(dataLabel)
  75.  
  76. let hashLabel = UILabel()
  77. hashLabel.frame = CGRect(x: 10, y: 110, width: 512, height: 100)
  78. hashLabel.text = hash
  79. if mineHash == true{hashLabel.textColor = .green}else{hashLabel.textColor = .red}
  80. view.addSubview(hashLabel)
  81.  
  82. let nonceLabel = UILabel()
  83. nonceLabel.frame = CGRect(x: 10, y: 80, width: 512, height: 100)
  84. nonceLabel.text = String(nonce)
  85. nonceLabel.textColor = .blue
  86. view.addSubview(nonceLabel)
  87.  
  88. print(hash)
  89. print(nonce)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top