5. The Bash in Tokyo
• MOXSSGとCocoa勉強会 関東、MOSAとの合同で開催予
定です。
• WWDCのチケットが買えず、日本残留組となった方々へ
の朗報です。The Bashモドキを東京で開催します。もち
ろん本家と違って、会場も屋内でクールなバンドもきま
せん。飲食も持ち込みとなる予定です。ですが、WWDC
をつまみに語らいましょう。
• 6/15(金)、場所は渋谷の hoops link tokyo です。
6. Learn Blockchains by Building One
The fastest way to learn how Blockchains work is to build one
https://hackernoon.com/learn-blockchains-by-building-one-117428612f46
https://github.com/dvf/blockchain
【参考】
ブロックチェーンを作ることで学ぶ 〜ブロックチェーンがどのように動いているのか学ぶ最速の方法は作ってみることだ〜
https://qiita.com/hidehiro98/items/841ece65d896aeaa8a2a
Swiftでブロックチェーンを実装してみる
https://qiita.com/shu223/items/ebe59325f36fbf25e3d6
Pythonで実装されたコードを参考サイトの情報を
利用して実装してみました。
15. ハッシュ
struct Block: Codable {
let index: Int /* 1, 2, ... */
let timestamp: Double /* Date().timeIntervalSince1970 */
let transactions: [Transaction]
let proof: Int
let previousHash: Data
func hash() -> Data {
let encoder = JSONEncoder()
let data = try! encoder.encode(self)
return data.sha256()!
}
}
struct Transaction: Codable {
let sender: String
let recipient: String
let amount: Int
}
extension Data {
// https://stackoverflow.com/questions/25388747/sha256-in-swift
func sha256() -> Data? {
guard let res = NSMutableData(length: Int(CC_SHA256_DIGEST_LENGTH)) else { return nil }
CC_SHA256((self as NSData).bytes, CC_LONG(self.count), res.mutableBytes.assumingMemoryBound(to: UInt8.self))
return res as Data
}
}
16. プルーフ・オブ・ワークアルゴリズム (PoW)
class func proofOfWork(lastProof: Int) -> Int {
var proof: Int = 0
while validProof(lastProof: lastProof, proof: proof) == false {
proof += 1
}
return proof
}
class func validProof(lastProof: Int, proof: Int) -> Bool {
let proofArray = [lastProof, proof]
let encoder = JSONEncoder()
let guess = try! encoder.encode(proofArray)
let guessHash = guess.sha256()!
return guessHash[0] == 0 && guessHash[1] == 0 && guessHash[2] == 0 && guessHash[3] == 0
}