Skip to content

Commit

Permalink
Add module with support for EIP191 - closes #51
Browse files Browse the repository at this point in the history
  • Loading branch information
ligi committed Jan 2, 2019
1 parent 62ee344 commit 12c136f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
1 change: 1 addition & 0 deletions eip191/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
9 changes: 9 additions & 0 deletions eip191/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
dependencies {
implementation project(":model")
implementation project(":crypto")
implementation project(":crypto_api")
implementation project(":functions")
implementation project(":rlp")

testImplementation "com.github.walleth:khex:$khex_version"
}
19 changes: 19 additions & 0 deletions eip191/src/main/kotlin/org/kethereum/eip191/EIP191.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.kethereum.eip191

import org.kethereum.crypto.model.ECKeyPair
import org.kethereum.crypto.signMessage

/*
*
* http://eips.ethereum.org/EIPS/eip-191
*
*/


fun ECKeyPair.signWithEIP191(version: Byte, versionSpecificData: ByteArray, message: ByteArray) =
signMessage(0x19.toByte().toByteArray() + version.toByteArray() + versionSpecificData + message)

fun ECKeyPair.signWithEIP191PersonalSign(message: ByteArray) =
signWithEIP191(0x45, ("thereum Signed Message:\n" + message.size).toByteArray(), message)

private fun Byte.toByteArray() = ByteArray(1) { this }
22 changes: 22 additions & 0 deletions eip191/src/test/kotlin/org/kethereum/eip191/TheEIP191.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.kethereum.eip191

import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.kethereum.crypto.createEthereumKeyPair
import org.kethereum.crypto.signMessage

val KEY_PAIR = createEthereumKeyPair()

class TheSignatures {

@Test
fun createsCorrectPersonalSignSignature() {

val payload = "Test Payload".toByteArray()

assertThat(KEY_PAIR.signWithEIP191PersonalSign(payload))
.isEqualTo(KEY_PAIR.signMessage(("\u0019Ethereum Signed Message:\n" + payload.size).toByteArray() + payload))
}


}
3 changes: 2 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ include ':crypto_api'
include ':crypto_api_java_provider'
include ':crypto_api_spongycastle'
include ':crypto_api_wrapper'
include ':eip155'
include ':erc55'
include ':erc67'
include ':eip155'
include ':eip191'
include ':erc681'
include ':erc831'
include ':erc961'
Expand Down

0 comments on commit 12c136f

Please sign in to comment.