From 43512fd8cff152a36759a3e61e588f78aeb82f47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20=C5=A0ev=C4=8D=C3=ADk?= Date: Fri, 28 Dec 2018 16:52:06 +0100 Subject: [PATCH] Refactor: Use static properties for wallet types --- BlueComponents.js | 16 ++++++------ class/abstract-hd-wallet.js | 8 +++--- class/abstract-wallet.js | 36 ++++++++++++++++----------- class/app-storage.js | 18 +++++++------- class/hd-legacy-breadwallet-wallet.js | 10 ++------ class/hd-legacy-p2pkh-wallet.js | 10 ++------ class/hd-segwit-p2sh-wallet.js | 10 ++------ class/legacy-wallet.js | 10 ++------ class/lightning-custodian-wallet.js | 8 +++--- class/segwit-bech-wallet.js | 10 ++------ class/segwit-p2sh-wallet.js | 10 ++------ class/watch-only-wallet.js | 10 ++------ screen/lnd/manageFunds.js | 2 +- screen/lnd/scanLndInvoice.js | 2 +- screen/send/details.js | 2 +- screen/settings/lightningSettings.js | 2 +- screen/wallets/add.js | 12 ++++----- screen/wallets/details.js | 10 ++++---- screen/wallets/export.js | 2 +- screen/wallets/import.js | 4 +-- screen/wallets/reorderWallets.js | 15 ++++++----- screen/wallets/scanQrWif.js | 6 ++--- screen/wallets/selectWallet.js | 17 ++++++------- screen/wallets/transactions.js | 10 ++++---- screen/wallets/xpub.js | 6 ++--- 25 files changed, 100 insertions(+), 146 deletions(-) diff --git a/BlueComponents.js b/BlueComponents.js index 21ccdc35..b0f08c6c 100644 --- a/BlueComponents.js +++ b/BlueComponents.js @@ -957,32 +957,32 @@ export class WalletsCarousel extends Component { let gradient1 = '#65ceef'; let gradient2 = '#68bbe1'; - if (new WatchOnlyWallet().type === item.type) { + if (WatchOnlyWallet.type === item.type) { gradient1 = '#7d7d7d'; gradient2 = '#4a4a4a'; } - if (new LegacyWallet().type === item.type) { + if (LegacyWallet.type === item.type) { gradient1 = '#40fad1'; gradient2 = '#15be98'; } - if (new HDLegacyP2PKHWallet().type === item.type) { + if (HDLegacyP2PKHWallet.type === item.type) { gradient1 = '#e36dfa'; gradient2 = '#bd10e0'; } - if (new HDLegacyBreadwalletWallet().type === item.type) { + if (HDLegacyBreadwalletWallet.type === item.type) { gradient1 = '#fe6381'; gradient2 = '#f99c42'; } - if (new HDSegwitP2SHWallet().type === item.type) { + if (HDSegwitP2SHWallet.type === item.type) { gradient1 = '#c65afb'; gradient2 = '#9053fe'; } - if (new LightningCustodianWallet().type === item.type) { + if (LightningCustodianWallet.type === item.type) { gradient1 = '#f1be07'; gradient2 = '#f79056'; } @@ -1015,9 +1015,7 @@ export class WalletsCarousel extends Component { }} > address @@ -93,10 +95,6 @@ export class AbstractHDWallet extends LegacyWallet { return bip39.mnemonicToSeedHex(this.secret); } - getTypeReadable() { - throw new Error('Not implemented'); - } - /** * Derives from hierarchy, returns next free address * (the one that has no transactions). Looks for several, diff --git a/class/abstract-wallet.js b/class/abstract-wallet.js index 5fc18df4..74d740c0 100644 --- a/class/abstract-wallet.js +++ b/class/abstract-wallet.js @@ -1,8 +1,20 @@ import { BitcoinUnit } from '../models/bitcoinUnits'; export class AbstractWallet { + static type = 'abstract'; + static typeReadable = 'abstract'; + + static fromJson(obj) { + let obj2 = JSON.parse(obj); + let temp = new this(); + for (let key2 of Object.keys(obj2)) { + temp[key2] = obj2[key2]; + } + + return temp; + } + constructor() { - this.type = 'abstract'; this.label = ''; this.secret = ''; // private key or recovery phrase this.balance = 0; @@ -15,12 +27,16 @@ export class AbstractWallet { this.preferredBalanceUnit = BitcoinUnit.BTC; } - getTransactions() { - return this.transactions; + get type() { + return this.constructor.type; } - getTypeReadable() { - return this.type; + get typeReadable() { + return this.constructor.typeReadable; + } + + getTransactions() { + return this.transactions; } /** @@ -84,16 +100,6 @@ export class AbstractWallet { return 0; } - static fromJson(obj) { - let obj2 = JSON.parse(obj); - let temp = new this(); - for (let key2 of Object.keys(obj2)) { - temp[key2] = obj2[key2]; - } - - return temp; - } - getAddress() {} // createTx () { throw Error('not implemented') } diff --git a/class/app-storage.js b/class/app-storage.js index 9b2ea4d4..fc01d213 100644 --- a/class/app-storage.js +++ b/class/app-storage.js @@ -132,25 +132,25 @@ export class AppStorage { let tempObj = JSON.parse(key); let unserializedWallet; switch (tempObj.type) { - case 'segwitBech32': + case SegwitBech32Wallet.type: unserializedWallet = SegwitBech32Wallet.fromJson(key); break; - case 'segwitP2SH': + case SegwitP2SHWallet.type: unserializedWallet = SegwitP2SHWallet.fromJson(key); break; - case 'watchOnly': + case WatchOnlyWallet.type: unserializedWallet = WatchOnlyWallet.fromJson(key); break; - case new HDLegacyP2PKHWallet().type: + case HDLegacyP2PKHWallet.type: unserializedWallet = HDLegacyP2PKHWallet.fromJson(key); break; - case new HDSegwitP2SHWallet().type: + case HDSegwitP2SHWallet.type: unserializedWallet = HDSegwitP2SHWallet.fromJson(key); break; - case new HDLegacyBreadwalletWallet().type: + case HDLegacyBreadwalletWallet.type: unserializedWallet = HDLegacyBreadwalletWallet.fromJson(key); break; - case new LightningCustodianWallet().type: + case LightningCustodianWallet.type: /** @type {LightningCustodianWallet} */ unserializedWallet = LightningCustodianWallet.fromJson(key); let lndhub = false; @@ -168,7 +168,7 @@ export class AppStorage { } unserializedWallet.init(); break; - case 'legacy': + case LegacyWallet.type: default: unserializedWallet = LegacyWallet.fromJson(key); break; @@ -218,7 +218,7 @@ export class AppStorage { let walletsToSave = []; for (let key of this.wallets) { if (typeof key === 'boolean') continue; - walletsToSave.push(JSON.stringify(key)); + walletsToSave.push(JSON.stringify({ ...key, type: key.type })); } let data = { diff --git a/class/hd-legacy-breadwallet-wallet.js b/class/hd-legacy-breadwallet-wallet.js index 8b9b15b2..4952d5a1 100644 --- a/class/hd-legacy-breadwallet-wallet.js +++ b/class/hd-legacy-breadwallet-wallet.js @@ -8,14 +8,8 @@ const bip39 = require('bip39'); * In particular, Breadwallet-compatible (Legacy addresses) */ export class HDLegacyBreadwalletWallet extends AbstractHDWallet { - constructor() { - super(); - this.type = 'HDLegacyBreadwallet'; - } - - getTypeReadable() { - return 'HD Legacy Breadwallet (P2PKH)'; - } + static type = 'HDLegacyBreadwallet'; + static typeReadable = 'HD Legacy Breadwallet (P2PKH)'; /** * @see https://github.com/bitcoinjs/bitcoinjs-lib/issues/584 diff --git a/class/hd-legacy-p2pkh-wallet.js b/class/hd-legacy-p2pkh-wallet.js index 578f235a..af4bfb31 100644 --- a/class/hd-legacy-p2pkh-wallet.js +++ b/class/hd-legacy-p2pkh-wallet.js @@ -11,14 +11,8 @@ const signer = require('../models/signer'); * @see https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki */ export class HDLegacyP2PKHWallet extends AbstractHDWallet { - constructor() { - super(); - this.type = 'HDlegacyP2PKH'; - } - - getTypeReadable() { - return 'HD Legacy (BIP44 P2PKH)'; - } + static type = 'HDlegacyP2PKH'; + static typeReadable = 'HD Legacy (BIP44 P2PKH)'; allowSend() { return true; diff --git a/class/hd-segwit-p2sh-wallet.js b/class/hd-segwit-p2sh-wallet.js index c1cafa8c..99768fb1 100644 --- a/class/hd-segwit-p2sh-wallet.js +++ b/class/hd-segwit-p2sh-wallet.js @@ -14,14 +14,8 @@ const signer = require('../models/signer'); * @see https://github.com/bitcoin/bips/blob/master/bip-0049.mediawiki */ export class HDSegwitP2SHWallet extends AbstractHDWallet { - constructor() { - super(); - this.type = 'HDsegwitP2SH'; - } - - getTypeReadable() { - return 'HD SegWit (BIP49 P2SH)'; - } + static type = 'HDsegwitP2SH'; + static typeReadable = 'HD SegWit (BIP49 P2SH)'; allowSend() { return true; diff --git a/class/legacy-wallet.js b/class/legacy-wallet.js index 40a74b59..9b2ec7be 100644 --- a/class/legacy-wallet.js +++ b/class/legacy-wallet.js @@ -13,10 +13,8 @@ const signer = require('../models/signer'); * (legacy P2PKH compressed) */ export class LegacyWallet extends AbstractWallet { - constructor() { - super(); - this.type = 'legacy'; - } + static type = 'legacy'; + static typeReadable = 'Legacy (P2PKH)'; /** * Simple function which says that we havent tried to fetch balance @@ -77,10 +75,6 @@ export class LegacyWallet extends AbstractWallet { }); } - getTypeReadable() { - return 'Legacy (P2PKH)'; - } - /** * * @returns {string} diff --git a/class/lightning-custodian-wallet.js b/class/lightning-custodian-wallet.js index 274e3e15..d93ad899 100644 --- a/class/lightning-custodian-wallet.js +++ b/class/lightning-custodian-wallet.js @@ -4,11 +4,13 @@ import { BitcoinUnit } from '../models/bitcoinUnits'; let BigNumber = require('bignumber.js'); export class LightningCustodianWallet extends LegacyWallet { + static type = 'lightningCustodianWallet'; + static typeReadable = 'Lightning'; + constructor() { super(); this.setBaseURI(); // no args to init with default value this.init(); - this.type = 'lightningCustodianWallet'; this.refresh_token = ''; this.access_token = ''; this._refresh_token_created_ts = 0; @@ -79,10 +81,6 @@ export class LightningCustodianWallet extends LegacyWallet { // nop } - getTypeReadable() { - return 'Lightning'; - } - async createAccount(isTest) { let response = await this._api.post('/create', { body: { partnerid: 'bluewallet', accounttype: (isTest && 'test') || 'common' }, diff --git a/class/segwit-bech-wallet.js b/class/segwit-bech-wallet.js index da8d8a75..a7a2904e 100644 --- a/class/segwit-bech-wallet.js +++ b/class/segwit-bech-wallet.js @@ -2,14 +2,8 @@ import { LegacyWallet } from './legacy-wallet'; const bitcoin = require('bitcoinjs-lib'); export class SegwitBech32Wallet extends LegacyWallet { - constructor() { - super(); - this.type = 'segwitBech32'; - } - - getTypeReadable() { - return 'P2 WPKH'; - } + static type = 'segwitBech32'; + static typeReadable = 'P2 WPKH'; getAddress() { if (this._address) return this._address; diff --git a/class/segwit-p2sh-wallet.js b/class/segwit-p2sh-wallet.js index dc520153..2f850748 100644 --- a/class/segwit-p2sh-wallet.js +++ b/class/segwit-p2sh-wallet.js @@ -4,19 +4,13 @@ const signer = require('../models/signer'); const BigNumber = require('bignumber.js'); export class SegwitP2SHWallet extends LegacyWallet { - constructor() { - super(); - this.type = 'segwitP2SH'; - } + static type = 'segwitP2SH'; + static typeReadable = 'SegWit (P2SH)'; allowRBF() { return true; } - getTypeReadable() { - return 'SegWit (P2SH)'; - } - static witnessToAddress(witness) { const pubKey = Buffer.from(witness, 'hex'); const pubKeyHash = bitcoin.crypto.hash160(pubKey); diff --git a/class/watch-only-wallet.js b/class/watch-only-wallet.js index 7ac2618b..9090f693 100644 --- a/class/watch-only-wallet.js +++ b/class/watch-only-wallet.js @@ -2,14 +2,8 @@ import { LegacyWallet } from './legacy-wallet'; const bitcoin = require('bitcoinjs-lib'); export class WatchOnlyWallet extends LegacyWallet { - constructor() { - super(); - this.type = 'watchOnly'; - } - - getTypeReadable() { - return 'Watch-only'; - } + static type = 'watchOnly'; + static typeReadable = 'Watch-only'; allowSend() { return false; diff --git a/screen/lnd/manageFunds.js b/screen/lnd/manageFunds.js index f344c357..a145dbe9 100644 --- a/screen/lnd/manageFunds.js +++ b/screen/lnd/manageFunds.js @@ -47,7 +47,7 @@ export default class ManageFunds extends Component { data = []; for (let c = 0; c < BlueApp.getWallets().length; c++) { let w = BlueApp.getWallets()[c]; - if (w.type !== new LightningCustodianWallet().type) { + if (w.type !== LightningCustodianWallet.type) { data.push({ value: c, label: w.getLabel() + ' (' + w.getBalance() + ' BTC)', diff --git a/screen/lnd/scanLndInvoice.js b/screen/lnd/scanLndInvoice.js index 7d00a208..5845f81a 100644 --- a/screen/lnd/scanLndInvoice.js +++ b/screen/lnd/scanLndInvoice.js @@ -30,7 +30,7 @@ export default class ScanLndInvoice extends React.Component { let fromWallet = {}; if (!fromSecret) { - const lightningWallets = BlueApp.getWallets().filter(item => item.type === new LightningCustodianWallet().type); + const lightningWallets = BlueApp.getWallets().filter(item => item.type === LightningCustodianWallet.type); if (lightningWallets.length > 0) { fromSecret = lightningWallets[0].getSecret(); } diff --git a/screen/send/details.js b/screen/send/details.js index bcd0d303..2c68673a 100644 --- a/screen/send/details.js +++ b/screen/send/details.js @@ -348,7 +348,7 @@ export default class SendDetails extends Component { fee: this.calculateFee( utxo, tx, - this.state.fromWallet.type === new HDSegwitP2SHWallet().type || this.state.fromWallet.type === new HDLegacyP2PKHWallet().type, + this.state.fromWallet.type === HDSegwitP2SHWallet.type || this.state.fromWallet.type === HDLegacyP2PKHWallet.type, ), address: this.state.address, memo: this.state.memo, diff --git a/screen/settings/lightningSettings.js b/screen/settings/lightningSettings.js index 0de6999e..802747ef 100644 --- a/screen/settings/lightningSettings.js +++ b/screen/settings/lightningSettings.js @@ -45,7 +45,7 @@ export default class LightningSettings extends Component { // set each lnd wallets and re-init api for (/** @type {LightningCustodianWallet} */ let w of BlueApp.getWallets()) { - if (w.type === new LightningCustodianWallet().type) { + if (w.type === LightningCustodianWallet.type) { w.setBaseURI(this.state.URI); w.init(); console.log('inited', w.baseURI); diff --git a/screen/wallets/add.js b/screen/wallets/add.js index 527401b1..e2bfaa14 100644 --- a/screen/wallets/add.js +++ b/screen/wallets/add.js @@ -156,11 +156,11 @@ export default class WalletsAdd extends Component { }} > this.onSelect(index, value)} selectedIndex={0}> - - {new HDSegwitP2SHWallet().getTypeReadable()} + + {HDSegwitP2SHWallet.typeReadable} - - {new SegwitP2SHWallet().getTypeReadable()} + + {SegwitP2SHWallet.typeReadable} @@ -197,14 +197,14 @@ export default class WalletsAdd extends Component { if (global.lightning_create_try++ < 9 && +new Date() < 1545264000000) return alert('Coming soon'); // eslint-disable-next-line for (let t of BlueApp.getWallets()) { - if (t.type === new LightningCustodianWallet().type) { + if (t.type === LightningCustodianWallet.type) { // already exist return alert('Only 1 Ligthning wallet allowed for now'); } } w = new LightningCustodianWallet(); - w.setLabel(this.state.label || w.getTypeReadable()); + w.setLabel(this.state.label || w.typeReadable); try { let lndhub = await AsyncStorage.getItem(AppStorage.LNDHUB); diff --git a/screen/wallets/details.js b/screen/wallets/details.js index d123ade7..d1c12d73 100644 --- a/screen/wallets/details.js +++ b/screen/wallets/details.js @@ -125,7 +125,7 @@ export default class WalletDetails extends Component { {loc.wallets.details.type.toLowerCase()} - {this.state.wallet.getTypeReadable()} + {this.state.wallet.typeReadable} @@ -141,9 +141,9 @@ export default class WalletDetails extends Component { - {(this.state.wallet.type === new HDLegacyBreadwalletWallet().type || - this.state.wallet.type === new HDLegacyP2PKHWallet().type || - this.state.wallet.type === new HDSegwitP2SHWallet().type) && ( + {(this.state.wallet.type === HDLegacyBreadwalletWallet.type || + this.state.wallet.type === HDLegacyP2PKHWallet.type || + this.state.wallet.type === HDSegwitP2SHWallet.type) && ( this.props.navigation.navigate('WalletXpub', { @@ -156,7 +156,7 @@ export default class WalletDetails extends Component { - {this.state.wallet.type !== new LightningCustodianWallet().type && ( + {this.state.wallet.type !== LightningCustodianWallet.type && ( - {this.state.wallet.getTypeReadable()} + {this.state.wallet.typeReadable} {(() => { diff --git a/screen/wallets/import.js b/screen/wallets/import.js index d6923864..1d35ad4d 100644 --- a/screen/wallets/import.js +++ b/screen/wallets/import.js @@ -53,7 +53,7 @@ export default class WalletsImport extends Component { async _saveWallet(w) { alert(loc.wallets.import.success); ReactNativeHapticFeedback.trigger('notificationSuccess', false); - w.setLabel(loc.wallets.import.imported + ' ' + w.getTypeReadable()); + w.setLabel(loc.wallets.import.imported + ' ' + w.typeReadable); BlueApp.wallets.push(w); await BlueApp.saveToDisk(); EV(EV.enum.WALLETS_COUNT_CHANGED); @@ -67,7 +67,7 @@ export default class WalletsImport extends Component { if (text.indexOf('blitzhub://') !== -1 || text.indexOf('lndhub://') !== -1) { // yep its lnd for (let t of BlueApp.getWallets()) { - if (t.type === new LightningCustodianWallet().type) { + if (t.type === LightningCustodianWallet.type) { // already exist return alert('Only 1 Ligthning wallet allowed for now'); } diff --git a/screen/wallets/reorderWallets.js b/screen/wallets/reorderWallets.js index ea0321f6..1931fffd 100644 --- a/screen/wallets/reorderWallets.js +++ b/screen/wallets/reorderWallets.js @@ -70,32 +70,32 @@ export default class ReorderWallets extends Component { let gradient1 = '#65ceef'; let gradient2 = '#68bbe1'; - if (new WatchOnlyWallet().type === item.type) { + if (WatchOnlyWallet.type === item.type) { gradient1 = '#7d7d7d'; gradient2 = '#4a4a4a'; } - if (new LegacyWallet().type === item.type) { + if (LegacyWallet.type === item.type) { gradient1 = '#40fad1'; gradient2 = '#15be98'; } - if (new HDLegacyP2PKHWallet().type === item.type) { + if (HDLegacyP2PKHWallet.type === item.type) { gradient1 = '#e36dfa'; gradient2 = '#bd10e0'; } - if (new HDLegacyBreadwalletWallet().type === item.type) { + if (HDLegacyBreadwalletWallet.type === item.type) { gradient1 = '#fe6381'; gradient2 = '#f99c42'; } - if (new HDSegwitP2SHWallet().type === item.type) { + if (HDSegwitP2SHWallet.type === item.type) { gradient1 = '#c65afb'; gradient2 = '#9053fe'; } - if (new LightningCustodianWallet().type === item.type) { + if (LightningCustodianWallet.type === item.type) { gradient1 = '#f1be07'; gradient2 = '#f79056'; } @@ -119,8 +119,7 @@ export default class ReorderWallets extends Component { > EV(EV.enum.WALLETS_COUNT_CHANGED), 500); diff --git a/screen/wallets/selectWallet.js b/screen/wallets/selectWallet.js index bc150d80..7fd53d71 100644 --- a/screen/wallets/selectWallet.js +++ b/screen/wallets/selectWallet.js @@ -28,7 +28,7 @@ export default class SelectWallet extends Component { } componentDidMount() { - const wallets = BlueApp.getWallets().filter(item => item.type !== new LightningCustodianWallet().type); + const wallets = BlueApp.getWallets().filter(item => item.type !== LightningCustodianWallet.type); this.setState({ data: wallets, isLoading: false, @@ -39,32 +39,32 @@ export default class SelectWallet extends Component { let gradient1 = '#65ceef'; let gradient2 = '#68bbe1'; - if (new WatchOnlyWallet().type === item.type) { + if (WatchOnlyWallet.type === item.type) { gradient1 = '#7d7d7d'; gradient2 = '#4a4a4a'; } - if (new LegacyWallet().type === item.type) { + if (LegacyWallet.type === item.type) { gradient1 = '#40fad1'; gradient2 = '#15be98'; } - if (new HDLegacyP2PKHWallet().type === item.type) { + if (HDLegacyP2PKHWallet.type === item.type) { gradient1 = '#e36dfa'; gradient2 = '#bd10e0'; } - if (new HDLegacyBreadwalletWallet().type === item.type) { + if (HDLegacyBreadwalletWallet.type === item.type) { gradient1 = '#fe6381'; gradient2 = '#f99c42'; } - if (new HDSegwitP2SHWallet().type === item.type) { + if (HDSegwitP2SHWallet.type === item.type) { gradient1 = '#c65afb'; gradient2 = '#9053fe'; } - if (new LightningCustodianWallet().type === item.type) { + if (LightningCustodianWallet.type === item.type) { gradient1 = '#f1be07'; gradient2 = '#f79056'; } @@ -94,8 +94,7 @@ export default class SelectWallet extends Component { > 0) { + } else if (wallet && wallet.type === LightningCustodianWallet.type && wallet.getBalance() > 0) { showManageFundsSmallButton = true; showManageFundsBigButton = false; } @@ -126,7 +126,7 @@ export default class WalletTransactions extends Component { isLightning() { let w = this.state.wallet; - if (w && w.type === new LightningCustodianWallet().type) { + if (w && w.type === LightningCustodianWallet.type) { return true; } @@ -198,7 +198,7 @@ export default class WalletTransactions extends Component { { - if (this.state.wallet.type === new LightningCustodianWallet().type) { + if (this.state.wallet.type === LightningCustodianWallet.type) { navigate('ScanLndInvoice', { fromSecret: this.state.wallet.getSecret() }); } else { navigate('SendDetails', { fromAddress: this.state.wallet.getAddress(), fromSecret: this.state.wallet.getSecret() }); diff --git a/screen/wallets/xpub.js b/screen/wallets/xpub.js index 010635a9..a329263f 100644 --- a/screen/wallets/xpub.js +++ b/screen/wallets/xpub.js @@ -80,12 +80,10 @@ export default class WalletXpub extends Component { return ( - {isIpad && ( - - )} + {isIpad && } - {this.state.wallet.getTypeReadable()} + {this.state.wallet.typeReadable} {(() => {