REF: better support for different lndhub uris

This commit is contained in:
Overtorment 2019-03-08 21:54:47 +00:00 committed by Igor Korsakov
parent c1a03cb1cb
commit d3b059c2b1
5 changed files with 78 additions and 37 deletions

View file

@ -159,12 +159,16 @@ export class AppStorage {
} catch (Error) {
console.warn(Error);
}
if (lndhub) {
console.log('using', lndhub, 'for lndhub wallet');
if (unserializedWallet.baseURI) {
unserializedWallet.setBaseURI(unserializedWallet.baseURI); // not really necessary, just for the sake of readability
console.log('using saved uri for for ln wallet:', unserializedWallet.baseURI);
} else if (lndhub) {
console.log('using wallet-wide settings ', lndhub, 'for ln wallet');
unserializedWallet.setBaseURI(lndhub);
} else {
unserializedWallet.setBaseURI();
console.log('using default uri for for lndhub wallet:', unserializedWallet.baseURI);
console.log('using default', LightningCustodianWallet.defaultBaseUri, 'for ln wallet');
unserializedWallet.setBaseURI(LightningCustodianWallet.defaultBaseUri);
}
unserializedWallet.init();
break;

View file

@ -6,7 +6,7 @@ let BigNumber = require('bignumber.js');
export class LightningCustodianWallet extends LegacyWallet {
static type = 'lightningCustodianWallet';
static typeReadable = 'Lightning';
static defaultBaseUri = 'https://lndhub.herokuapp.com/';
constructor(props) {
super(props);
this.setBaseURI(); // no args to init with default value
@ -32,7 +32,7 @@ export class LightningCustodianWallet extends LegacyWallet {
if (URI) {
this.baseURI = URI;
} else {
this.baseURI = 'https://lndhub.herokuapp.com/';
this.baseURI = LightningCustodianWallet.defaultBaseUri;
}
}
@ -48,6 +48,13 @@ export class LightningCustodianWallet extends LegacyWallet {
return '';
}
getSecret() {
if (this.baseURI === LightningCustodianWallet.defaultBaseUri) {
return this.secret;
}
return this.secret + '@' + this.baseURI;
}
timeToRefreshBalance() {
return (+new Date() - this._lastBalanceFetch) / 1000 > 3600; // 1hr
}
@ -548,10 +555,30 @@ export class LightningCustodianWallet extends LegacyWallet {
if (!json.identity_pubkey) {
throw new Error('API unexpected response: ' + JSON.stringify(response.body));
}
this.info_raw = json;
}
static async isValidNodeAddress(address) {
let apiCall = new Frisbee({
baseURI: address,
});
let response = await apiCall.get('/getinfo', {
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
},
});
let json = response.body;
if (typeof json === 'undefined') {
throw new Error('API failure: ' + response.err + ' ' + JSON.stringify(response.body));
}
if (json && json.code && json.code !== 1) {
throw new Error('API error: ' + json.message + ' (code ' + json.code + ')');
}
return true;
}
allowReceive() {
return true;
}

View file

@ -1,3 +1,4 @@
/* global alert */
import React, { Component } from 'react';
import { AsyncStorage, View, TextInput, Linking } from 'react-native';
import { AppStorage } from '../../class';
@ -28,29 +29,28 @@ export default class LightningSettings extends Component {
this.setState({
isLoading: false,
URI,
defaultURI: new LightningCustodianWallet().getBaseURI(),
});
}
async save() {
this.state.URI = this.state.URI ? this.state.URI : '';
await AsyncStorage.setItem(AppStorage.LNDHUB, this.state.URI);
// set each lnd wallets and re-init api
for (/** @type {LightningCustodianWallet} */ let w of BlueApp.getWallets()) {
if (w.type === LightningCustodianWallet.type) {
w.setBaseURI(this.state.URI);
w.init();
console.log('inited', w.baseURI);
save = () => {
this.setState({ isLoading: true }, async () => {
this.state.URI = this.state.URI ? this.state.URI : '';
try {
if (this.state.URI) {
await LightningCustodianWallet.isValidNodeAddress(this.state.URI);
// validating only if its not empty. empty means use default
}
await AsyncStorage.setItem(AppStorage.LNDHUB, this.state.URI);
alert('Your changes have been saved successfully');
} catch (error) {
alert('Not a valid LndHub URI');
console.log(error);
}
}
}
this.setState({ isLoading: false });
});
};
render() {
if (this.state.isLoading) {
return <BlueLoading />;
}
return (
<SafeBlueArea forceInset={{ horizontal: 'always' }} style={{ flex: 1 }}>
<BlueCard>
@ -90,7 +90,7 @@ export default class LightningSettings extends Component {
}}
>
<TextInput
placeholder={this.state.defaultURI}
placeholder={LightningCustodianWallet.defaultBaseUri}
value={this.state.URI}
onChangeText={text => this.setState({ URI: text })}
numberOfLines={1}
@ -101,12 +101,7 @@ export default class LightningSettings extends Component {
</View>
<BlueSpacing20 />
<BlueButton
onPress={() => {
this.save();
}}
title={loc.settings.save}
/>
{this.state.isLoading ? <BlueLoading /> : <BlueButton onPress={this.save} title={loc.settings.save} />}
</BlueCard>
</SafeBlueArea>
);

View file

@ -1,7 +1,7 @@
/* global alert */
import React, { Component } from 'react';
import { ActivityIndicator, View, Text, TextInput, Alert, TouchableOpacity, Keyboard, TouchableWithoutFeedback } from 'react-native';
import { BlueButton, SafeBlueArea, BlueCard, BlueSpacing20, BlueNavigationStyle } from '../../BlueComponents';
import { BlueButton, SafeBlueArea, BlueCard, BlueSpacing20, BlueNavigationStyle, BlueText } from '../../BlueComponents';
import PropTypes from 'prop-types';
import { LightningCustodianWallet } from '../../class/lightning-custodian-wallet';
import { HDLegacyBreadwalletWallet } from '../../class/hd-legacy-breadwallet-wallet';
@ -22,7 +22,9 @@ export default class WalletDetails extends Component {
disabled={navigation.getParam('isLoading') === true}
style={{ marginHorizontal: 16, height: 40, width: 40, justifyContent: 'center', alignItems: 'center' }}
onPress={() => {
navigation.getParam('saveAction')();
if (navigation.state.params.saveAction) {
navigation.getParam('saveAction')();
}
}}
>
<Text style={{ color: '#0c2550' }}>{loc.wallets.details.save}</Text>
@ -54,7 +56,7 @@ export default class WalletDetails extends Component {
setLabel() {
this.props.navigation.setParams({ isLoading: true });
this.setState({ isLoading: true }, () => {
this.setState({ isLoading: true }, async () => {
this.state.wallet.setLabel(this.state.walletName);
BlueApp.saveToDisk();
alert('Wallet updated.');
@ -120,6 +122,12 @@ export default class WalletDetails extends Component {
{loc.wallets.details.type.toLowerCase()}
</Text>
<Text style={{ color: '#81868e', fontWeight: '500', fontSize: 14 }}>{this.state.wallet.typeReadable}</Text>
{this.state.wallet.type === LightningCustodianWallet.type && (
<React.Fragment>
<Text style={{ color: '#0c2550', fontWeight: '500', fontSize: 14, marginVertical: 12 }}>{'connected to'}</Text>
<BlueText>{this.state.wallet.getBaseURI()}</BlueText>
</React.Fragment>
)}
<View>
<BlueSpacing20 />
<BlueButton

View file

@ -31,10 +31,10 @@ let loc = require('../../loc');
const { width } = Dimensions.get('window');
export default class WalletsImport extends Component {
static navigationOptions = ({ navigation }) => ({
static navigationOptions = {
...BlueNavigationStyle(),
title: loc.wallets.import.title,
});
};
constructor(props) {
super(props);
@ -75,7 +75,14 @@ export default class WalletsImport extends Component {
// is it lightning custodian?
if (text.indexOf('blitzhub://') !== -1 || text.indexOf('lndhub://') !== -1) {
let lnd = new LightningCustodianWallet();
lnd.setSecret(text);
if (text.includes('@')) {
const split = text.split('@');
lnd.setBaseURI(split[1]);
lnd.setSecret(split[0]);
} else {
lnd.setBaseURI(LightningCustodianWallet.defaultBaseUri);
lnd.setSecret(text);
}
await lnd.authorize();
await lnd.fetchTransactions();
await lnd.fetchBalance();