Skip to content
This repository was archived by the owner on Jul 25, 2020. It is now read-only.

Commit da73e64

Browse files
authored
Merge pull request #5 from osmose-collective/update_blockchainlib_docker
fix: fixed blockchain library to use latest available ARK API
2 parents 1a87099 + 72acf84 commit da73e64

File tree

12 files changed

+960
-617
lines changed

12 files changed

+960
-617
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,5 @@ typings/
7373
.serverless
7474

7575
.vscode
76+
77+
docker/data

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
11.15

docker/.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
npm-debug.log

docker/Dockerfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
FROM node:11-alpine
2+
3+
# Create app directory
4+
WORKDIR /home/node/faucet
5+
6+
# Install app dependencies
7+
# A wildcard is used to ensure both package.json AND package-lock.json are copied
8+
# where available (npm@5+)
9+
COPY package*.json ./
10+
11+
RUN apk add --no-cache --virtual .build-deps make gcc g++ python git bash \
12+
&& npm ci --only=production
13+
14+
COPY . /home/node/faucet
15+
16+
USER node
17+
18+
EXPOSE 8080
19+
20+
CMD [ "node", "main.js"]

docker/docker-compose-build.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
version: '2'
2+
services:
3+
mongodb:
4+
image: "mongo:latest"
5+
container_name: mongodb-faucet
6+
environment:
7+
- MONGO_DATA_DIR=/data/db
8+
- MONGO_LOG_DIR=/dev/null
9+
volumes:
10+
- ./data/db:/data/db
11+
restart: always
12+
command: mongod --smallfiles --logpath=/dev/null # --quiet
13+
14+
faucet:
15+
build:
16+
context: ../
17+
dockerfile: docker/Dockerfile
18+
ports:
19+
- "8080:8080"

lib/dbclient.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var MongoClient = require('mongodb').MongoClient;
2-
var url = "mongodb://localhost:27017/osmose_faucet";
2+
var url = "mongodb://mongodb-faucet:27017/osmose_faucet";
33

