use serde::{Serialize, Deserialize};
use std::io::prelude::*;
use hex_literal::hex;
use k256::{ecdsa::{SigningKey, Signature, signature::Signer, signature::Verifier, VerifyingKey}};
use chrono::{Utc, Local, DateTime, Date};
#[derive(Serialize, Deserialize, Debug)]
struct UnsignedTransaction {
time: String,
sender: String,
receiver: String,
amount: i32,
}
#[derive(Serialize, Deserialize, Debug)]
struct SignedTransaction {
time: String,
sender: String,
receiver: String,
amount: i32,
signature: String,
}
fn hex(bytes: &[u8]) -> String {
bytes.iter().fold("".to_owned(), |s, b| format!("{}{:x}", s, b))
}
#[async_std::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
let private_key: SigningKey = SigningKey::from_bytes(&hex!(
"DCDFF4B7CA287CC7BD30ECAEF0622265DB4E14054E12954225457C3A6B84F135"
).into()).unwrap();
let public_key: &VerifyingKey = private_key.verifying_key();
let public_key_str = hex(&public_key.to_encoded_point(false).to_bytes());
let public_key_b_str = "4bac6cb0f4ad6397752c3d73b88c5c86e3d88ac695118494a1732e2abd16c76acad3d6586c37c8db7e69c2f812f99275198936957d72c38d71981991124";
let utc_datetime: DateTime<Utc> = Utc::now();
let ut1 = UnsignedTransaction {time: utc_datetime.to_string(), sender: public_key_str.to_string(), receiver: public_key_b_str.to_string(), amount: 10};
println!("{:?}", ut1);
let serialized: String = serde_json::to_string(&ut1).unwrap();
let sig1: Signature = private_key.sign(serialized.as_bytes());
let signed_ut1 = SignedTransaction {time: utc_datetime.to_string(), sender: public_key_str.to_string(), receiver: public_key_b_str.to_string(), amount: 10, signature: sig1.to_string()};
let uri = "https://httpbin.org/post";
let mut res = surf::post(uri).body_json(&signed_ut1)?.await?;
let body = res.body_string().await?;
println!("{}", body);
Ok(())
}
“files”: {},
“form”: {},
“headers”: {
“Accept”: “*/*”,
“Accept-Encoding”: “deflate, gzip”,
“Content-Length”: “471”,
“Content-Type”: “application/json”,
“Host”: “httpbin.org”,
“User-Agent”: “curl/8.11.0-DEV isahc/0.7.6”,
“X-Amzn-Trace-Id”: “Root=1-676c8cf1-39979cc169871725084b307e”
},
“json”: {
“amount”: 10,
“receiver”: “4bac6cb0f4ad6397752c3d73b88c5c86e3d88ac695118494a1732e2abd16c76acad3d6586c37c8db7e69c2f812f99275198936957d72c38d71981991124”,
“sender”: “4bac6cb0f4ad6397752c3d73b88c5c86e3d88ac695118494a1732e2abd16c76acad3d6586c37c8db7e69c2f812f99275198936957d72c38d71981991123”,
“signature”: “8000E340A55A517D0F27F3A63FBE39ED576BA491DFAC89B44654AB147EC66B206B054BAAF53E318EB2721DC892B4736630F400547989AE9F7C069034ECB4DF98”,
“time”: “2024-12-25 22:53:36.824066840 UTC”
},
“origin”: “hoge”,
“url”: “https://httpbin.org/post”
}
なるほど、送信側は相手のURL(IP)がわかっていれば、POSTするだけなので問題なく出来ますね。