2018-03-18 03:48:23 +01:00
|
|
|
/* global alert */
|
2018-01-30 23:42:38 +01:00
|
|
|
import React from 'react';
|
2018-03-17 21:39:21 +01:00
|
|
|
import {
|
|
|
|
Text,
|
|
|
|
ActivityIndicator,
|
|
|
|
Button,
|
|
|
|
View,
|
|
|
|
TouchableOpacity,
|
|
|
|
} from 'react-native';
|
2018-04-08 18:21:07 +02:00
|
|
|
import { BlueText, SafeBlueArea, BlueButton } from '../../BlueComponents';
|
2018-01-30 23:42:38 +01:00
|
|
|
import { Camera, Permissions } from 'expo';
|
2018-05-20 11:38:50 +02:00
|
|
|
import { SegwitP2SHWallet, LegacyWallet } from '../../class';
|
2018-03-18 03:48:23 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2018-05-20 11:38:50 +02:00
|
|
|
/** @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
|
|
|
|
2018-05-20 11:38:50 +02:00
|
|
|
export default class ScanQrWif extends React.Component {
|
2018-01-30 23:42:38 +01:00
|
|
|
static navigationOptions = {
|
2018-06-28 03:43:28 +02:00
|
|
|
tabBarVisible: false,
|
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,
|
|
|
|
type: Camera.Constants.Type.back,
|
|
|
|
};
|
|
|
|
|
2018-03-17 21:39:21 +01:00
|
|
|
async onBarCodeRead(ret) {
|
2018-04-08 18:21:07 +02:00
|
|
|
if (+new Date() - this.lastTimeIveBeenHere < 3000) {
|
|
|
|
this.lastTimeIveBeenHere = +new Date();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.lastTimeIveBeenHere = +new Date();
|
|
|
|
|
2018-03-18 03:52:01 +01:00
|
|
|
console.log('onBarCodeRead', 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
|
|
|
|
let password = await prompt(
|
2018-05-28 21:18:11 +02:00
|
|
|
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 {
|
|
|
|
let decryptedKey = await bip38.decrypt(ret.data, password, function(
|
|
|
|
status,
|
|
|
|
) {
|
|
|
|
that.setState({
|
|
|
|
message:
|
2018-05-28 21:18:11 +02:00
|
|
|
loc.wallets.scanQrWif.decoding +
|
|
|
|
'... ' +
|
|
|
|
status.percent.toString().substr(0, 4) +
|
|
|
|
' %',
|
2018-04-08 18:21:07 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
ret.data = wif.encode(
|
|
|
|
0x80,
|
|
|
|
decryptedKey.privateKey,
|
|
|
|
decryptedKey.compressed,
|
|
|
|
);
|
|
|
|
} 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) {
|
|
|
|
// lookig for duplicates
|
2018-01-30 23:42:38 +01:00
|
|
|
if (w.getSecret() === ret.data) {
|
2018-05-28 21:18:11 +02:00
|
|
|
alert(loc.wallets.scanQrWif.wallet_already_exists);
|
2018-03-17 21:39:21 +01:00
|
|
|
return; // duplicate, not adding
|
2018-01-30 23:42:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-17 21:39:21 +01:00
|
|
|
let newWallet = new SegwitP2SHWallet();
|
|
|
|
newWallet.setSecret(ret.data);
|
2018-05-20 11:38:50 +02:00
|
|
|
let newLegacyWallet = new LegacyWallet();
|
|
|
|
newLegacyWallet.setSecret(ret.data);
|
2018-01-30 23:42:38 +01:00
|
|
|
|
2018-05-20 11:38:50 +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
|
|
|
|
2018-05-20 11:38:50 +02: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);
|
2018-05-20 11:38:50 +02:00
|
|
|
BlueApp.wallets.push(newLegacyWallet);
|
|
|
|
alert(
|
2018-05-28 21:18:11 +02:00
|
|
|
loc.wallets.scanQrWif.imported_wif +
|
2018-05-20 11:38:50 +02:00
|
|
|
ret.data +
|
2018-05-28 21:18:11 +02:00
|
|
|
loc.wallets.scanQrWif.with_address +
|
2018-05-20 11:38:50 +02:00
|
|
|
newLegacyWallet.getAddress(),
|
|
|
|
);
|
|
|
|
} 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);
|
2018-05-20 11:38:50 +02:00
|
|
|
BlueApp.wallets.push(newWallet);
|
|
|
|
alert(
|
2018-05-28 21:18:11 +02:00
|
|
|
loc.wallets.scanQrWif.imported_wif +
|
|
|
|
ret.data +
|
|
|
|
loc.wallets.scanQrWif.with_address +
|
|
|
|
newWallet.getAddress(),
|
2018-05-20 11:38:50 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
await BlueApp.saveToDisk();
|
2018-07-06 18:28:27 +02:00
|
|
|
this.props.navigation.popToTop();
|
2018-05-20 11:38:50 +02:00
|
|
|
setTimeout(() => EV(EV.enum.WALLETS_COUNT_CHANGED), 500);
|
2018-01-30 23:42:38 +01:00
|
|
|
} // end
|
|
|
|
|
|
|
|
async componentWillMount() {
|
|
|
|
const { status } = await Permissions.askAsync(Permissions.CAMERA);
|
|
|
|
this.setState({
|
|
|
|
hasCameraPermission: status === 'granted',
|
2018-03-17 21:39:21 +01:00
|
|
|
onCameraReady: function() {
|
2018-01-30 23:42:38 +01:00
|
|
|
alert('onCameraReady');
|
|
|
|
},
|
|
|
|
barCodeTypes: [Camera.Constants.BarCodeType.qr],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
return <Text>No access to camera</Text>;
|
|
|
|
} 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 (
|
|
|
|
<Camera
|
|
|
|
style={{ flex: 1 }}
|
|
|
|
type={this.state.type}
|
|
|
|
onBarCodeRead={ret => this.onBarCodeRead(ret)}
|
|
|
|
>
|
|
|
|
<View
|
|
|
|
style={{
|
|
|
|
flex: 1,
|
|
|
|
backgroundColor: 'transparent',
|
|
|
|
flexDirection: 'row',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<TouchableOpacity
|
|
|
|
style={{
|
|
|
|
flex: 0.2,
|
|
|
|
alignSelf: 'flex-end',
|
|
|
|
alignItems: 'center',
|
|
|
|
}}
|
|
|
|
onPress={() => {
|
|
|
|
this.setState({
|
|
|
|
type:
|
|
|
|
this.state.type === Camera.Constants.Type.back
|
|
|
|
? Camera.Constants.Type.front
|
|
|
|
: Camera.Constants.Type.back,
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Button
|
|
|
|
style={{ fontSize: 18, marginBottom: 10 }}
|
2018-05-28 21:18:11 +02:00
|
|
|
title={loc.wallets.scanQrWif.go_back}
|
2018-04-08 18:21:07 +02:00
|
|
|
onPress={() => this.props.navigation.goBack()}
|
|
|
|
/>
|
|
|
|
</TouchableOpacity>
|
|
|
|
</View>
|
|
|
|
</Camera>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})()}
|
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
|
|
|
|
2018-05-20 11:38:50 +02: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,
|
2018-05-20 11:38:50 +02:00
|
|
|
navigate: PropTypes.func,
|
2018-03-18 03:48:23 +01:00
|
|
|
}),
|
|
|
|
};
|