Skip to content

Commit ac1372d

Browse files
committed
Require explicit ord binary for env
1 parent 55ec18f commit ac1372d

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

docs/src/guides/testing.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ to `env.json`.
1919
`env.json` contains the commands needed to invoke `bitcoin-cli` and `ord
2020
wallet`, as well as the ports `bitcoind` and `ord server` are listening on.
2121

22+
`ord env` requires the `--ord-bin <PATH>` option to specify which `ord` binary
23+
to run.
24+
2225
These can be extracted into shell commands using `jq`:
2326

2427
```shell

src/subcommand/env.rs

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ pub(crate) struct Env {
2626
help = "Proxy `/content/INSCRIPTION_ID` and other recursive endpoints to `<PROXY>` if the inscription is not present on current chain."
2727
)]
2828
pub(crate) proxy: Option<Url>,
29+
#[arg(
30+
long,
31+
help = "Path to ord binary to run.",
32+
value_name = "PATH"
33+
)]
34+
ord_bin: Option<PathBuf>,
2935
}
3036

3137
#[derive(Serialize)]
@@ -38,6 +44,8 @@ struct Info {
3844

3945
impl Env {
4046
pub(crate) fn run(self) -> SubcommandResult {
47+
let ord = self.resolve_ord_bin()?;
48+
4149
let bitcoind_port = TcpListener::bind("127.0.0.1:9000")
4250
.ok()
4351
.map(|listener| listener.local_addr().unwrap().port());
@@ -134,8 +142,6 @@ rpcport={bitcoind_port}
134142
)?;
135143
}
136144

137-
let ord = std::env::current_exe()?;
138-
139145
let decompress = self.decompress;
140146
let proxy = self.proxy.map(|url| url.to_string());
141147

@@ -209,7 +215,7 @@ rpcport={bitcoind_port}
209215
ord_port,
210216
bitcoin_cli_command: vec!["bitcoin-cli".into(), format!("-datadir={relative}")],
211217
ord_wallet_command: vec![
212-
ord.to_str().unwrap().into(),
218+
ord.to_string_lossy().into(),
213219
"--datadir".into(),
214220
absolute.to_str().unwrap().into(),
215221
"wallet".into(),
@@ -247,4 +253,48 @@ bitcoin-cli -datadir={datadir} getblockchaininfo
247253
thread::sleep(Duration::from_millis(100));
248254
}
249255
}
256+
257+
fn resolve_ord_bin(&self) -> Result<PathBuf> {
258+
let Some(path) = &self.ord_bin else {
259+
bail!("ord binary must be provided with --ord-bin");
260+
};
261+
262+
Self::validate_ord_bin(path)
263+
}
264+
265+
fn validate_ord_bin(path: &Path) -> Result<PathBuf> {
266+
let canonical = fs::canonicalize(path)
267+
.with_context(|| format!("ord binary `{}` does not exist", path.display()))?;
268+
269+
let metadata = fs::metadata(&canonical)
270+
.with_context(|| format!("failed to read ord binary `{}`", canonical.display()))?;
271+
272+
ensure!(
273+
metadata.is_file(),
274+
"ord binary `{}` is not a file",
275+
canonical.display()
276+
);
277+
278+
#[cfg(unix)]
279+
{
280+
use std::os::unix::fs::PermissionsExt;
281+
282+
let mode = metadata.permissions().mode();
283+
284+
ensure!(
285+
mode & 0o111 != 0,
286+
"ord binary `{}` is not executable",
287+
canonical.display()
288+
);
289+
290+
ensure!(
291+
mode & 0o022 == 0,
292+
"ord binary `{}` is writable by group or others",
293+
canonical.display()
294+
);
295+
}
296+
297+
Ok(canonical)
298+
}
299+
250300
}

0 commit comments

Comments
 (0)