Skip to content
This repository has been archived by the owner on Feb 1, 2021. It is now read-only.

Commit

Permalink
Merge pull request #6 from uport-project/support/upgrade-android-tool…
Browse files Browse the repository at this point in the history
…s-3.3.2

Upgrade android build tools and native signer version
  • Loading branch information
mirceanis authored Mar 18, 2019
2 parents dee772d + 328154b commit 4831500
Show file tree
Hide file tree
Showing 9 changed files with 4,682 additions and 227 deletions.
61 changes: 42 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@

### Mostly automatic installation

`$ react-native link react-native-uport-signer`
1. `$ react-native link react-native-uport-signer`
2. Insert the following line inside the `allprojects.repositories` block in `android/build.gradle`:
```groovy
allprojects {
repositories {
//...
//add this line
maven { url 'https://jitpack.io' }
}
}
```

### Manual installation

Expand All @@ -21,26 +31,39 @@

#### Android

1. Open up `android/app/src/main/java/[...]/MainActivity.java`
1. Insert the following line inside the `allprojects.repositories` block in `android/build.gradle`:

```groovy
allprojects {
repositories {
//...
//add this line
maven { url 'https://jitpack.io' }
}
}
```
2. Insert the following lines inside the `dependencies` block in `android/app/build.gradle`:
```groovy
dependencies {
// add this line
implementation project(':react-native-uport-signer')
//...
}
```

3. Append the following lines to `android/settings.gradle`:
```groovy
include ':react-native-uport-signer'
project(':react-native-uport-signer').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-uport-signer/android')
```

4. Open up `android/app/src/main/java/[...]/MainActivity.java`
- Add `import com.reactlibrary.RNUportSignerPackage;` to the imports at the top of the file
- Add `new RNUportSignerPackage()` to the list returned by the `getPackages()` method
2. Append the following lines to `android/settings.gradle`:
```
include ':react-native-uport-signer'
project(':react-native-uport-signer').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-uport-signer/android')
```
3. Insert the following lines inside the dependencies block in `android/app/build.gradle`:
```
compile project(':react-native-uport-signer')
```

#### Windows
[Read it! :D](https://github.com/ReactWindows/react-native)

1. In Visual Studio add the `RNUportSigner.sln` in `node_modules/react-native-uport-signer/windows/RNUportSigner.sln` folder to their solution, reference from their app.
2. Open up your `MainPage.cs` app
- Add `using Uport.Signer.RNUportSigner;` to the usings at the top of the file
- Add `new RNUportSignerPackage()` to the `List<IReactPackage>` returned by the `Packages` method

5. [optional] If not already done, update the `minSdkVersion` of your `app` to 21
This is usually in `android/app/build.gradle` but can also be defined in `android/build.gradle`
depending on when your project was created


## Usage
Expand Down
15 changes: 7 additions & 8 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.1.4'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.61"
classpath 'com.android.tools.build:gradle:3.3.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.21"
}
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 27
buildToolsVersion "27.0.3"
compileSdkVersion 28
buildToolsVersion "28.0.3"

defaultConfig {
minSdkVersion 21
targetSdkVersion 27
targetSdkVersion 28
versionCode 1
versionName "1.0"
}
Expand All @@ -30,19 +30,18 @@ android {
}

repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
google()
jcenter()
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url "$rootDir/../node_modules/react-native/android"
}
maven { url 'https://jitpack.io' }
}

dependencies {
implementation 'com.facebook.react:react-native:+'

api "com.github.uport-project:uport-android-signer:0.2.1"
api "com.github.uport-project.uport-android-sdk:signer:v0.4.1"
}

4 changes: 2 additions & 2 deletions android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Thu Oct 25 18:06:18 EDT 2018
#Mon Mar 18 13:55:06 EET 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
159 changes: 79 additions & 80 deletions android/src/main/java/com/reactlibrary/RNUportHDSignerModule.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("unused", "KDocUnresolvedReference", "TooManyFunctions")

package com.reactlibrary

import android.text.TextUtils
Expand All @@ -10,15 +12,19 @@ import com.uport.sdk.signer.UportSigner
import com.uport.sdk.signer.UportSigner.Companion.ERR_BLANK_KEY
import com.uport.sdk.signer.encryption.KeyProtection.Level
import com.uport.sdk.signer.keyToBase64
import org.kethereum.bip32.generateKey
import org.kethereum.bip39.Mnemonic
import java.util.HashMap
import org.kethereum.bip39.entropyToMnemonic
import org.kethereum.bip39.model.MnemonicWords
import org.kethereum.bip39.toKey
import org.kethereum.bip39.wordlists.WORDLIST_ENGLISH
import java.util.*


/**
* wrapper for the UportSigner functionality
* wrapper for the UportHDSigner functionality.
* This provides methods for creating and importing seeds, and using private keys derived
* using a Hierarchically Deterministic algorithm (BIP32) starting from those seeds
*/
class RNUportHDSignerModule(reactContext: ReactApplicationContext?)
class RNUportHDSignerModule(reactContext: ReactApplicationContext)
: RNUportSignerModule(reactContext) {

override fun getName(): String = "RNUportHDSignerModule"
Expand Down Expand Up @@ -55,17 +61,16 @@ class RNUportHDSignerModule(reactContext: ReactApplicationContext?)

UportHDSigner().createHDSeed(
reactApplicationContext,
keyLevel,
{ err, address, pubKey ->
if (err != null) {
return@createHDSeed promise.reject(err)
}
val map = WritableNativeMap()
map.putString("address", address)
map.putString("pubKey", pubKey)
return@createHDSeed promise.resolve(map)
}
)
keyLevel
) { err, address, pubKey ->
if (err != null) {
return@createHDSeed promise.reject(err)
}
val map = WritableNativeMap()
map.putString("address", address)
map.putString("pubKey", pubKey)
return@createHDSeed promise.resolve(map)
}
}

