Skip to content

Commit

Permalink
Support personal_sign rpc (#561)
Browse files Browse the repository at this point in the history
  • Loading branch information
eiz committed Feb 2, 2022
1 parent 033ac37 commit e81265b
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/api/personal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::{
api::Namespace,
helpers::{self, CallFuture},
types::{Address, RawTransaction, TransactionRequest, H256},
types::{Address, Bytes, RawTransaction, TransactionRequest, H256, H520},
Transport,
};

Expand Down Expand Up @@ -62,6 +62,17 @@ impl<T: Transport> Personal<T> {
)
}

/// Signs an Ethereum specific message with `sign(keccak256("\x19Ethereum Signed Message: " + len(data) + data)))`
///
/// The account does not need to be unlocked to make this call, and will not be left unlocked after.
/// Returns encoded signature.
pub fn sign(&self, data: Bytes, account: Address, password: &str) -> CallFuture<H520, T::Out> {
let data = helpers::serialize(&data);
let address = helpers::serialize(&account);
let password = helpers::serialize(&password);
CallFuture::new(self.transport.execute("personal_sign", vec![data, address, password]))
}

/// Signs a transaction without dispatching it to the network.
/// The account does not need to be unlocked to make this call, and will not be left unlocked after.
/// Returns a signed transaction in raw bytes along with it's details.
Expand Down Expand Up @@ -98,7 +109,7 @@ mod tests {
use crate::{
api::Namespace,
rpc::Value,
types::{Address, RawTransaction, TransactionRequest},
types::{Address, Bytes, RawTransaction, TransactionRequest, H160, H520},
};
use hex_literal::hex;

Expand Down Expand Up @@ -176,4 +187,11 @@ mod tests {
"personal_importRawKey", vec![r#""0000000000000000000000000000000000000000000000000000000000000000""#, r#""hunter2""#];
Value::String("0x0000000000000000000000000000000000000123".into()) => Address::from_low_u64_be(0x123)
}

rpc_test! {
Personal:sign, Bytes(hex!("7f0d39b8347598e20466233ce2fb3e824f0f93dfbf233125d3ab09b172c62591ec24dc84049242e364895c3abdbbd843d4a0a188").to_vec()), H160(hex!("7f0d39b8347598e20466233ce2fb3e824f0f93df")), "hunter2"
=>
"personal_sign", vec![r#""0x7f0d39b8347598e20466233ce2fb3e824f0f93dfbf233125d3ab09b172c62591ec24dc84049242e364895c3abdbbd843d4a0a188""#, r#""0x7f0d39b8347598e20466233ce2fb3e824f0f93df""#, r#""hunter2""#];
Value::String("0xdac1cba443d72e2088ed0cd2e6608ce696eb4728caf119dcfeea752f57a1163274de0b25007aa70201d0d80190071b26be2287b4a473767e5f7bc443c080b4fc1c".into()) => H520(hex!("dac1cba443d72e2088ed0cd2e6608ce696eb4728caf119dcfeea752f57a1163274de0b25007aa70201d0d80190071b26be2287b4a473767e5f7bc443c080b4fc1c"))
}
}

0 comments on commit e81265b

Please sign in to comment.