You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

244 lines
8.2 KiB

7 years ago
/* global alert */
7 years ago
import React from 'react';
import { ActivityIndicator, Image, View, TouchableOpacity } from 'react-native';
7 years ago
import { BlueText, SafeBlueArea, BlueButton } from '../../BlueComponents';
import Camera from 'react-native-camera';
import Permissions from 'react-native-permissions';
import { SegwitP2SHWallet, LegacyWallet, WatchOnlyWallet } from '../../class';
7 years ago
import PropTypes from 'prop-types';
import { HDSegwitP2SHWallet } from '../../class/hd-segwit-p2sh-wallet';
import { LightningCustodianWallet } from '../../class/lightning-custodian-wallet';
/** @type {AppStorage} */
7 years ago
let BlueApp = require('../../BlueApp');
7 years ago
let EV = require('../../events');
7 years ago
let bip38 = require('../../bip38');
7 years ago
let wif = require('wif');
let prompt = require('../../prompt');
let loc = require('../../loc');
7 years ago
export default class ScanQrWif extends React.Component {
7 years ago
static navigationOptions = {
header: null,
7 years ago
};
7 years ago
state = {
7 years ago
isLoading: false,
7 years ago
hasCameraPermission: null,
};
async onBarCodeScanned(ret) {
7 years ago
if (+new Date() - this.lastTimeIveBeenHere < 3000) {
this.lastTimeIveBeenHere = +new Date();
return;
}
this.lastTimeIveBeenHere = +new Date();
console.log('onBarCodeScanned', ret);
7 years ago
if (ret.data[0] === '6') {
// password-encrypted, need to ask for password and decrypt
7 years ago
console.log('trying to decrypt...');
7 years ago
this.setState({
message: loc.wallets.scanQrWif.decoding,
7 years ago
});
shold_stop_bip38 = undefined; // eslint-disable-line
let password = await prompt(loc.wallets.scanQrWif.input_password, loc.wallets.scanQrWif.password_explain);
7 years ago
if (!password) {
return;
}
let that = this;
try {
let decryptedKey = await bip38.decrypt(ret.data, password, function(status) {
7 years ago
that.setState({
message: loc.wallets.scanQrWif.decoding + '... ' + status.percent.toString().substr(0, 4) + ' %',
7 years ago
});
});
ret.data = wif.encode(0x80, decryptedKey.privateKey, decryptedKey.compressed);
7 years ago
} catch (e) {
7 years ago
console.log(e.message);
7 years ago
this.setState({ message: false });
return alert(loc.wallets.scanQrWif.bad_password);
7 years ago
}
this.setState({ message: false });
}
7 years ago
for (let w of BlueApp.wallets) {
7 years ago
if (w.getSecret() === ret.data) {
// lookig for duplicates
return alert(loc.wallets.scanQrWif.wallet_already_exists); // duplicate, not adding
7 years ago
}
}
// is it HD mnemonic?
let hd = new HDSegwitP2SHWallet();
hd.setSecret(ret.data);
if (hd.validateMnemonic()) {
for (let w of BlueApp.wallets) {
if (w.getSecret() === hd.getSecret()) {
// lookig for duplicates
return alert(loc.wallets.scanQrWif.wallet_already_exists); // duplicate, not adding
}
}
this.setState({ isLoading: true });
hd.setLabel(loc.wallets.import.imported + ' ' + hd.getTypeReadable());
BlueApp.wallets.push(hd);
await hd.fetchBalance();
await hd.fetchTransactions();
await BlueApp.saveToDisk();
alert(loc.wallets.import.success);
this.props.navigation.popToTop();
setTimeout(() => EV(EV.enum.WALLETS_COUNT_CHANGED), 500);
return;
}
// nope
// is it lndhub?
if (ret.data.indexOf('blitzhub://') !== -1 || ret.data.indexOf('lndhub://') !== -1) {
this.setState({ isLoading: true });
let lnd = new LightningCustodianWallet();
lnd.setSecret(ret.data);
try {
await lnd.authorize();
await lnd.fetchTransactions();
await lnd.fetchBalance();
} catch (Err) {
console.log(Err);
return;
}
BlueApp.wallets.push(lnd);
lnd.setLabel(loc.wallets.import.imported + ' ' + lnd.getTypeReadable());
this.props.navigation.popToTop();
alert(loc.wallets.import.success);
setTimeout(() => EV(EV.enum.WALLETS_COUNT_CHANGED), 500);
return;
}
// nope
// is it just address..?
let watchOnly = new WatchOnlyWallet();
if (watchOnly.isAddressValid(ret.data)) {
watchOnly.setSecret(ret.data);
watchOnly.setLabel(loc.wallets.scanQrWif.imported_watchonly);
BlueApp.wallets.push(watchOnly);
alert(loc.wallets.scanQrWif.imported_watchonly + loc.wallets.scanQrWif.with_address + watchOnly.getAddress());
this.props.navigation.popToTop();
await watchOnly.fetchBalance();
await watchOnly.fetchTransactions();
await BlueApp.saveToDisk();
setTimeout(() => EV(EV.enum.WALLETS_COUNT_CHANGED), 500);
return;
}
// nope
7 years ago
let newWallet = new SegwitP2SHWallet();
newWallet.setSecret(ret.data);
let newLegacyWallet = new LegacyWallet();
newLegacyWallet.setSecret(ret.data);
7 years ago
if (newWallet.getAddress() === false || newLegacyWallet.getAddress() === false) {
alert(loc.wallets.scanQrWif.bad_wif);
7 years ago
return;
}
7 years ago
this.setState({ isLoading: true });
await newLegacyWallet.fetchBalance();
console.log('newLegacyWallet == ', newLegacyWallet.getBalance());
if (newLegacyWallet.getBalance()) {
newLegacyWallet.setLabel(loc.wallets.scanQrWif.imported_legacy);
BlueApp.wallets.push(newLegacyWallet);
alert(loc.wallets.scanQrWif.imported_wif + ret.data + loc.wallets.scanQrWif.with_address + newLegacyWallet.getAddress());
await newLegacyWallet.fetchTransactions();
} else {
await newWallet.fetchBalance();
await newWallet.fetchTransactions();
newWallet.setLabel(loc.wallets.scanQrWif.imported_segwit);
BlueApp.wallets.push(newWallet);
alert(loc.wallets.scanQrWif.imported_wif + ret.data + loc.wallets.scanQrWif.with_address + newWallet.getAddress());
}
await BlueApp.saveToDisk();
this.props.navigation.popToTop();
setTimeout(() => EV(EV.enum.WALLETS_COUNT_CHANGED), 500);
7 years ago
} // end
async componentWillMount() {
Permissions.request('camera').then(response => {
// Response is one of: 'authorized', 'denied', 'restricted', or 'undetermined'
this.setState({ hasCameraPermission: response === 'authorized' });
});
7 years ago
}
render() {
if (this.state.isLoading) {
return (
7 years ago
<View style={{ flex: 1, paddingTop: 20 }}>
7 years ago
<ActivityIndicator />
</View>
);
}
const { hasCameraPermission } = this.state;
if (hasCameraPermission === null) {
return <View />;
} else if (hasCameraPermission === false) {
alert('BlueWallet does not have permission to use your camera.');
this.props.navigation.goBack(null);
return <View />;
7 years ago
} else {
return (
<View style={{ flex: 1 }}>
7 years ago
{(() => {
if (this.state.message) {
return (
<SafeBlueArea>
<View
style={{
flex: 1,
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
}}
>
<BlueText>{this.state.message}</BlueText>
<BlueButton
icon={{ name: 'stop', type: 'octicon' }}
onPress={async () => {
this.setState({ message: false });
shold_stop_bip38 = true; // eslint-disable-line
}}
title={loc.wallets.scanQrWif.cancel}
7 years ago
/>
</View>
</SafeBlueArea>
);
} else {
return (
<SafeBlueArea style={{ flex: 1 }}>
<Camera style={{ flex: 1 }} onBarCodeRead={ret => this.onBarCodeScanned(ret)}>
7 years ago
<TouchableOpacity
style={{ width: 80, height: 80, padding: 14, marginTop: 32 }}
onPress={() => this.props.navigation.goBack(null)}
7 years ago
>
<Image style={{ alignSelf: 'center' }} source={require('../../img/close.png')} />
7 years ago
</TouchableOpacity>
</Camera>
</SafeBlueArea>
7 years ago
);
}
})()}
7 years ago
</View>
);
}
}
7 years ago
}
7 years ago
ScanQrWif.propTypes = {
7 years ago
navigation: PropTypes.shape({
goBack: PropTypes.func,
popToTop: PropTypes.func,
navigate: PropTypes.func,
7 years ago
}),
};