Skip to content

Revert "[ETCM-893] Add support for chains with ids bigger than 127" #1008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 14 additions & 19 deletions crypto/src/main/scala/io/iohk/ethereum/crypto/ECDSASignature.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ object ECDSASignature {

val allowedPointSigns = Set(negativePointSign, positivePointSign)

def apply(r: ByteString, s: ByteString, v: BigInt): ECDSASignature = {
def apply(r: ByteString, s: ByteString, v: Byte): ECDSASignature = {
ECDSASignature(BigInt(1, r.toArray), BigInt(1, s.toArray), v)
}

Expand All @@ -46,11 +46,7 @@ object ECDSASignature {
sign(messageHash.toArray, keyPairFromPrvKey(prvKey.toArray), None)

/** Sign a messageHash, expected to be a Keccak256 hash of the original data. */
def sign(
messageHash: Array[Byte],
keyPair: AsymmetricCipherKeyPair,
chainId: Option[BigInt] = None
): ECDSASignature = {
def sign(messageHash: Array[Byte], keyPair: AsymmetricCipherKeyPair, chainId: Option[Byte] = None): ECDSASignature = {
require(
messageHash.size == 32,
s"The message should be a hash, expected to be 32 bytes; got ${messageHash.size} bytes."
Expand All @@ -65,10 +61,10 @@ object ECDSASignature {
.getOrElse(throw new RuntimeException("Failed to calculate signature rec id"))

val pointSign = chainId match {
case Some(id) if v == negativePointSign => id * 2 + newNegativePointSign
case Some(id) if v == positivePointSign => id * 2 + newPositivePointSign
case None => BigInt(v)
case _ => throw new IllegalStateException(s"Unexpected pointSign. ChainId: $chainId, v: $v")
case Some(id) if v == negativePointSign => (id * 2 + newNegativePointSign).toByte
case Some(id) if v == positivePointSign => (id * 2 + newPositivePointSign).toByte
case None => v
case _ => throw new IllegalStateException(s"Unexpected pointSign. ChainId: ${chainId}, v: ${v}")
}

ECDSASignature(r, s, pointSign)
Expand All @@ -78,18 +74,17 @@ object ECDSASignature {
* new formula for calculating point sign post EIP 155 adoption
* v = CHAIN_ID * 2 + 35 or v = CHAIN_ID * 2 + 36
*/
private def getRecoveredPointSign(pointSign: BigInt, chainId: Option[BigInt]): Option[Byte] = {
val signByte = pointSign.toByte
private def getRecoveredPointSign(pointSign: Byte, chainId: Option[Byte]): Option[Byte] = {
(chainId match {
case Some(id) =>
if (signByte == negativePointSign || signByte == (id * 2 + newNegativePointSign).toByte) {
if (pointSign == negativePointSign || pointSign == (id * 2 + newNegativePointSign).toByte) {
Some(negativePointSign)
} else if (signByte == positivePointSign || signByte == (id * 2 + newPositivePointSign).toByte) {
} else if (pointSign == positivePointSign || pointSign == (id * 2 + newPositivePointSign).toByte) {
Some(positivePointSign)
} else {
None
}
case None => Some(signByte)
case None => Some(pointSign)
}).filter(pointSign => allowedPointSigns.contains(pointSign))
}

Expand All @@ -111,8 +106,8 @@ object ECDSASignature {
private def recoverPubBytes(
r: BigInt,
s: BigInt,
recId: BigInt,
chainId: Option[BigInt],
recId: Byte,
chainId: Option[Byte],
messageHash: Array[Byte]
): Option[Array[Byte]] = {
Try {
Expand Down Expand Up @@ -157,15 +152,15 @@ object ECDSASignature {
* @param s - part of the signature calculated with signer private key
* @param v - public key recovery id
*/
case class ECDSASignature(r: BigInt, s: BigInt, v: BigInt) {
case class ECDSASignature(r: BigInt, s: BigInt, v: Byte) {

/**
* returns ECC point encoded with on compression and without leading byte indicating compression
* @param messageHash message to be signed; should be a hash of the actual data.
* @param chainId optional value if you want new signing schema with recovery id calculated with chain id
* @return
*/
def publicKey(messageHash: Array[Byte], chainId: Option[BigInt] = None): Option[Array[Byte]] =
def publicKey(messageHash: Array[Byte], chainId: Option[Byte] = None): Option[Array[Byte]] =
ECDSASignature.recoverPubBytes(r, s, v, chainId, messageHash)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ECDSASignatureSpec extends AnyFlatSpec with Matchers with ScalaCheckProper
val pointSign = 28

val sig =
ECDSASignature(BigInt(1, signatureRandom.toArray[Byte]), BigInt(1, signature.toArray[Byte]), pointSign)
ECDSASignature(BigInt(1, signatureRandom.toArray[Byte]), BigInt(1, signature.toArray[Byte]), pointSign.toByte)

sig.publicKey(bytesToSign).isEmpty shouldBe false
}
Expand All @@ -30,7 +30,7 @@ class ECDSASignatureSpec extends AnyFlatSpec with Matchers with ScalaCheckProper
val pointSign = 0x1f

val sig =
ECDSASignature(BigInt(1, signatureRandom.toArray[Byte]), BigInt(1, signature.toArray[Byte]), pointSign)
ECDSASignature(BigInt(1, signatureRandom.toArray[Byte]), BigInt(1, signature.toArray[Byte]), pointSign.toByte)

sig.publicKey(bytesToSign).isEmpty shouldBe true
}
Expand All @@ -39,7 +39,7 @@ class ECDSASignatureSpec extends AnyFlatSpec with Matchers with ScalaCheckProper
val sig = ECDSASignature(
ByteStringUtils.string2hash("149a2046f51f5d043633664d76eef4f99cdba8e53851dcda57224dfe8770f98a"),
ByteStringUtils.string2hash("a8898478e9aae9fadb71c7ab5451d47d2efa4199fc26ecc1da62ce8fb77e06f1"),
28
28.toByte
)
val messageHash = ByteStringUtils.string2hash("a1ede9cdf0b6fe37a384b265dce6b74a7464f11799dcee022f628450a19cf4eb")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class RLPSpeedSuite
pointSign = 28,
signatureRandom = ByteString(Hex.decode("cfe3ad31d6612f8d787c45f115cc5b43fb22bcc210b62ae71dc7cbf0a6bea8df")),
signature = ByteString(Hex.decode("57db8998114fae3c337e99dbd8573d4085691880f4576c6c1f6c5bbfe67d6cf0")),
chainId = 0x3d
chainId = 0x3d.toByte
)

lazy val blockGen: Gen[Block] = for {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ECIP1017Test extends AnyFlatSpec with Matchers {
monetaryPolicyConfig = MonetaryPolicyConfig(EraDuration, 0.2, 5000000000000000000L, 3000000000000000000L),
// unused
maxCodeSize = None,
chainId = 0x3d,
chainId = 0x3d.toByte,
networkId = 1,
forkBlockNumbers = ForkBlockNumbers(
frontierBlockNumber = 0,
Expand Down
2 changes: 1 addition & 1 deletion src/it/scala/io/iohk/ethereum/txExecTest/ForksTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ForksTest extends AnyFlatSpec with Matchers {
ecip1099BlockNumber = Long.MaxValue,
ecip1049BlockNumber = None
),
chainId = 0x3d,
chainId = 0x3d.toByte,
monetaryPolicyConfig = MonetaryPolicyConfig(5000000, 0.2, 5000000000000000000L, 3000000000000000000L),
// unused
bootstrapNodes = Set(),
Expand Down
2 changes: 0 additions & 2 deletions src/main/resources/conf/testmode.conf
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,3 @@ mantis {
}

}

akka.http.server.request-timeout = 30.seconds
17 changes: 6 additions & 11 deletions src/main/scala/io/iohk/ethereum/domain/SignedTransaction.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ object SignedTransaction {

def apply(
tx: Transaction,
pointSign: BigInt,
pointSign: Byte,
signatureRandom: ByteString,
signature: ByteString,
chainId: BigInt
chainId: Byte
): SignedTransaction = {
val txSignature = ECDSASignature(
r = new BigInteger(1, signatureRandom.toArray),
Expand All @@ -58,12 +58,7 @@ object SignedTransaction {
SignedTransaction(tx, txSignature)
}

def apply(
tx: Transaction,
pointSign: BigInt,
signatureRandom: ByteString,
signature: ByteString
): SignedTransaction = {
def apply(tx: Transaction, pointSign: Byte, signatureRandom: ByteString, signature: ByteString): SignedTransaction = {
val txSignature = ECDSASignature(
r = new BigInteger(1, signatureRandom.toArray),
s = new BigInteger(1, signature.toArray),
Expand All @@ -72,14 +67,14 @@ object SignedTransaction {
SignedTransaction(tx, txSignature)
}

def sign(tx: Transaction, keyPair: AsymmetricCipherKeyPair, chainId: Option[BigInt]): SignedTransactionWithSender = {
def sign(tx: Transaction, keyPair: AsymmetricCipherKeyPair, chainId: Option[Byte]): SignedTransactionWithSender = {
val bytes = bytesToSign(tx, chainId)
val sig = ECDSASignature.sign(bytes, keyPair, chainId)
val address = Address(keyPair)
SignedTransactionWithSender(tx, sig, address)
}

private def bytesToSign(tx: Transaction, chainId: Option[BigInt]): Array[Byte] = {
private def bytesToSign(tx: Transaction, chainId: Option[Byte]): Array[Byte] = {
chainId match {
case Some(id) =>
chainSpecificTransactionBytes(tx, id)
Expand Down Expand Up @@ -133,7 +128,7 @@ object SignedTransaction {
crypto.kec256(rlpEncode(RLPList(tx.nonce, tx.gasPrice, tx.gasLimit, receivingAddressAsArray, tx.value, tx.payload)))
}

private def chainSpecificTransactionBytes(tx: Transaction, chainId: BigInt): Array[Byte] = {
private def chainSpecificTransactionBytes(tx: Transaction, chainId: Byte): Array[Byte] = {
val receivingAddressAsArray: Array[Byte] = tx.receivingAddress.map(_.toArray).getOrElse(Array.emptyByteArray)
crypto.kec256(
rlpEncode(
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/io/iohk/ethereum/extvm/VMServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class VMServer(messageHandler: MessageHandler) extends Logger {
aghartaBlockNumber = BigInt(9573000), //TODO include agharta block number in protobuf
petersburgBlockNumber = BigInt(10000000), //TODO include petersburg block number in protobuf
phoenixBlockNumber = BigInt(10500839), //TODO include phoenix block number in protobuf
chainId = 0x3d //TODO include chainId in protobuf
chainId = 0x3d.toByte //TODO include chainId in protobuf
)
}
}
4 changes: 2 additions & 2 deletions src/main/scala/io/iohk/ethereum/jsonrpc/EthInfoService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import io.iohk.ethereum.utils.BlockchainConfig

object EthInfoService {
case class ChainIdRequest()
case class ChainIdResponse(value: BigInt)
case class ChainIdResponse(value: Byte)

case class ProtocolVersionRequest()
case class ProtocolVersionResponse(value: String)
Expand Down Expand Up @@ -174,7 +174,7 @@ class EthInfoService(
val toAddress = req.tx.to.map(Address.apply)

val tx = Transaction(0, req.tx.gasPrice, gasLimit, toAddress, req.tx.value, req.tx.data)
val fakeSignature = ECDSASignature(0, 0, 0)
val fakeSignature = ECDSASignature(0, 0, 0.toByte)
SignedTransactionWithSender(tx, fakeSignature, fromAddress)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ object JsonMethodsImplicits extends JsonMethodsImplicits {
implicit val personal_sign = new JsonMethodCodec[SignRequest, SignResponse] {
override def encodeJson(t: SignResponse): JValue = {
import t.signature._
encodeAsHex(ByteString(r.toUnsignedByteArray ++ s.toUnsignedByteArray ++ v.toUnsignedByteArray))
encodeAsHex(ByteString(r.toUnsignedByteArray ++ s.toUnsignedByteArray :+ v))
}

override def decodeJson(params: Option[JArray]): Either[JsonRpcError, SignRequest] =
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/io/iohk/ethereum/keystore/Wallet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import org.bouncycastle.crypto.AsymmetricCipherKeyPair
case class Wallet(address: Address, prvKey: ByteString) {
lazy val keyPair: AsymmetricCipherKeyPair = keyPairFromPrvKey(prvKey.toArray)

def signTx(tx: Transaction, chainId: Option[BigInt]): SignedTransactionWithSender =
def signTx(tx: Transaction, chainId: Option[Byte]): SignedTransactionWithSender =
SignedTransaction.sign(tx, keyPair, chainId)
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ object CommonMessages {

object SignedTransactions {

lazy val chainId: BigInt = Config.blockchains.blockchainConfig.chainId
lazy val chainId: Byte = Config.blockchains.blockchainConfig.chainId

implicit class SignedTransactionEnc(val signedTx: SignedTransaction) extends RLPSerializable {
override def toRLPEncodable: RLPEncodeable = {
Expand Down Expand Up @@ -187,7 +187,7 @@ object CommonMessages {
val receivingAddressOpt = if (receivingAddress.bytes.isEmpty) None else Some(Address(receivingAddress.bytes))
SignedTransaction(
Transaction(nonce, gasPrice, gasLimit, receivingAddressOpt, value, payload),
pointSign: BigInt,
(pointSign: Int).toByte,
signatureRandom,
signature,
chainId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ trait AuthInitiateEcdsaCodec {
val r = input.take(RLength)
val s = input.slice(SIndex, SIndex + SLength)
val v = input(VIndex) + 27
ECDSASignature(BigInt(1, r), BigInt(1, s), v)
ECDSASignature(BigInt(1, r), BigInt(1, s), v.toByte)
}
}
8 changes: 5 additions & 3 deletions src/main/scala/io/iohk/ethereum/utils/BlockchainConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ case class BlockchainConfig(
customGenesisJsonOpt: Option[String],
daoForkConfig: Option[DaoForkConfig],
accountStartNonce: UInt256,
chainId: BigInt,
chainId: Byte,
networkId: Int,
monetaryPolicyConfig: MonetaryPolicyConfig,
gasTieBreaker: Boolean,
Expand Down Expand Up @@ -104,9 +104,11 @@ object BlockchainConfig {
val daoForkConfig = Try(blockchainConfig.getConfig("dao")).toOption.map(DaoForkConfig(_))
val accountStartNonce: UInt256 = UInt256(BigInt(blockchainConfig.getString("account-start-nonce")))

val chainId: BigInt = {
val chainId: Byte = {
val s = blockchainConfig.getString("chain-id")
parseHexOrDecNumber(s)
val n = parseHexOrDecNumber(s)
require(n >= 0 && n <= 127, "chain-id must be a number in range [0, 127]")
n.toByte
}

val networkId: Int = blockchainConfig.getInt("network-id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ case class BlockchainConfigForEvm(
aghartaBlockNumber: BigInt,
petersburgBlockNumber: BigInt,
phoenixBlockNumber: BigInt,
chainId: BigInt
chainId: Byte
) {
def etcForkForBlockNumber(blockNumber: BigInt): EtcFork = blockNumber match {
case _ if blockNumber < atlantisBlockNumber => BeforeAtlantis
Expand Down
Loading