Skip to content

Commit

Permalink
Added support for eip-1191
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathansmirnoff committed Apr 16, 2020
1 parent 04e0bf2 commit 63162cc
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ This web tool should help getting the checksum: https://ethsum.netlify.com
- `github`: Where token or project-related code lives.
- `community`: Twitter, Reddit, Slack or wherever else people hang out.
- `website`: Official URL of the website.

- `address_eip1191`: Ethereum (or other chain) address of ERC-20 token, in [EIP-1191](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1191.md) mixed-case format.

# The assembled lists

Expand Down
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
buildscript {
ext {
KOTLIN_VERSION = "1.3.71"
KETHEREUM_VERSION = "0.81.6"
KETHEREUM_VERSION = "0.81.7"
}

repositories {
Expand Down Expand Up @@ -51,6 +51,7 @@ dependencies {

compile "com.github.komputing.kethereum:erc20:${KETHEREUM_VERSION}"
compile "com.github.komputing.kethereum:erc55:${KETHEREUM_VERSION}"
compile "com.github.komputing.kethereum:erc1191:${KETHEREUM_VERSION}"
compile "com.github.komputing.kethereum:model:${KETHEREUM_VERSION}"
compile "com.github.komputing.kethereum:rpc:${KETHEREUM_VERSION}"
compile "com.github.komputing.kethereum:rpc_min3:${KETHEREUM_VERSION}"
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/org/ethereum/lists/tokens/Constant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import java.io.File

val mandatoryFields = listOf("name", "symbol", "address", "decimals")
val optionalFields = listOf("comment", "logo", "support", "community", "website", "github", "img-16x16", "img-128x128", "social", "ens_address", "deprecation", "type", "invalid_erc20_symbol", "invalid_erc20_decimals")
val optionalFields = listOf("comment", "logo", "support", "community", "website", "github", "img-16x16", "img-128x128", "social", "ens_address", "deprecation", "type", "invalid_erc20_symbol", "invalid_erc20_decimals", "address_eip1191")

val outDir = File("build/output")
val allNetworksTokenDir = File("tokens")
Expand Down
19 changes: 14 additions & 5 deletions src/main/kotlin/org/ethereum/lists/tokens/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ import org.ethereum.lists.cilib.copyFields
import java.io.File
import java.nio.file.Files
import kotlin.system.exitProcess
import org.kethereum.model.ChainId


val networkMapping = mapOf("etc" to 61, "eth" to 1, "kov" to 42, "rin" to 4, "rop" to 3, "rsk" to 40, "ella" to 64, "esn" to 2, "gor" to 5)
val networkMapping = mapOf("etc" to 61, "eth" to 1, "kov" to 42, "rin" to 4, "rop" to 3, "rsk" to 30, "ella" to 64, "esn" to 2, "gor" to 5)

suspend fun main() {
checkForTokenDefinitionsInWrongPath()

allNetworksTokenDir.listFiles()?.forEach { singleNetworkTokenDirectory ->
val jsonArray = JsonArray<JsonObject>()
val jsonArray = JsonArray<JsonObject>()
singleNetworkTokenDirectory.listFiles()?.forEach {
try {
checkTokenFile(it, true)
try {
checkTokenFile(it, true, getChainId(networkMapping, singleNetworkTokenDirectory.name))
val jsonObject = it.reader().use { reader ->
Klaxon().parseJsonObject(reader)
}
Expand All @@ -42,6 +43,14 @@ suspend fun main() {
}
}

private fun getChainId(networkMapping: Map<String, Int>, networkName: String): ChainId? {
var chainId: ChainId? = null
if (networkMapping[networkName] != null){
return ChainId((networkMapping[networkName]!!).toBigInteger())
}
return chainId
}

private fun checkForTokenDefinitionsInWrongPath() {
File(".").walk().forEach { path ->
if (path.isDirectory
Expand All @@ -60,4 +69,4 @@ fun JsonArray<*>.writeJSON(pathName: String, filename: String) {
val fullOutFile = File(fullOutDir, "$filename.json")

fullOutFile.writeText(toJsonString(false))
}
}
17 changes: 14 additions & 3 deletions src/main/kotlin/org/ethereum/lists/tokens/TokenChecker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import org.ethereum.lists.tokens.model.Token
import org.kethereum.erc55.hasValidERC55Checksum
import org.kethereum.erc55.isValid
import org.kethereum.erc55.withERC55Checksum
import org.kethereum.erc1191.hasValidERC1191Checksum
import org.kethereum.erc1191.withERC1191Checksum
import org.kethereum.model.Address
import org.kethereum.model.ChainId
import org.kethereum.rpc.min3.getMin3RPC
import org.komputing.kethereum.erc20.ERC20RPCConnector
import java.io.File
Expand All @@ -18,6 +21,7 @@ import java.time.format.DateTimeFormatter

open class InvalidTokenException(message: String) : IllegalArgumentException(message)
class InvalidChecksum(message: String) : InvalidTokenException("The address is not valid with ERC-55 checksum $message")
class Invalid1191Checksum(message: String) : InvalidTokenException("The address is not valid with EIP-1191 checksum $message")

class InvalidAddress(address: Address) : InvalidTokenException("The address is not valid $address")
class InvalidDecimals : InvalidTokenException("Decimals must be a number")
Expand All @@ -37,20 +41,27 @@ val onChainIgnore by lazy {
File("onChainIgnore.lst").readText().split("\n")
}

suspend fun checkTokenFile(file: File, onChainCheck: Boolean = false) {
suspend fun checkTokenFile(file: File, onChainCheck: Boolean = false, chainId: ChainId? = null) {
val handle = file.name.removeSuffix(".json")
if (onChainCheck && (notToProcessFiles.contains(handle) || onChainIgnore.contains(handle))) {
return
}

val jsonObject = Klaxon().parseJsonObject(file.reader())
val address = Address(jsonObject["address"] as String)
val address_eip1191 = if (jsonObject["address_eip1191"] != null) Address(jsonObject["address_eip1191"] as String) else null

when {
!address.isValid() -> throw InvalidAddress(address)

!address.hasValidERC55Checksum()
address_eip1191 != null && !(address_eip1191.isValid()) -> throw InvalidAddress(address)

(!address.hasValidERC55Checksum() && address_eip1191 == null)
-> throw InvalidChecksum(address.toString() + " expected: " + address.withERC55Checksum())

(address_eip1191 != null && chainId != null && !address_eip1191.hasValidERC1191Checksum(chainId))
-> throw Invalid1191Checksum(address_eip1191.toString() + " expected: " + address_eip1191.withERC1191Checksum(chainId))

file.name != "${address.hex}.json" -> throw InvalidFileName()
}
if (jsonObject["decimals"] !is Int) {
Expand Down Expand Up @@ -90,7 +101,7 @@ suspend fun checkTokenFile(file: File, onChainCheck: Boolean = false) {
throw InvalidWebsite()
}
}
try {
try {
val token = moshi.adapter(Token::class.java).failOnUnknown().fromJson(file.readText())

token?.deprecation?.let {
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/org/ethereum/lists/tokens/model/Token.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ data class Token(val symbol: String,
val logo: Logo? = null,
val support: Support? = null,
val social: Social? = null,
val deprecation: Deprecation? = null)
val deprecation: Deprecation? = null,
val address_eip1191: String? = null)
15 changes: 15 additions & 0 deletions src/test/kotlin/TheTokenChecker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import kotlinx.coroutines.runBlocking
import org.ethereum.lists.tokens.*
import org.junit.Test
import java.io.File
import org.kethereum.model.ChainId

class TheTokenChecker {

Expand Down Expand Up @@ -34,6 +35,13 @@ class TheTokenChecker {
checkTokenFile(file)
}

@Test
fun shouldPassForValidTokenEip1191(): Unit = runBlocking {
val file = getFile("valid_eip1191/0x2aCc95758f8b5F583470bA265Eb685a8f45fC9D5.json")

checkTokenFile(file, false, ChainId(30))
}


@Test(expected = InvalidAddress::class)
fun shouldFailForInvalidAddress(): Unit = runBlocking {
Expand Down Expand Up @@ -99,6 +107,13 @@ class TheTokenChecker {
checkTokenFile(file)
}

@Test(expected = Invalid1191Checksum::class)
fun shouldFailForInvalidChecksumEip1191(): Unit = runBlocking {
val file = getFile("invalid_eip1191/0x2acc95758f8b5f583470ba265eb685a8f45fc9d5.json")

checkTokenFile(file, false, ChainId(30))
}

private fun getFile(s: String) = File(javaClass.classLoader.getResource("test_tokens/$s").file)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"symbol": "NONE",
"name": "None",
"address": "0x2acc95758f8b5f583470ba265eb685a8f45fc9d5",
"decimals": 0,
"support": {
"email": "[email protected]"
},
"social": {
"github": "https://github.com/walleth/contracts/tree/master/NoneToken"
},
"address_eip1191": "0x2acc95758f8b5f583470ba265eb685a8f45fc9d5"
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"symbol": "NONE",
"name": "None",
"address": "0x2aCc95758f8b5F583470bA265Eb685a8f45fC9D5",
"decimals": 0,
"support": {
"email": "[email protected]"
},
"social": {
"github": "https://github.com/walleth/contracts/tree/master/NoneToken"
},
"address_eip1191": "0x2aCc95758f8b5F583470bA265Eb685a8f45fC9D5"
}

0 comments on commit 63162cc

Please sign in to comment.