44
module.exports = {
55
CheckAddressInDatabase: async function (address) {

lib/osmose.js

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,48 @@
1-
var arkjs = require("arkjs");
2-
var fs = require('fs');
3-
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
1+
const fs = require('fs');
2+
const Connection = require("@arkecosystem/client").Connection;
3+
const Crypto = require("@arkecosystem/crypto").Crypto;
4+
const Managers = require("@arkecosystem/crypto").Managers;
5+
const Transactions = require("@arkecosystem/crypto").Transactions;
6+
const Identities = require("@arkecosystem/crypto").Identities;
7+
8+
// Global var
9+
Managers.configManager.setConfig(
10+
JSON.parse(fs.readFileSync("networks.json", "utf8"))
11+
);
12+
13+
const client = new Connection("http://testnet.osmose.world:4003/api/v2");
414

515
module.exports = {
616
CheckAddress: function (address) {
7-
return arkjs.crypto.validateAddress(address, 115);
17+
return Identities.Address.validate(address, 65);
818
},
9-
SendOSM: function (address) {
10-
let wallet = JSON.parse(fs.readFileSync('config.json', 'utf8')).faucetWallet;
11-
let nethash = "fa976091894eee4cad258bdae4e3323d0768c4f8610e471237408ac4aa0a92d0";
12-
let sender = arkjs.crypto.getKeys(wallet);
13-
let amountSatoshi = 1000 * Math.pow(10, 8);
19+
SendOSM: async function (address) {
20+
const walletPassphrase = JSON.parse(fs.readFileSync('config.json', 'utf8')).faucetWallet.normalize('NFD');
21+
const sender = Identities.Address.fromPublicKey(Identities.Keys.fromPassphrase(walletPassphrase).publicKey, 65);
22+
const amountoOSMtoshi = 100 * Math.pow(10, 8);
1423

15-
let transaction = arkjs.transaction.createTransaction(address, amountSatoshi, "Courtesy of our faucet", sender, undefined, 115);
24+
const data = "Courtesy from OSMOSE Faucet"
1625

17-
let transactions = {
18-
transactions: [transaction]
19-
}
26+
let transaction = Transactions.BuilderFactory
27+
.transfer()
28+
.amount(`${amountoOSMtoshi}`)
29+
.recipientId(address)
30+
.network(65)
31+
.vendorField(data)
32+
.sign(walletPassphrase);
2033

21-
let http = new XMLHttpRequest();
22-
let url = 'http://blockchain.osmose.world:4100/peer/transactions';
23-
http.open('POST', url, true);
24-
http.setRequestHeader("nethash", nethash);
25-
http.setRequestHeader("version", "1.6.0");
26-
http.setRequestHeader("port", "1");
27-
28-
//Send the proper header information along with the request
29-
http.setRequestHeader("Content-Type", "application/json");
30-
31-
http.onreadystatechange = function () {
32-
if (http.readyState === 4 && http.status === 200) {
33-
console.log(new Date() + " | 1000 OSM sent from : " + arkjs.crypto.getAddress(sender.publicKey, 115) + " --> " + address);
34+
const response = await client.api("transactions").create({ transactions: [transaction.getStruct()] });
35+
36+
if (response.status === 200) {
37+
if(response.body.errors)
38+
console.log('Error during transaction, please check if there is a valid passphrase in config.json file');
39+
else
40+
{
41+
console.log(new Date() + " | 100 OSM sent from : " + sender + " --> " + address);
42+
return true;
3443
}
35-
};
44+
}
3645

37-
http.send(JSON.stringify(transactions));
46+
return false;
3847
}
3948
};

main.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
var express = require('express');
2-
var bodyParser = require('body-parser');
3-
var osmose = require('./lib/osmose');
4-
var dbclient = require('./lib/dbclient');
1+
const express = require('express');
2+
const bodyParser = require('body-parser');
3+
const osmose = require('./lib/osmose');
4+
const dbclient = require('./lib/dbclient');
5+
6+
const PORT = 8080;
7+
const HOST = '0.0.0.0';
58

69
var app = express();
710
app.use(express.static('public'));
@@ -16,6 +19,7 @@ app.get('/', function(req, res) {
1619
addressError: false,
1720
delayError: false,
1821
delayAddressError: false,
22+
error: false,
1923
success: false
2024
}
2125
};
@@ -37,9 +41,12 @@ app.post('/', async function(req, res) {
3741
if(!param.formData.addressError) {
3842
if(await dbclient.CheckAddressInDatabase(address)) {
3943
if(await dbclient.CanSend()) {
40-
osmose.SendOSM(address);
41-
dbclient.AddAddressToDatabase(address);
42-
param.formData.success = true;
44+
if(await osmose.SendOSM(address)) {
45+
dbclient.AddAddressToDatabase(address);
46+
param.formData.success = true;
47+
}
48+
else
49+
param.formData.error = true;
4350
}
4451
else {
4552
param.formData.delayError = true;
@@ -52,4 +59,5 @@ app.post('/', async function(req, res) {
5259
res.render('index.ejs', param);
5360
});
5461

55-
app.listen(8080);
62+
app.listen(PORT, HOST);
63+
console.log(`Running on http://${HOST}:${PORT}`);

networks.json

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"network": {
3+
"name": "osmose",
4+
"messagePrefix": "OSMOSE message:\n",
5+
"bip32": {
6+
"public": 70617039,
7+
"private": 70615956
8+
},
9+
"pubKeyHash": 65,
10+
"nethash": "2ff10d167c01e661eef477b55a4915cf881e924076f63f7f005e6f10a4383b53",
11+
"wif": 129,
12+
"slip44": 1,
13+
"aip20": 0,
14+
"client": {
15+
"token": "OSMOSE",
16+
"symbol": "OSM",
17+
"explorer": "http://testnet.osmose.world"
18+
}
19+
},
20+
"milestones": [
21+
{
22+
"height": 1,
23+
"reward": 0,
24+
"activeDelegates": 42,
25+
"blocktime": 8,
26+
"block": {
27+
"version": 0,
28+
"maxTransactions": 150,
29+
"maxPayload": 6291456
30+
},
31+
"epoch": "2019-05-15T12:16:01.019Z",
32+
"fees": {
33+
"staticFees": {
34+
"transfer": 1000000,
35+
"secondSignature": 500000000,
36+
"delegateRegistration": 5000000000,
37+
"vote": 100000000,
38+
"multiSignature": 500000000,
39+
"ipfs": 500000000,
40+
"timelockTransfer": 0,
41+
"multiPayment": 0,
42+
"delegateResignation": 2500000000
43+
}
44+
},
45+
"vendorFieldLength": 255
46+
},
47+
{
48+
"height": 10000,
49+
"reward": 100000000
50+
}
51+
],
52+
"exceptions": {
53+
"blocks": [],
54+
"transactions": []
55+
}
56+
}

0 commit comments

Comments
 (0)