Commit 0418c48e authored by Marek Kotewicz's avatar Marek Kotewicz Committed by GitHub
Browse files

fixed #36, proper random source (#55)

* fixed #36, proper random source

* fixed IconChooser refreshIcons performance
parent b32ab715
......@@ -74,10 +74,16 @@ public class EthkeyBridge extends ReactContextBaseJavaModule {
promise.resolve(ethkeyBlockiesIcon(seed));
}
@ReactMethod
public void randomPhrase(int words, Promise promise) {
promise.resolve(ethkeyRandomPhrase(words));
}
private static native String ethkeyBrainwalletAddress(String seed);
private static native String ethkeyBrainwalletSecret(String seed);
private static native String ethkeyBrainwalletSign(String seed, String message);
private static native String ethkeyRlpItem(String data, int position);
private static native String ethkeyKeccak(String data);
private static native String ethkeyBlockiesIcon(String seed);
private static native String ethkeyRandomPhrase(int words);
}
\ No newline at end of file
......@@ -82,4 +82,13 @@ class EthkeyBridge: NSObject {
rust_string_destroy(icon_rust_str)
resolve(icon)
}
@objc func randomPhrase(_ words: UInt32, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
let words_rust_str = random_phrase(words)
let words_rust_str_ptr = rust_string_ptr(words_rust_str)
let words = String.fromStringPtr(ptr: words_rust_str_ptr!.pointee)
rust_string_ptr_destroy(words_rust_str_ptr)
rust_string_destroy(words_rust_str)
resolve(words)
}
}
......@@ -31,5 +31,6 @@ RCT_EXTERN_METHOD(brainWalletSign:(NSString*)seed message:(NSString*)message res
RCT_EXTERN_METHOD(rlpItem:(NSString*)rlp position:(NSUInteger)position resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(keccak:(NSString*)data resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(blockiesIcon:(NSString*)seed resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(randomPhrase:(NSUInteger)words resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
@end
......@@ -15,7 +15,6 @@
"commit": "commit-wizard"
},
"dependencies": {
"@parity/wordlist": "^1.0.1",
"bignumber.js": "^4.0.0",
"debounce": "^1.0.0",
"react": "^15.4.2",
......
......@@ -6,6 +6,7 @@ dependencies = [
"ethkey 0.2.0 (git+https://github.com/paritytech/parity)",
"jni 0.3.1 (git+https://github.com/prevoty/jni-rs)",
"libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-wordlist 1.0.0 (git+https://github.com/paritytech/wordlist)",
"rlp 0.1.0 (git+https://github.com/paritytech/parity)",
"rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)",
"tiny-keccak 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
......@@ -82,6 +83,11 @@ dependencies = [
"byteorder 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "either"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "elastic-array"
version = "0.6.0"
......@@ -184,6 +190,14 @@ name = "inflate"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "itertools"
version = "0.5.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"either 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "jni"
version = "0.3.1"
......@@ -269,6 +283,16 @@ name = "odds"
version = "0.2.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "parity-wordlist"
version = "1.0.0"
source = "git+https://github.com/paritytech/wordlist#75362c0d2f9ab354de402594096e0369abc1dc57"
dependencies = [
"itertools 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "png"
version = "0.6.2"
......@@ -378,6 +402,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum cesu8 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6d43a04d8753f35258c91f8ec639f792891f748a1edbd759cf1dcea3382ad83c"
"checksum combine 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "89749770bffc77cb34db11b60d5d4b197388b46bc7774ac8acbde33c26ad6c08"
"checksum deflate 0.7.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ebb02aaf4b775afc96684b8402510a338086974e38570a1f65bea8c202eb77a7"
"checksum either 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "18785c1ba806c258137c937e44ada9ee7e69a37e3c72077542cd2f069d78562a"
"checksum elastic-array 0.6.0 (git+https://github.com/paritytech/elastic-array)" = "<none>"
"checksum enum_primitive 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "be4551092f4d519593039259a9ed8daedf0da12e5109c5280338073eaeb81180"
"checksum error-chain 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e92ecf0a508c8e074c0e6fa8fe0fa38414848ad4dfc4db6f74c5e9753330b248"
......@@ -390,6 +415,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum hsl 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "575fb7f1167f3b88ed825e90eb14918ac460461fdeaa3965c6a50951dee1c970"
"checksum image 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)" = "979bad0502082fd60053a490282e87d6c89650942e3a270e0d4c83569c7f5899"
"checksum inflate 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e7e0062d2dc2f17d2f13750d95316ae8a2ff909af0fda957084f5defd87c43bb"
"checksum itertools 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4833d6978da405305126af4ac88569b5d71ff758581ce5a987dbfa3755f694fc"
"checksum jni 0.3.1 (git+https://github.com/prevoty/jni-rs)" = "<none>"
"checksum jni-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "83ba0fad4625733b25833da250d0c84058314e1a276f4939f3e87c195997c206"
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
......@@ -402,6 +428,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum num-rational 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)" = "c2dc5ea04020a8f18318ae485c751f8cfa1c0e69dcf465c29ddaaa64a313cc44"
"checksum num-traits 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "e1cbfa3781f3fe73dc05321bed52a06d2d491eaa764c52335cf4399f046ece99"
"checksum odds 0.2.25 (registry+https://github.com/rust-lang/crates.io-index)" = "c3df9b730298cea3a1c3faa90b7e2f9df3a9c400d0936d6015e6165734eefcba"
"checksum parity-wordlist 1.0.0 (git+https://github.com/paritytech/wordlist)" = "<none>"
"checksum png 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3cb773e9a557edb568ce9935cf783e3cdcabe06a9449d41b3e5506d88e582c82"
"checksum rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d"
"checksum redox_syscall 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "8dd35cc9a8bdec562c757e3d43c1526b5c6d2653e23e2315065bc25556550753"
......
......@@ -10,6 +10,7 @@ ethkey = { git = "https://github.com/paritytech/parity" }
rlp = { git = "https://github.com/paritytech/parity" }
tiny-keccak = "1.1"
blockies = "0.1"
parity-wordlist = { git = "https://github.com/paritytech/wordlist" }
[target.'cfg(target_os="android")'.dependencies]
jni = { git = "https://github.com/prevoty/jni-rs", default-features = false }
......
......@@ -70,3 +70,7 @@ struct rust_string* keccak256(const struct rust_string_ptr* data);
// blockies ffi
struct rust_string* blockies_icon(const struct rust_string_ptr* blockies_seed);
// random phrase ffi
struct rust_string* random_phrase(const uint32_t words);
......@@ -17,6 +17,7 @@
extern crate libc;
extern crate rustc_serialize;
extern crate tiny_keccak;
extern crate parity_wordlist as wordlist;
extern crate ethkey;
extern crate rlp;
extern crate blockies;
......@@ -142,11 +143,19 @@ pub unsafe extern fn blockies_icon(blockies_seed: *mut StringPtr) -> *mut String
Box::into_raw(Box::new(icon))
}
// random phrase ffi
#[no_mangle]
pub unsafe extern fn random_phrase(words: u32) -> *mut String {
let words = wordlist::random_phrase(words as usize);
Box::into_raw(Box::new(words))
}
#[cfg(target_os = "android")]
#[allow(non_snake_case)]
pub mod android {
extern crate jni;
use wordlist;
use super::*;
use self::jni::JNIEnv;
use self::jni::objects::{JClass, JString};
......@@ -209,6 +218,12 @@ pub mod android {
let icon = blockies_icon_in_base64(seed.into());
env.new_string(icon).expect("Could not create java string").into_inner()
}
#[no_mangle]
pub unsafe extern fn Java_com_nativesigner_EthkeyBridge_ethkeyRandomPhrase(env: JNIEnv, _: JClass, words: jint) -> jstring {
let words = wordlist::random_phrase(words as usize);
env.new_string(words).expect("Could not create java string").into_inner()
}
}
#[cfg(test)]
......
......@@ -6,8 +6,7 @@ import AppStyles from '../styles'
import AccountIcon from './AccountIcon'
import { words } from '../util/random'
import { brainWalletAddress } from '../util/native'
import { brainWalletAddress, words } from '../util/native'
export default class IconChooser extends Component {
static propTypes = {
......@@ -18,13 +17,17 @@ export default class IconChooser extends Component {
icons: []
}
componentDidMount () {
this.refreshIcons()
refreshIcons = async () => {
try {
const icons = await Promise.all(Array(6).join(' ').split(' ').map(() => words()))
this.setState({ icons })
} catch (e) {
console.error(e)
}
}
refreshIcons = () => {
const icons = Array(6).join(' ').split(' ').map(() => words())
this.setState({ icons })
componentDidMount () {
this.refreshIcons()
}
render () {
......
......@@ -30,3 +30,4 @@ export const brainWalletSign = (seed, message) => EthkeyBridge.brainWalletSign(s
export const rlpItem = (rlp, position) => EthkeyBridge.rlpItem(rlp, position)
export const keccak = (data) => EthkeyBridge.keccak(data)
export const blockiesIcon = (seed) => EthkeyBridge.blockiesIcon(seed.toLowerCase()).then(icon => 'data:image/png;base64,' + icon)
export const words = () => EthkeyBridge.randomPhrase(11)
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import {randomPhrase} from '@parity/wordlist'
export function words () {
return randomPhrase(11)
}
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import { words } from './random'
describe('words', () => {
it('should create a list of 10 random words', () => {
let randomWords = words()
let count = randomWords.split(' ').length
expect(count).toEqual(11)
})
})
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment