mirror of
https://github.com/BlueWallet/BlueWallet.git
synced 2025-02-21 14:34:55 +01:00
REF: better support for different lndhub uris
This commit is contained in:
parent
c1a03cb1cb
commit
d3b059c2b1
5 changed files with 78 additions and 37 deletions
|
@ -159,12 +159,16 @@ export class AppStorage {
|
||||||
} catch (Error) {
|
} catch (Error) {
|
||||||
console.warn(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);
|
unserializedWallet.setBaseURI(lndhub);
|
||||||
} else {
|
} else {
|
||||||
unserializedWallet.setBaseURI();
|
console.log('using default', LightningCustodianWallet.defaultBaseUri, 'for ln wallet');
|
||||||
console.log('using default uri for for lndhub wallet:', unserializedWallet.baseURI);
|
unserializedWallet.setBaseURI(LightningCustodianWallet.defaultBaseUri);
|
||||||
}
|
}
|
||||||
unserializedWallet.init();
|
unserializedWallet.init();
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -6,7 +6,7 @@ let BigNumber = require('bignumber.js');
|
||||||
export class LightningCustodianWallet extends LegacyWallet {
|
export class LightningCustodianWallet extends LegacyWallet {
|
||||||
static type = 'lightningCustodianWallet';
|
static type = 'lightningCustodianWallet';
|
||||||
static typeReadable = 'Lightning';
|
static typeReadable = 'Lightning';
|
||||||
|
static defaultBaseUri = 'https://lndhub.herokuapp.com/';
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props);
|
||||||
this.setBaseURI(); // no args to init with default value
|
this.setBaseURI(); // no args to init with default value
|
||||||
|
@ -32,7 +32,7 @@ export class LightningCustodianWallet extends LegacyWallet {
|
||||||
if (URI) {
|
if (URI) {
|
||||||
this.baseURI = URI;
|
this.baseURI = URI;
|
||||||
} else {
|
} else {
|
||||||
this.baseURI = 'https://lndhub.herokuapp.com/';
|
this.baseURI = LightningCustodianWallet.defaultBaseUri;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,6 +48,13 @@ export class LightningCustodianWallet extends LegacyWallet {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getSecret() {
|
||||||
|
if (this.baseURI === LightningCustodianWallet.defaultBaseUri) {
|
||||||
|
return this.secret;
|
||||||
|
}
|
||||||
|
return this.secret + '@' + this.baseURI;
|
||||||
|
}
|
||||||
|
|
||||||
timeToRefreshBalance() {
|
timeToRefreshBalance() {
|
||||||
return (+new Date() - this._lastBalanceFetch) / 1000 > 3600; // 1hr
|
return (+new Date() - this._lastBalanceFetch) / 1000 > 3600; // 1hr
|
||||||
}
|
}
|
||||||
|
@ -548,10 +555,30 @@ export class LightningCustodianWallet extends LegacyWallet {
|
||||||
if (!json.identity_pubkey) {
|
if (!json.identity_pubkey) {
|
||||||
throw new Error('API unexpected response: ' + JSON.stringify(response.body));
|
throw new Error('API unexpected response: ' + JSON.stringify(response.body));
|
||||||
}
|
}
|
||||||
|
|
||||||
this.info_raw = json;
|
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() {
|
allowReceive() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
/* global alert */
|
||||||
import React, { Component } from 'react';
|
import React, { Component } from 'react';
|
||||||
import { AsyncStorage, View, TextInput, Linking } from 'react-native';
|
import { AsyncStorage, View, TextInput, Linking } from 'react-native';
|
||||||
import { AppStorage } from '../../class';
|
import { AppStorage } from '../../class';
|
||||||
|
@ -28,29 +29,28 @@ export default class LightningSettings extends Component {
|
||||||
this.setState({
|
this.setState({
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
URI,
|
URI,
|
||||||
defaultURI: new LightningCustodianWallet().getBaseURI(),
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async save() {
|
save = () => {
|
||||||
this.state.URI = this.state.URI ? this.state.URI : '';
|
this.setState({ isLoading: true }, async () => {
|
||||||
await AsyncStorage.setItem(AppStorage.LNDHUB, this.state.URI);
|
this.state.URI = this.state.URI ? this.state.URI : '';
|
||||||
|
try {
|
||||||
// set each lnd wallets and re-init api
|
if (this.state.URI) {
|
||||||
for (/** @type {LightningCustodianWallet} */ let w of BlueApp.getWallets()) {
|
await LightningCustodianWallet.isValidNodeAddress(this.state.URI);
|
||||||
if (w.type === LightningCustodianWallet.type) {
|
// validating only if its not empty. empty means use default
|
||||||
w.setBaseURI(this.state.URI);
|
}
|
||||||
w.init();
|
await AsyncStorage.setItem(AppStorage.LNDHUB, this.state.URI);
|
||||||
console.log('inited', w.baseURI);
|
alert('Your changes have been saved successfully');
|
||||||
|
} catch (error) {
|
||||||
|
alert('Not a valid LndHub URI');
|
||||||
|
console.log(error);
|
||||||
}
|
}
|
||||||
}
|
this.setState({ isLoading: false });
|
||||||
}
|
});
|
||||||
|
};
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
if (this.state.isLoading) {
|
|
||||||
return <BlueLoading />;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<SafeBlueArea forceInset={{ horizontal: 'always' }} style={{ flex: 1 }}>
|
<SafeBlueArea forceInset={{ horizontal: 'always' }} style={{ flex: 1 }}>
|
||||||
<BlueCard>
|
<BlueCard>
|
||||||
|
@ -90,7 +90,7 @@ export default class LightningSettings extends Component {
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<TextInput
|
<TextInput
|
||||||
placeholder={this.state.defaultURI}
|
placeholder={LightningCustodianWallet.defaultBaseUri}
|
||||||
value={this.state.URI}
|
value={this.state.URI}
|
||||||
onChangeText={text => this.setState({ URI: text })}
|
onChangeText={text => this.setState({ URI: text })}
|
||||||
numberOfLines={1}
|
numberOfLines={1}
|
||||||
|
@ -101,12 +101,7 @@ export default class LightningSettings extends Component {
|
||||||
</View>
|
</View>
|
||||||
|
|
||||||
<BlueSpacing20 />
|
<BlueSpacing20 />
|
||||||
<BlueButton
|
{this.state.isLoading ? <BlueLoading /> : <BlueButton onPress={this.save} title={loc.settings.save} />}
|
||||||
onPress={() => {
|
|
||||||
this.save();
|
|
||||||
}}
|
|
||||||
title={loc.settings.save}
|
|
||||||
/>
|
|
||||||
</BlueCard>
|
</BlueCard>
|
||||||
</SafeBlueArea>
|
</SafeBlueArea>
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
/* global alert */
|
/* global alert */
|
||||||
import React, { Component } from 'react';
|
import React, { Component } from 'react';
|
||||||
import { ActivityIndicator, View, Text, TextInput, Alert, TouchableOpacity, Keyboard, TouchableWithoutFeedback } from 'react-native';
|
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 PropTypes from 'prop-types';
|
||||||
import { LightningCustodianWallet } from '../../class/lightning-custodian-wallet';
|
import { LightningCustodianWallet } from '../../class/lightning-custodian-wallet';
|
||||||
import { HDLegacyBreadwalletWallet } from '../../class/hd-legacy-breadwallet-wallet';
|
import { HDLegacyBreadwalletWallet } from '../../class/hd-legacy-breadwallet-wallet';
|
||||||
|
@ -22,7 +22,9 @@ export default class WalletDetails extends Component {
|
||||||
disabled={navigation.getParam('isLoading') === true}
|
disabled={navigation.getParam('isLoading') === true}
|
||||||
style={{ marginHorizontal: 16, height: 40, width: 40, justifyContent: 'center', alignItems: 'center' }}
|
style={{ marginHorizontal: 16, height: 40, width: 40, justifyContent: 'center', alignItems: 'center' }}
|
||||||
onPress={() => {
|
onPress={() => {
|
||||||
navigation.getParam('saveAction')();
|
if (navigation.state.params.saveAction) {
|
||||||
|
navigation.getParam('saveAction')();
|
||||||
|
}
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Text style={{ color: '#0c2550' }}>{loc.wallets.details.save}</Text>
|
<Text style={{ color: '#0c2550' }}>{loc.wallets.details.save}</Text>
|
||||||
|
@ -54,7 +56,7 @@ export default class WalletDetails extends Component {
|
||||||
|
|
||||||
setLabel() {
|
setLabel() {
|
||||||
this.props.navigation.setParams({ isLoading: true });
|
this.props.navigation.setParams({ isLoading: true });
|
||||||
this.setState({ isLoading: true }, () => {
|
this.setState({ isLoading: true }, async () => {
|
||||||
this.state.wallet.setLabel(this.state.walletName);
|
this.state.wallet.setLabel(this.state.walletName);
|
||||||
BlueApp.saveToDisk();
|
BlueApp.saveToDisk();
|
||||||
alert('Wallet updated.');
|
alert('Wallet updated.');
|
||||||
|
@ -120,6 +122,12 @@ export default class WalletDetails extends Component {
|
||||||
{loc.wallets.details.type.toLowerCase()}
|
{loc.wallets.details.type.toLowerCase()}
|
||||||
</Text>
|
</Text>
|
||||||
<Text style={{ color: '#81868e', fontWeight: '500', fontSize: 14 }}>{this.state.wallet.typeReadable}</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>
|
<View>
|
||||||
<BlueSpacing20 />
|
<BlueSpacing20 />
|
||||||
<BlueButton
|
<BlueButton
|
||||||
|
|
|
@ -31,10 +31,10 @@ let loc = require('../../loc');
|
||||||
const { width } = Dimensions.get('window');
|
const { width } = Dimensions.get('window');
|
||||||
|
|
||||||
export default class WalletsImport extends Component {
|
export default class WalletsImport extends Component {
|
||||||
static navigationOptions = ({ navigation }) => ({
|
static navigationOptions = {
|
||||||
...BlueNavigationStyle(),
|
...BlueNavigationStyle(),
|
||||||
title: loc.wallets.import.title,
|
title: loc.wallets.import.title,
|
||||||
});
|
};
|
||||||
|
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props);
|
||||||
|
@ -75,7 +75,14 @@ export default class WalletsImport extends Component {
|
||||||
// is it lightning custodian?
|
// is it lightning custodian?
|
||||||
if (text.indexOf('blitzhub://') !== -1 || text.indexOf('lndhub://') !== -1) {
|
if (text.indexOf('blitzhub://') !== -1 || text.indexOf('lndhub://') !== -1) {
|
||||||
let lnd = new LightningCustodianWallet();
|
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.authorize();
|
||||||
await lnd.fetchTransactions();
|
await lnd.fetchTransactions();
|
||||||
await lnd.fetchBalance();
|
await lnd.fetchBalance();
|
||||||
|
|
Loading…
Add table
Reference in a new issue