Skip to content

Commit

Permalink
Merge branch 'nep2'
Browse files Browse the repository at this point in the history
  • Loading branch information
snowypowers committed Aug 23, 2017
2 parents fe0cbd9 + 3efb984 commit 9b78579
Show file tree
Hide file tree
Showing 12 changed files with 1,126 additions and 565 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"env": {
"es6": true,
"browser": true,
"jest/globals": true
"jest/globals": true,
"node":true
},
"parserOptions": {
"sourceType": "module",
Expand Down
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"standard.enable": false
}
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ansy",
"version": "1.4.0",
"version": "2.0.0",
"main": "src/index.js",
"author": "Yak Jun Xiang <[email protected]>",
"license": "MIT",
Expand All @@ -15,6 +15,7 @@
"dependencies": {
"bigi": "^1.4.2",
"bs58": "^4.0.1",
"bs58check": "^2.0.2",
"crypto-js": "^3.1.9-1",
"ecurve": "^1.0.5",
"hash.js": "^1.1.3",
Expand All @@ -24,7 +25,8 @@
"qrcode": "^0.8.2",
"react": "^15.6.1",
"react-dom": "^15.6.1",
"react-scripts": "^1.0.10"
"react-scripts": "^1.0.10",
"scryptsy": "^2.0.0"
},
"devDependencies": {
"eslint": "^3.19.0",
Expand Down
30 changes: 26 additions & 4 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { Component } from 'react'
import Form from './components/Form'
import Footer from './components/Footer'
import Wallet from './components/Wallet'
import NEP2Wallet from './components/NEP2Wallet'
import './App.css'

class App extends Component {
Expand All @@ -13,6 +14,7 @@ class App extends Component {
this.print = this.print.bind(this)
this.addWallet = this.addWallet.bind(this)
this.removeWallet = this.removeWallet.bind(this)
this.toggleWallet = this.toggleWallet.bind(this)
}

addWallet(newWallet) {
Expand All @@ -29,6 +31,18 @@ class App extends Component {
this.setState({ wallets: this.state.wallets.filter((w) => w.address !== addr) })
}

toggleWallet(addr, newType) {
this.setState({
wallets: this.state.wallets.filter((w) => {
if (w.address === addr) {
return Object.assign(w, {type: newType})
} else {
return w
}
})
})
}

print() {
if (this.state.wallets.length > 0) {
window.print()
Expand All @@ -38,12 +52,20 @@ class App extends Component {
}

WalletList(props) {
const listItems = props.map((p) =>
<Wallet key={p.address} address={p.address} private={p.private} removeCallback={this.removeWallet} />
)
const listItems = props.map((p) => {
if (p.type === "NEP2") {
return (
<NEP2Wallet key={p.address} data={p} removeCallback={this.removeWallet} toggleCallback={this.toggleWallet}/>
)
} else {
return (
<Wallet key={p.address} data={p} removeCallback={this.removeWallet} toggleCallback={this.toggleWallet}/>
)
}
})
const pages = []
while (listItems.length) {
pages.push(this.WalletPage(listItems.splice(0,4)))
pages.push(this.WalletPage(listItems.splice(0, 4)))
}
return (
<div className="wallets">{pages}</div>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Footer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Footer extends Component {
render() {
return (
<div id="foot">
<p>Made by snowypowers | v1.4.0</p>
<p>Made by snowypowers | v2.0.0</p>
<p>Tips appreciated!</p>
<div>NEO: AbUKT3KXcAMpLD1MsAMRNhgN3hqmzEqqVP</div>
<div>ETH: 0x2E15cd6Ca9f8dcdCd08Ad62d901b6E6a815a36fd </div>
Expand Down
45 changes: 37 additions & 8 deletions src/components/Form.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import crypto from '../modules/crypto'
import NEP2 from '../modules/nep2'

export default class Form extends Component {
constructor(props) {
super(props)
this.state = {
wallets: [],
error: '',
progress: 0
}
this.genWallet = this.genWallet.bind(this)
this.genKey = this.genKey.bind(this)
Expand All @@ -17,15 +19,32 @@ export default class Form extends Component {
this.setState({ error: 'Empty Field!' })
return
}
if (this.private.value.length !== 64 && this.private.value.length !== 52) {
if (this.private.value.length !== 64 && this.private.value.length !== 52 && this.private.value.length !== 58) {
this.setState({ error: 'Wrong Private Key Length!' })
return
}

let privateKey = this.private.value
let verifyAddr
try {
let newWallet = {
address: verifyAddr,
private: privateKey,
nep2: ''
}
// Decrypt NEP2
if (this.private.value.length === 58 && this.conPassword.value.length > 0) {
try {
newWallet.nep2 = this.private.value
privateKey = NEP2.decrypt(this.private.value, this.conPassword.value)
this.conPassword.value = ''
} catch(e) {
this.setState({error: 'Invalid Password'})
return
}
}

// Test Private key and get Address
try {
if (this.private.value.length === 52) {
privateKey = crypto.getHexFromWif(this.private.value)
}
Expand All @@ -38,14 +57,16 @@ export default class Form extends Component {
this.setState({ error: "Invalid Private key!" })
return
}
let newWallet = {
address: verifyAddr,
private: privateKey
newWallet.address = verifyAddr
newWallet.private = privateKey
if (this.conPassword.value.length > 0) {
newWallet.nep2 = NEP2.encrypt(newWallet.private, this.conPassword.value)
}
const done = this.props.addWallet(newWallet)
if (done) {
this.private.value = ''
this.address.value = ''
this.conPassword.value = ''
} else {
this.setState({ error: "Duplicate wallet!" })
}
Expand All @@ -54,7 +75,11 @@ export default class Form extends Component {
genKey() {
const privateKey = crypto.genPriKey()
const address = crypto.getAddrFromPri(privateKey)
let newWallet = { address, private: privateKey }
let newWallet = { address, private: privateKey, type:'Normal' }
if (this.genPassword.value.length > 0) {
newWallet.nep2 = NEP2.encrypt(privateKey, this.genPassword.value)
newWallet.type='NEP2'
}
const done = this.props.addWallet(newWallet)
if (!done) {
this.genKey()
Expand All @@ -66,6 +91,7 @@ export default class Form extends Component {
<p className="center-text" key="error"><span className="label error">{this.state.error}</span></p>
)
}

render() {
const formContainer = {
padding: 0,
Expand All @@ -86,13 +112,16 @@ export default class Form extends Component {
</div>
<div className="row">
<div style={tabContainer}>
<input id="private" className="stack" placeholder="Private Key" ref={(i) => this.private = i} />
<input id="addr" className="stack" placeholder="Address (Optional)" ref={(i) => this.address = i} />
<input type="text" id="private" className="stack" placeholder="Private Key" ref={(i) => this.private = i} />
<input type="text" id="addr" className="stack" placeholder="Address (Optional)" ref={(i) => this.address = i} />
<input type="password" id="con-pwd" className="stack" placeholder="Password (Optional)" ref={(i) => this.conPassword = i} />
<button id="convert" className="stack center-text" onClick={this.genWallet} >Convert</button>
</div>
<div style={tabContainer}>
<p className="center-text"> Or generate a new Private Key! </p>
<input type="password" id="gen-pwd" className="stack" placeholder="Password (Optional)" ref={(i) => this.genPassword = i} />
<button id="gen" className="stack center-text" onClick={this.genKey} >Generate!</button>
<p> Warning: Encryption will take a while</p>
</div>
</div>
</div>
Expand Down
68 changes: 68 additions & 0 deletions src/components/NEP2Wallet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import React from 'react'
import PropTypes from 'prop-types'
import QR from './QR.js'
import Wallet from './Wallet'

export default class NEP2Wallet extends Wallet {
constructor(props) {
super(props)
this.state.nep2 = this.props.data.nep2
}
toggle() {
this.props.toggleCallback(this.state.address, 'Normal')
}
render() {
let leftAlign = { textAlign: 'left' }
const uiStyle = { flexGrow: 0, flexBasis: 0, width: 0 }
const cardStyle = { flexGrow: 1, margin: 0 }
const noSpace = { padding: 0, paddingLeft: "0.6rem" }
const noResize = { resize: "none" }
return (
<div className="wallet full flex three">
<article className="card paper" style={cardStyle}>
<header style={noSpace}>
<div className="flex grow">
<input className="wallet-input" placeholder="Address / Public Key" />
</div>
</header>
<div>
<div className="flex two">
<QR name="Address" str={this.state.address} />
<QR name="Public Key" str={this.state.public} />
</div>
<footer style={noSpace}>
<div className="flex grow">
<textarea className="wallet-input" rows={2} style={noResize} placeholder={this.state.address} />
</div>
</footer>
</div>
</article>
<article className="card paper" style={cardStyle}>
<header style={noSpace}>
<div className="flex grow">
<input className="wallet-input" defaultValue="Private Keys" style={leftAlign} />
</div>
</header>
<div>
<div className="flex one">
<QR name="NEP2" str={this.state.nep2} />
</div>
<footer style={noSpace}>
<div className="flex grow">
<textarea className="wallet-input" rows={2} defaultValue={this.state.nep2} style={noResize} />
</div>
</footer>
</div>
</article>
<div className="wallet-ui no-print" style={uiStyle}>
<button onClick={this.remove}>X</button>
{this.props.data.private ? <button onClick={this.toggle}>{'<'}</button> : null}
</div>
</div>
)
}
}
NEP2Wallet.propTypes = {
data: PropTypes.object.isRequired,
removeCallback: PropTypes.func
}
22 changes: 14 additions & 8 deletions src/components/Wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,27 @@ import './Wallet.css'
export default class Wallet extends Component {
constructor(props) {
super(props)
const wif = crypto.getWifFromHex(props.private)
const publicKey = crypto.getPubFromHex(props.private)
const address = crypto.getAddrFromPri(props.private)
const wif = crypto.getWifFromHex(props.data.private)
const publicKey = crypto.getPubFromHex(props.data.private)
const address = crypto.getAddrFromPri(props.data.private)
this.state = {
address,
wif,
private: props.private,
private: props.data.private,
public: publicKey
}
this.remove = this.remove.bind(this)
this.toggle = this.toggle.bind(this)
}

remove() {
this.props.removeCallback(this.state.address)
}

toggle() {
this.props.toggleCallback(this.state.address, 'NEP2')
}

render() {
let leftAlign = { textAlign: 'left' }
const uiStyle = { flexGrow: 0, flexBasis: 0, width: 0 }
Expand Down Expand Up @@ -62,21 +67,22 @@ export default class Wallet extends Component {
</div>
<footer style={noSpace}>
<div className="flex grow">
<textarea className="wallet-input" rows={2} defaultValue={this.state.wif} style={leftAlign,noResize} />
<textarea className="wallet-input" rows={2} defaultValue={this.state.wif} style={noResize} />
</div>
</footer>
</div>
</article>
<div className="wallet-ui no-print" style={uiStyle}>
<button onClick={this.remove}>X</button>
{this.props.data.nep2 ? <button onClick={this.toggle}>{'>'}</button> : null}
</div>
</div>
)
}
}

Wallet.propTypes = {
address: PropTypes.string.isRequired,
private: PropTypes.string.isRequired,
removeCallback: PropTypes.func
data: PropTypes.object.isRequired,
removeCallback: PropTypes.func,
toggleCallback: PropTypes.func,
}
8 changes: 4 additions & 4 deletions src/modules/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const ec = EC.getCurveByName('secp256r1')
const ADDR_VERS = '17'


const toHexString = function(arrayBuffer) {
export const toHexString = function(arrayBuffer) {
let s = "";
for (const i of arrayBuffer) {
s += (i >>> 4).toString(16);
Expand All @@ -19,15 +19,15 @@ const toHexString = function(arrayBuffer) {
return s;
}

const toArrayBuffer = function(s) {
export const toArrayBuffer = function(s) {
let result = []
for (let i=0;i < s.length;i+=2) {
result.push(parseInt(s.substring(i, i+2), 16))
}
return Uint8Array.from(result)
}

const WIF = {
export const WIF = {
encode: (s) => {
let extended = "80" + s + "01"
const shaOut = SHA256(SHA256(enc.Hex.parse(extended))).toString()
Expand All @@ -40,7 +40,7 @@ const WIF = {
}
}

const crypto = {
export const crypto = {
getCurvePtFromHex: (privateKey) => {
const privateKeyBuffer = new BigInteger.fromHex(privateKey)
const curvePt = ec.G.multiply(privateKeyBuffer)
Expand Down
Loading

0 comments on commit 9b78579

Please sign in to comment.