Skip to content

Commit

Permalink
Refactors: Try to communicate with Ethereum network
Browse files Browse the repository at this point in the history
  • Loading branch information
raihan2006i committed Jul 6, 2020
1 parent 7ed0ced commit 7faf603
Show file tree
Hide file tree
Showing 11 changed files with 310 additions and 244 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
# blockchain2-demo
blockchain2 demo

This repository contains a PoC of Anchor APIs using the Quorum blockchain network.

* API documentation can be found here [Swagger](anchor_api/swagger.yaml)
The actual implementation is designed using Node.js ecosystem.
The web service will expose operations in which, an anchor defined as JSON, will be stored, updated, fetched or removed.
All operations are executed over the Quorum blockchain network using a smart contract.
The smart contract are intended to be implemented using Solidity and Truffle framework [47]. U
sing the demo Anchor API, one can store a data anchor (generating a proof of existence receipt) to prove the existence of some data at some point in time using the Quorum blockchain.
Following operations will be exposed by the PoC

* POST /data_anchors - Create a new data anchor
* GET - /data_anchors /{anchorId} - Get an anchor by its identifier
* PUT - /data_anchors /{anchorId} - Update an anchor by its identifier

5 changes: 3 additions & 2 deletions anchor_api/.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
WEB3_PROVIDER=http://3.10.195.154:8545
WEB3_ACCOUNT=0x66d66805E29EaB59901f8B7c4CAE0E38aF31cb0e
WEB3_PROVIDER=http://18.132.141.64:8545
WEB3_ACCOUNT=0x66d66805E29EaB59901f8B7c4CAE0E38aF31cb0e
WEB3_PASSWORD=
3 changes: 3 additions & 0 deletions anchor_api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```
helm install --set env.web3_provider=http://172.20.123.13:8545 --set env.web3_account=0x66d66805E29EaB59901f8B7c4CAE0E38aF31cb0e anchor-api helm-charts/
```
2 changes: 1 addition & 1 deletion anchor_api/build/contracts/DataAnchor.json
Original file line number Diff line number Diff line change
Expand Up @@ -1736,7 +1736,7 @@
},
"networks": {},
"schemaVersion": "3.2.0",
"updatedAt": "2020-06-29T16:03:04.665Z",
"updatedAt": "2020-07-06T11:13:56.783Z",
"devdoc": {
"methods": {}
},
Expand Down
372 changes: 182 additions & 190 deletions anchor_api/build/contracts/Migrations.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion anchor_api/config/custom-environment-variables.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"web3": {
"provider": "WEB3_PROVIDER",
"account": "WEB3_ACCOUNT"
"account": "WEB3_ACCOUNT",
"password": "WEB3_PASSWORD"
}
}
110 changes: 84 additions & 26 deletions anchor_api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const express = require('express')
, account = config.get('web3.account');

DataAnchor.setProvider(web3.currentProvider);
DataAnchor.autoGas = true;
DataAnchor.defaults({
from: account
})

// adding Helmet to enhance your API's security
app.use(helmet());
Expand Down Expand Up @@ -47,38 +51,92 @@ app.get('/', (req, res) => {
});

app.post('/data_anchors', async (req, res) => {
const instance = await DataAnchor.new(req.body.name, req.body.hash, JSON.stringify(req.body.metadata), {from: account});
const data = await instance.getData.call();
await res.json({
anchorId: data[0],
name: data[1],
hash: data[2],
metadata: JSON.parse(data[3]),
});

try {
let unlocked = false;

if (config.get('web3.password') && !unlocked) {
console.log('>> Unlocking account ' + account + ' with password ' + config.get('web3.password'));
unlocked = await web3.eth.personal.unlockAccount(account, config.get('web3.password'), 36000);
}

const instance = await DataAnchor.new(req.body.name, req.body.hash, JSON.stringify(req.body.metadata), {from: account});
const data = await instance.getData.call();

if (config.get('web3.password') && unlocked) {
console.log('>> Locking account ' + account);
unlocked = await web3.eth.personal.lockAccount(account);
}

await res.json({
anchorId: data[0],
name: data[1],
hash: data[2],
metadata: JSON.parse(data[3]),
});
} catch (error) {
console.log(error)
res.json({});
}
});

