added signature with keys from disk
This commit is contained in:
		
							parent
							
								
									a20d46bb49
								
							
						
					
					
						commit
						7c90c2ceda
					
				| @ -13,7 +13,7 @@ docker build -t hacker-challenge:latest . | ||||
| 
 | ||||
| docker ps -a | grep 'hacker-challenge' | awk '{ print $NF }' | xargs docker rm -f || true | ||||
| 
 | ||||
| for i in {0..10} | ||||
| for i in {0..50} | ||||
| do | ||||
|   docker run -d --name "hacker-challenge_$i" \ | ||||
|     --env INIT_NODES="172.17.0.2 172.17.0.3 172.17.0.4" \ | ||||
|  | ||||
| @ -45,6 +45,12 @@ impl From<std::array::TryFromSliceError> for SigningError { | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| impl From<std::io::Error> for SigningError { | ||||
|     fn from(_: std::io::Error) -> Self { | ||||
|         Self::CorruptedKey | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| type IP = String; | ||||
| 
 | ||||
| impl std::fmt::Display for SigningError { | ||||
| @ -107,7 +113,7 @@ impl Store { | ||||
|     } | ||||
| 
 | ||||
|     pub async fn tabled_disk_list(&self, page: u64) -> String { | ||||
|         let mut offset = page.wrapping_mul(10); | ||||
|         let mut offset = page.wrapping_mul(20); | ||||
|         #[derive(Tabled)] | ||||
|         struct OutputRow { | ||||
|             id: u64, | ||||
| @ -147,6 +153,20 @@ impl Store { | ||||
|         Table::new(output).to_string() | ||||
|     } | ||||
| 
 | ||||
|     pub async fn disk_sign_message_with_key( | ||||
|         &self, | ||||
|         message: &str, | ||||
|         key_id: u64, | ||||
|     ) -> Result<String, SigningError> { | ||||
|         let crate::persistence::Node{keypair, ..} = | ||||
|             self.persistence.get_node_by_id(key_id).await?; | ||||
| 
 | ||||
|         // let signature = format!("{:?}", signing_key.sign(message.as_bytes()));
 | ||||
|         let signature = hex::encode(keypair.sign(message.as_bytes()).to_bytes()); | ||||
| 
 | ||||
|         Ok(signature) | ||||
|     } | ||||
| 
 | ||||
|     pub async fn sign_message_with_key( | ||||
|         &self, | ||||
|         message: &str, | ||||
|  | ||||
| @ -23,7 +23,7 @@ To access keys that are saved on disk, navigate to /disk. Disk entries are pagin | ||||
| You can navigate to a specific page by using get params. Example: https://{ip}/disk?page={number}.
 | ||||
| To sign a random message using a key from disk, use /disk/sign and send the key id as a get param: | ||||
| curl -G \ | ||||
|     --data-urlencode "pubkey_id=1337" \ | ||||
|     --data-urlencode "key=1337" \ | ||||
|     --data-urlencode "something=YOUR_MESSAGE_HERE" \ | ||||
|     'IP_OF_THE_NODE:31372/disk/sign' | ||||
| 
 | ||||
| @ -34,6 +34,7 @@ Good luck! | ||||
| "#;
 | ||||
| 
 | ||||
| enum HTTPError { | ||||
|     NoKeyID, | ||||
|     NoPubkey, | ||||
|     NoMessage, | ||||
|     Store(SigningError), | ||||
| @ -44,6 +45,7 @@ impl Writer for HTTPError { | ||||
|     async fn write(self, _req: &mut Request, _depot: &mut Depot, res: &mut Response) { | ||||
|         res.status_code(StatusCode::BAD_REQUEST); | ||||
|         match self { | ||||
|             HTTPError::NoKeyID => res.render("key ID must be specified as a get param"), | ||||
|             HTTPError::NoPubkey => res.render("pubkey must be specified as GET param"), | ||||
|             HTTPError::NoMessage => res.render("something must be specified as GET param"), | ||||
|             HTTPError::Store(e) => res.render(format!("{e}")), | ||||
| @ -92,6 +94,25 @@ async fn disk_list(req: &mut Request, depot: &mut Depot) -> Result<String, HTTPE | ||||
|     Ok(ds.tabled_disk_list(page).await) | ||||
| } | ||||
| 
 | ||||
| #[handler] | ||||
| async fn disk_sign(req: &mut Request, depot: &mut Depot) -> Result<String, HTTPError> { | ||||
|     let ds = depot.obtain::<Arc<Store>>().unwrap(); | ||||
|     let key = match req.query::<u64>("key") { | ||||
|         Some(k) => k, | ||||
|         None => return Err(HTTPError::NoKeyID), | ||||
|     }; | ||||
| 
 | ||||
|     let something = match req.query::<String>("something") { | ||||
|         Some(k) => k, | ||||
|         None => return Err(HTTPError::NoMessage), | ||||
|     }; | ||||
| 
 | ||||
|     match ds.disk_sign_message_with_key(&something, key).await { | ||||
|         Ok(s) => Ok(s), | ||||
|         Err(e) => Err(HTTPError::Store(e)), | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| pub async fn init(ds: Arc<Store>) { | ||||
|     let acceptor = TcpListener::new("0.0.0.0:31372").bind().await; | ||||
|     let router = Router::new() | ||||
| @ -102,7 +123,11 @@ pub async fn init(ds: Arc<Store>) { | ||||
|                 .get(memory_list) | ||||
|                 .push(Router::with_path("sign").get(memory_sign)), | ||||
|         ) | ||||
|         .push(Router::with_path("disk").get(disk_list)); | ||||
|         .push( | ||||
|             Router::with_path("disk") | ||||
|                 .get(disk_list) | ||||
|                 .push(Router::with_path("sign").get(disk_sign)), | ||||
|         ); | ||||
|     println!("{:?}", router); | ||||
|     Server::new(acceptor).serve(router).await; | ||||
| } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user