Skip to content

Commit

Permalink
wip: feat: move config out to begin to librarify
Browse files Browse the repository at this point in the history
  • Loading branch information
coolaj86 committed Jul 14, 2024
1 parent 63d9314 commit 3aca2fd
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 92 deletions.
39 changes: 5 additions & 34 deletions demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ var CJDemo = ('object' === typeof module && exports) || {};
(function (window, CJDemo) {
'use strict';

let ENV = window.ENV || require('./node-env.js');

//@ts-ignore - ts can't understand JSON, still...
let pkg = ENV.package || require('./package.json');

//@ts-ignore
let Packer = window.CJPacker || require('./packer.js');
//@ts-ignore
Expand Down Expand Up @@ -36,26 +31,13 @@ var CJDemo = ('object' === typeof module && exports) || {};
// const COINJOIN_ENTRY_MAX_SIZE = 9; // real
const COINJOIN_ENTRY_MAX_SIZE = 2; // just for testing right now

let rpcConfig = {
protocol: ENV.DASHD_RPC_PROTOCOL || 'http', // https for remote, http for local / private networking
user: ENV.DASHD_RPC_USER,
pass: ENV.DASHD_RPC_PASS || ENV.DASHD_RPC_PASSWORD,
host: ENV.DASHD_RPC_HOST || '127.0.0.1',
port: ENV.DASHD_RPC_PORT || '19898', // mainnet=9998, testnet=19998, regtest=19898
timeout: 10 * 1000, // bump default from 5s to 10s for up to 10k addresses
onconnected: async function () {
console.info(`[info] rpc client connected ${rpcConfig.host}`);
},
};
if (ENV.DASHD_RPC_TIMEOUT) {
let rpcTimeoutSec = parseFloat(ENV.DASHD_RPC_TIMEOUT);
rpcConfig.timeout = rpcTimeoutSec * 1000;
}

async function main() {
CJDemo.run = async function (ENV, rpcConfig) {
/* jshint maxstatements: 1000 */
/* jshint maxcomplexity: 100 */

//@ts-ignore - ts can't understand JSON, still...
let pkg = ENV.package || require('./package.json');

let walletSalt = ENV.DASH_WALLET_SALT || '';
let isHelp = walletSalt === 'help' || walletSalt === '--help';
if (isHelp) {
Expand Down Expand Up @@ -1367,7 +1349,7 @@ var CJDemo = ('object' === typeof module && exports) || {};
}

console.log('Sweet, sweet victory!');
}
};

/**
* @param {Object} a
Expand Down Expand Up @@ -1433,17 +1415,6 @@ var CJDemo = ('object' === typeof module && exports) || {};
});
}

main()
.then(function () {
console.info('Done');
process.exit(0);
})
.catch(function (err) {
console.error('Fail:');
console.error(err.stack || err);
process.exit(1);
});

// @ts-ignore
window.CJDemo = CJDemo;
})(('object' === typeof window && window) || {}, CJDemo);
Expand Down
36 changes: 36 additions & 0 deletions run-demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

let CJDemo = require('./demo.js');

// TODO move to
let ENV = require('./node-env.js');
let rpcConfig = {
protocol: ENV.DASHD_RPC_PROTOCOL || 'http', // https for remote, http for local / private networking
user: ENV.DASHD_RPC_USER,
pass: ENV.DASHD_RPC_PASS || ENV.DASHD_RPC_PASSWORD,
host: ENV.DASHD_RPC_HOST || '127.0.0.1',
port: ENV.DASHD_RPC_PORT || '19898', // mainnet=9998, testnet=19998, regtest=19898
timeout: 10 * 1000, // bump default from 5s to 10s for up to 10k addresses
onconnected: async function () {
console.info(`[info] rpc client connected ${rpcConfig.host}`);
},
};
if (ENV.DASHD_RPC_TIMEOUT) {
let rpcTimeoutSec = parseFloat(ENV.DASHD_RPC_TIMEOUT);
rpcConfig.timeout = rpcTimeoutSec * 1000;
}

CJDemo.run(ENV, rpcConfig)
.then(function () {
console.info('Done');
if (typeof process !== 'undefined') {
process.exit(0);
}
})
.catch(function (err) {
console.error('Fail:');
console.error(err.stack || err);
if (typeof process !== 'undefined') {
process.exit(1);
}
});
89 changes: 31 additions & 58 deletions test.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<html>
<head>
<script src="./env.js"></script>

<script src="./node_modules/dashphrase/dashphrase.js"></script>
<script src="./node_modules/dashkeys/dashkeys.js"></script>
<script src="./node_modules/dashhd/dashhd.js"></script>
Expand All @@ -12,72 +10,47 @@
<script src="./packer.js"></script>
<script src="./parser.js"></script>
<script src="./demo.js"></script>

<script src="./env.js"></script>
<script>
async function main() {
return;
(function () {
'use strict';

let CJDemo = window.CJDemo || require('./demo.js');

// TODO move to
let ENV = window.ENV || require('./node-env.js');
let rpcConfig = {
protocol: 'http', // https for remote, http for local / private networking
protocol: ENV.DASHD_RPC_PROTOCOL || 'http', // https for remote, http for local / private networking
user: ENV.DASHD_RPC_USER,
pass: ENV.DASHD_RPC_PASS || ENV.DASHD_RPC_PASSWORD,
host: ENV.DASHD_RPC_HOST || '127.0.0.1',
port: ENV.DASHD_RPC_PORT || '19898', // mainnet=9998, testnet=19998, regtest=19898
timeout: 10 * 1000, // bump default from 5s to 10s for up to 10k addresses
onconnected: async function () {
console.info(`[info] rpc client connected ${rpcConfig.host}`);
},
};
let rpc = DashRpc.create(rpcConfig);

if (!ENV.DASHD_RPC_HOST) {
console.error(
`[SKIP] missing 'DASHD_RPC_HOST' (may be missing ./env.js)`,
);
return;
if (ENV.DASHD_RPC_TIMEOUT) {
let rpcTimeoutSec = parseFloat(ENV.DASHD_RPC_TIMEOUT);
rpcConfig.timeout = rpcTimeoutSec * 1000;
}

void (await rpc.init(rpc));

{
let decodeTx = await rpc.request('/', {
method: 'decodeRawTransaction'.toLowerCase(),
params: [
'03000000012b35e8bd64852ae0277a7a4ab6d6293f477f27e859251d27a9a3ebcb5855307f000000006b483045022100f88938da326af08203495a94b9a91b4bd11266df096cb67757a17eed1cb761b702205f90d94ead2d68086ba9141959115961cc491d560ce422c1a56a6c165697897e012103755be68d084e7ead4d83e23fb37c3076b16ead432de1b0bdf249290400f263cbffffffff011e140000000000001976a9141e0a6ef6085bb8af443a9e7f8941e61deb09fb5488ac00000000',
],
CJDemo.run(ENV, rpcConfig)
.then(function () {
console.info('Done');
if (typeof process !== 'undefined') {
process.exit(0);
}
})
.catch(function (err) {
console.error('Fail:');
console.error(err.stack || err);
if (typeof process !== 'undefined') {
process.exit(1);
}
});

let txid =
'2dd7112a9cbeff5fc56b761bf8b375a37c7f9fe2fc180f41361c62f0e248d4a0';
if (decodeTx?.result?.txid !== txid) {
throw new Error(
`'decodeRawTransaction' missing 'data.result.txid'`,
);
}
console.info(`PASS: correctly decoded raw transaction`);
}

{
let mnList = await rpc.request('/', {
method: 'masternodelist'.toLowerCase(),
params: [],
});
console.log('DEBUG mnList', mnList);
if (!mnList?.result) {
throw new Error(`'masternodelist' missing 'result'`);
}

let keys = Object.keys(mnList.result);
let key = keys[0];
if (!mnList.result?.[key]?.proTxHash) {
throw new Error(`'masternodelist' 'result' missing 'proTxHash'`);
}
console.info(`PASS: fetched 'masternodelist'`);
}
}

main()
.then(function () {
console.info('Done');
})
.catch(function (err) {
console.error('Fail:');
console.error(err.stack || err);
});
})();
</script>
</head>
<body>
Expand Down

0 comments on commit 3aca2fd

Please sign in to comment.