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.

217 lines
7.2 KiB

7 years ago
/* global alert */
7 years ago
import React from 'react';
import { Text, ActivityIndicator, Button, View, TouchableOpacity } from 'react-native';
7 years ago
import { BlueText, SafeBlueArea, BlueButton } from '../../BlueComponents';
6 years ago
import { Camera, Permissions, BarCodeScanner } from 'expo';
import { SegwitP2SHWallet, LegacyWallet, WatchOnlyWallet } from '../../class';
7 years ago
import PropTypes from 'prop-types';
/** @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 = {
6 years ago
tabBarVisible: false,
7 years ago
};
7 years ago
state = {
7 years ago
isLoading: false,
7 years ago
hasCameraPermission: null,
type: Camera.Constants.Type.back,
};
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) {
// lookig for duplicates
7 years ago
if (w.getSecret() === ret.data) {
alert(loc.wallets.scanQrWif.wallet_already_exists);
7 years ago
return; // duplicate, not adding
7 years ago
}
}
// 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());
await watchOnly.fetchBalance();
await watchOnly.fetchTransactions();
await BlueApp.saveToDisk();
this.props.navigation.popToTop();
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() {
const { status } = await Permissions.askAsync(Permissions.CAMERA);
this.setState({
hasCameraPermission: status === 'granted',
7 years ago
onCameraReady: function() {
7 years ago
alert('onCameraReady');
},
6 years ago
barCodeTypes: [BarCodeScanner.Constants.BarCodeType.qr],
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) {
return <Text>No access to camera</Text>;
} 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 (
<Camera style={{ flex: 1 }} type={this.state.type} onBarCodeScanned={ret => this.onBarCodeScanned(ret)}>
7 years ago
<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,
7 years ago
});
}}
>
<Button
style={{ fontSize: 18, marginBottom: 10 }}
title={loc.wallets.scanQrWif.go_back}
7 years ago
onPress={() => this.props.navigation.goBack()}
/>
</TouchableOpacity>
</View>
</Camera>
);
}
})()}
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
}),
};