From b06b1414c1452e7f2cc771483c8970aa5e1b0aaa Mon Sep 17 00:00:00 2001
From: Pedro Felix
Date: Thu, 7 Jul 2022 16:18:13 -0700
Subject: [PATCH] Add the add_data option to create_tx to create an op_return
output
---
CHANGELOG.md | 1 +
src/commands.rs | 22 +++++++++++++++++++++-
src/handlers.rs | 9 +++++++++
3 files changed, 31 insertions(+), 1 deletion(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 41f1481..c86496d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Rewrite relevant doc comments as `structopt` help document.
- Update `bdk` and `bdk-reserves` to v0.19.0.
- Change default database to `sqlite`.
+- Add `add_data` and `add_string` options in `create_tx` to create OP_RETURN outputs.
## [0.5.0]
diff --git a/src/commands.rs b/src/commands.rs
index d5c42fc..0b89937 100644
--- a/src/commands.rs
+++ b/src/commands.rs
@@ -339,6 +339,22 @@ pub enum OfflineWalletSubCommand {
/// Selects which policy should be used to satisfy the internal descriptor
#[structopt(name = "INT_POLICY", long = "internal_policy")]
internal_policy: Option,
+ /// Optionally create an OP_RETURN output containing given String.
+ #[structopt(
+ name = "ADD_STRING",
+ long = "add_string",
+ short = "s",
+ conflicts_with = "ADD_DATA"
+ )]
+ add_string: Option,
+ /// Optionally create an OP_RETURN output containing given base64 encoded String. (max 80 bytes)
+ #[structopt(
+ name = "ADD_DATA",
+ long = "add_data",
+ short = "o",
+ conflicts_with = "ADD_STRING"
+ )]
+ add_data: Option,
},
/// Bumps the fees of an RBF transaction
BumpFee {
@@ -874,7 +890,9 @@ mod test {
"--change_descriptor", "wpkh(tpubDEnoLuPdBep9bzw5LoGYpsxUQYheRQ9gcgrJhJEcdKFB9cWQRyYmkCyRoTqeD4tJYiVVgt6A3rN6rWn9RYhR9sBsGxji29LYWHuKKbdb1ev/1/*)",
"create_tx", "--to", "n2Z3YNXtceeJhFkTknVaNjT1mnCGWesykJ:123456","mjDZ34icH4V2k9GmC8niCrhzVuR3z8Mgkf:78910",
"--utxos","87345e46bfd702d24d54890cc094d08a005f773b27c8f965dfe0eb1e23eef88e:1",
- "--utxos","87345e46bfd702d24d54890cc094d08a005f773b27c8f965dfe0eb1e23eef88e:2"];
+ "--utxos","87345e46bfd702d24d54890cc094d08a005f773b27c8f965dfe0eb1e23eef88e:2",
+ "--add_string","Hello BDK",
+ "--add_data","SGVsbG8gQkRLCg==" ];
let cli_opts = CliOpts::from_iter(&cli_args);
@@ -944,6 +962,8 @@ mod test {
fee_rate: None,
external_policy: None,
internal_policy: None,
+ add_data: Some("SGVsbG8gQkRLCg==car".to_string()),
+ add_string: Some("Hello BDK".to_string()),
}),
},
};
diff --git a/src/handlers.rs b/src/handlers.rs
index 9e0ded0..80118ac 100644
--- a/src/handlers.rs
+++ b/src/handlers.rs
@@ -125,6 +125,8 @@ where
fee_rate,
external_policy,
internal_policy,
+ add_data,
+ add_string,
} => {
let mut tx_builder = wallet.build_tx();
@@ -154,6 +156,13 @@ where
tx_builder.unspendable(unspendable);
}
+ if let Some(base64_data) = add_data {
+ let op_return_data = base64::decode(&base64_data).unwrap();
+ tx_builder.add_data(op_return_data.as_slice());
+ } else if let Some(string_data) = add_string {
+ tx_builder.add_data(string_data.as_bytes());
+ }
+
let policies = vec![
external_policy.map(|p| (p, KeychainKind::External)),
internal_policy.map(|p| (p, KeychainKind::Internal)),