Skip to content

Commit

Permalink
Merge pull request #10 from 42School/dev/go/eth
Browse files Browse the repository at this point in the history
[Version]: 2.4 Metrics & Skills (key/val) in smart-contract
  • Loading branch information
lpieri authored Apr 16, 2021
2 parents 20a9a1b + a958a6e commit e438dda
Show file tree
Hide file tree
Showing 33 changed files with 1,035 additions and 474 deletions.
4 changes: 2 additions & 2 deletions .env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
RUN_ENV=prod
COMPOSE_PROFILE=prod
RUN_ENV=dev
COMPOSE_PROFILES=dev
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules/
config-dev/
build/
.idea/**
config/blockchain-service.env
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ La stack choisi pour ce projet est `go`, la blockchain `ethereum` et `solidity`

Si vous trouvez des bugs sur cette version n'hésitez pas à faire une `issue`

Le smart-contract à été déployé sur Ropsten en test à cette [adresse](https://ropsten.etherscan.io/address/0x29a5c09219a5c71a81d26922d708e472677f4548) `0x29a5c09219a5c71a81d26922d708e472677f4548`
Le smart-contract à été déployé sur Ropsten en test à cette [adresse](https://ropsten.etherscan.io/address/0x524e9d84B91889E5D5d8489c24E24A89e592A1e1) `0x524e9d84B91889E5D5d8489c24E24A89e592A1e1`

La branche dev en cours est [dev/go/eth](https://github.com/42School/blockchain-service/tree/dev/go/eth).

Expand Down Expand Up @@ -45,6 +45,8 @@ Le port par défaut de l'API est `8080`.

L'API contains à ce jour 3 routes différentes:

- `/metrics` permet de récupérer des metriques pour Prometheus.

- `/create-diploma` permet de crée un nouveau diplôme dans la blockchain. ⚠️ Token requis

- Méthode `POST`
Expand Down Expand Up @@ -120,8 +122,8 @@ L'API contains à ce jour 3 routes différentes:
"Data":
{
"Hash":"",
"Level":21,
"Skills":[21.57,21.42,21.2,21.42,6.61,4.16,9.02,9.02,9.02,3.6,2.22,5.5,1.45,4.34]
"Level":21.00,
"Skills":[{"slug":"Security","level":16.42},{"slug":"Unix","level":13.87},{},{}]
}
}
```
Expand All @@ -136,7 +138,7 @@ L'API contains à ce jour 3 routes différentes:
[
{
"Level": 2100,
"Skills": [857,542,620,942,661,416,902,902,902,360,222,550,145, ...],
"Skills": [{"Slug":"Security","Level":1642},{"Slug":"Unix","Level":1387},...],
"Hash": [xxx,xx,xx,xx,xx,xxx,x,xx,xxx,xx,xxx,xx,xxx,xx,xx,xx,xxx,xx,xxx,xx, ...],
"Signature":
{
Expand All @@ -147,7 +149,7 @@ L'API contains à ce jour 3 routes différentes:
},
{
"Level": 2100,
"Skills": [857,542,620,942,661,416,902,902,902,360,222,550,145, ...],
"Skills": [{"Slug":"Security","Level":1642},{"Slug":"Unix","Level":1387},...],
"Hash": [xxx,xx,xx,xx,xx,xxx,x,xx,xxx,xx,xxx,xx,xxx,xx,xx,xx,xxx,xx,xxx,xx, ...],
"Signature":
{
Expand Down
49 changes: 38 additions & 11 deletions contracts/FtDiploma.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ contract FtDiploma {
string public constant name = "42 Alumni";
string public constant symbol = "42A";
string public constant linkOfRepo = "github.com/42School/blockchain-service";
address public constant ftPubAddress = ;
address public constant ftPubAddress = 0x7e12234E994384A757E2689aDdB2A463ccD3B47d;

event Publish42Diploma(address ftPubAddress, string _link);
event CreateDiploma(bytes32 student);
Expand All @@ -21,9 +21,21 @@ contract FtDiploma {
bytes32 s;
}

struct Skill {
string name;
uint64 level;
}

struct Diplomas {
uint64 level;
Skill[30] skills;
bytes32 hash;
Sign signature;
}

struct Diploma {
uint64 level;
uint64[30] skills;
mapping(uint64 => Skill) skills;
bytes32 hash;
Sign signature;
}
Expand All @@ -35,26 +47,41 @@ contract FtDiploma {
emit Publish42Diploma(ftPubAddress, linkOfRepo);
}

function createDiploma(uint64 _level, uint64[30] memory _skills, uint8 _v, bytes32 _r, bytes32 _s, bytes32 _studentHash) public {
function createDiploma(uint64 _level, uint64[30] memory _skillLevel, string[30] memory _skillSlug, uint8 _v, bytes32 _r, bytes32 _s, bytes32 _studentHash) public {
require(ecrecover(_studentHash, _v, _r, _s) == ftPubAddress, "FtDiploma: Is not 42 sign this diploma.");
require(hashToDiploma[_studentHash].level == 0, "FtDiploma: The diploma already exists.");
hashToDiploma[_studentHash] = Diploma(_level, _skills, _studentHash, Sign(_v, _r, _s));
Diploma storage dp = hashToDiploma[_studentHash];
dp.level = _level;
dp.hash = _studentHash;
dp.signature = Sign(_v, _r, _s);
for (uint64 i = 0; i < 30; i++) {
dp.skills[i] = Skill(_skillSlug[i], _skillLevel[i]);
}
intToHash.push(_studentHash);
emit CreateDiploma(_studentHash);
}

function getDiploma(bytes32 _studentHash) public view returns (uint64 level, uint64[30] memory skills) {
Diploma memory _getDiploma = hashToDiploma[_studentHash];
function getDiploma(bytes32 _studentHash) public view returns (uint64, Skill[] memory) {
Diploma storage _getDiploma = hashToDiploma[_studentHash];
uint64 levelDiploma = _getDiploma.level;
uint64[30] memory skillsDiploma = _getDiploma.skills;
return (levelDiploma, skillsDiploma);
Skill[] memory skills = new Skill[](30);
for (uint64 i = 0; i < 30; i++) {
skills[i] = _getDiploma.skills[i];
}
return (levelDiploma, skills);
}

function getAllDiploma() public view returns (Diploma[] memory) {
function getAllDiploma() public view returns (Diplomas[] memory) {
require(msg.sender == ftPubAddress, "FtDiploma: Is not 42.");
Diploma[] memory diplomas = new Diploma[](intToHash.length);
Diplomas[] memory diplomas = new Diplomas[](intToHash.length);
for (uint256 i = 0; i < intToHash.length; i++) {
diplomas[i] = hashToDiploma[intToHash[i]];
Diploma storage dp = hashToDiploma[intToHash[i]];
diplomas[i].level = dp.level;
for (uint64 y = 0; y < 30; y++) {
diplomas[i].skills[y] = dp.skills[y];
}
diplomas[i].hash = dp.hash;
diplomas[i].signature = dp.signature;
}
return (diplomas);
}
Expand Down
35 changes: 32 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,44 @@ services:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example

# eth-client:
# image: ethereum/client-go:latest
# hostname: geth
# ports:
# - "30303:30303"
# expose:
# - "8545"
# - "8546"
# - "6060"
# - "8080"
# networks:
# - back
# volumes:
# - ~/Library/Ethereum:/root/.ethereum/
# command:
# - --ropsten
## - --light.serve=20
# - --syncmode
# - "fast"
## - --rpc
# - --ws
# - --ws.port=8546
# - --ws.addr=0.0.0.0
# - --ws.api
# - admin,debug,web3,eth,txpool,personal,ethash,net
# - --ws.origins="*"
# - --metrics
# - --metrics.addr=0.0.0.0
# - --metrics.port=6060

app:
image: blockchain-service
ports:
- 8080:8080
networks:
- back
env_file:
- config/blockchain-service.env
- config-dev/blockchain-service.env
environment:
RUN_ENV: "${RUN_ENV}"
depends_on:
Expand All @@ -46,5 +76,4 @@ services:
ME_CONFIG_MONGODB_ADMINPASSWORD: example
depends_on:
- mongo
profiles:
- dev
profiles: ["dev"]
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ require (
github.com/ethereum/go-ethereum v1.9.25
github.com/google/uuid v1.2.0
github.com/gorilla/mux v1.8.0
github.com/prometheus/client_golang v1.10.0
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.0
go.mongodb.org/mongo-driver v1.4.5
golang.org/x/oauth2 v0.0.0-20210126194326-f9ce19ea3013
)
Loading

0 comments on commit e438dda

Please sign in to comment.