Skip to content

Commit

Permalink
version 1.0.0
Browse files Browse the repository at this point in the history
Co-authored-by: William Occelli <william.occelli@gmail.com>
  • Loading branch information
corentingiraud committed Jan 30, 2020
1 parent feabc27 commit 6eb8fbe
Show file tree
Hide file tree
Showing 14 changed files with 889 additions and 86 deletions.
13 changes: 7 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ WORKDIR /root

RUN apt-get update
RUN apt-get install -y software-properties-common
RUN apt-get install -y wget
RUN apt-get install -y curl
RUN apt-get install -y unzip
RUN add-apt-repository -y ppa:ethereum/ethereum
RUN apt-get update
Expand All @@ -16,19 +16,20 @@ ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64
ENV PATH $JAVA_HOME/bin:$PATH

# Install maven
RUN wget http://mirrors.ircam.fr/pub/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.zip
RUN curl http://mirrors.ircam.fr/pub/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.zip
RUN unzip apache-maven-3.6.3-bin.zip
ENV PATH /root/apache-maven-3.6.3/bin:$PATH

# Download & install Web3J
RUN wget https://github.com/web3j/web3j/releases/download/v3.5.0/web3j-3.5.0.zip
# Download & install Web3J v3.5
RUN curl https://github.com/web3j/web3j/releases/download/v3.5.0/web3j-3.5.0.zip
RUN unzip web3j-3.5.0.zip
RUN echo '#!/bin/bash' >> /bin/web3j
RUN echo '~/web3j-3.5.0/bin/web3j' >> /bin/web3j
RUN chmod +x /bin/web3j

# Install solidity
RUN apt-get install -y solc
# Install solidity v0.4.25
RUN curl -o /usr/bin/solc -fL https://github.com/ethereum/solidity/releases/download/v0.4.25/solc-static-linux \
&& chmod u+x /usr/bin/solc

# Init private blockchain called chaine1T
RUN mkdir .ethereum && cd .ethereum
Expand Down
2 changes: 1 addition & 1 deletion PROJECT-REQUIREMENTS.MD
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
- smart contracts
- Garanteed conditional execution
- stateful
- Distributed (min 2 nodes)
- (Distributed (min 2 nodes)) nop

## Livrable

