ec_client/
lib.rs

1extern crate crypto;
2extern crate reqwest;
3extern crate serde_cbor;
4
5use serde::{Deserialize, Serialize};
6pub use serde_cbor::Value;
7use serde_cbor::{from_slice, to_vec};
8
9#[derive(Deserialize, Serialize, Debug)]
10pub struct Transaction {
11    #[serde(with = "serde_bytes")]
12    pub sender: Vec<u8>,
13    #[serde(with = "serde_bytes")]
14    pub contract_address: Vec<u8>,
15    pub contract_name: String,
16    pub nonce: u64,
17    pub function: String,
18    pub arguments: Vec<Value>,
19}
20
21#[derive(Deserialize, Serialize, Debug)]
22pub struct SignedTransaction {
23    #[serde(with = "serde_bytes")]
24    pub sender: Vec<u8>,
25    #[serde(with = "serde_bytes")]
26    pub contract_address: Vec<u8>,
27    pub contract_name: String,
28    pub nonce: u64,
29    pub function: String,
30    pub arguments: Vec<Value>,
31    #[serde(with = "serde_bytes")]
32    pub signature: Vec<u8>,
33}
34
35pub fn create_contract(
36    name: &str,
37    code: &[u8],
38    constructor_arguments: Vec<Value>,
39    private_key: &[u8],
40) {
41    post(
42        Transaction {
43            sender: private_key[32..].to_vec(),
44            contract_address: [0; 32].to_vec(),
45            contract_name: "system".to_string(),
46            nonce: 0,
47            function: "create_contract".to_string(),
48            arguments: vec![
49                name.to_string().into(),
50                Value::Bytes(code.to_vec()),
51                Value::Array(constructor_arguments),
52            ],
53        },
54        private_key,
55    );
56}
57
58pub fn post(transaction: Transaction, private_key: &[u8]) {
59    let transaction_bytes = to_vec(&transaction).unwrap();
60    let signature = crypto::ed25519::signature(&transaction_bytes, private_key).to_vec();
61    let signed_transaction = SignedTransaction {
62        sender: transaction.sender,
63        contract_address: transaction.contract_address,
64        contract_name: transaction.contract_name,
65        nonce: transaction.nonce,
66        function: transaction.function,
67        arguments: transaction.arguments,
68        signature: signature,
69    };
70    let signed_transaction_bytes = to_vec(&signed_transaction).unwrap();
71    let client = reqwest::Client::new();
72    let resp = client
73        .post("http://davenport.ellipticoin.org:4460/transactions")
74        .header("Content-Type", "application/cbor")
75        .body(signed_transaction_bytes)
76        .send();
77}