Skip to content

Commit

Permalink
Add the add_data option to create_tx to create an op_return output
Browse files Browse the repository at this point in the history
  • Loading branch information
waterst0ne committed Jul 8, 2022
1 parent d8e93ab commit be6fb98
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
22 changes: 21 additions & 1 deletion src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
/// 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<String>,
/// 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<String>,
},
/// Bumps the fees of an RBF transaction
BumpFee {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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()),
}),
},
};
Expand Down
9 changes: 9 additions & 0 deletions src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ where
fee_rate,
external_policy,
internal_policy,
add_data,
add_string,
} => {
let mut tx_builder = wallet.build_tx();

Expand Down Expand Up @@ -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)),
Expand Down

0 comments on commit be6fb98

Please sign in to comment.