在 iOS 中实现区块链 OC版本

作者: IT | 来源:发表于2018-02-02 16:35 被阅读595次

参照网上的 swift 版写了个OC版本,欢迎交流
刚搞了个QYB地址,木有BB,有木有大神打赏点玩玩
QfPddnv9jLtMa5Qt4nK1g3kveiFdmSMJBB

//
//  ViewController.m
//  BlockChainDemo
//
//  Created by Couchsurfing on 2018/2/2.
//  Copyright © 2018年 Couchsurfing. All rights reserved.
//

#import "ViewController.h"
#import "HaokBlock.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    HaokBlockChain *blockchain = [[HaokBlockChain alloc]init];
    
    for (int i = 0; i < 1024; i++) {
        HaokBlock *block =  [[HaokBlock alloc]init];
        block.data = [NSString stringWithFormat:@"%d",i];
        [blockchain addBlock:block];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
//
//  HaokBlock.h
//  BlockChainDemo
//
//  Created by Couchsurfing on 2018/2/2.
//  Copyright © 2018年 Couchsurfing. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface HaokBlock : NSObject

@property (nonatomic, assign) int index;
@property (nonatomic,copy) NSString *dateCreated;
@property (nonatomic, copy) NSString *previousHash;
@property (nonatomic, copy) NSString *blockHash;
@property (nonatomic, assign) int nonce;
@property (nonatomic, copy) NSString *data;
@property (nonatomic, copy,readonly) NSString *key;

@end


@interface HaokBlockChain : NSObject

@property (nonatomic, strong) NSMutableArray<HaokBlock *> *blocks;

- (void)addBlock:(HaokBlock *)block;

@end
//
//  HaokBlock.m
//  BlockChainDemo
//
//  Created by Couchsurfing on 2018/2/2.
//  Copyright © 2018年 Couchsurfing. All rights reserved.
//

#import "HaokBlock.h"
#import <CommonCrypto/CommonDigest.h>

@implementation HaokBlock

- (NSString *)key{
    return [NSString stringWithFormat:
            @"%d%@%@%@%d",self.index,self.dateCreated,self.previousHash,self.data,self.nonce];
}

- (instancetype)init{
    if (self = [super init]) {
        self.dateCreated = [self dateDescription];
        self.nonce = 0;
        self.data = @"";
    }
    return self;
}

- (NSString *)dateDescription{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
   return [formatter stringFromDate:[NSDate date]];
}

@end


@implementation HaokBlockChain

- (void)addBlock:(HaokBlock *)block{
    if (_blocks == nil) {
        // 添加创世区块
        // 第一个区块没有 previous hash
        block.previousHash = @"0";
        block.blockHash = [self generateHashForblock:block];
    }else{
        HaokBlock *previousBlock = [self getPreviousBlock];
        block.previousHash = previousBlock.blockHash;
        block.index = @(self.blocks.count).intValue;
        block.blockHash = [self generateHashForblock:block];
    }
    [self.blocks addObject:block];
    [self displayBlock:block];
}

- (HaokBlock *)getPreviousBlock{
    return self.blocks[self.blocks.count - 1];
}

- (void)displayBlock:(HaokBlock *)block{
    NSLog(@"%@", [NSString stringWithFormat:@"------ 第 (%d) 个区块 --------",block.index]);
    NSLog(@"%@", [NSString stringWithFormat:@"创建日期:%@",block.dateCreated]);
    NSLog(@"%@", [NSString stringWithFormat:@"数据:%@",block.data]);
    NSLog(@"%@", [NSString stringWithFormat:@"Nonce:%d",block.nonce]);
    NSLog(@"%@", [NSString stringWithFormat:@"前一个区块的哈希值:%@",block.previousHash]);
    NSLog(@"%@", [NSString stringWithFormat:@"哈希值:%@",block.blockHash]);
}

- (NSString *)generateHashForblock:(HaokBlock *)block{
    NSString *hash = [self sha1Hash:block.key];
    while (![hash hasPrefix:@"00"]) {
        block.nonce += 1;
        hash = [self sha1Hash:block.key];
    }
    return hash;
}

//sha1加密方式
- (NSString *)sha1Hash:(NSString *)input
{
    const char *cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithBytes:cstr length:input.length];
    
    uint8_t digest[CC_SHA1_DIGEST_LENGTH];
    CC_SHA1(data.bytes, (unsigned int)data.length, digest);
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
    
    for(int i=0; i<CC_SHA1_DIGEST_LENGTH; i++) {
        [output appendFormat:@"%02x", digest[i]];
    }
    return output;
}

- (NSMutableArray<HaokBlock *> *)blocks{
    if (_blocks == nil) {
        _blocks = [NSMutableArray array];
    }
    return _blocks;
}

@end

相关文章

  • 在 iOS 中实现区块链

    在 iOS 中实现区块链 在 iOS 中实现区块链

  • 在 iOS 中实现区块链 OC版本

    参照网上的 swift 版写了个OC版本,欢迎交流刚搞了个QYB地址,木有BB,有木有大神打赏点玩玩QfPddnv...

  • 区块链开发:持久化 #C12

    在之前的版本中,我们已经实现了区块链的基本模型,包括区块链的基本结构、不同的共识机制与简易的P2P网络。但是仍有一...

  • iOS web交互

    iOS极简模式实现Webview网页图片原生预览 IOS中 使用JavaScriptCore 实现OC与JS的交互...

  • UIControl扩大响应区域

    OC版本 声明 实现 Swift 版本

  • 2020-12-03

    Filecoin区块链与区块的详细基础知识 Filecoin区块链是一个分布式虚拟机,在Filecoin协议中实现...

  • 浅谈iOS区块链项目的架构设计

    浅谈iOS区块链项目的架构设计 浅谈iOS区块链项目的架构设计

  • MIXIN下载指南

    Xin是安卓和IOS双版本 欢迎大家向不懂区块链,但又想了解区块链的人转载,比如你父母,亲戚朋友之类的,Xin是一...

  • 比特币原理第七讲-节点通讯

    引言 区块链网络 节点角色 网络简化 实现 场景 版本 getblocks inv getdata block 和...

  • 区块链的应用场景

    2018年8月10日,全国首张区块链电子发票在深圳实现落地。通过区块链电子发票,企业可以在区块链上实现发票申领和报...

网友评论

    本文标题:在 iOS 中实现区块链 OC版本

    本文链接:https://www.haomeiwen.com/subject/rdeezxtx.html