【IPFS + 区块链 系列】 入门篇
来源:互联网 发布:windows策略管理 编辑:程序博客网 时间:2024/05/16 08:46
区块链技术博客:http://liyuechun.org
区块链视频网站:http://www.kongyixueyuan.com
Ebay项目
基于以太坊Ethereum & IPFS的去中心化Ebay区块链项目详情链接
目录
- 1. 系列文章
- 2. 项目描述及效果展示
- 3. 阅读本文需要掌握的知识
- 4. 源码
- 5. 运行程序
- 6. 技术交流
1. 系列文章
- 【IPFS + 区块链 系列】 入门篇 - IPFS环境配置
- 【IPFS + 区块链 系列】 入门篇 - IPFS+IPNS+个人博客搭建
- 【IPFS + 区块链 系列】 入门篇 - IPFS + Ethereum (上篇)-js-ipfs-api - 数据上传到IPFS
- 【IPFS + 区块链 系列】 入门篇 - IPFS + Ethereum (中篇)-js-ipfs-api - 图片上传到IPFS以及下载
2. 项目描述及效果展示
这篇文章通过truffle unbox react
创建项目,安装ipfs-api
,将图片存储到ipfs
,将图片hash
存储到Ethereum
区块链,取数据时先从区块链读取图片hash
,再通过hash
从ipfs
读取数据,解决了区块链大数据存储成本高昂的问题。
3. 阅读本文需要掌握的知识
阅读本文需要将先学习上面的系列文章,由于本文前端使用了大量的React语法,所以建议学习一些React语法,还需要学习truffle framework。
4. 源码
其实这篇文章的内容就是上面几篇文章的综合结合体,所以在这里我将不再对代码做过多的概述。
import React, {Component} from 'react'import SimpleStorageContract from '../build/contracts/SimpleStorage.json'import getWeb3 from './utils/getWeb3'import './css/oswald.css'import './css/open-sans.css'import './css/pure-min.css'import './App.css'const ipfsAPI = require('ipfs-api');const ipfs = ipfsAPI({host: 'localhost', port: '5001', protocol: 'http'});const contract = require('truffle-contract')const simpleStorage = contract(SimpleStorageContract)let account;// Declaring this for later so we can chain functions on SimpleStorage.let contractInstance;let saveImageOnIpfs = (reader) => { return new Promise(function(resolve, reject) { const buffer = Buffer.from(reader.result); ipfs.add(buffer).then((response) => { console.log(response) resolve(response[0].hash); }).catch((err) => { console.error(err) reject(err); }) })}class App extends Component { constructor(props) { super(props) this.state = { blockChainHash: null, web3: null, address: null, imgHash: null, isWriteSuccess: false } } componentWillMount() { ipfs.swarm.peers(function(err, res) { if (err) { console.error(err); } else { // var numPeers = res.Peers === null ? 0 : res.Peers.length; // console.log("IPFS - connected to " + numPeers + " peers"); console.log(res); } }); getWeb3.then(results => { this.setState({web3: results.web3}) // Instantiate contract once web3 provided. this.instantiateContract() }).catch(() => { console.log('Error finding web3.') }) } instantiateContract = () => { simpleStorage.setProvider(this.state.web3.currentProvider); this.state.web3.eth.getAccounts((error, accounts) => { account = accounts[0]; simpleStorage.at('0x66e9bbfe244799149a9c4eb708a34ea7c9ce67e2').then((contract) => { console.log(contract.address); contractInstance = contract; this.setState({address: contractInstance.address}); return; }); }) } render() { return (<div className="App"> { this.state.address ? <h1>合约地址:{this.state.address}</h1> : <div/> } <h2>上传图片到IPFS:</h2> <div> <label id="file">Choose file to upload</label> <input type="file" ref="file" id="file" name="file" multiple="multiple"/> </div> <div> <button onClick={() => { var file = this.refs.file.files[0]; var reader = new FileReader(); // reader.readAsDataURL(file); reader.readAsArrayBuffer(file) reader.onloadend = function(e) { console.log(reader); saveImageOnIpfs(reader).then((hash) => { console.log(hash); this.setState({imgHash: hash}) }); }.bind(this); }}>将图片上传到IPFS并返回图片HASH</button> </div> { this.state.imgHash ? <div> <h2>imgHash:{this.state.imgHash}</h2> <button onClick={() => { contractInstance.set(this.state.imgHash, {from: account}).then(() => { console.log('图片的hash已经写入到区块链!'); this.setState({isWriteSuccess: true}); }) }}>将图片hash写到区块链:contractInstance.set(imgHash)</button> </div> : <div/> } { this.state.isWriteSuccess ? <div> <h1>图片的hash已经写入到区块链!</h1> <button onClick={() => { contractInstance.get({from: account}).then((data) => { console.log(data); this.setState({blockChainHash: data}); }) }}>从区块链读取图片hash:contractInstance.get()</button> </div> : <div/> } { this.state.blockChainHash ? <div> <h3>从区块链读取到的hash值:{this.state.blockChainHash}</h3> </div> : <div/> } { this.state.blockChainHash ? <div> <h2>浏览器访问:{"http://localhost:8080/ipfs/" + this.state.imgHash}</h2> <img alt="" style={{ width: 1600 }} src={"http://localhost:8080/ipfs/" + this.state.imgHash}/> </div> : <img alt=""/> } </div>); }}export default App
5. 运行程序
- 项目下载
$ git clone https://github.com/liyuechun/IPFS-Ethereum-Image.git$ cd IPFS-Ethereum-Image$ npm install
- 运行程序
$ ipfs daemon // 终端一$ npm start // 终端二
6. 技术交流
- 区块链技术交流QQ群:
348924182
- 进微信群请加微信:
liyc1215
- 「区块链部落」官方公众号
阅读全文
0 0
- 【IPFS + 区块链 系列】 入门篇
- 【IPFS + 区块链 系列】 入门篇
- 【IPFS + 区块链 系列】 入门篇
- 【IPFS + 区块链 系列】 入门篇
- 【IPFS + 区块链 系列】 入门篇
- IPFS 入门笔记
- 如何使用星际文件传输网络(IPFS)搭建区块链服务(一)
- 基于Ethereum & IPFS的去中心化Ebay区块链项目开发实战
- 区块链架构与应用(区块链入门篇)
- 【区块链系列1】区块链科普
- 区块链 入门 基础知识
- 区块链应用开发入门
- 区块链应用开发入门
- 区块链应用开发入门
- 1.如何入门区块链?
- 区块链了解及入门
- 区块链投资入门篇—骨灰版
- “区块链技术”系列交流会:区块链在…
- 学以致用-使用Spotfire创建自己的简单知识库查询系统
- 【IPFS + 区块链 系列】 入门篇
- weMosD1--12864 OLED/I2C显示
- Spring Boot 热加载
- Android沉浸式(侵入式)标题栏(状态栏)Status(一)
- 【IPFS + 区块链 系列】 入门篇
- 深度学习之卷积神经网络CNN及tensorflow代码实现示例
- 码农和程序员之间的5个关键差异
- 计组2-3章图表汇总
- Guava EventBus体验
- 049day(构造和析构函数的调用时机,递归例题(四则运算表达式求值)的学习)
- Android沉浸式(侵入式)标题栏(状态栏)Status(二)
- js运算符(是用来返回结果的)
- gradle-3.5.1-bin.zip下载