#signer #alloy #ethereum #abstraction #linera #key #sign

linera-alloy-signer

Ethereum 签名抽象

1 个不稳定版本

0.1.0 2024 年 5 月 31 日

#14#linera

Download history 91/week @ 2024-05-25 203/week @ 2024-06-01 79/week @ 2024-06-08 97/week @ 2024-06-15 29/week @ 2024-06-22 75/week @ 2024-06-29 19/week @ 2024-07-06 85/week @ 2024-07-13 82/week @ 2024-07-20 108/week @ 2024-07-27 94/week @ 2024-08-03 100/week @ 2024-08-10 116/week @ 2024-08-17

445 次每月下载
10 个 crate (9 直接) 中使用

MIT/Apache

23KB
330

linera-alloy-signer

Ethereum 签名抽象。

您可以通过实现 Signer 特性来扩展功能,以支持其他签名者,如硬件安全模块、KMS 等。更多详情请参见 其文档

Alloy 中的签名者实现

示例

签名一个以太坊前缀消息 (EIP-712)

use linera_alloy_signer::{Signer, SignerSync};

// Instantiate a signer.
let signer = linera_alloy_signer_wallet::LocalWallet::random();

// Sign a message.
let message = "Some data";
let signature = signer.sign_message_sync(message.as_bytes())?;

// Recover the signer from the message.
let recovered = signature.recover_address_from_msg(message)?;
assert_eq!(recovered, signer.address());
# Ok::<_, Box<dyn std::error::Error>>(())

签名一笔交易

use linera_alloy_consensus::TxLegacy;
use linera_alloy_primitives::{U256, address, bytes};
use linera_alloy_signer::{Signer, SignerSync};
use linera_alloy_network::{TxSignerSync};

// Instantiate a signer.
let signer = "dcf2cbdd171a21c480aa7f53d77f31bb102282b3ff099c78e3118b37348c72f7"
    .parse::<linera_alloy_signer_wallet::LocalWallet>()?;

// Create a transaction.
let mut tx = TxLegacy {
    to: address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into(),
    value: U256::from(1_000_000_000),
    gas_limit: 2_000_000,
    nonce: 0,
    gas_price: 21_000_000_000,
    input: bytes!(),
    chain_id: Some(1),
};

// Sign it.
let signature = signer.sign_transaction_sync(&mut tx)?;
# Ok::<_, Box<dyn std::error::Error>>(())

依赖关系

~16MB
~354K SLoC