Skip to content

Commit

Permalink
Add ZECTEST as test coin, setup socket initialization when a dlight/p…
Browse files Browse the repository at this point in the history
…rivate enabled coin is added, and teardown when it is removed
  • Loading branch information
michaeltout committed Apr 29, 2020
1 parent 588b3d2 commit 2a966b5
Show file tree
Hide file tree
Showing 38 changed files with 844 additions and 197 deletions.
24 changes: 24 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Android",
"program": "${workspaceRoot}/.vscode/launchReactNative.js",
"type": "reactnative",
"request": "launch",
"platform": "android",
"sourceMaps": true,
"cwd": "${workspaceFolder}",
},
{
"name": "Debug iOS",
"program": "${workspaceRoot}/.vscode/launchReactNative.js",
"type": "reactnative",
"request": "launch",
"platform": "ios",
"target": "iPhone 5s",
"cwd": "${workspaceFolder}",
"sourceMaps": true,
}
]
}
19 changes: 19 additions & 0 deletions __mocks__/react-native-alert-async/mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
let AsyncAlerts = []
let AlertResponse = false

function mockRnAlertAsync() {
return {
AlertAsync: (title, desc, buttons, options) => {
AsyncAlerts.push({title, desc, buttons, options})
return new Promise((resolve) => {
resolve(AlertResponse)
})
},
getAlerts: () => AsyncAlerts,
setAlertResponse: (response) => {
AlertResponse = response
}
}
}

module.exports = mockRnAlertAsync
26 changes: 25 additions & 1 deletion __mocks__/react-native-verus-light-client/WalletFolder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const formatResponse = require('./requests/JsonRpc')

const WalletFolder = {}

function createWallet(coinId, coinProto, address, port, accountHash, numAddresses, seed, birthday) {
function createWallet(coinId, coinProto, accountHash, address, port, numAddresses, seed, birthday) {
WalletFolder[coinId + '_' + coinProto + '_' + accountHash] = { open: false }

return new Promise((resolve, reject) => resolve(true))
Expand All @@ -20,6 +20,28 @@ function openWallet(coinId, coinProto, accountHash) {
})
}

function startSync(coinId, coinProto, accountHash) {
return new Promise((resolve, reject) => {
if (WalletFolder[coinId + '_' + coinProto + '_' + accountHash] == null) reject("Failed to open " + coinId + " wallet.")
else {
WalletFolder[coinId + '_' + coinProto + '_' + accountHash].open = true

resolve(true)
}
})
}

function stopSync(coinId, coinProto, accountHash) {
return new Promise((resolve, reject) => {
if (WalletFolder[coinId + '_' + coinProto + '_' + accountHash] == null) reject("Failed to open " + coinId + " wallet.")
else {
WalletFolder[coinId + '_' + coinProto + '_' + accountHash].open = true

resolve(true)
}
})
}

