Skip to content

Commit 59ddab1

Browse files
authored
api: add Client (#87)
* api: add Client * add integration tests Signed-off-by: Sam Batschelet <[email protected]>
1 parent 96568f5 commit 59ddab1

File tree

12 files changed

+798
-213
lines changed

12 files changed

+798
-213
lines changed

spaces-cli/Cargo.toml

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ description = "spacesvm cli for issuing RPC commands"
88
license = "BSD-3-Clause"
99
homepage = "https://avax.network"
1010

11+
[[bin]]
12+
name = "spaces-cli"
13+
path = "src/bin/spaces-cli/main.rs"
14+
1115
[dependencies]
12-
avalanche-types = { version = "0.0.138", features = ["subnet"] }
1316
clap = { version = "4.0", features = ["derive"] }
1417
hex = "0.4.3"
1518
jsonrpc-core = "18.0.0"
16-
jsonrpc-core-client = { version = "18.0.0" }
17-
jsonrpc-client-transports = "18.0.0"
18-
jsonrpc-derive = "18.0"
1919
log = "0.4.17"
20-
spacesvm = { path = "../spacesvm" }
2120
serde = { version = "1.0.147", features = ["derive"] }
2221
serde_json = "1.0.87"
23-
tokio = { version = "1.21.2", features = ["full"] }
22+
spacesvm = { path = "../spacesvm" }
23+
tokio = { version = "1.22.0", features = ["full"] }

spaces-cli/src/bin/spaces-cli/main.rs

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
use std::error;
2+
3+
use clap::{Parser, Subcommand};
4+
use jsonrpc_core::futures;
5+
use spacesvm::{
6+
api::{
7+
client::{claim_tx, delete_tx, get_or_create_pk, set_tx, Client, Uri},
8+
DecodeTxArgs, IssueTxArgs, ResolveArgs,
9+
},
10+
chain::tx::{decoder, unsigned::TransactionData},
11+
};
12+
13+
#[derive(Subcommand, Debug)]
14+
enum Command {
15+
Claim {
16+
space: String,
17+
},
18+
Set {
19+
space: String,
20+
key: String,
21+
value: String,
22+
},
23+
Delete {
24+
space: String,
25+
key: String,
26+
},
27+
Get {
28+
space: String,
29+
key: String,
30+
},
31+
Ping {},
32+
}
33+
34+
#[derive(Parser)]
35+
#[command(version, about, long_about = None)]
36+
struct Cli {
37+
/// Endpoint for RPC calls.
38+
#[clap(long)]
39+
endpoint: String,
40+
41+
/// Private key file.
42+
#[clap(long, default_value = ".spacesvm-cli-pk")]
43+
private_key_file: String,
44+
45+
/// Which subcommand to call.
46+
#[command(subcommand)]
47+
command: Command,
48+
}
49+
50+
#[tokio::main]
51+
async fn main() -> Result<(), Box<dyn error::Error>> {
52+
let cli = Cli::parse();
53+
54+
let secret_key = get_or_create_pk(&cli.private_key_file)?;
55+
let uri = cli.endpoint.parse::<Uri>()?;
56+
let mut client = Client::new(uri);
57+
58+
if let Command::Get { space, key } = &cli.command {
59+
let resp = futures::executor::block_on(client.resolve(ResolveArgs {
60+
space: space.as_bytes().to_vec(),
61+
key: key.as_bytes().to_vec(),
62+
}))
63+
.map_err(|e| e.to_string())?;
64+
log::debug!("resolve response: {:?}", resp);
65+
66+
println!("{}", serde_json::to_string(&resp)?);
67+
return Ok(());
68+
}
69+
70+
if let Command::Ping {} = &cli.command {
71+
let resp = futures::executor::block_on(client.ping()).map_err(|e| e.to_string())?;
72+
73+
println!("{}", serde_json::to_string(&resp)?);
74+
return Ok(());
75+
}
76+
77+
// decode tx
78+
let tx_data = command_to_tx(cli.command)?;
79+
let resp = futures::executor::block_on(client.decode_tx(DecodeTxArgs { tx_data }))
80+
.map_err(|e| e.to_string())?;
81+
82+
let typed_data = &resp.typed_data;
83+
84+
// create signature
85+
let dh = decoder::hash_structured_data(typed_data)?;
86+
let sig = secret_key.sign_digest(&dh.as_bytes())?;
87+
88+
// issue tx
89+
let resp = futures::executor::block_on(client.issue_tx(IssueTxArgs {
90+
typed_data: resp.typed_data,
91+
signature: sig.to_bytes().to_vec(),
92+
}))
93+
.map_err(|e| e.to_string())?;
94+
println!("{}", serde_json::to_string(&resp)?);
95+
96+
Ok(())
97+
}
98+
99+
/// Takes a TX command and returns transaction data.
100+
fn command_to_tx(command: Command) -> std::io::Result<TransactionData> {
101+
match command {
102+
Command::Claim { space } => Ok(claim_tx(space)),
103+
Command::Set { space, key, value } => Ok(set_tx(space, key, value.as_bytes().to_vec())),
104+
Command::Delete { space, key } => Ok(delete_tx(space, key)),
105+
_ => Err(std::io::Error::new(
106+
std::io::ErrorKind::Other,
107+
"not a supported tx",
108+
)),
109+
}
110+
}

spaces-cli/src/main.rs

-194
This file was deleted.

spacesvm/Cargo.toml

+7-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ name = "spacesvm"
1515
path = "src/bin/spaces/main.rs"
1616

1717
[dependencies]
18-
avalanche-types = { version = "0.0.138", features = ["subnet"] }
18+
avalanche-types = { version = "0.0.140", features = ["subnet"] }
1919
byteorder = "1.4.3"
2020
chrono = "0.4.22"
2121
crossbeam-channel = "0.5.6"
@@ -27,6 +27,7 @@ eip-712 = "0.1.0"
2727
env_logger = "0.9.3"
2828
hex = "0.4.3"
2929
http = "0.2.8"
30+
hyper = "0.14.23"
3031
jsonrpc-core = "18.0.0"
3132
jsonrpc-core-client = { version = "18.0.0" }
3233
jsonrpc-derive = "18.0"
@@ -47,4 +48,8 @@ typetag = "0.2"
4748

4849
[dev-dependencies]
4950
jsonrpc-tcp-server = "18.0.0"
50-
futures-test = "0.3.24"
51+
futures-test = "0.3.24"
52+
53+
[[test]]
54+
name = "integration"
55+
path = "tests/integration_tests.rs"

0 commit comments

Comments
 (0)