app.get('/data_anchors/:anchorId', async (req, res) => {
const instance = await DataAnchor.at(req.params.anchorId);
const data = await instance.getData.call();
await res.json({
anchorId: data[0],
name: data[1],
hash: data[2],
metadata: JSON.parse(data[3]),
});
try {
let unlocked = false;

if (config.get('web3.password') && !unlocked) {
console.log('>> Unlocking account ' + account + ' with password ' + config.get('web3.password'));
unlocked = await web3.eth.personal.unlockAccount(account, config.get('web3.password'), 36000);
}

const instance = await DataAnchor.at(req.params.anchorId);
const data = await instance.getData.call();

if (config.get('web3.password') && unlocked) {
console.log('>> Locking account ' + account);
unlocked = await web3.eth.personal.lockAccount(account);
}

await res.json({
anchorId: data[0],
name: data[1],
hash: data[2],
metadata: JSON.parse(data[3]),
});
} catch (error) {
console.log(error)
res.json({});
}
});

app.put('/data_anchors/:anchorId', async (req, res) => {
console.log(req.body);
const instance = await DataAnchor.at(req.params.anchorId);
const result = await instance.setData.sendTransaction(req.body.name, JSON.stringify(req.body.metadata), {from: account});
const data = await instance.getData.call();
await res.json({
anchorId: data[0],
name: data[1],
hash: data[2],
metadata: JSON.parse(data[3]),
});
try {
let unlocked = false;

if (config.get('web3.password') && !unlocked) {
console.log('>> Unlocking account ' + account + ' with password ' + config.get('web3.password'));
unlocked = await web3.eth.personal.unlockAccount(account, config.get('web3.password'), 36000);
}

const instance = await DataAnchor.at(req.params.anchorId);
const result = await instance.setData.sendTransaction(req.body.name, JSON.stringify(req.body.metadata), {from: account});
const data = await instance.getData.call();

if (config.get('web3.password') && unlocked) {
console.log('>> Locking account ' + account);
unlocked = await web3.eth.personal.lockAccount(account);
}

await res.json({
anchorId: data[0],
name: data[1],
hash: data[2],
metadata: JSON.parse(data[3]),
});
} catch (error) {
console.log(error)
res.json({error: error});
}
});

// starting the server
Expand Down
13 changes: 13 additions & 0 deletions anchor_api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions anchor_api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"express": "^4.17.1",
"helmet": "^3.23.1",
"morgan": "^1.10.0",
"swagger-ui-express": "^4.1.4",
"truffle": "^5.1.30",
"web3": "^1.2.9"
},
Expand Down
22 changes: 2 additions & 20 deletions anchor_api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,6 @@ paths:
description: Unexpected error
schema:
$ref: '#/definitions/Error'
delete:
tags:
- data-anchoring
summary: Delete an anchor by its identifier
operationId: deleteDataAnchor
parameters:
- name: anchorId
in: path
required: true
description: The id of the anchor to delete
type: string
responses:
'204':
description: The anchor was deleted successfully.
default:
description: Unexpected error
schema:
$ref: '#/definitions/Error'
definitions:
DataAnchor:
description: 'A data anchor (generating a proof of existence receipt) allows to prove the existence of some data at some point in time.'
Expand Down Expand Up @@ -163,9 +145,9 @@ definitions:
type: string
format: date-time
description: "Time of last modification. Format should be RFC 3339"
txId:
anchorId:
type: string
description: "Identifier of the blockchain transaction where the anchoring occurred."
description: "Identifier of the anchor"
Error:
description: Error object
type: object
Expand Down
7 changes: 4 additions & 3 deletions anchor_api/truffle-config/default.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"truffle": {
"host": "3.10.195.154",
"host": "3.9.53.187",
"port": 8545,
"network_id": "*",
"gasPrice": 0,
"gas": 4500000,
"type": "quorum",
"from": "0x66d66805E29EaB59901f8B7c4CAE0E38aF31cb0e"
"type": "ethereum",
"from": "0x3852360755845889E675C4b683f3F26bf8f12aeA",
"password": "lst7upm"
}
}

0 comments on commit 7faf603

Please sign in to comment.