Skip to content

Commit

Permalink
Merge pull request onflow#5932 from onflow/giovanni/evm-address-seria…
Browse files Browse the repository at this point in the history
…lization

[EVM] EVMAddress de/serialization follow up
  • Loading branch information
sisyphusSmiling authored May 16, 2024
2 parents e206053 + 0539f71 commit fc3e499
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 34 deletions.
2 changes: 1 addition & 1 deletion engine/execution/state/bootstrap/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestBootstrapLedger(t *testing.T) {
}

func TestBootstrapLedger_ZeroTokenSupply(t *testing.T) {
expectedStateCommitmentBytes, _ := hex.DecodeString("fe020f31345d3484fdbd24ea02b1a06f474671c421b056ec3b01b78c128c20ca")
expectedStateCommitmentBytes, _ := hex.DecodeString("1668d2976c830ef5b8d860d8bee72a0cdf4ddf01bfa63b4b2f2981a453752499")
expectedStateCommitment, err := flow.ToStateCommitment(expectedStateCommitmentBytes)
require.NoError(t, err)

Expand Down
37 changes: 8 additions & 29 deletions fvm/evm/stdlib/contract.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,7 @@ contract EVM {
/// Future implementations should pass data to InternalEVM for native serialization
access(all)
view fun toString(): String {
let addressBytes: [UInt8] = [
self.bytes[0], self.bytes[1], self.bytes[2], self.bytes[3],
self.bytes[4], self.bytes[5], self.bytes[6], self.bytes[7],
self.bytes[8], self.bytes[9], self.bytes[10], self.bytes[11],
self.bytes[12], self.bytes[13], self.bytes[14], self.bytes[15],
self.bytes[16], self.bytes[17], self.bytes[18], self.bytes[19]
]
return String.encodeHex(addressBytes)
return String.encodeHex(self.bytes.toVariableSized())
}

/// Compares the address with another address
Expand All @@ -174,28 +167,14 @@ contract EVM {
/// Converts a hex string to an EVM address if the string is a valid hex string
/// Future implementations should pass data to InternalEVM for native deserialization
access(all)
fun addressFromString(_ asHex: String): EVMAddress? {
fun addressFromString(_ asHex: String): EVMAddress {
pre {
asHex.length == 40 || asHex.length == 42: "Invalid hex string length"
}
// Remove the 0x prefix if it exists
let sanitized = (asHex[1] == "x" ? asHex.split(separator: "x")[1] : asHex).toLower()
if sanitized.length != 40 {
return nil
}
// Decode the hex string
var addressBytes: [UInt8] = asHex.decodeHex()
if addressBytes.length != 20 {
return nil
}
// Return the EVM address from the decoded hex string
return EVM.EVMAddress(bytes: [
addressBytes[0], addressBytes[1], addressBytes[2], addressBytes[3],
addressBytes[4], addressBytes[5], addressBytes[6], addressBytes[7],
addressBytes[8], addressBytes[9], addressBytes[10], addressBytes[11],
addressBytes[12], addressBytes[13], addressBytes[14], addressBytes[15],
addressBytes[16], addressBytes[17], addressBytes[18], addressBytes[19]
])
asHex.length == 40 || asHex.length == 42: "Invalid hex string length for an EVM address"
}
// Strip the 0x prefix if it exists
var withoutPrefix = (asHex[1] == "x" ? asHex.slice(from: 2, upTo: asHex.length) : asHex).toLower()
let bytes = withoutPrefix.decodeHex().toConstantSized<[UInt8;20]>()!
return EVMAddress(bytes: bytes)
}

access(all)
Expand Down
2 changes: 1 addition & 1 deletion fvm/evm/stdlib/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2822,7 +2822,7 @@ func TestEVMAddressSerializationAndDeserialization(t *testing.T) {
access(all)
fun main(hexString: String): EVM.EVMAddress {
return EVM.addressFromString(hexString) ?? panic("Invalid address provided")
return EVM.addressFromString(hexString)
}
`)

Expand Down
6 changes: 3 additions & 3 deletions utils/unittest/execution_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const ServiceAccountPrivateKeySignAlgo = crypto.ECDSAP256
const ServiceAccountPrivateKeyHashAlgo = hash.SHA2_256

// Pre-calculated state commitment with root account with the above private key
const GenesisStateCommitmentHex = "dfcc8c01fa316461d6734a93d3291269b559a1a2d57c061cb3d9b5121fa352d7"
const GenesisStateCommitmentHex = "96493d5fe1772a2c89df85337bd46d089e200cca91a6873f7b4bf872e27a6d7c"

var GenesisStateCommitment flow.StateCommitment

Expand Down Expand Up @@ -87,10 +87,10 @@ func genesisCommitHexByChainID(chainID flow.ChainID) string {
return GenesisStateCommitmentHex
}
if chainID == flow.Testnet {
return "973771c938eec6833222d2b123ff4e3ac65bba1f8368c36d18f47c5c872a3d42"
return "57c1a8b33cf96d6c006ff86e9ab307ac09e91118440d7a1679cbcaf464233e5d"
}
if chainID == flow.Sandboxnet {
return "e1c08b17f9e5896f03fe28dd37ca396c19b26628161506924fbf785834646ea1"
}
return "bfaf048a4b86b1851ca3e9fe19d8adbe198b5d8eafa846d452e5fac64757fb3d"
return "586452c116b30e96fad18b95eaee32c9762a5324dc3ecc0e9948781f13736fdf"
}

0 comments on commit fc3e499

Please sign in to comment.