Skip to content

Commit

Permalink
Add an optional encrypted qr code for the private key (bitshares#542)
Browse files Browse the repository at this point in the history
* 为私钥添加可加密二维码显示功能

* 为私钥添加可加密二维码显示功能
  • Loading branch information
xiangxn authored and svk31 committed Oct 11, 2017
1 parent 1d14969 commit 733e855
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 2 deletions.
5 changes: 5 additions & 0 deletions app/assets/locales/locale-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,11 @@
"approval_remove": "同意移除",
"key_approval_add": "同意添加密钥",
"key_approval_remove": "同意移除密钥"
},
"qrcode":{
"label":"二维码",
"title":"私钥二维码",
"input_messate":"请输入用于加密二维码的密码,密码为空表示不加密"
}
},
"init_error": {
Expand Down
7 changes: 6 additions & 1 deletion app/assets/locales/locale-en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,12 @@
"key_approval_add": "Key approval to add",
"key_approval_remove": "Key approval to remove"
},
"ok": "OK"
"ok": "OK",
"qrcode":{
"label":"qrcode",
"title":"Private key QR code",
"input_messate":"Please enter the password used to encrypt the QR code, the password is empty that does not encrypt."
}
},
"init_error": {
"title": "Application initialization issues",
Expand Down
119 changes: 119 additions & 0 deletions app/components/Modal/QrcodeModal.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import React, {PropTypes} from "react";
import ZfApi from "react-foundation-apps/src/utils/foundation-api";
import BaseModal from "./BaseModal";
import Translate from "react-translate-component";
import QRCode from "qrcode.react";
import {Aes} from "bitsharesjs/es";


class QrcodeModal extends React.Component {
constructor(props) {
super(props);
this.state = this._getInitialState();
this.onPasswordEnter = this.onPasswordEnter.bind(this);
this.onKeyDown = this.onKeyDown.bind(this);
this.onCancel = this.onCancel.bind(this);
this.onClose = this.onClose.bind(this);
}

_getInitialState() {
return {
isShowQrcode: false,
keyString: null
};
}

show() {
ZfApi.publish(this.props.modalId, "open");
}

onCancel() {
ZfApi.publish(this.props.modalId, "close");
this.onClose();
}

onClose() {
if (this.refs.password_input) this.refs.password_input.value = "";
this.setState(this._getInitialState());
}

onPasswordEnter(e) {
e.preventDefault();
let pwd = this.refs.password_input.value;
let key = this.props.keyValue;
if (pwd != null && pwd != "") {
if (key !== undefined && key != null && key != "") {
let pwd_aes = Aes.fromSeed(pwd);
let qrkey = pwd_aes.encryptToHex(key);
this.setState({isShowQrcode: true, keyString: qrkey});
}
} else {
//notify.error("You'd better enter a password to encrypt the qr code");
this.setState({isShowQrcode: true, keyString: key});
}
}

onKeyDown(e) {
if (e.keyCode === 13) this.onPasswordEnter(e);
}


render() {
let pos = null;
if(this.state.isShowQrcode)pos = {textAlign: "center"};
return (
<BaseModal onClose={this.onClose} id={this.props.modalId} ref="modal" overlay={true} overlayClose={false}>
<h3>
<Translate content="modal.qrcode.title"/>
</h3>
<form onSubmit={this.onPasswordEnter} noValidate>
<div className="form-group">
{this.state.isShowQrcode ?
<section style={pos}>
<QRCode size={256} value={this.state.keyString}/>
</section>
:
<section>
<label className="left-label"><Translate content="modal.qrcode.input_messate"/></label>
<input
name="password"
type="text"
onFocus={() => {
this.refs.password_input.setAttribute("type", "password");
}}
ref="password_input"
autoComplete="off"
onKeyDown={this.onKeyDown}
/>
</section>
}
</div>
<div style={pos}>
<div className="button-group">
{this.state.isShowQrcode == false ?
<button className="button" data-place="bottom" data-html onClick={this.onPasswordEnter}>
<Translate content="modal.ok"/>
</button>
:
null
}
<button className="button" data-place="bottom" data-html onClick={this.onCancel}>
<Translate content="cancel"/>
</button>
</div>
</div>
</form>
</BaseModal>
);
}
}

QrcodeModal.propTypes = {
modalId: PropTypes.string.isRequired,
keyValue: PropTypes.string
};
QrcodeModal.defaultProps = {
modalId: "qr_code_password_modal"
};
export default QrcodeModal;

8 changes: 7 additions & 1 deletion app/components/PrivateKeyView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import WalletUnlockActions from "actions/WalletUnlockActions";
import WalletDb from "stores/WalletDb";
import Translate from "react-translate-component";
import PrivateKeyStore from "stores/PrivateKeyStore";
import QrcodeModal from "./Modal/QrcodeModal";

export default class PrivateKeyView extends Component {

Expand Down Expand Up @@ -58,7 +59,7 @@ export default class PrivateKeyView extends Component {
<div>
{this.state.wif ? <span>
{this.state.wif}
&nbsp;(<a onClick={this.onHide.bind(this)}>hide</a>)
&nbsp;(<a onClick={this.onHide.bind(this)}>hide</a>,<a onClick={this.showQrCode.bind(this)}><Translate content="modal.qrcode.label" /></a>)
</span>:<span>
(<a onClick={this.onShow.bind(this)}><Translate content="account.perm.show" /></a>)
</span>}
Expand Down Expand Up @@ -86,6 +87,7 @@ export default class PrivateKeyView extends Component {
<div onClick={this.onClose.bind(this)} className=" button"><Translate content="transfer.close" /></div>
</div>
</BaseModal>
<QrcodeModal ref="qrmodal" keyValue={this.state.wif} />
</span>
}

Expand All @@ -111,4 +113,8 @@ export default class PrivateKeyView extends Component {
this.setState({ wif: null })
}

showQrCode(){
this.refs.qrmodal.show();
}

}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
"numeral": "2.0.4",
"object-assign": "^4.0.1",
"perfect-scrollbar": "git+https://github.com/bitshares/perfect-scrollbar.git",
"qrcode.react": "^0.7.1",
"react": "^15.6.1",
"react-clipboard.js": "^1.0.1",
"react-dom": "^15.6.1",
Expand Down

0 comments on commit 733e855

Please sign in to comment.