Skip to content

Commit

Permalink
feat: import addresses with EIP-55
Browse files Browse the repository at this point in the history
  • Loading branch information
yangby-cryptape committed Aug 21, 2023
1 parent 1d89fa1 commit c4f633f
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 16 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion common/config-parser/src/types/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use std::{ffi::OsStr, io, path::PathBuf};
use clap::builder::{StringValueParser, TypedValueParser, ValueParserFactory};
use serde::Deserialize;

use protocol::types::{Block, Bytes, Header, RichBlock, SignedTransaction, H160, U256};
use protocol::{
codec::deserialize_address,
types::{Block, Bytes, Header, RichBlock, SignedTransaction, H160, U256},
};

use crate::parse_file;

Expand Down Expand Up @@ -39,6 +42,7 @@ pub struct Params {}

#[derive(Clone, Debug, Deserialize)]
pub struct InitialAccount {
#[serde(deserialize_with = "deserialize_address")]
pub address: H160,
pub balance: U256,
}
Expand Down
23 changes: 19 additions & 4 deletions devtools/chain/specs/multi_nodes/chain-spec.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
#
# Data of the genesis block.
#

[genesis]
timestamp = 1680249207
extra_data = []
base_fee_per_gas = "0x539"
chain_id = 2022
# A JSON file which includes all transactions in the genesis block.
transactions = "genesis_transactions.json"

[[accounts]]
address = "0xa0ee7a142d267c1f36714e4a8f75612f20a79720"
balance = "04ee2d6d415b85acef8100000000"
#
# Accounts since the genesis block.
#
# WARNING: The following accounts are publicly known, DO NOT USE them in any production environment.
# Generated with the mnemonic "test test test test test test test test test test test junk".

[[accounts]]
address = "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266"
address = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"
balance = "04ee2d6d415b85acef8100000000"

[[accounts]]
Expand Down Expand Up @@ -45,4 +52,12 @@ balance = "04ee2d6d415b85acef8100000000"
address = "0x23618e81E3f5cdF7f54C3d65f7FBc0aBf5B21E8f"
balance = "04ee2d6d415b85acef8100000000"

[[accounts]]
address = "0xa0Ee7A142d267C1f36714E4a8F75612F20a79720"
balance = "04ee2d6d415b85acef8100000000"

#
# Parameters which make the chain to be unique.
#

[params]
23 changes: 19 additions & 4 deletions devtools/chain/specs/multi_nodes_short_epoch_len/chain-spec.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
#
# Data of the genesis block.
#

[genesis]
timestamp = 1679656015
extra_data = []
base_fee_per_gas = "0x539"
chain_id = 2022
# A JSON file which includes all transactions in the genesis block.
transactions = "genesis_transactions.json"

[[accounts]]
address = "0xa0ee7a142d267c1f36714e4a8f75612f20a79720"
balance = "04ee2d6d415b85acef8100000000"
#
# Accounts since the genesis block.
#
# WARNING: The following accounts are publicly known, DO NOT USE them in any production environment.
# Generated with the mnemonic "test test test test test test test test test test test junk".

[[accounts]]
address = "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266"
address = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"
balance = "04ee2d6d415b85acef8100000000"

[[accounts]]
Expand Down Expand Up @@ -45,4 +52,12 @@ balance = "04ee2d6d415b85acef8100000000"
address = "0x23618e81E3f5cdF7f54C3d65f7FBc0aBf5B21E8f"
balance = "04ee2d6d415b85acef8100000000"

[[accounts]]
address = "0xa0Ee7A142d267C1f36714E4a8F75612F20a79720"
balance = "04ee2d6d415b85acef8100000000"

#
# Parameters which make the chain to be unique.
#

[params]
13 changes: 8 additions & 5 deletions devtools/chain/specs/single_node/chain-spec.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#
# Data of the genesis block.
#

[genesis]
timestamp = 1679656015
extra_data = []
Expand All @@ -12,13 +13,11 @@ transactions = "genesis_transactions.json"
#
# Accounts since the genesis block.
#
# WARNING: The following accounts are publicly known, DO NOT USE them in any production environment.
# Generated with the mnemonic "test test test test test test test test test test test junk".

[[accounts]]
address = "0xa0ee7a142d267c1f36714e4a8f75612f20a79720"
balance = "04ee2d6d415b85acef8100000000"

[[accounts]]
address = "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266"
address = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"
balance = "04ee2d6d415b85acef8100000000"

[[accounts]]
Expand Down Expand Up @@ -53,6 +52,10 @@ balance = "04ee2d6d415b85acef8100000000"
address = "0x23618e81E3f5cdF7f54C3d65f7FBc0aBf5B21E8f"
balance = "04ee2d6d415b85acef8100000000"

[[accounts]]
address = "0xa0Ee7A142d267C1f36714E4a8F75612F20a79720"
balance = "04ee2d6d415b85acef8100000000"

#
# Parameters which make the chain to be unique.
#
Expand Down
1 change: 1 addition & 0 deletions protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ creep = "0.2"
derive_more = "0.99"
ethereum = { version = "0.14", features = ["with-codec", "with-serde"] }
ethereum-types = { version = "0.14", features = ["arbitrary", "codec", "rlp", "serialize", "std"] }
ethers-core = "2.0"
evm = { version = "0.37", features = ["with-serde"] }
faster-hex = "0.8"
lazy_static = "1.4"
Expand Down
16 changes: 14 additions & 2 deletions protocol/src/codec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ pub mod transaction;

pub use transaction::truncate_slice;

use ethers_core::utils::parse_checksummed;
use rlp::{Decodable, DecoderError, Encodable, Rlp, RlpStream};
use serde::Serializer;
use serde::{Deserialize as _, Deserializer, Serializer};

use crate::types::{Address, Bytes, DBBytes, Hex, TypesError, U256};
use crate::types::{Address, Bytes, DBBytes, Hex, TypesError, H160, U256};
use crate::ProtocolResult;

static CHARS: &[u8] = b"0123456789abcdef";
Expand Down Expand Up @@ -114,6 +115,17 @@ where
}
}

pub fn deserialize_address<'de, D>(deserializer: D) -> Result<H160, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
parse_checksummed(&s, None).map_err(|err| {
let msg = format!("failed to parse the mixed-case checksum address \"{s}\", since {err}.");
serde::de::Error::custom(msg)
})
}

fn to_hex_raw<'a>(v: &'a mut [u8], bytes: &[u8], skip_leading_zero: bool) -> &'a str {
debug_assert!(v.len() > 1 + bytes.len() * 2);

Expand Down

0 comments on commit c4f633f

Please sign in to comment.