function closeWallet(coinId, coinProto, accountHash) {
return new Promise((resolve, reject) => {
if (WalletFolder[coinId + '_' + coinProto + '_' + accountHash] == null) reject("Failed to close " + coinId + " wallet.")
Expand Down Expand Up @@ -88,5 +110,7 @@ module.exports = {
openWallet,
closeWallet,
deleteWallet,
startSync,
stopSync,
request
}
6 changes: 5 additions & 1 deletion __mocks__/react-native-verus-light-client/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ const {
openWallet,
closeWallet,
deleteWallet,
request
request,
startSync,
stopSync
} = require('./WalletFolder')

function mockVerusLightClient() {
Expand All @@ -12,6 +14,8 @@ function mockVerusLightClient() {
openWallet,
closeWallet,
deleteWallet,
startSync,
stopSync,
request
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ function getTransactions(id, params) {
{
address: null,
amount: 12.160704,
category: "sent",
category: "received",
status: "confirmed",
time: "341431",
txid: "3242edc2c2",
height: "312312"
height: "312312",
memo: "746573746d656d6f"
},
{
address: "2ei2joffd2",
Expand Down
2 changes: 2 additions & 0 deletions debug/setupJest.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const mockAgamaWalletLibElectrums = require('../__mocks__/agama-wallet-lib/agama-wallet-lib-electrums')
const mockVerusLightClient = require('../__mocks__/react-native-verus-light-client/mock')
const mockRnAlertAsync = require('../__mocks__/react-native-alert-async/mock')
const mockRedux = require('../__mocks__/redux/mock')

jest.mock('agama-wallet-lib/src/electrum-servers', () => mockAgamaWalletLibElectrums());
jest.mock('react-native-verus-light-client', () => mockVerusLightClient());
jest.mock('react-native-alert-async', () => mockRnAlertAsync());
jest.mock('redux', () => mockRedux());

global.fetch = require('../__mocks__/react-native-fetch/fetch')
42 changes: 2 additions & 40 deletions src/VerusMobile.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import {
initializeWallet,
openWallet
} from "./utils/api/channels/dlight/callCreators";
import VerusLightClient from "react-native-verus-light-client";
import { hashAccountId } from "./utils/crypto/hash";


class VerusMobile extends React.Component {
Expand All @@ -43,46 +45,6 @@ class VerusMobile extends React.Component {
}

componentDidMount() {

//DELETE/REFACTOR
/*VerusLightClient.addWallet('ZEC', 'btc', "lightwalletd.testnet.z.cash", 9067, "abcde12345", "a seed that is at least 32 bytes long so that it will work with the ZIP 32 protocol.")
.then(res => {
console.log("ADD WALLET RES")
console.log(res)
return VerusLightClient.request(20, "getblockcount", ["ZEC", "abcde12345", "btc"])
})
.then(res => {
console.log("BLOCK COUNT")
console.log(res)
})
.catch(err => {
console.log("ADD WALLET OR REQ REJ")
console.log(err)
})*/
/*initializeWallet('ZEC', 'btc', "lightwalletd.testnet.z.cash", 9067, "8ccb033c0e48b27ff91e1ab948367e3bbc6921487c97624ed7ad064025e3dc99", 100, "a seed that is at least 32 bytes long so that it will work with the ZIP 32 protocol.", 0)
.then(res => {
console.log("INIT WALLET RES")
console.log(res)
return openWallet('ZEC', 'btc', '8ccb033c0e48b27ff91e1ab948367e3bbc6921487c97624ed7ad064025e3dc99')
})
.then(res => {
console.log("ADD WALLET RES")
console.log(res)
return startSync('ZEC', 'btc', '8ccb033c0e48b27ff91e1ab948367e3bbc6921487c97624ed7ad064025e3dc99')
})
.then(res => {
console.log("START SYNC RES")
console.log(res)
})
.catch(e => {
console.log("ERROR")
console.log(e)
})*/


//TODO: Figure out what should trigger a cache clear on startup of server
//versions. (The action that triggers it should indicate a server upgraded it's
//version)
Expand Down
27 changes: 21 additions & 6 deletions src/actions/actionCreators.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './actions/Coins'
export * from './actions/coins/Coins'
export * from './actions/UserData'
export * from './actions/WalletSettings'
export * from './actions/cache/Electrum'
Expand Down Expand Up @@ -51,7 +51,9 @@ import {
SET_COIN_STATUS,
SIGN_IN_USER,
AUTHENTICATE_USER,
SET_COINMENU_FOCUS
SET_COINMENU_FOCUS,
OPEN_DLIGHT_SOCKET,
CLOSE_DLIGHT_SOCKET
} from "../utils/constants/storeType";

//Reducer Name: authentication
Expand Down Expand Up @@ -197,6 +199,22 @@ export const setCurrentUserCoins = (activeCoinsForUser) => {
}
}

//Reducer name: coins
export const openDlightWallet = (chainTicker) => {
return {
type: OPEN_DLIGHT_SOCKET,
payload: { chainTicker }
}
}

//Reducer name: coins
export const closeDlightWallet = (chainTicker) => {
return {
type: CLOSE_DLIGHT_SOCKET,
payload: { chainTicker }
}
}

//Reducer name: ledger
export const setBalances = (balances) => {
return {
Expand Down Expand Up @@ -287,11 +305,8 @@ export const setActiveSectionCustomCoins = (activeSection) => {
}
}

// Reducer name: coinOverview
//Reducer name: coinOverview
export const setActiveOverviewFilter = (chainTicker, filterType) => {
//DELET
console.log("SETTING FILTER FOR")
console.log(chainTicker + ', ' + filterType)
return {
type: SET_OVERVIEW_FILTER,
payload: {
Expand Down
69 changes: 38 additions & 31 deletions src/actions/actions/Coins.js → src/actions/actions/coins/Coins.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,78 @@
import {
setCoinList,
setCurrentUserCoins,
} from '../actionCreators';
import {
createCoinObj
} from '../../utils/CoinData/CoinData';
} from '../../actionCreators';
import {
storeCoins,
getActiveCoinList
} from '../../utils/asyncStore/asyncStore';
} from '../../../utils/asyncStore/asyncStore';
import { DLIGHT } from '../../../utils/constants/intervalConstants';
import { initDlightWallet, closeDlightWallet } from '../dlight/dispatchers/LightWalletReduxManager';

// Add coin that exists in default list of coins
export const addExistingCoin = (fullCoinObj, activeCoins, userName) => {
// Add coin by saving it to localstorage, and optionally intialize dlight backend
export const addCoin = (fullCoinObj, activeCoins, userName, channels) => {
let coinIndex = activeCoins.findIndex(x => x.id === fullCoinObj.id);
let dlightInit = channels.includes(DLIGHT) ? [initDlightWallet(fullCoinObj)] : []

if (coinIndex > -1) {
if (activeCoins[coinIndex].users.includes(userName)) {
throw new Error("Coin already added for user " + userName);
// Do nothing except for conditionally enable dlight backend
return new Promise((resolve, reject) => {
Promise.all(dlightInit)
.then(() => {
resolve(setCoinList(activeCoins))
})
.catch(err => reject(err))
});
}
else {
activeCoins[coinIndex].users.push(userName);
return new Promise((resolve, reject) => {
storeCoins(activeCoins)
.then((res) => {
if (res === true) {
resolve(setCoinList(activeCoins))
}
else {
resolve(false);
}
.then(() => {
return Promise.all(dlightInit)
})
.then(() => {
resolve(setCoinList(activeCoins))
})
.catch(err => reject(err))
});
}
}
else {
activeCoins.push({...fullCoinObj, users: [userName]});
return new Promise((resolve, reject) => {
storeCoins(activeCoins)
.then((res) => {
if (res === true) {
resolve(setCoinList(activeCoins))
}
else {
resolve(false);
}
.then(() => {
return Promise.all(dlightInit)
})
.then(() => {
resolve(setCoinList(activeCoins))
})
.catch(err => reject(err))
});
}
}

// Remove a user's name from an active coin
export const removeExistingCoin = (coinID, activeCoins, userName) => {
export const removeExistingCoin = (coinID, activeCoins, userName, closeSocket, deleteWallet = false) => {
let coinIndex = activeCoins.findIndex(x => x.id === coinID);

if (coinIndex > -1 && activeCoins[coinIndex].users.includes(userName)) {
let userIndex = activeCoins[coinIndex].users.findIndex(n => n === userName);
activeCoins[coinIndex].users.splice(userIndex, 1);
let dlightClose = closeSocket ? [closeDlightWallet(activeCoins[coinIndex], deleteWallet)] : []

activeCoins[coinIndex].users.splice(userIndex, 1)

return new Promise((resolve, reject) => {
storeCoins(activeCoins)
.then((res) => {
if (res === true) {
resolve(setCoinList(activeCoins))
}
else {
resolve(false);
}
.then(() => {
return Promise.all(dlightClose)
})
.then(() => {
resolve(setCoinList(activeCoins))
})
.catch(err => reject(err))
});
}
else {
Expand Down
Loading

0 comments on commit 2a966b5

Please sign in to comment.