Fix up the transaction JSON serialisation for RPC.

This commit is contained in:
Gav Wood 2016-11-27 14:11:37 +01:00
parent a7037f8e5b
commit 3b6d886860
No known key found for this signature in database
GPG Key ID: C49C1ACA1CC9B252
2 changed files with 19 additions and 6 deletions

View File

@ -304,6 +304,9 @@ impl SignedTransaction {
/// 0 if `v` would have been 27 under "Electrum" notation, 1 if 28 or 4 if invalid. /// 0 if `v` would have been 27 under "Electrum" notation, 1 if 28 or 4 if invalid.
pub fn standard_v(&self) -> u8 { match self.v { v if v == 27 || v == 28 || v > 36 => (v - 1) % 2, _ => 4 } } pub fn standard_v(&self) -> u8 { match self.v { v if v == 27 || v == 28 || v > 36 => (v - 1) % 2, _ => 4 } }
/// The `v` value that appears in the RLP.
pub fn original_v(&self) -> u8 { self.v }
/// The network ID, or `None` if this is a global transaction. /// The network ID, or `None` if this is a global transaction.
pub fn network_id(&self) -> Option<u8> { pub fn network_id(&self) -> Option<u8> {
match self.v { match self.v {

View File

@ -57,12 +57,18 @@ pub struct Transaction {
/// Public key of the signer. /// Public key of the signer.
#[serde(rename="publicKey")] #[serde(rename="publicKey")]
pub public_key: Option<H512>, pub public_key: Option<H512>,
/// The V field of the signature. /// The network id of the transaction, if any.
#[serde(rename="networkId")]
pub network_id: Option<u8>,
/// The standardised V field of the signature (0 or 1).
#[serde(rename="standardV")]
pub standard_v: u8,
/// The standardised V field of the signature.
pub v: u8, pub v: u8,
/// The R field of the signature. /// The R field of the signature.
pub r: H256, pub r: U256,
/// The S field of the signature. /// The S field of the signature.
pub s: H256, pub s: U256,
} }
/// Local Transaction Status /// Local Transaction Status
@ -176,7 +182,9 @@ impl From<LocalizedTransaction> for Transaction {
}, },
raw: ::rlp::encode(&t.signed).to_vec().into(), raw: ::rlp::encode(&t.signed).to_vec().into(),
public_key: t.public_key().ok().map(Into::into), public_key: t.public_key().ok().map(Into::into),
v: signature.v(), network_id: t.network_id(),
standard_v: t.standard_v(),
v: t.original_v(),
r: signature.r().into(), r: signature.r().into(),
s: signature.s().into(), s: signature.s().into(),
} }
@ -207,7 +215,9 @@ impl From<SignedTransaction> for Transaction {
}, },
raw: ::rlp::encode(&t).to_vec().into(), raw: ::rlp::encode(&t).to_vec().into(),
public_key: t.public_key().ok().map(Into::into), public_key: t.public_key().ok().map(Into::into),
v: signature.v(), network_id: t.network_id(),
standard_v: t.standard_v(),
v: t.original_v(),
r: signature.r().into(), r: signature.r().into(),
s: signature.s().into(), s: signature.s().into(),
} }
@ -238,7 +248,7 @@ mod tests {
fn test_transaction_serialize() { fn test_transaction_serialize() {
let t = Transaction::default(); let t = Transaction::default();
let serialized = serde_json::to_string(&t).unwrap(); let serialized = serde_json::to_string(&t).unwrap();
assert_eq!(serialized, r#"{"hash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0","blockHash":null,"blockNumber":null,"transactionIndex":null,"from":"0x0000000000000000000000000000000000000000","to":null,"value":"0x0","gasPrice":"0x0","gas":"0x0","input":"0x","creates":null,"raw":"0x","publicKey":null,"v":0,"r":"0x0000000000000000000000000000000000000000000000000000000000000000","s":"0x0000000000000000000000000000000000000000000000000000000000000000"}"#); assert_eq!(serialized, r#"{"hash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0","blockHash":null,"blockNumber":null,"transactionIndex":null,"from":"0x0000000000000000000000000000000000000000","to":null,"value":"0x0","gasPrice":"0x0","gas":"0x0","input":"0x","creates":null,"raw":"0x","publicKey":null,"v":0,"r":"0x","s":"0x"}"#);
} }
#[test] #[test]