#primitive #ethereum #byte #abi #type #address #mapping

ethers_primitives

Ethereum 的原始类型,包括 Rlp/ABI 编码/解码支持

1 个不稳定版本

0.2.2 2023年2月24日

#119 in #abi

Download history 149/week @ 2024-04-07 247/week @ 2024-04-14 144/week @ 2024-04-21 90/week @ 2024-04-28 211/week @ 2024-05-05 180/week @ 2024-05-12 133/week @ 2024-05-19 83/week @ 2024-05-26 212/week @ 2024-06-02 56/week @ 2024-06-09 85/week @ 2024-06-16 103/week @ 2024-06-23 34/week @ 2024-06-30 48/week @ 2024-07-07 66/week @ 2024-07-14 51/week @ 2024-07-21

每月下载量 202
用于 8 crates

MIT 许可证

56KB
1.5K SLoC

Ethereum 核心类型

该包定义了 Rust 对 enthereum core types 的映射。

abi rust 状态
uint<M>/int<M> crate::IntM 支持
bytes<M> crate::BytesM 支持
bytes crate::Bytes 支持
address crate::Bytes 支持
bool bool 支持
<类型>[M] [T;N]/&[T;N] 支持
<类型>[] Vec<T>/&[T] 支持
fixed<M>x<N> 正在开发
(T1,T2,...,Tn) (T1,T2,...,Tn)/结构体 A(T1,T2,...,Tn)/结构体 A { t1:T1,t2:T2,...,t3:Tn} 支持

使用 serde

该包使用 serde 框架来处理合约 ABI 序列化和反序列化。因此,它很容易定义支持 Ethereum ABI 的结构。


use ethers_types_rs::*;

#[derive(Debug,Serialize,Deserilize)]
struct Transfer {
    to: Address,
    amount: U256,
}

EIP712 支持

再次感谢 serde 的良好框架设计。该包定义了一系列 serializers 以支持 eip712 协议。

let domain = json!({
    "name": "Ether Mail",
    "version": "1",
    "chainId": 1,
    "verifyingContract": "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
});

let domain: EIP712Domain = serde_json::from_value(domain).unwrap();
assert_eq!(
    eip712_hash_struct(&domain).unwrap().to_eth_hex(),
    "0xf2cee375fa42b42143804025fc449deafd50cc031ca257e0b194a650a912090f"
);

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct Person {
    pub name: String,
    pub wallet: Address,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct Mail {
    pub from: Person,
    pub to: Person,
    pub contents: String,
}

let json = json!({
  "from": {
    "name": "Cow",
    "wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"
  },
  "to": {
    "name": "Bob",
    "wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"
  },
  "contents": "Hello, Bob!"
});

let mail: Mail = serde_json::from_value(json).expect("parse domain");

assert_eq!(
    "Mail(Person from,Person to,string contents)Person(string name,address wallet)",
    eip712_encode_type(&mail).expect("generate e712 types")
);

assert_eq!(
    eip712_hash_struct(&mail.from).unwrap().to_eth_hex(),
    "0xfc71e5fa27ff56c350aa531bc129ebdf613b772b6604664f5d8dbe21b85eb0c8"
);

assert_eq!(
    eip712_hash_struct(&mail.to).unwrap().to_eth_hex(),
    "0xcd54f074a4af31b4411ff6a60c9719dbd559c221c8ac3492d9d872b041d703d1"
);

assert_eq!(
    eip712_hash_struct(&mail).unwrap().to_eth_hex(),
    "0xc52c0ee5d84264471806290a3f2c4cecfc5490626bf912d01f240d7a274b371e"
);

assert_eq!(
    eip712_encode(&domain, &mail).unwrap().to_eth_hex(),
    "0x1901f2cee375fa42b42143804025fc449deafd50cc031ca257e0b194a650a912090fc52c0ee5d84264471806290a3f2c4cecfc5490626bf912d01f240d7a274b371e"
);

let expect_request: TypedData<Mail> =
    serde_json::from_str(include_str!("./eip712.json")).unwrap();

 assert_eq!(eip712_into_request(domain, mail).unwrap(), expect_request);

依赖项

~4.5–7MB
~117K SLoC