BlueWallet/screen/wallets/scanQrWif.js

281 lines
9.4 KiB
JavaScript
Raw Normal View History

2018-03-18 03:48:23 +01:00
/* global alert */
2018-01-30 23:42:38 +01:00
import React from 'react';
import { ActivityIndicator, Image, View, TouchableOpacity } from 'react-native';
2018-04-08 18:21:07 +02:00
import { BlueText, SafeBlueArea, BlueButton } from '../../BlueComponents';
import Camera from 'react-native-camera';
import Permissions from 'react-native-permissions';
2018-12-22 04:43:49 +01:00
import { SegwitP2SHWallet, LegacyWallet, WatchOnlyWallet, HDLegacyP2PKHWallet } from '../../class';
2018-03-18 03:48:23 +01:00
import PropTypes from 'prop-types';
import { HDSegwitP2SHWallet } from '../../class/hd-segwit-p2sh-wallet';
import { LightningCustodianWallet } from '../../class/lightning-custodian-wallet';
import bip21 from 'bip21';
/** @type {AppStorage} */
2018-03-18 03:48:23 +01:00
let BlueApp = require('../../BlueApp');
2018-03-17 21:39:21 +01:00
let EV = require('../../events');
2018-04-28 14:13:36 +02:00
let bip38 = require('../../bip38');
2018-04-08 18:21:07 +02:00
let wif = require('wif');
let prompt = require('../../prompt');
2018-05-28 21:18:11 +02:00
let loc = require('../../loc');
2018-01-30 23:42:38 +01:00
export default class ScanQrWif extends React.Component {
2018-01-30 23:42:38 +01:00
static navigationOptions = {
header: null,
2018-03-17 21:39:21 +01:00
};
2018-01-30 23:42:38 +01:00
state = {
2018-03-17 21:39:21 +01:00
isLoading: false,
2018-01-30 23:42:38 +01:00
hasCameraPermission: null,
};
2018-09-18 08:51:44 +02:00
async onBarCodeScanned(ret) {
if (+new Date() - this.lastTimeIveBeenHere < 6000) {
2018-04-08 18:21:07 +02:00
this.lastTimeIveBeenHere = +new Date();
return;
}
this.lastTimeIveBeenHere = +new Date();
2018-09-18 08:51:44 +02:00
console.log('onBarCodeScanned', ret);
2018-04-08 18:21:07 +02:00
if (ret.data[0] === '6') {
// password-encrypted, need to ask for password and decrypt
2018-04-28 14:13:36 +02:00
console.log('trying to decrypt...');
2018-04-08 18:21:07 +02:00
this.setState({
2018-05-28 21:18:11 +02:00
message: loc.wallets.scanQrWif.decoding,
2018-04-08 18:21:07 +02:00
});
shold_stop_bip38 = undefined; // eslint-disable-line
2018-07-07 15:04:32 +02:00
let password = await prompt(loc.wallets.scanQrWif.input_password, loc.wallets.scanQrWif.password_explain);
2018-04-08 18:21:07 +02:00
if (!password) {
return;
}
let that = this;
try {
2018-07-07 15:04:32 +02:00
let decryptedKey = await bip38.decrypt(ret.data, password, function(status) {
2018-04-08 18:21:07 +02:00
that.setState({
2018-07-07 15:04:32 +02:00
message: loc.wallets.scanQrWif.decoding + '... ' + status.percent.toString().substr(0, 4) + ' %',
2018-04-08 18:21:07 +02:00
});
});
2018-07-07 15:04:32 +02:00
ret.data = wif.encode(0x80, decryptedKey.privateKey, decryptedKey.compressed);
2018-04-08 18:21:07 +02:00
} catch (e) {
2018-04-28 14:13:36 +02:00
console.log(e.message);
2018-04-08 18:21:07 +02:00
this.setState({ message: false });
2018-05-28 21:18:11 +02:00
return alert(loc.wallets.scanQrWif.bad_password);
2018-04-08 18:21:07 +02:00
}
this.setState({ message: false });
}
2018-03-17 21:39:21 +01:00
for (let w of BlueApp.wallets) {
2018-01-30 23:42:38 +01:00
if (w.getSecret() === ret.data) {
// lookig for duplicates
return alert(loc.wallets.scanQrWif.wallet_already_exists); // duplicate, not adding
2018-01-30 23:42:38 +01:00
}
}
// is it HD legacy (BIP44) mnemonic?
let hd = new HDLegacyP2PKHWallet();
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 });
await hd.fetchTransactions();
if (hd.getTransactions().length !== 0) {
await hd.fetchBalance();
hd.setLabel(loc.wallets.import.imported + ' ' + hd.typeReadable);
BlueApp.wallets.push(hd);
await BlueApp.saveToDisk();
alert(loc.wallets.import.success);
this.props.navigation.popToTop();
setTimeout(() => EV(EV.enum.WALLETS_COUNT_CHANGED), 500);
return;
}
}
// nope
// is it HD mnemonic?
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.typeReadable);
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.typeReadable);
this.props.navigation.popToTop();
alert(loc.wallets.import.success);
setTimeout(() => EV(EV.enum.WALLETS_COUNT_CHANGED), 500);
return;
}
// nope
2018-07-14 21:53:43 +02:00
// is it just address..?
let watchOnly = new WatchOnlyWallet();
let watchAddr = ret.data;
// Is it BIP21 format?
if (ret.data.indexOf('bitcoin:') === 0 || ret.data.indexOf('BITCOIN:') === 0) {
try {
watchAddr = bip21.decode(ret.data).address;
} catch (err) {
console.log(err);
}
}
if (watchOnly.isAddressValid(watchAddr)) {
watchOnly.setSecret(watchAddr);
2018-07-14 21:53:43 +02:00
watchOnly.setLabel(loc.wallets.scanQrWif.imported_watchonly);
BlueApp.wallets.push(watchOnly);
alert(loc.wallets.scanQrWif.imported_watchonly + loc.wallets.scanQrWif.with_address + watchOnly.getAddress());
2018-10-10 17:46:36 +02:00
this.props.navigation.popToTop();
2018-07-14 21:53:43 +02:00
await watchOnly.fetchBalance();
await watchOnly.fetchTransactions();
await BlueApp.saveToDisk();
setTimeout(() => EV(EV.enum.WALLETS_COUNT_CHANGED), 500);
return;
}
// nope
2018-03-17 21:39:21 +01:00
let newWallet = new SegwitP2SHWallet();
newWallet.setSecret(ret.data);
let newLegacyWallet = new LegacyWallet();
newLegacyWallet.setSecret(ret.data);
2018-01-30 23:42:38 +01:00
2018-07-07 15:04:32 +02:00
if (newWallet.getAddress() === false || newLegacyWallet.getAddress() === false) {
2018-05-28 21:18:11 +02:00
alert(loc.wallets.scanQrWif.bad_wif);
2018-04-08 18:21:07 +02:00
return;
}
2018-01-30 23:42:38 +01:00
this.setState({ isLoading: true });
await newLegacyWallet.fetchBalance();
console.log('newLegacyWallet == ', newLegacyWallet.getBalance());
if (newLegacyWallet.getBalance()) {
2018-05-28 21:18:11 +02:00
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());
2018-07-14 21:53:43 +02:00
await newLegacyWallet.fetchTransactions();
} else {
2018-07-06 18:28:27 +02:00
await newWallet.fetchBalance();
await newWallet.fetchTransactions();
2018-05-28 21:18:11 +02:00
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();
2018-07-06 18:28:27 +02:00
this.props.navigation.popToTop();
setTimeout(() => EV(EV.enum.WALLETS_COUNT_CHANGED), 500);
2018-01-30 23:42:38 +01:00
} // end
async componentWillMount() {
Permissions.request('camera').then(response => {
// Response is one of: 'authorized', 'denied', 'restricted', or 'undetermined'
this.setState({ hasCameraPermission: response === 'authorized' });
});
2018-01-30 23:42:38 +01:00
}
render() {
if (this.state.isLoading) {
return (
2018-03-17 21:39:21 +01:00
<View style={{ flex: 1, paddingTop: 20 }}>
2018-01-30 23:42:38 +01:00
<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 />;
2018-01-30 23:42:38 +01:00
} else {
return (
<View style={{ flex: 1 }}>
2018-04-08 18:21:07 +02:00
{(() => {
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
}}
2018-05-28 21:18:11 +02:00
title={loc.wallets.scanQrWif.cancel}
2018-04-08 18:21:07 +02:00
/>
</View>
</SafeBlueArea>
);
} else {
return (
<SafeBlueArea style={{ flex: 1 }}>
<Camera style={{ flex: 1 }} onBarCodeRead={ret => this.onBarCodeScanned(ret)}>
2018-04-08 18:21:07 +02:00
<TouchableOpacity
style={{ width: 80, height: 80, padding: 14, marginTop: 32 }}
onPress={() => this.props.navigation.goBack(null)}
2018-04-08 18:21:07 +02:00
>
<Image style={{ alignSelf: 'center' }} source={require('../../img/close.png')} />
2018-04-08 18:21:07 +02:00
</TouchableOpacity>
</Camera>
</SafeBlueArea>
2018-04-08 18:21:07 +02:00
);
}
})()}
2018-01-30 23:42:38 +01:00
</View>
);
}
}
2018-03-17 21:39:21 +01:00
}
2018-03-18 03:48:23 +01:00
ScanQrWif.propTypes = {
2018-03-18 03:48:23 +01:00
navigation: PropTypes.shape({
goBack: PropTypes.func,
2018-07-06 18:28:27 +02:00
popToTop: PropTypes.func,
navigate: PropTypes.func,
2018-03-18 03:48:23 +01:00
}),
};