Skip to content

Commit

Permalink
✨Import WalletConnect Library
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsinsar committed Jun 17, 2021
1 parent 1fcacdd commit c600e54
Show file tree
Hide file tree
Showing 46 changed files with 1,482 additions and 0 deletions.
1 change: 1 addition & 0 deletions walletconnect/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
54 changes: 54 additions & 0 deletions walletconnect/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
apply plugin: 'com.android.library'

apply plugin: 'kotlin-android'

group='com.github.TrustWallet'

android {
compileSdkVersion 28


defaultConfig {
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

testOptions {
unitTests {
includeAndroidResources = true
}
}
compileOptions {
sourceCompatibility = "1.8"
targetCompatibility = 1.8
}
buildToolsVersion = '28.0.3'
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.squareup.okhttp3:okhttp:4.9.2'
implementation 'com.google.code.gson:gson:2.8.7'
implementation 'com.github.salomonbrys.kotson:kotson:2.5.0'
implementation 'com.android.support:appcompat-v7:28.0.0'
testImplementation 'junit:junit:4.13'
testImplementation 'org.robolectric:robolectric:4.3'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

tasks.withType(Javadoc).all { enabled = false }
apply from: "$rootDir/publish/publish.gradle"
4 changes: 4 additions & 0 deletions walletconnect/library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
artifact=walletconnect
libraryName=com.hipo.macaron
libraryVersion=1.0.0
libraryDescription=WalletConnect Library that's been forked from TrustWallet
21 changes: 21 additions & 0 deletions walletconnect/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
2 changes: 2 additions & 0 deletions walletconnect/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.trustwallet.walletconnect"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.trustwallet.walletconnect

import com.trustwallet.walletconnect.exceptions.InvalidHmacException
import com.trustwallet.walletconnect.extensions.hexStringToByteArray
import com.trustwallet.walletconnect.extensions.toHex
import com.trustwallet.walletconnect.models.WCEncryptionPayload
import java.security.SecureRandom
import javax.crypto.Cipher
import javax.crypto.Mac
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec

private val CIPHER_ALGORITHM = "AES/CBC/PKCS7Padding"
private val MAC_ALGORITHM = "HmacSHA256"

object WCCipher {
fun encrypt(data: ByteArray, key: ByteArray): WCEncryptionPayload {
val iv = randomBytes(16)
val keySpec = SecretKeySpec(key, "AES")
val ivSpec = IvParameterSpec(iv)
val cipher = Cipher.getInstance(CIPHER_ALGORITHM)
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec)

val encryptedData = cipher.doFinal(data)
val hmac = computeHmac(
data = encryptedData,
iv = iv,
key = key
)

return WCEncryptionPayload(
data = encryptedData.toHex(),
iv = iv.toHex(),
hmac = hmac
)
}

fun decrypt(payload: WCEncryptionPayload, key: ByteArray): ByteArray {
val data = payload.data.hexStringToByteArray()
val iv = payload.iv.hexStringToByteArray()
val computedHmac = computeHmac(
data = data,
iv = iv,
key = key
)

if (computedHmac != payload.hmac.toLowerCase()) {
throw InvalidHmacException()
}

val keySpec = SecretKeySpec(key, "AES")
val ivSpec = IvParameterSpec(iv)
val cipher = Cipher.getInstance(CIPHER_ALGORITHM)
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec)

return cipher.doFinal(data)
}

private fun computeHmac(data: ByteArray, iv: ByteArray, key: ByteArray): String {
val mac = Mac.getInstance(MAC_ALGORITHM)
val payload = data + iv
mac.init(SecretKeySpec(key, MAC_ALGORITHM))
return mac.doFinal(payload).toHex()
}

private fun randomBytes(size: Int): ByteArray {
val secureRandom = SecureRandom()
val bytes = ByteArray(size)
secureRandom.nextBytes(bytes)

return bytes
}
}
Loading

0 comments on commit c600e54

Please sign in to comment.