-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgateway.rs
More file actions
64 lines (58 loc) · 2.02 KB
/
Copy pathgateway.rs
File metadata and controls
64 lines (58 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use crate::{
clients::utils::{MsgSign, MsgVerify},
cmds::gateway::GatewayInfo,
impl_sign, impl_verify, Result,
};
use anyhow::anyhow;
use helium_crypto::{Keypair, PublicKey};
use helium_proto::{
services::iot_config::{
gateway_client, GatewayInfoReqV1, GatewayInfoResV1, GatewayLocationReqV1,
GatewayLocationResV1,
},
Message,
};
use std::str::FromStr;
pub struct GatewayClient {
client: gateway_client::GatewayClient<helium_proto::services::Channel>,
server_pubkey: PublicKey,
}
impl GatewayClient {
pub async fn new(host: &str, server_pubkey: &str) -> Result<Self> {
Ok(Self {
client: gateway_client::GatewayClient::connect(host.to_owned()).await?,
server_pubkey: helium_crypto::PublicKey::from_str(server_pubkey)?,
})
}
pub async fn location(
&mut self,
hotspot: &PublicKey,
keypair: &Keypair,
) -> Result<GatewayLocationResV1> {
let mut request = GatewayLocationReqV1 {
gateway: hotspot.into(),
signer: keypair.public_key().into(),
signature: vec![],
};
request.signature = request.sign(keypair)?;
let response = self.client.location(request).await?.into_inner();
response.verify(&self.server_pubkey)?;
Ok(response)
}
pub async fn info(&mut self, hotspot: &PublicKey, keypair: &Keypair) -> Result<GatewayInfo> {
let mut request = GatewayInfoReqV1 {
address: hotspot.into(),
signer: keypair.public_key().into(),
signature: vec![],
};
request.signature = request.sign(keypair)?;
let response = self.client.info(request).await?.into_inner();
response.verify(&self.server_pubkey)?;
let info = response.info.ok_or_else(|| anyhow!("No hotspot found"))?;
info.try_into()
}
}
impl_sign!(GatewayLocationReqV1, signature);
impl_sign!(GatewayInfoReqV1, signature);
impl_verify!(GatewayLocationResV1, signature);
impl_verify!(GatewayInfoResV1, signature);