Skip to content

Commit

Permalink
Bug fix: using sr25519 to generate keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael committed Jul 27, 2019
1 parent 74cfb26 commit e1c3976
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
"_stream_readable": "readable-stream/readable",
"_stream_transform": "readable-stream/transform",
"_stream_writable": "readable-stream/writable",
"@polkadot/wasm-crypto": "@plugnet/wasm-crypto-js",
"@plugnet/util": "@polkadot/util",
"vm": "vm-browserify"
},
"throwForNonStringLiteral": true
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@babel/plugin-transform-runtime": "^7.4.4",
"@babel/runtime": "^7.4.5",
"@polkadot/api": "^0.79.0-beta.31",
"@polkadot/wasm-crypto": "^0.13.1",
"@plugnet/wasm-crypto-js": "^0.11.102",
"@tradle/react-native-http": "^2.0.1",
"buffer": "^4.9.1",
"events": "^3.0.0",
Expand Down
23 changes: 12 additions & 11 deletions src/page/Assets/secondary/CreateAccount.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,19 @@ import { formatBalance, hexToU8a, isHex, stringToU8a, u8aToHex } from '@polkadot
import { NavigationActions, StackActions } from 'react-navigation'
import SInfo from 'react-native-sensitive-info'
import Keyring from '@polkadot/keyring'
import { randomAsU8a, mnemonicGenerate } from '@polkadot/util-crypto'

import * as CustomKeyboard from 'react-native-yusha-customkeyboard'
import { observer, inject } from 'mobx-react'

import { mnemonicGenerate, randomAsU8a } from '../../../util/bip39Util'
import { ScreenWidth, ScreenHeight, doubleClick } from '../../../util/Common'
import RNKeyboardAvoidView from '../../../components/RNKeyboardAvoidView'
import polkadotAPI from '../../../util/polkadotAPI'
import i18n from '../../../locales/i18n'
import RNPicker from '../../../components/RNPicker'

const keyring = new Keyring()
// TODO: this is just show case to use sr25519
const keyring = new Keyring({ type: 'sr25519' })

@inject('rootStore')
@observer
Expand Down Expand Up @@ -96,7 +97,7 @@ class CreateAccount extends Component {
componentDidMount() {
InteractionManager.runAfterInteractions(() => {
;(async () => {
let key = mnemonicGenerate()
let key = await mnemonicGenerate()
this.pair = keyring.addFromMnemonic(key)
this.setState({
key,
Expand Down Expand Up @@ -124,7 +125,7 @@ class CreateAccount extends Component {
* @description 切换单位|Switching unit
* @param {String} way_change 单位|unit
*/
Modify_way(way_change) {
async Modify_way(way_change) {
this.setState({
isModel: false,
way: way_change,
Expand All @@ -138,11 +139,11 @@ class CreateAccount extends Component {
})
return
} else if (way_change == 'Mnemonic') {
key = mnemonicGenerate()
key = await mnemonicGenerate()
} else if (way_change == 'Mnemonic24') {
key = mnemonicGenerate(24)
key = await mnemonicGenerate(24)
} else {
key = u8aToHex(randomAsU8a())
key = u8aToHex(await randomAsU8a())
}
this.pair = keyring.addFromMnemonic(key)
this.setState({
Expand Down Expand Up @@ -277,7 +278,7 @@ class CreateAccount extends Component {
/**
* @description 点击重置|Click Reset
*/
Reset() {
async Reset() {
let key
if (this.state.way == 'Keystore') {
this.setState({
Expand All @@ -287,11 +288,11 @@ class CreateAccount extends Component {
})
return
} else if (this.state.way === 'Mnemonic') {
key = mnemonicGenerate()
key = await mnemonicGenerate()
} else if (this.state.way === 'Mnemonic24') {
key = mnemonicGenerate(24)
key = await mnemonicGenerate(24)
} else {
key = u8aToHex(randomAsU8a())
key = u8aToHex(await randomAsU8a())
}
this.pair = keyring.addFromMnemonic(key)
this.setState({
Expand Down
38 changes: 38 additions & 0 deletions src/util/bip39Util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { entropyToMnemonic } from 'bip39'
import { randomBytes } from 'react-native-randombytes'

const STRENGTH_MAP = {
12: 16 * 8,
15: 20 * 8,
18: 24 * 8,
21: 28 * 8,
24: 32 * 8
}

/**
* Generate random bytes
*
* @param {Number} length
* @returns {Buffer} Random bytes
*/
const randomAsU8a = length =>
new Promise((resolve, reject) => {
randomBytes(length, (err, bytes) => {
if (err) return reject(err)
resolve(bytes)
})
})

/**
* Use bip39 to generate mnemonic words
*
* @param {Number} words length
* @returns {String} Mnemonic words
*/
const mnemonicGenerate = async (words = 12) => {
const strength = STRENGTH_MAP[words]
const entropy = await randomAsU8a(strength / 8)
return entropyToMnemonic(entropy)
}

export { mnemonicGenerate, randomAsU8a }
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,11 @@
resolved "https://registry.npmjs.org/@emotion/unitless/-/unitless-0.7.4.tgz#a87b4b04e5ae14a88d48ebef15015f6b7d1f5677"
integrity sha512-kBa+cDHOR9jpRJ+kcGMsysrls0leukrm68DmFQoMIWQcXdr2cZvyvypWuGYT7U+9kAExUE7+T7r6G3C3A6L8MQ==

"@plugnet/wasm-crypto-js@^0.11.102":
version "0.11.102"
resolved "https://registry.npmjs.org/@plugnet/wasm-crypto-js/-/wasm-crypto-js-0.11.102.tgz#3f38703022fe1a55195fd812e9a7fb92f5398d6d"
integrity sha512-+ieVYsCPtb/JOlIfsfHwnhrajk/EkpyGoonWFsPTDzedDE1mQumv0FRElx39qEr1CVKnoFrxIWeREerqa9482A==

"@polkadot/api-derive@^0.79.1":
version "0.79.1"
resolved "https://registry.npmjs.org/@polkadot/api-derive/-/api-derive-0.79.1.tgz#0c91bc1450849fad092fd8c201066a4c0fc38bcd"
Expand Down Expand Up @@ -841,11 +846,6 @@
resolved "https://registry.npmjs.org/@polkadot/wasm-crypto/-/wasm-crypto-0.10.1.tgz#284e57e102050cca1586b7b1f008f64224147eb4"
integrity sha512-hI3OYftMGJkDetTe1DeU9aH4euk6WVmAMFszslNrCxHMFFkemEgACykwpyuOdCjv24VZP/cMsrVskKix1GzSYQ==

"@polkadot/wasm-crypto@^0.13.1":
version "0.13.1"
resolved "https://registry.npmjs.org/@polkadot/wasm-crypto/-/wasm-crypto-0.13.1.tgz#602305b2ca86fc320a35ce820835e0e2dd9e646e"
integrity sha512-24a63FynhyBHEGxqoDMZHAcaSxJqnjBPnEcmXXYCN2lI7b4iKaJKF2t+/FUmY7XTST+xNgFTJZ7A/o8jjgC/mA==

"@react-native-community/cli@^1.2.1":
version "1.10.0"
resolved "https://registry.npmjs.org/@react-native-community/cli/-/cli-1.10.0.tgz#66e3c9f407763281f7060c034145650bf0d6786c"
Expand Down

0 comments on commit e1c3976

Please sign in to comment.