@ReactMethod
Expand Down Expand Up @@ -104,17 +109,16 @@ class RNUportHDSignerModule(reactContext: ReactApplicationContext?)
reactApplicationContext,
rootAddr,
hdPath,
prompt ?: "",
{ err, address, pubKey ->
if (err != null) {
return@computeAddressForPath promise.reject(err)
}
val map = WritableNativeMap()
map.putString("address", address)
map.putString("pubKey", pubKey)
return@computeAddressForPath promise.resolve(map)
}
)
prompt ?: ""
) { err, address, pubKey ->
if (err != null) {
return@computeAddressForPath promise.reject(err)
}
val map = WritableNativeMap()
map.putString("address", address)
map.putString("pubKey", pubKey)
return@computeAddressForPath promise.resolve(map)
}
}

/**
Expand All @@ -138,15 +142,14 @@ class RNUportHDSignerModule(reactContext: ReactApplicationContext?)
UportHDSigner().showHDSeed(
reactApplicationContext,
rootAddr,
prompt ?: "",
{ err, phrase ->
return@showHDSeed if (err != null) {
promise.reject(err)
} else {
promise.resolve(phrase)
}
}
)
prompt ?: ""
) { err, phrase ->
return@showHDSeed if (err != null) {
promise.reject(err)
} else {
promise.resolve(phrase)
}
}
}

/**
Expand Down Expand Up @@ -175,18 +178,17 @@ class RNUportHDSignerModule(reactContext: ReactApplicationContext?)
UportHDSigner().importHDSeed(
reactApplicationContext,
keyLevel,
seed,
{ err, address, pubKey ->
if (err != null) {
return@importHDSeed promise.reject(err)
}

val map = WritableNativeMap()
map.putString("address", address)
map.putString("pubKey", pubKey)
return@importHDSeed promise.resolve(map)
}
)
seed
) { err, address, pubKey ->
if (err != null) {
return@importHDSeed promise.reject(err)
}

val map = WritableNativeMap()
map.putString("address", address)
map.putString("pubKey", pubKey)
return@importHDSeed promise.resolve(map)
}
}

/**
Expand Down Expand Up @@ -223,18 +225,17 @@ class RNUportHDSignerModule(reactContext: ReactApplicationContext?)
rootAddr,
hdPath,
payload,
prompt ?: "",
{ err, sigData ->
if (err != null) {
return@signTransaction promise.reject(err)
}
val map = WritableNativeMap()
map.putInt("v", sigData.v.toInt())
map.putString("r", sigData.r.keyToBase64())
map.putString("s", sigData.s.keyToBase64())
return@signTransaction promise.resolve(map)
}
)
prompt ?: ""
) { err, sigData ->
if (err != null) {
return@signTransaction promise.reject(err)
}
val map = WritableNativeMap()
map.putInt("v", sigData.v.toInt())
map.putString("r", sigData.r.keyToBase64())
map.putString("s", sigData.s.keyToBase64())
return@signTransaction promise.resolve(map)
}
}

/**
Expand Down Expand Up @@ -271,19 +272,18 @@ class RNUportHDSignerModule(reactContext: ReactApplicationContext?)
rootAddr,
hdPath,
payload,
prompt ?: "",
{ err, sigData ->
if (err != null) {
return@signJwtBundle promise.reject(err)
}
val map = WritableNativeMap()
map.putInt("v", sigData.v.toInt())
map.putString("r", sigData.r.keyToBase64())
map.putString("s", sigData.s.keyToBase64())
return@signJwtBundle promise.resolve(map)

}
)
prompt ?: ""
) { err, sigData ->
if (err != null) {
return@signJwtBundle promise.reject(err)
}
val map = WritableNativeMap()
map.putInt("v", sigData.v.toInt())
map.putString("r", sigData.r.keyToBase64())
map.putString("s", sigData.s.keyToBase64())
return@signJwtBundle promise.resolve(map)

}
}

/**
Expand Down Expand Up @@ -314,31 +314,30 @@ class RNUportHDSignerModule(reactContext: ReactApplicationContext?)
return promise.reject(storageError)
}

encryptionLayer.decrypt(activity, prompt ?: "", encryptedEntropy, { err, entropyBuff ->
encryptionLayer.decrypt(activity, prompt ?: "", encryptedEntropy) { err, entropyBuff ->
if (err != null) {
return@decrypt promise.reject(err)
}

try {
val phrase = Mnemonic.entropyToMnemonic(entropyBuff)
val seed = Mnemonic.mnemonicToSeed(phrase)
val extendedKey = generateKey(seed, hdPath)
val phrase = entropyToMnemonic(entropyBuff, WORDLIST_ENGLISH)
val extendedKey = MnemonicWords(phrase).toKey(hdPath)

val keyPair = extendedKey.keyPair

val encodedKey = keyPair.privateKey.keyToBase64()
val encodedKey = keyPair.privateKey.key.keyToBase64()
return@decrypt promise.resolve(encodedKey)
} catch (exception: Exception) {
return@decrypt promise.reject(exception)
}
})
}
}

/**
* Verifies if the provided mnemonic phrase is usable for generating keys from a seed phrase
*/
@ReactMethod
fun validateMnemonic(phrase : String?, promise: Promise?) {
fun validateMnemonic(phrase: String?, promise: Promise?) {
promise!!

promise.resolve(UportHDSigner().validateMnemonic(phrase ?: ""))
Expand Down
Loading

0 comments on commit 4831500

Please sign in to comment.