Skip to content

Commit

Permalink
Create nonce.json in configurable directory when app starting
Browse files Browse the repository at this point in the history
  • Loading branch information
mbidenaio committed Mar 4, 2021
1 parent c7d377d commit 8b6b581
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 43 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ DB_USERNAME=""
```

Then execute ```node install.js```
This will create or edit the idena/nonce.json file and add the current nonce of idena wallet and will also recreate the db table
This will recreate the db table

This can be skipped if u already setted the local nonce manually and also created the db table
This can be skipped if u already created the db table

To start the bridge backend execute this ```npm start```
Notice : start only 1 instance and do not use pm2 ( because of the checker function)
Expand Down
53 changes: 49 additions & 4 deletions idena/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,55 @@ const {
privateKeyToAddress
} = require('./script.js'),
axios = require("axios"),
fs = require('fs');
fs = require('fs'),
path = require('path');
require('dotenv').config();
const logger = require('../logger').child({component: "idena"})

const nonceDir = process.env.NONCE_DIR
const nonceFile = path.join(nonceDir || '', 'nonce.json')

async function setNonce() {
try {
const apiEpochResp = await axios.post(process.env.IDENA_PROVIDER, {
"method": "dna_epoch",
"id": 1,
"key": process.env.IDENA_API_KEY,
"params": []
})
let apiBalanceResp = await axios.post(process.env.IDENA_PROVIDER, {
"method": "dna_getBalance",
"id": 1,
"key": process.env.IDENA_API_KEY,
"params": [privateKeyToAddress(process.env.IDENA_PRIVATE_KEY)]
})
fs.writeFileSync(nonceFile, JSON.stringify({
nonce: apiBalanceResp.data.result.nonce,
epoch: apiEpochResp.data.result.epoch
}), "utf8")
const msg = "The idena local nonce has has been set"
logger.info(msg);
console.log(msg)
} catch (error) {
const msg = `Error while trying to set the idena local nonce: ${error}`
logger.error(msg);
console.error(msg)
throw error
}
}

exports.initNonce = async function () {
if (nonceDir && !fs.existsSync(nonceDir)) {
fs.mkdirSync(nonceDir, {recursive: true});
const msg = `Directory for nonce file created: ${nonceDir}`
logger.info(msg)
console.log(msg)
}
if (!fs.existsSync(nonceFile)) {
await setNonce()
}
}

exports.send = async function (address, amount) {
try {
let epoch = await getEpoch();
Expand Down Expand Up @@ -122,15 +167,15 @@ async function getEpoch() {

async function getNonce(epoch) {
try {
if (fs.existsSync("./idena/nonce.json")) {
const current = JSON.parse(fs.readFileSync('./idena/nonce.json'))
if (fs.existsSync(nonceFile)) {
const current = JSON.parse(fs.readFileSync(nonceFile))
let newEpoch = current.epoch
let newNonce = current.nonce + 1;
if (epoch > newEpoch) {
newEpoch = epoch
newNonce = 1
}
fs.writeFileSync("./idena/nonce.json", JSON.stringify({
fs.writeFileSync(nonceFile, JSON.stringify({
nonce: newNonce,
epoch: newEpoch
}), "utf8")
Expand Down
1 change: 0 additions & 1 deletion idena/nonce.json

This file was deleted.

11 changes: 7 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,16 @@ async function loopCheckSwaps() {
setTimeout(loopCheckSwaps, parseInt(process.env.CHECKING_DELAY));
}

loopCheckSwaps();

const swaps = require('./routes/swaps');
app.use(cors())
app.use(bodyParser.json());
app.use('/swaps', swaps);

async function start() {
await idena.initNonce()
loopCheckSwaps();
const port = 8000;
app.listen(port, () => logger.info(`Server started, listening on port: ${port}`));
}

var port = 8000;
app.listen(port, () => logger.info(`Server started, listening on port: ${port}`));
start()
33 changes: 1 addition & 32 deletions install.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
const {
privateKeyToAddress
} = require('./idena/script.js'),
axios = require('axios'),
fs = require("fs"),
const fs = require("fs"),
mysql = require('mysql2');
require('dotenv').config();


async function createDb() {
const db = fs.readFileSync('./db.sql', "utf-8");
var con = mysql.createConnection({
Expand All @@ -32,32 +27,6 @@ async function createDb() {

}
con.destroy();

}

async function setNonce() {
try {
const apiEpochResp = await axios.post(process.env.IDENA_PROVIDER, {
"method": "dna_epoch",
"id": 1,
"key": process.env.IDENA_API_KEY,
"params": []
})
let apiBalanceResp = await axios.post(process.env.IDENA_PROVIDER, {
"method": "dna_getBalance",
"id": 1,
"key": process.env.IDENA_API_KEY,
"params": [privateKeyToAddress(process.env.IDENA_PRIVATE_KEY)]
})
fs.writeFileSync("./idena/nonce.json", JSON.stringify({
nonce: apiBalanceResp.data.result.nonce,
epoch: apiEpochResp.data.result.epoch
}), "utf8")
console.log("the idena local nonce has has been set");
} catch (error) {
console.log("error while trying to set the idena local nonce:", error);
}
}

setNonce()
createDb()

0 comments on commit 8b6b581

Please sign in to comment.