Skip to content

Commit

Permalink
Create nodejs client lib
Browse files Browse the repository at this point in the history
  • Loading branch information
ssantos21 committed May 28, 2024
1 parent d849680 commit cb11d64
Show file tree
Hide file tree
Showing 18 changed files with 2,124 additions and 208 deletions.
133 changes: 133 additions & 0 deletions clients/libs/nodejs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

wallet.db*
wallet.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ const utils = require('./utils');

const { CoinStatus } = require('./coin_enum');

const execute = async (electrumClient, db, walletName, statechainId, toAddress, feeRate) => {
const execute = async (clientConfig, electrumClient, db, walletName, statechainId, toAddress, feeRate) => {

let wallet = await sqlite_manager.getWallet(db, walletName);

if (!feeRate) {
const serverInfo = await utils.infoConfig(electrumClient);
const serverInfo = await utils.infoConfig(clientConfig, electrumClient);
const feeRateSatsPerByte = serverInfo.fee_rate_sats_per_byte;
feeRate = feeRateSatsPerByte;
} else {
Expand Down Expand Up @@ -57,7 +57,7 @@ const execute = async (electrumClient, db, walletName, statechainId, toAddress,

await sqlite_manager.updateWallet(db, wallet);

utils.completeWithdraw(coin.statechain_id, coin.signed_statechain_id);
utils.completeWithdraw(clientConfig, coin.statechain_id, coin.signed_statechain_id);

return {
backupTx: backupTxTxid,
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@

const config = require('config');
const sqlite_manager = require('./sqlite_manager');
const axios = require('axios').default;
const { SocksProxyAgent } = require('socks-proxy-agent');
const utils = require('./utils');
const bitcoinjs = require("bitcoinjs-lib");
const ecc = require("tiny-secp256k1");
const deposit = require('./deposit');
const { CoinStatus } = require('./coin_enum');
const mercury_wasm = require('mercury-wasm');

const checkDeposit = async (electrumClient, coin, wallet_network) => {
const checkDeposit = async (clientConfig, electrumClient, coin, wallet_network) => {

if (!coin.statechain_id && !coin.utxo_txid && !coin.utxo_vout) {
if (coin.status != CoinStatus.INITIALISED) {
Expand Down Expand Up @@ -56,7 +53,7 @@ const checkDeposit = async (electrumClient, coin, wallet_network) => {
const utxo_txid = utxo.tx_hash;
const utxo_vout = utxo.tx_pos;

const backup_tx = await deposit.createTx1(electrumClient, coin, wallet_network, utxo_txid, utxo_vout);
const backup_tx = await deposit.createTx1(clientConfig, electrumClient, coin, wallet_network, utxo_txid, utxo_vout);

const activity_utxo = `${utxo_txid}:${utxo_vout}`;

Expand All @@ -75,7 +72,7 @@ const checkDeposit = async (electrumClient, coin, wallet_network) => {

const confirmations = blockheight - utxo.height + 1;

const confirmationTarget = config.get('confirmationTarget');
const confirmationTarget = clientConfig.confirmationTarget;

coin.status = CoinStatus.UNCONFIRMED;

Expand All @@ -87,13 +84,13 @@ const checkDeposit = async (electrumClient, coin, wallet_network) => {
return depositResult;
}

const checkTransfer = async (coin) => {
const checkTransfer = async (clientConfig, coin) => {

if (!coin.statechain_id) {
throw new Error(`The coin with the aggregated address ${coin.aggregated_address} does not have a statechain ID`);
}

let statechainInfo = await utils.getStatechainInfo(coin.statechain_id);
let statechainInfo = await utils.getStatechainInfo(clientConfig, coin.statechain_id);

// if the statechain info is not found, we assume the coin has been transferred
if (!statechainInfo) {
Expand All @@ -107,7 +104,7 @@ const checkTransfer = async (coin) => {
return isTransferred;
}

const checkWithdrawal = async (electrumClient, coin, wallet_network) => {
const checkWithdrawal = async (clientConfig, electrumClient, coin, wallet_network) => {

let txid = undefined;

Expand Down Expand Up @@ -162,15 +159,15 @@ const checkWithdrawal = async (electrumClient, coin, wallet_network) => {

const confirmations = blockheight - utxo.height + 1;

const confirmationTarget = config.get('confirmationTarget');
const confirmationTarget = clientConfig.confirmationTarget;

return confirmations >= confirmationTarget;
}

return false;
}

const updateCoins = async (electrumClient, db, wallet_name) => {
const updateCoins = async (clientConfig, electrumClient, db, wallet_name) => {

let wallet = await sqlite_manager.getWallet(db, wallet_name);

Expand All @@ -181,20 +178,20 @@ const updateCoins = async (electrumClient, db, wallet_name) => {

if (coin.status == CoinStatus.INITIALISED || coin.status == CoinStatus.IN_MEMPOOL || coin.status == CoinStatus.UNCONFIRMED) {

let depositResult = await checkDeposit(electrumClient, coin, network);
let depositResult = await checkDeposit(clientConfig, electrumClient, coin, network);

if (depositResult) {
wallet.activities.push(depositResult.activity);
await sqlite_manager.insertTransaction(db, coin.statechain_id, [depositResult.backup_tx]);
}
} else if (coin.status === CoinStatus.IN_TRANSFER) {
let is_transferred = await checkTransfer(coin);
let is_transferred = await checkTransfer(clientConfig, coin);

if (is_transferred) {
coin.status = CoinStatus.TRANSFERRED;
}
} else if (coin.status == CoinStatus.WITHDRAWING) {
let is_withdrawn = await checkWithdrawal(electrumClient, coin, network);
let is_withdrawn = await checkWithdrawal(clientConfig, electrumClient, coin, network);

if (is_withdrawn) {
coin.status = CoinStatus.WITHDRAWN;
Expand Down
33 changes: 13 additions & 20 deletions clients/nodejs/deposit.js → clients/libs/nodejs/deposit.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
const axios = require('axios').default;
const { SocksProxyAgent } = require('socks-proxy-agent');
const bitcoinjs = require("bitcoinjs-lib");
const ecc = require("tiny-secp256k1");
const utils = require('./utils');
const transaction = require('./transaction');
const config = require('config');

// used only for random token. Can be removed later
// const crypto = require('crypto');

const mercury_wasm = require('mercury-wasm');

const sqlite_manager = require('./sqlite_manager');

const { CoinStatus } = require('./coin_enum');

const getDepositBitcoinAddress = async (db, wallet_name, amount) => {
const getDepositBitcoinAddress = async (clientConfig, db, wallet_name, amount) => {

let wallet = await sqlite_manager.getWallet(db, wallet_name);

Expand All @@ -25,7 +18,7 @@ const getDepositBitcoinAddress = async (db, wallet_name, amount) => {
throw new Error(`There is no token available`);
}

await init(db, wallet, foundToken.token_id);
await init(clientConfig, db, wallet, foundToken.token_id);

let coin = wallet.coins[wallet.coins.length - 1];

Expand All @@ -42,7 +35,7 @@ const getDepositBitcoinAddress = async (db, wallet_name, amount) => {
return { "deposit_address": coin.aggregated_address, "statechain_id": coin.statechain_id };
}

const createTx1 = async (electrumClient, coin, wallet_network, tx0_hash, tx0_vout) => {
const createTx1 = async (clientConfig, electrumClient, coin, wallet_network, tx0_hash, tx0_vout) => {

if (coin.status !== CoinStatus.INITIALISED) {
throw new Error(`The coin with the aggregated address ${aggregated_address} is not in the INITIALISED state`);
Expand All @@ -60,7 +53,7 @@ const createTx1 = async (electrumClient, coin, wallet_network, tx0_hash, tx0_vou
const isWithdrawal = false;
const qtBackupTx = 0;

let signed_tx = await transaction.new_transaction(electrumClient, coin, toAddress, isWithdrawal, qtBackupTx, null, wallet_network);
let signed_tx = await transaction.new_transaction(clientConfig, electrumClient, coin, toAddress, isWithdrawal, qtBackupTx, null, wallet_network);

let backup_tx = {
tx_n: 1,
Expand All @@ -77,7 +70,7 @@ const createTx1 = async (electrumClient, coin, wallet_network, tx0_hash, tx0_vou
return backup_tx;
}

const init = async (db, wallet, token_id) => {
const init = async (clientConfig, db, wallet, token_id) => {

let coin = mercury_wasm.getNewCoin(wallet);

Expand All @@ -89,11 +82,11 @@ const init = async (db, wallet, token_id) => {

let depositMsg1 = mercury_wasm.createDepositMsg1(coin, token_id);

const statechain_entity_url = config.get('statechainEntity');
const statechain_entity_url = clientConfig.statechainEntity;
const path = "deposit/init/pod";
const url = statechain_entity_url + '/' + path;

const torProxy = config.get('torProxy');
const torProxy = clientConfig.torProxy;

let socksAgent = undefined;

Expand All @@ -119,13 +112,13 @@ const init = async (db, wallet, token_id) => {
await sqlite_manager.updateWallet(db, wallet);
}

const getTokenFromServer = async () => {
const getTokenFromServer = async (clientConfig) => {

const statechain_entity_url = config.get('statechainEntity');
const statechain_entity_url = clientConfig.statechainEntity;
const path = "tokens/token_init";
const url = statechain_entity_url + '/' + path;

const torProxy = config.get('torProxy');
const torProxy = clientConfig.torProxy;

let socksAgent = undefined;

Expand All @@ -144,11 +137,11 @@ const getTokenFromServer = async () => {
return token;
}

const getToken = async (db, walletName) => {
const getToken = async (clientConfig, db, walletName) => {

let wallet = await sqlite_manager.getWallet(db, walletName);

let token = await getTokenFromServer();
let token = await getTokenFromServer(clientConfig);

// for dev purposes
token.confirmed = true;
Expand All @@ -160,4 +153,4 @@ const getToken = async (db, walletName) => {
return token;
}

module.exports = { /*execute, createStatecoin,*/ getDepositBitcoinAddress, createTx1, getToken };
module.exports = { getDepositBitcoinAddress, createTx1, getToken };
Loading

0 comments on commit cb11d64

Please sign in to comment.