· 6 years ago · Mar 27, 2020, 11:08 AM
1# Database in Docker:
2MongoDB with Mongo-express as GUI
3Start: `docker-compose up`
4
5# JAVA Backend - Spring Boot
6Start: `java -jar spring-server.jar`
7
8Runs at `http://localhost:8085` and connects to what is set as RPC provider in: `fiscalization-case-backend/spring-server/src/main/java/io/swagger/api/HashAllAndStoreApiController.java`
9(currently: `https://mainnet.infura.io/v3/d1a6f114c31d4e2cb5cd238907f207ea` - ethereum mainnet ! Used to be `http://46.105.108.175/vmganache/`, , replace with some other ganache instance if necessary, but don't forget to deploy the contract there.)
10
11Base account: `0x4c904eaD406c345208038Eae4f4C7516a3AB0609`,
12Key: `6a4fce36461fee3d24a10461e6673339144b6eb2ed51eb04a8ec7ecec9ea6b63`
13Using Mnemonic: `leisure floor knee senior virtual wrong tank orange goat lunar torch choice`
14`Kassen.sol` contract deployed at `0x8b0e6460919b6d7551aa2e3db9bc11162c75bd8b`
15
16REST API:
17
18## 0. Add Data to a data table
19
20`POST /addPurchase/`
21
22```
23{
24 name: "Some Product",
25 price: "20.32"
26}
27```
28
29## 1. Edit Data (for simulating the case that someone manipulates the accounting)
30
31`PUT /editPurchase/`
32
33```
34{
35 _id: "32",
36 name: "The new Name"
37 price: "0.32" // the new price
38}
39```
40
41## 2. Delete Data
42
43`DELETE /delete/:id` // pragmatic delete by ID
44
45## 3. Get all purchases from Database
46
47`GET /getAllPurchases/` // get all table entries
48
49returns:
50
51```
52[
53 {
54 "_id": "id1",
55 "name": "name1",
56 "price": 10.0
57 },
58 {
59 "_id" : "id2",
60 "name": "name2",
61 "price": 20.0
62 }
63]
64```
65
66## 4. Hash Data from DB table and store hash on chain
67
68`GET /hashAllAndStore/`
69
70returns:
71```
72{
73 dbHash: "0x12345", // hash of the DB table
74 txID: "0x67890" // transaction ID
75}
76```
77
78## 5. hash all data and compare - hashes DB table and compares hash with the one in smart contract
79
80`GET /hashAllAndCompare/`
81
82returns:
83```
84{
85 valid: "true" // or false
86}
87```
88
89
90# Ethereum connector library
91
92The basis is a simple Java library that connects to the Ethereum Node available at a provided url
93and interacts with blockchain by sending transaction or querying the GK's `Kassen.sol` contract state.
94
95## JAR
96
97The current library version is available and packed in `ethtest-0.0.2-jar-with-dependencies.jar`. It is ready to be imported in a new project. It's also integrated in the Spring Boot Application.
98
99
100## Basic access
101
102```
103[...]
104import org.web3j.protocol.Web3j;
105import eth.EthereumService;
106
107public class Example {
108
109 public static void main(String[] args) throws TransactionException, Exception {
110 EthereumService service = new EthereumService("http://46.105.108.175/vmganache/",
111 "PRIV_KEY_HERE");
112 Web3j web3 = service.getWeb3();
113 Web3ClientVersion web3ClientVersion = web3.web3ClientVersion().sendAsync().get();
114 String clientVersion = web3ClientVersion.getWeb3ClientVersion();
115 System.out.println(clientVersion);
116 }
117}
118```
119The url http://46.105.108.175:80/vmganache/ is the JSON RPC url of a running Ethereum Node.
120
121## Setup
122
123To interact with the GKSoftware `Kassen.sol` smart contract, an address needs to be provided first,
124whether in the constructor or with a setter method.
125
126```
127service.setContractAddress("0x1a6c89764ef822a4b107d134c848b2462ab1b745");
128
129```
130
131## Customized methods
132
133For ease of the use, there are 2 methods customized for Kassen.sol contract.
134
135- `storeHash` creates a transaction, given the day and the data to be hashed
136
137```
138TransactionReceipt contractReceipt2 = service.storeHash(
139 4,
140 "foo");
141```
142
143- `verify` compares the data stored on the blockchain (for the given day and address) with the provided data
144
145```
146boolean bool = service.verify(
147 "foo",
148 4,
149 "0x6392c7228fFb06447b5c2e7dead1a64e7Fbce809");
150```
151
152## Generalized methods
153
154Additionally, there are the following methods:
155
156- `sendTransaction` to send a general ETH transfering transaction
157
158 ```
159 TransactionReceipt transferReceipt = service.sendTransaction(
160 "0x54c7f5329b5Bc860c9A99C50b0501E16052A7c83",
161 BigInteger.TEN, BigInteger.valueOf(200000), BigInteger.valueOf(30000000), "0x0");
162 System.out.println(transferReceipt);
163 ```
164
165- `sendContractTransaction` to send a contract transaction
166
167 ```
168 Function function = new Function(
169 "storeHash",
170 Arrays.asList(new Uint256(20), new Bytes32(Hash.sha3("pkkkk".getBytes()))),
171 (List) new ArrayList<TypeReference>());
172 TransactionReceipt contractReceipt = service.sendContractTransaction(
173 "0x1a6c89764ef822a4b107d134c848b2462ab1b745",
174 BigInteger.ZERO, BigInteger.valueOf(200000), BigInteger.valueOf(7000000),
175 function);
176 ```
177
178.