大家好,今天我将介绍一个IPFS实现数据库的用例。介于回收行业面临着严峻的挑战——回收材料的数量不一致,难以估计,我们用区块链来解决这个问题。
应用场景
消费者A想回收他的笔记本电脑、智能手机或手表等等。A使用他智能手机上的NFC扫描消费的产品,然后去我们的智能垃圾桶。我们的智能垃圾桶扫描这个NFC标签,给A发送一些MIOTA作为回收的奖励。
下面将详细解释图表
工作原理
假设一个笔记本电脑工厂有兴趣回收用户消费后的笔记本电脑。
工厂将把所有的笔记本电脑数据和id发送到我们的系统,以保存在我们的系统上。(id将是访问每台笔记本电脑的关键)
所有笔记本电脑都将以如下状态被保存:未找到,由用户A找到,在垃圾桶B中找到
数据库将是这样的↓↓↓
所有笔记本电脑将有NFC标签来编写其ID。
现在消费者将扫描他想回收的笔记本电脑。扫描笔记本后,API调用会将使用者设置为回收笔记本的所有者。
使用手机扫描产品
当消费者把他的笔记本电脑扔进我们的垃圾箱,他就会得到代币作为奖励。
智能垃圾扫描产品,并验证其所有者,给予代币。
API的实现
我想设计的API看起来会像这样:
每个API端点的工作原理: ‘init’get-call来初始化MAM根,并准备使用数据库。 路径:
app.route(‘/init’)
.get(recycler.init);
控制器:
exports.init = async (req,res) =>{
const response = await manageClients.init()
res.json('DB initialized with some clients with root',response )}
实现:(将初始化MAM根,并添加客户端虚拟数字 ) const init = async()=>
{products['HELLODRAYMANRNFELVTJREBCXJETCFEUGXBZHEGHCJHIYIFPQEQGDXILJZYUQMOYSELXIG9KUYOSYBFAY'] = {
'name':'product01',
'address':'HELLODRAYMANRNFELVTJREBCXJETCFEUGXBZHEGHCJHIYIFPQEQGDXILJZYUQMOYSELXIG9KUYOSYBFAY',
'owner':false,
'producer':'client01'
}
products['AB9CCUACOSC9VHQQFSVUGOVLAGNYJYDPROPBYTZIGNVOF9KMFNXBMSUGFFA9HTVHBPKPGGIBGSRJPUXZK'] = {
'name':'product02',
'address':'AB9CCUACOSC9VHQQFSVUGOVLAGNYJYDPROPBYTZIGNVOF9KMFNXBMSUGFFA9HTVHBPKPGGIBGSRJPUXZK',
'owner':false,
'producer':'client01'
}
products['LHSQELEGSRM9IAQCUBPXOLYXDMFOAPNJFS9JPOJBSRUDJAIRTWHTZCJCFLVYVRJPGPNXEWDDRGTCGKRAB'] = {
'name':'product03',
'address':'LHSQELEGSRM9IAQCUBPXOLYXDMFOAPNJFS9JPOJBSRUDJAIRTWHTZCJCFLVYVRJPGPNXEWDDRGTCGKRAB',
'owner':false,
'producer':'client01'
}
const ipfsHash = await addIPFS.execute(products)
const root = await mamManage.send(ipfsHash)
return root
}
‘addClient’post-call生产商/回收注册到我的服务。
路径:
app.route('/addClient')
.post(recycler.addClient);
控制器:
exports.addClient = async (req,res) =>{
const response = await manageClients.addNewClient(req.body.root,req.body.products)
res.json("added new client succefuly "+response)
}
实现:
const addNewClient = async (_root,_products)=>{
const currentPropretiesRoot = _root
const currentPropretiesHash = await mamManage.fetch(currentPropretiesRoot)
const currentPropretiesString = await catIPFS.execute(getLastHash(currentPropretiesHash))
const currentPropretiesJSON = JSON.parse(currentPropretiesString)
const productsJSON = JSON.parse(JSON.stringify(_products))
for(let i = 0; i < productsJSON.length;i++){
currentPropretiesJSON[productsJSON[i].address] = productsJSON[i]}
const newPropretiesHash = await addIPFS.execute(currentPropretiesJSON)
const newPropretiesRoot = await mamManage.send(newPropretiesHash)
return newPropretiesRoot}
‘addOwner’post-call将会让顾客拥有某个产品。
路径:
app.route('/addOwner')
.post(recycler.addOwner);
控制器:
exports.addOwner = async (req,res) =>{
const response = await manageClients.addNewOwner(req.body.root,req.body.productAddress,req.body.ownerAddress)
res.json("added new owner "+ response)
}
实现:
const addNewOwner = async(_root,productAddress,ownerAddress)=>{
const proprietiesHash = await mamManage.fetch(_root)
const currentPropretiesString = await catIPFS.execute(getLastHash(proprietiesHash))
const currentPropretiesJSON = JSON.parse(currentPropretiesString)
currentPropretiesJSON[productAddress].owner = ownerAddressconst newPropretiesHash = await addIPFS.execute(currentPropretiesJSON)
const root = await mamManage.send(newPropretiesHash)
return root
}
‘giveReward’post-call将发送奖励给产品的所有者。 路径:
app.route('/giveReward').post(recycler.giveReward);
控制器:
exports.giveReward = async (req,res) =>{const response = await giveReward.execute(req.body.root,req.body.productAddress)res.json(response)}
实现:
const sendToken = require('./sendToken')
const fetchToMap = require('./fetchToMap')
giveRewards = async(root,productAddress) =>{
const OwnerAddrress = await fetchToMap.execute(root)
console.log(OwnerAddrress[productAddress].owner)
await sendToken.execute(OwnerAddrress[productAddress].owner,1)
return "Send 1 i. Congrats"
}
演示现在,我为消费者和智能垃圾制作了一个android应用程序。这是它的演示(短片,时长1:16)。https://ipfser.org/wp-content/uploads/2020/02/2020021102572033.mp4?_=1
你可以在以下库中找到android应用和API:https://github.com/yehia67/Recycling-System-IOTA
【IPFS原力区】
价值观:价值 共建 共享 荣耀
总部位于上海,聚集基于分布式网络&存储的众多技术大咖和爱好者,深耕基于 IPFS 的商业生态建设和社区发展。
【原力开放日】
周二举办,聚集了众多技术大咖和 IPFS 爱好者,通过持续输出全面、精细、优质的IPFS咨询和技术支持,将生态中的爱好者转化为IPFS支持者和参与者,共建IPFS生态的健康发展