Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

u64 chain_id #15

Merged
merged 3 commits into from
Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ethereum-tx-sign"
version = "2.0.0"
version = "2.1.0"
description = "Allows you to model a raw ethereum transaction and sign it with your private key"
repository = "https://github.com/synlestidae/ethereum-tx-sign"
license = "MIT"
Expand All @@ -16,3 +16,4 @@ tiny-keccak="1.4.2"
secp256k1 = "0.12"
rlp = "0.4.2"
ethereum-types = "0.6.0"
num-traits = "0.2.10"
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ extern crate rlp;
extern crate secp256k1;
extern crate serde_json;
extern crate tiny_keccak;
extern crate num_traits;

mod raw_transaction;

Expand Down
32 changes: 17 additions & 15 deletions src/raw_transaction.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use ethereum_types::{H160, H256, U256};
use rlp::RlpStream;
use num_traits::int;
use secp256k1::key::SecretKey;
use secp256k1::Message;
use secp256k1::Secp256k1;
Expand All @@ -24,10 +25,12 @@ pub struct RawTransaction {
}

impl RawTransaction {
/// Signs and returns the RLP-encoded transaction
pub fn sign(&self, private_key: &H256, chain_id: &u8) -> Vec<u8> {
let hash = self.hash(*chain_id);
let sig = ecdsa_sign(&hash, &private_key.0, &chain_id);

/// Signs and returns the RLP-encoded transaction
pub fn sign<T: int::PrimInt>(&self, private_key: &H256, chain_id: &T) -> Vec<u8> {
let chain_id_u64: u64 = chain_id.to_u64().unwrap();
let hash = self.hash(chain_id_u64);
let sig = ecdsa_sign(&hash, &private_key.0, &chain_id_u64);
let mut r_n = sig.r;
let mut s_n = sig.s;
while r_n[0] == 0 {
Expand All @@ -42,18 +45,18 @@ impl RawTransaction {
tx.append(&sig.v);
tx.append(&r_n);
tx.append(&s_n);
tx.complete_unbounded_list();
tx.finalize_unbounded_list();
tx.out()
}

fn hash(&self, chain_id: u8) -> Vec<u8> {
fn hash(&self, chain_id: u64) -> Vec<u8> {
let mut hash = RlpStream::new();
hash.begin_unbounded_list();
self.encode(&mut hash);
hash.append(&mut vec![chain_id]);
hash.append(&mut chain_id.clone());
hash.append(&mut U256::zero());
hash.append(&mut U256::zero());
hash.complete_unbounded_list();
hash.finalize_unbounded_list();
keccak256_hash(&hash.out())
}

Expand All @@ -72,24 +75,24 @@ impl RawTransaction {
}

fn keccak256_hash(bytes: &[u8]) -> Vec<u8> {
keccak256(bytes).into_iter().cloned().collect()
keccak256(bytes).iter().cloned().collect()
}

fn ecdsa_sign(hash: &[u8], private_key: &[u8], chain_id: &u8) -> EcdsaSig {
fn ecdsa_sign(hash: &[u8], private_key: &[u8], chain_id: &u64) -> EcdsaSig {
let s = Secp256k1::signing_only();
let msg = Message::from_slice(hash).unwrap();
let key = SecretKey::from_slice(private_key).unwrap();
let (v, sig_bytes) = s.sign_recoverable(&msg, &key).serialize_compact();

EcdsaSig {
v: vec![v.to_i32() as u8 + chain_id * 2 + 35],
v: v.to_i32() as u64 + chain_id * 2 + 35,
r: sig_bytes[0..32].to_vec(),
s: sig_bytes[32..64].to_vec(),
}
}

pub struct EcdsaSig {
v: Vec<u8>,
v: u64,
r: Vec<u8>,
s: Vec<u8>,
}
Expand All @@ -114,7 +117,7 @@ mod test {
let mut f_string = String::new();
file.read_to_string(&mut f_string).unwrap();
let txs: Vec<(RawTransaction, Signing)> = serde_json::from_str(&f_string).unwrap();
let chain_id = 0;
let chain_id = 1 as u64;
for (tx, signed) in txs.into_iter() {
assert_eq!(signed.signed, tx.sign(&signed.private_key, &chain_id));
}
Expand All @@ -127,7 +130,6 @@ mod test {
use serde_json;
use std::fs::File;
use std::io::Read;

#[derive(Deserialize)]
struct Signing {
signed: Vec<u8>,
Expand All @@ -138,7 +140,7 @@ mod test {
let mut f_string = String::new();
file.read_to_string(&mut f_string).unwrap();
let txs: Vec<(RawTransaction, Signing)> = serde_json::from_str(&f_string).unwrap();
let chain_id = 3;
let chain_id = 3 as i32;
for (tx, signed) in txs.into_iter() {
assert_eq!(signed.signed, tx.sign(&signed.private_key, &chain_id));
}
Expand Down
6 changes: 3 additions & 3 deletions test/test_txs.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
{
"private_key": "0x4646464646464646464646464646464646464646464646464646464646464646",
"signed": [248, 108, 9, 133, 4, 168, 23, 200, 0, 130, 82, 8, 148, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 136, 13, 224, 182, 179, 167, 100, 0, 0, 128, 35, 160, 252, 75, 179, 203, 183, 59, 165, 78, 216, 79, 250, 46, 27, 53, 141, 53, 71, 132, 114, 169, 103, 15, 101, 139, 140, 214, 133, 0, 170, 80, 209, 236, 160, 81, 184, 225, 44, 42, 86, 48, 109, 196, 39, 204, 47, 83, 131, 165, 30, 237, 232, 212, 226, 240, 55, 205, 178, 224, 31, 146, 171, 253, 114, 30, 153]
"signed": [248, 108, 9, 133, 4, 168, 23, 200, 0, 130, 82, 8, 148, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 136, 13, 224, 182, 179, 167, 100, 0, 0, 128, 37, 160, 40, 239, 97, 52, 11, 217, 57, 188, 33, 149, 254, 83, 117, 103, 134, 96, 3, 225, 161, 93, 60, 113, 255, 99, 225, 89, 6, 32, 170, 99, 98, 118, 160, 103, 203, 233, 216, 153, 127, 118, 26, 236, 183, 3, 48, 75, 56, 0, 204, 245, 85, 201, 243, 220, 100, 33, 75, 41, 127, 177, 150, 106, 59, 109, 131]
}
],
[
Expand All @@ -24,7 +24,7 @@
},
{
"private_key": "0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318",
"signed": [248, 106, 128, 134, 213, 86, 152, 55, 36, 49, 131, 30, 132, 128, 148, 240, 16, 159, 200, 223, 40, 48, 39, 182, 40, 92, 200, 137, 245, 170, 98, 78, 172, 31, 85, 132, 59, 154, 202, 0, 128, 36, 160, 31, 49, 247, 117, 12, 212, 195, 73, 65, 180, 66, 235, 204, 17, 5, 190, 206, 237, 4, 251, 252, 220, 98, 140, 158, 123, 88, 213, 236, 183, 123, 207, 160, 76, 250, 11, 158, 106, 231, 225, 132, 153, 116, 74, 72, 201, 5, 6, 41, 120, 123, 163, 168, 103, 139, 244, 14, 54, 58, 95, 169, 4, 216, 60, 223]
"signed": [248, 106, 128, 134, 213, 86, 152, 55, 36, 49, 131, 30, 132, 128, 148, 240, 16, 159, 200, 223, 40, 48, 39, 182, 40, 92, 200, 137, 245, 170, 98, 78, 172, 31, 85, 132, 59, 154, 202, 0, 128, 37, 160, 9, 235, 182, 202, 5, 122, 5, 53, 214, 24, 100, 98, 188, 11, 70, 91, 86, 28, 148, 162, 149, 189, 176, 98, 31, 193, 146, 8, 171, 20, 154, 156, 160, 68, 15, 253, 119, 92, 233, 26, 131, 58, 180, 16, 119, 114, 4, 213, 52, 26, 111, 159, 169, 18, 22, 166, 243, 238, 44, 5, 31, 234, 106, 4, 40]
}
],
[
Expand All @@ -38,7 +38,7 @@
},
{
"private_key": "0xe331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109",
"signed": [248, 117, 128, 134, 9, 24, 78, 114, 160, 0, 130, 39, 16, 128, 128, 164, 127, 116, 101, 115, 116, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, 0, 87, 35, 160, 99, 229, 207, 38, 9, 40, 235, 251, 58, 38, 122, 27, 101, 74, 188, 18, 248, 108, 128, 212, 42, 104, 80, 218, 83, 23, 60, 148, 97, 133, 80, 171, 160, 7, 89, 31, 205, 153, 45, 102, 65, 227, 88, 38, 123, 123, 62, 50, 233, 78, 106, 125, 110, 44, 34, 219, 3, 240, 237, 125, 45, 220, 105, 77, 234]
"signed": [248, 117, 128, 134, 9, 24, 78, 114, 160, 0, 130, 39, 16, 128, 128, 164, 127, 116, 101, 115, 116, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, 0, 87, 38, 160, 122, 155, 12, 58, 133, 108, 183, 145, 181, 210, 141, 44, 236, 17, 96, 40, 55, 87, 204, 250, 142, 83, 122, 168, 250, 5, 113, 172, 203, 5, 12, 181, 160, 9, 100, 95, 141, 167, 178, 53, 101, 115, 131, 83, 172, 199, 242, 208, 96, 246, 121, 25, 18, 211, 89, 60, 94, 165, 169, 71, 3, 176, 157, 167, 50]
}
]
]