Skip to content

Commit

Permalink
Add encodePacked (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmeissner authored Dec 9, 2019
1 parent ca9b64a commit 3ea89d8
Show file tree
Hide file tree
Showing 21 changed files with 635 additions and 152 deletions.
2 changes: 1 addition & 1 deletion bivrost-abi-parser/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dependencies {
implementation "com.squareup.moshi:moshi:$versions.moshi"
implementation "com.squareup.moshi:moshi-kotlin:$versions.moshi"
implementation "com.squareup:kotlinpoet:$versions.kotlinPoet"
implementation group: 'org.bouncycastle', name: 'bcprov-jdk15on', version: '1.57'
implementation group: 'org.bouncycastle', name: 'bcprov-jdk15on', version: '1.61'

testImplementation group: 'junit', name: 'junit', version: '4.12'
}
Expand Down
10 changes: 10 additions & 0 deletions bivrost-abi-parser/src/main/kotlin/pm/gnosis/AbiParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ object AbiParser {
.addModifiers(KModifier.DATA)
.addSuperinterface(if (typeHolder.isDynamic()) SolidityBase.DynamicType::class else SolidityBase.StaticType::class)
.addFunction(generateTupleEncoder(typeHolder))
.addFunction(generateTuplePackedEncoder())
.addType(
GeneratorUtils.generateDecoder(
typeHolder.name,
Expand Down Expand Up @@ -199,6 +200,15 @@ object AbiParser {
)
.build()

private fun generateTuplePackedEncoder() =
FunSpec.builder("encodePacked")
.returns(String::class)
.addModifiers(KModifier.OVERRIDE)
.addStatement(
"throw·UnsupportedOperationException(\"Structs are not supported via encodePacked\")"
)
.build()

private fun generateFunctionEncoder(functionJson: AbiElementJson): FunSpec {
val funSpec = FunSpec.builder("encode")
functionJson.inputs.forEachIndexed { index, parameter ->
Expand Down
3 changes: 1 addition & 2 deletions bivrost-abi-parser/src/main/kotlin/pm/gnosis/EventParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ internal object EventParser {
codeBlock.addStatement("// Decode topics")
if (!isAnonymous) {
codeBlock.addStatement(
"if·($TOPIC_ARG_NAME.first()·!=·$EVENT_ID_PROPERTY_NAME)·throw·%1T(\"topics[0]·does·not·match·event·id\")",
IllegalArgumentException::class
"if·($TOPIC_ARG_NAME.first().removePrefix(\"0x\")·!=·$EVENT_ID_PROPERTY_NAME)·throw·IllegalArgumentException(\"topics[0]·does·not·match·event·id\")"
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Abi6 {
return Arguments(arg0)
}

data class Arguments(val owner: Solidity.Address)
data class Arguments(
val owner: Solidity.Address
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ class Abi7 {
return Arguments(arg0)
}

data class Return(val data: Solidity.Bytes)
data class Return(
val data: Solidity.Bytes
)

data class Arguments(val owner: Solidity.Address)
data class Arguments(
val owner: Solidity.Address
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ class Abi8 {
return Arguments(arg0)
}

data class Return(val param0: Solidity.Bytes)
data class Return(
val param0: Solidity.Bytes
)

data class Arguments(val param0: Solidity.Address)
data class Arguments(
val param0: Solidity.Address
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class Abi9 {
object Owners {
const val METHOD_ID: String = "9f767eb7"

fun encode(c: SolidityBase.Vector<TupleA>, arg2:
SolidityBase.Vector<SolidityBase.Vector<Array7<Array5<Solidity.UInt256>>>>): String
{
fun encode(c: SolidityBase.Vector<TupleA>,
arg2: SolidityBase.Vector<SolidityBase.Vector<Array7<Array5<Solidity.UInt256>>>>):
String {
return "0x" + METHOD_ID + pm.gnosis.model.SolidityBase.encodeFunctionArguments(c, arg2)
}

Expand All @@ -42,10 +42,15 @@ class Abi9 {
return Arguments(arg0, arg1)
}

data class Return(val param0: TupleB, val param1: SolidityBase.Vector<TupleB>)
data class Return(
val param0: TupleB,
val param1: SolidityBase.Vector<TupleB>
)

data class Arguments(val c: SolidityBase.Vector<TupleA>, val param1:
SolidityBase.Vector<SolidityBase.Vector<Array7<Array5<Solidity.UInt256>>>>)
data class Arguments(
val c: SolidityBase.Vector<TupleA>,
val param1: SolidityBase.Vector<SolidityBase.Vector<Array7<Array5<Solidity.UInt256>>>>
)
}

data class TupleA(
Expand All @@ -57,6 +62,10 @@ class Abi9 {
return SolidityBase.encodeFunctionArguments(a, b, param2)
}

override fun encodePacked(): String {
throw UnsupportedOperationException("Structs are not supported via encodePacked")
}

class Decoder : SolidityBase.TypeDecoder<TupleA> {
override fun isDynamic(): Boolean = true
override fun decode(source: SolidityBase.PartitionData): TupleA {
Expand All @@ -73,11 +82,18 @@ class Abi9 {
}
}

data class TupleB(val x: Solidity.UInt256, val y: Solidity.UInt256) : SolidityBase.StaticType {
data class TupleB(
val x: Solidity.UInt256,
val y: Solidity.UInt256
) : SolidityBase.StaticType {
override fun encode(): String {
return SolidityBase.encodeFunctionArguments(x, y)
}

override fun encodePacked(): String {
throw UnsupportedOperationException("Structs are not supported via encodePacked")
}

class Decoder : SolidityBase.TypeDecoder<TupleB> {
override fun isDynamic(): Boolean = false
override fun decode(source: SolidityBase.PartitionData): TupleB {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import kotlin.Boolean
import kotlin.collections.List
import pm.gnosis.model.SolidityBase

class Array5<T : SolidityBase.Type>(items: List<T>) : SolidityBase.Array<T>(items, 5) {
class Decoder<T : SolidityBase.Type>(val itemDecoder: SolidityBase.TypeDecoder<T>) :
SolidityBase.TypeDecoder<Array5<T>> {
class Array5<T : SolidityBase.Type>(
items: List<T>
) : SolidityBase.Array<T>(items, 5) {
class Decoder<T : SolidityBase.Type>(
val itemDecoder: SolidityBase.TypeDecoder<T>
) : SolidityBase.TypeDecoder<Array5<T>> {
override fun isDynamic(): Boolean = itemDecoder.isDynamic()
override fun decode(source: SolidityBase.PartitionData): Array5<T> {
return Array5(SolidityBase.decodeList(source, 5, itemDecoder))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import kotlin.Boolean
import kotlin.collections.List
import pm.gnosis.model.SolidityBase

class Array7<T : SolidityBase.Type>(items: List<T>) : SolidityBase.Array<T>(items, 7) {
class Decoder<T : SolidityBase.Type>(val itemDecoder: SolidityBase.TypeDecoder<T>) :
SolidityBase.TypeDecoder<Array7<T>> {
class Array7<T : SolidityBase.Type>(
items: List<T>
) : SolidityBase.Array<T>(items, 7) {
class Decoder<T : SolidityBase.Type>(
val itemDecoder: SolidityBase.TypeDecoder<T>
) : SolidityBase.TypeDecoder<Array7<T>> {
override fun isDynamic(): Boolean = itemDecoder.isDynamic()
override fun decode(source: SolidityBase.PartitionData): Array7<T> {
return Array7(SolidityBase.decodeList(source, 7, itemDecoder))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package expected

import java.lang.IllegalArgumentException
import kotlin.String
import kotlin.collections.List
import pm.gnosis.model.Solidity
Expand All @@ -14,13 +13,15 @@ class Abi11 {

fun decode(topics: List<String>): Arguments {
// Decode topics
if (topics.first() != EVENT_ID) throw IllegalArgumentException("topics[0] does not match event id")
if (topics.first().removePrefix("0x") != EVENT_ID) throw IllegalArgumentException("topics[0] does not match event id")
val source1 = SolidityBase.PartitionData.of(topics[1])
val t1 = Solidity.UInt256.DECODER.decode(source1)
return Arguments(t1)
}

data class Arguments(val transactionid: Solidity.UInt256)
data class Arguments(
val transactionid: Solidity.UInt256
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package expected

import java.lang.IllegalArgumentException
import kotlin.String
import kotlin.collections.List
import pm.gnosis.model.Solidity
Expand All @@ -14,15 +13,17 @@ class Abi12 {

fun decode(topics: List<String>, data: String): Arguments {
// Decode topics
if (topics.first() != EVENT_ID) throw IllegalArgumentException("topics[0] does not match event id")
if (topics.first().removePrefix("0x") != EVENT_ID) throw IllegalArgumentException("topics[0] does not match event id")

// Decode data
val source = SolidityBase.PartitionData.of(data)
val arg0 = Solidity.UInt256.DECODER.decode(source)
return Arguments(arg0)
}

data class Arguments(val transactionid: Solidity.UInt256)
data class Arguments(
val transactionid: Solidity.UInt256
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package expected

import java.lang.IllegalArgumentException
import kotlin.Boolean
import kotlin.String
import kotlin.collections.List
Expand All @@ -15,7 +14,7 @@ class Abi13 {

fun decode(topics: List<String>): Arguments {
// Decode topics
if (topics.first() != EVENT_ID) throw IllegalArgumentException("topics[0] does not match event id")
if (topics.first().removePrefix("0x") != EVENT_ID) throw IllegalArgumentException("topics[0] does not match event id")
val t1 = topics[1]
val t2 = topics[2]
val t3 = topics[3]
Expand All @@ -30,11 +29,18 @@ class Abi13 {
}
}

data class TupleA(val x: Solidity.UInt256, val y: Solidity.UInt256) : SolidityBase.StaticType {
data class TupleA(
val x: Solidity.UInt256,
val y: Solidity.UInt256
) : SolidityBase.StaticType {
override fun encode(): String {
return SolidityBase.encodeFunctionArguments(x, y)
}

override fun encodePacked(): String {
throw UnsupportedOperationException("Structs are not supported via encodePacked")
}

class Decoder : SolidityBase.TypeDecoder<TupleA> {
override fun isDynamic(): Boolean = false
override fun decode(source: SolidityBase.PartitionData): TupleA {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package expected

import java.lang.IllegalArgumentException
import java.math.BigInteger
import kotlin.Boolean
import kotlin.String
Expand All @@ -17,7 +16,7 @@ class Abi14 {

fun decode(topics: List<String>, data: String): Arguments {
// Decode topics
if (topics.first() != EVENT_ID) throw IllegalArgumentException("topics[0] does not match event id")
if (topics.first().removePrefix("0x") != EVENT_ID) throw IllegalArgumentException("topics[0] does not match event id")

// Decode data
val source = SolidityBase.PartitionData.of(data)
Expand All @@ -37,11 +36,18 @@ class Abi14 {
}
}

data class TupleA(val x: Solidity.UInt256, val y: Solidity.UInt256) : SolidityBase.StaticType {
data class TupleA(
val x: Solidity.UInt256,
val y: Solidity.UInt256
) : SolidityBase.StaticType {
override fun encode(): String {
return SolidityBase.encodeFunctionArguments(x, y)
}

override fun encodePacked(): String {
throw UnsupportedOperationException("Structs are not supported via encodePacked")
}

class Decoder : SolidityBase.TypeDecoder<TupleA> {
override fun isDynamic(): Boolean = false
override fun decode(source: SolidityBase.PartitionData): TupleA {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ class Abi15 {
return Arguments(t1)
}

data class Arguments(val transactionid: Solidity.UInt256)
data class Arguments(
val transactionid: Solidity.UInt256
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,23 @@ class Abi16 {
return Arguments(arg0)
}

data class Arguments(val c: TupleA)
data class Arguments(
val c: TupleA
)
}

data class TupleA(val bytesvar: Solidity.Bytes, val stringvar: Solidity.String) :
SolidityBase.DynamicType {
data class TupleA(
val bytesvar: Solidity.Bytes,
val stringvar: Solidity.String
) : SolidityBase.DynamicType {
override fun encode(): String {
return SolidityBase.encodeFunctionArguments(bytesvar, stringvar)
}

override fun encodePacked(): String {
throw UnsupportedOperationException("Structs are not supported via encodePacked")
}

class Decoder : SolidityBase.TypeDecoder<TupleA> {
override fun isDynamic(): Boolean = true
override fun decode(source: SolidityBase.PartitionData): TupleA {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,13 @@ private fun generateDynamicBytes(): TypeSpec {
.addStatement("return %1T(length, contents)", SolidityBase.DynamicType.Parts::class)
.build())
.build())
.addFunction(FunSpec.builder("encodePacked")
.addModifiers(KModifier.OVERRIDE)
.returns(String::class)
.addCode(CodeBlock.builder()
.addStatement("return items.toHex()")
.build())
.build())
.addType(GeneratorUtils.generateDecoder(name,
CodeBlock.builder()
.addStatement("return %1N(%2T.decodeBytes(source))", name, SolidityBase::class)
Expand Down
Loading

0 comments on commit 3ea89d8

Please sign in to comment.