Skip to content

Commit 35003c5

Browse files
authored
Add a command to generate a secret key (#8)
1 parent 1126d1c commit 35003c5

File tree

6 files changed

+218
-20
lines changed

6 files changed

+218
-20
lines changed

Cargo.lock

Lines changed: 117 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ borsh = "0.9.3"
99
clap = { version = "4.5.39", features = ["derive", "env"] }
1010
hex = { version = "0.4.3", features = ["serde"] }
1111
reqwest = { version = "0.12.19", features = ["json"] }
12-
secp256k1 = { version = "0.31.0", features = ["recovery"] }
12+
secp256k1 = { version = "0.30.0", features = ["recovery", "rand"] }
1313
serde = "1.0.219"
1414
serde_wormhole = "0.1.0"
1515
sha3 = "0.10.8"
@@ -19,6 +19,7 @@ solana-sdk = "2.2.2"
1919
tokio = "1.45.1"
2020
tokio-stream = "0.1.17"
2121
tracing = "0.1.41"
22+
tracing-subscriber = { version = "0.3.19", features = ["env-filter", "json"] }
2223
wormhole-vaas-serde = "0.1.0"
2324

2425
[dev-dependencies]

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ cargo build
2323
### ▶️ Run the Project
2424

2525
You can run the project using `cargo run` by passing the required flags:
26+
Make sure to set `RUST_LOG=INFO` to enable logs from tracing:
2627

2728
```bash
28-
cargo run -- \
29+
RUST_LOG=INFO cargo run -- run \
2930
--pythnet-url wss://api2.pythnet.pyth.network \
3031
--server-url https://watcher.pyth.network \
3132
--secret-key /path/to/secret.key \
@@ -43,12 +44,25 @@ export PYTHNET_URL=wss://api2.pythnet.pyth.network
4344
export SERVER_URL=https://watcher.pyth.network
4445
export SECRET_KEY=/path/to/secret.key
4546
export WORMHOLE_PID=H3fxXJ86ADW2PNuDDmZJg6mzTtPxkYCpNuQUTgmJ7AjU
47+
export RUST_LOG=INFO
4648

4749
cargo run
4850
```
4951

5052
---
5153

54+
### 🔑 Generate a Secret Key
55+
56+
To generate a new secp256k1 secret key and write it to a file:
57+
58+
```bash
59+
RUST_LOG=INFO cargo run -- generate-key --output-file .secret
60+
```
61+
62+
This will save the key in raw byte format to the file named `.secret`.
63+
64+
---
65+
5266
### 🧪 Testing Locally
5367

5468
To test in a non-production environment (e.g. with devnet or a local Pythnet fork), just provide a different `--pythnet-url`, and `--server-url`, and optionally use custom `--wormhole-pid`.

src/api_client.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl<P: Serialize> Observation<P> {
4444
pub fn try_new(body: Body<P>, secret_key: SecretKey) -> Result<Self, anyhow::Error> {
4545
let digest = body.digest()?;
4646
let signature = Secp256k1::new()
47-
.sign_ecdsa_recoverable(Message::from_digest(digest.secp256k_hash), &secret_key);
47+
.sign_ecdsa_recoverable(&Message::from_digest(digest.secp256k_hash), &secret_key);
4848
let (recovery_id, signature_bytes) = signature.serialize_compact();
4949
let recovery_id: i32 = recovery_id.into();
5050
let mut signature = [0u8; 65];
@@ -126,7 +126,7 @@ mod tests {
126126

127127
#[test]
128128
fn test_new_signed_observation() {
129-
let secret_key = SecretKey::from_byte_array([1u8; 32]).expect("Invalid secret key length");
129+
let secret_key = SecretKey::from_byte_array(&[1u8; 32]).expect("Invalid secret key length");
130130
let body = Body {
131131
timestamp: 1234567890,
132132
nonce: 42,
@@ -154,7 +154,7 @@ mod tests {
154154
.expect("Invalid recoverable signature");
155155

156156
let pubkey = secp
157-
.recover_ecdsa(message, &recoverable_sig)
157+
.recover_ecdsa(&message, &recoverable_sig)
158158
.expect("Failed to recover pubkey");
159159

160160
let expected_pubkey = PublicKey::from_secret_key(&secp, &secret_key);

src/config.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,18 @@ pub struct RunOptions {
1818
#[arg(long = "server-url", env = "SERVER_URL")]
1919
pub server_url: String,
2020
}
21+
22+
#[derive(Parser, Clone, Debug)]
23+
pub struct GenerateKeyOptions {
24+
/// Output path for the generated secret key.
25+
#[arg(long = "output-file", env = "OUTPUT_FILE")]
26+
pub output_path: String,
27+
}
28+
29+
#[derive(Parser, Debug)]
30+
pub enum Command {
31+
/// Run the auction server service.
32+
Run(RunOptions),
33+
/// Run db migrations and exit.
34+
GenerateKey(GenerateKeyOptions),
35+
}

0 commit comments

Comments
 (0)