Expand Down
28 changes: 28 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
- Messages de validation (success)
- gérer les messages d'erreur qui viennent de solidity
- Creation d'un Will a deux héritiers
- change wei to ether in add message
- gérer la destruction du contract (voir si on peut ne plus l'appeler)

RAPPORT
- Diagramme de classe
- Diagramme d'état

A TESTER
- Notion de temps
- Pourcentage de validation #DONE by willy
- Pas de doublon dans les héritiers #DONE by willy
- Ne pas pouvoir déclarer deux fois mort #DONE by willy

FUTURE WORK:
- Conserver la clé privée de l'utilisateur (conserver un secret dans la block chain)
- (Modifier la liste des héritiers)
- (Modifier le pourcentage d'un héritier)

Account #0: {e087b47782c968bc3e31a3b1d9196af833501260} keystore:///root/.ethereum/chaine1T/keystore/UTC--2020-01-27T14-18-03.069304925Z--e087b47782c968bc3e31a3b1d9196af833501260
Account #1: {79dcbbbe41c3b5a1e5ce9029b30478f1be308d96} keystore:///root/.ethereum/chaine1T/keystore/UTC--2020-01-27T16-46-29.621517206Z--79dcbbbe41c3b5a1e5ce9029b30478f1be308d96

Contract address: 0xf2c04eabb64d7f183e9a1100ff7550f76c0569a7


0x6493eecd972c1ba7ec0a4fdb783ae090fd55137d
7 changes: 7 additions & 0 deletions java-projects/smarties/compileAll.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
rm -rf contracts/build &&\
solc contracts/solidity/Smartie.sol --bin --abi --optimize -o contracts/build &&\
./../../web3j-3.5.0/bin/web3j solidity generate contracts/build/smartie.bin contracts/build/smartie.abi -p fr.insa.smarties -o src/main/java &&\
chmod 777 src/main/java/fr/insa/smarties/Smartie.java &&\
mvn clean install

# java -cp ./target/smarties-1.0-SNAPSHOT-jar-with-dependencies.jar fr.insa.smarties.Launcher '0xe087b47782c968bc3e31a3b1d9196af833501260'
112 changes: 103 additions & 9 deletions java-projects/smarties/contracts/solidity/Smartie.sol
Original file line number Diff line number Diff line change
@@ -1,30 +1,124 @@
pragma solidity ^0.6.1;
pragma solidity 0.4.25;
// pragma experimental ABIEncoderV2;

contract smartie {
struct Heir {
address addr;
uint percentage;
}

address payable owner;
Heir public heir;
address owner;
Heir[] public heirs;
address[] heirsThatDeclaredDead;
uint countClaimPercentage;
uint256 inheritanceValue;
bool public isDead;
uint public deathDate;

constructor(address _address) public {
event LogCoinsSent(address sentTo, uint amount);
event LogDeath(bool isDead, uint nbOfValid);

constructor(address[] memory _heirsAddr, uint[] memory _heirsPercentage) public payable {
require(msg.value > 1, 'Not enough wei added to contract');
// Check if the owner declares one or more heir(s)
uint addrLength = _heirsAddr.length;
uint percLength = _heirsPercentage.length;
assert(addrLength > 0 && percLength > 0 && addrLength == percLength);

// Check if the owner declares a total percentage equal to 100%
uint totalPercentage = 0;
for (uint i = 0; i < percLength; i++){
// Check duplicates in heirs
for (uint j = 0; j < heirs.length; j++){
require(heirs[j].addr != _heirsAddr[i], 'Error, the heir has already been declared in the will');
}
Heir memory heir = Heir(_heirsAddr[i],_heirsPercentage[i]);
heirs.push(heir);
totalPercentage += _heirsPercentage[i];
}
require(totalPercentage == 100, 'Total percentage not equal to 100');
owner = msg.sender;
heir.addr = _address;
isDead = false;
countClaimPercentage = 0;
// Set funds
inheritanceValue = msg.value;
}

function isDeadSaMere() public {
if (msg.sender == heir.addr) {
isDead = true;
function declareDead() public returns (string memory) {
// Check duplicate declarations
for (uint k = 0; k < heirsThatDeclaredDead.length; k++){
require(heirsThatDeclaredDead[k] != msg.sender, 'Error, the heir has already declared the owner dead');
}
for (uint i = 0; i < heirs.length; i++){
// Check if heir is legitimate
if(heirs[i].addr == msg.sender) {
countClaimPercentage += heirs[i].percentage;
heirsThatDeclaredDead.push(heirs[i].addr);
if (countClaimPercentage >= 80) {
isDead = true;
deathDate = block.timestamp;
return 'The owner is officially declared dead, a 7 days latency is needed to claim the will';
}
return 'The heir officially declared the owner dead. Other heirs will need to declare the owner dead.';
}
}
}

function claimWill() public {
require(isDead, 'The owner is not officially dead. Heirs representing at least 80% of the inheritance need to declare the owner dead');
require((deathDate) <= block.timestamp, 'The latency needed for the will to be claimed is not yet reached, you will have to wait');
executeContract();
}

function addWeiToInheritance() public payable returns(uint256) {
require(msg.sender == owner, 'Only the owner of the contract can add ether');
require(msg.value >= 1, 'Not enough wei provided');
inheritanceValue += msg.value;
return address(this).balance;
}

function removeWeiFromInheritance(uint256 valueToSubstract) public returns(uint256){
require(msg.sender == owner, 'Only the owner of the contract can add ether');
require(inheritanceValue >= valueToSubstract, 'Not enough wei in the contract');
inheritanceValue -= valueToSubstract;
owner.transfer(valueToSubstract);
return address(this).balance;
}

function displayInheritence() public view returns (uint){
return address(this).balance;
}

function deleteWill() public {
function deleteContract() public {
if (msg.sender == owner) {
selfdestruct(owner);
}
}

function executeContract() public {
uint amount;
for (uint i = 0; i < heirs.length; i++) {
amount = inheritanceValue * heirs[i].percentage;
amount = amount / 100;
heirs[i].addr.transfer(amount);
emit LogCoinsSent(heirs[i].addr, amount);
}
selfdestruct(owner);
}

function getAddress() public view returns(address) {
return address(this);
}

function getNumberOfHeirsThatClaimedWill() public view returns(uint) {
return heirsThatDeclaredDead.length;
}

function getHeirsNumber() public view returns(uint) {
return heirs.length;
}

function getOwner() public view returns(address) {
return owner;
}
}
3 changes: 0 additions & 3 deletions java-projects/smarties/gethlauncher.sh

This file was deleted.

10 changes: 10 additions & 0 deletions java-projects/smarties/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
<artifactId>core</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
</dependencies>

<properties>
Expand Down
40 changes: 40 additions & 0 deletions java-projects/smarties/src/main/java/fr/insa/smarties/Account.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package fr.insa.smarties;

import org.web3j.crypto.Credentials;
import org.web3j.crypto.WalletUtils;
import org.web3j.crypto.CipherException;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.response.EthGetBalance;
import org.web3j.utils.Convert;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

public class Account {
private String _address;
private Credentials _credentials;

public Account(String accountPath, String password) throws IOException, CipherException {
this._credentials = WalletUtils.loadCredentials(password, accountPath);
this._address = this._credentials.getAddress();
}

public String getBalance() throws InterruptedException, ExecutionException {
EthGetBalance balanceWei = Launcher.web3
.ethGetBalance(this._address, DefaultBlockParameterName.LATEST)
.sendAsync()
.get();
BigDecimal balanceInEther = Convert.fromWei(balanceWei.getBalance().toString(), Convert.Unit.ETHER);
return balanceInEther.toString();
}

public String getAddress() {
return this._address;
}

public Credentials getCredentials() {
return this._credentials;
}
}
30 changes: 30 additions & 0 deletions java-projects/smarties/src/main/java/fr/insa/smarties/Heir.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package fr.insa.smarties;

import java.math.BigInteger;

public class Heir {

private String _address;
private int _percentage;

public Heir (String address, int percentage) {
this._address = address;
this._percentage = percentage;
}

public int getPercentage () {
return this._percentage;
}

public void setPercentage (int percentage) {
this._percentage = percentage;
}

public String getAddress() {
return this._address;
}

public void setAddress(String address) {
this._address = address;
}
}
Loading

0 comments on commit 6eb8fbe

Please sign in to comment.