cypherpoker.js
cypherpoker.js copied to clipboard
Update Services API endpoints to support Monero / multiple cryptocurrencies
This task is preceded by https://github.com/monicanagent/cypherpoker.js/issues/53
Overview
With the adapter in place, the CP_Account API endpoint must be updated to use it while maintaining existing functionality. Functions should be updated in place whenever possible -- that is, names and existing parameters should not change.
Functional Branching
To enable branching based on cryptocurrency, if not already supported, additional parameters may be added that should default to existing functionality.
For example, if the following function needs to be updated to support Monero and other cryptocurrencies:
https://github.com/monicanagent/cypherpoker.js/blob/1a4e605e2dbbcf394e2adaf3a2d428a67ae125e8/src/server/api/CP_Account.js#L487-L503
... an additional parameter should be added that will default to the current functionality if not supplied:
function makeHDWallet(privKey, currencyType="bitcoin") {
if (currencyType == "bitcoin") {
try {
if (privKey.indexOf("xprv") == 0) {
//mainnet
var wallet = bitcoin.bip32.fromBase58(privKey);
} else {
//testnett
wallet = bitcoin.bip32.fromBase58(privKey, bitcoin.networks.testnet);
}
} catch (err) {
console.error(err.stack);
return (null);
}
if (wallet == undefined) {
return (null);
}
return (wallet);
} else if (currencyType == "monero") {
//make a Monero HD wallet
}
}
Since any existing calls are providing only one parameter and the new, second parameter defaults to "bitcoin", any current functionality will continue to work while any new functionality can incorporate the new parameter.
That being said, anywhere that makeHDWallet is called will probably still need to be updated since the HD wallet for the new cryptocurrency will need to be generated somewhere.
Luckily, on the server side there are only two API endpoints that include cryptocurrency transactions; primarily CP_Account, and sometimes CP_SmartContract.
Additionally, startup and configuration information for the newly-added cryptocurrency will need to be processed at server startup:
https://github.com/monicanagent/cypherpoker.js/blob/1a4e605e2dbbcf394e2adaf3a2d428a67ae125e8/src/server/server.js#L892-L1144
As with the adapter, it's best to keep any newly-added functionality in modular form. That is, as async (where required) functions that are passed references rather than relying on static locations of objects such as the main configuration.
Function Updates
The functions that can be may need to be updated or re-created include:
https://github.com/monicanagent/cypherpoker.js/blob/1a4e605e2dbbcf394e2adaf3a2d428a67ae125e8/src/server/api/CP_Account.js#L487 https://github.com/monicanagent/cypherpoker.js/blob/1a4e605e2dbbcf394e2adaf3a2d428a67ae125e8/src/server/api/CP_Account.js#L535 https://github.com/monicanagent/cypherpoker.js/blob/1a4e605e2dbbcf394e2adaf3a2d428a67ae125e8/src/server/api/CP_Account.js#L558 https://github.com/monicanagent/cypherpoker.js/blob/1a4e605e2dbbcf394e2adaf3a2d428a67ae125e8/src/server/api/CP_Account.js#L833 https://github.com/monicanagent/cypherpoker.js/blob/1a4e605e2dbbcf394e2adaf3a2d428a67ae125e8/src/server/api/CP_Account.js#L870 https://github.com/monicanagent/cypherpoker.js/blob/1a4e605e2dbbcf394e2adaf3a2d428a67ae125e8/src/server/api/CP_Account.js#L912 https://github.com/monicanagent/cypherpoker.js/blob/1a4e605e2dbbcf394e2adaf3a2d428a67ae125e8/src/server/api/CP_Account.js#L964 https://github.com/monicanagent/cypherpoker.js/blob/1a4e605e2dbbcf394e2adaf3a2d428a67ae125e8/src/server/api/CP_Account.js#L1019 https://github.com/monicanagent/cypherpoker.js/blob/1a4e605e2dbbcf394e2adaf3a2d428a67ae125e8/src/server/api/CP_Account.js#L1052 https://github.com/monicanagent/cypherpoker.js/blob/1a4e605e2dbbcf394e2adaf3a2d428a67ae125e8/src/server/api/CP_Account.js#L1070 https://github.com/monicanagent/cypherpoker.js/blob/1a4e605e2dbbcf394e2adaf3a2d428a67ae125e8/src/server/api/CP_Account.js#L1224 https://github.com/monicanagent/cypherpoker.js/blob/1a4e605e2dbbcf394e2adaf3a2d428a67ae125e8/src/server/api/CP_Account.js#L1260
Obviously some of these functions are fairly specific to Bitcoin so the creation of new functions in their place may be good idea.
When it comes to generic functions such as cashOutToAddress or sendTransaction, however, functional branching (if..else, switch..case, etc.), within the functions makes sense since they can be applied more broadly than just for a single cryptocurrency (e.g. Bitcoin). If in doubt, branch within the function rather than creating a new one.
Additional information, including code documentation and formatting, can be found here: https://github.com/monicanagent/cypherpoker.js/issues/48
Server functions have been updated to be generic and routed to appropriate cryptocurrency handlers as of Bitcoin Cash integration.