Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sign for inscription and output #4027

Merged
merged 21 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 44 additions & 8 deletions src/subcommand/wallet/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,33 @@ use {
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Output {
pub address: Address<NetworkUnchecked>,
#[serde(skip_serializing_if = "Option::is_none")]
pub inscription: Option<InscriptionId>,
#[serde(skip_serializing_if = "Option::is_none")]
pub output: Option<OutPoint>,
#[serde(skip_serializing_if = "Option::is_none")]
pub message: Option<String>,
pub witness: String,
}

#[derive(Debug, Parser)]
#[clap(group(
#[clap(
group(
ArgGroup::new("input")
.required(true)
.args(&["message", "file"]))
.args(&["message", "file"])),
group(
ArgGroup::new("signer")
.required(true)
.args(&["address", "inscription", "output"]))
)]
pub(crate) struct Sign {
#[arg(long, help = "Sign for <ADDRESS>.")]
address: Address<NetworkUnchecked>,
address: Option<Address<NetworkUnchecked>>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's combine these into a Signer enum. We could either have this as an argument, or maybe a --sign-with flag.

#[arg(long, help = "Sign for <INSCRIPTION>.")]
inscription: Option<InscriptionId>,
#[arg(long, help = "Sign for <UTXO>.")]
output: Option<OutPoint>,
#[arg(long, help = "Sign <MESSAGE>.")]
message: Option<String>,
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
#[arg(long, help = "Sign contents of <FILE>.")]
Expand All @@ -27,10 +41,30 @@ pub(crate) struct Sign {

impl Sign {
pub(crate) fn run(&self, wallet: Wallet) -> SubcommandResult {
let address = &self
.address
.clone()
.require_network(wallet.chain().network())?;
let address = if let Some(address) = &self.address {
address.clone().require_network(wallet.chain().network())?
} else if let Some(inscription) = &self.inscription {
Address::from_str(
&wallet
.inscription_info()
.get(inscription)
.ok_or_else(|| anyhow!("inscription {inscription} not in wallet"))?
.address
.clone()
.ok_or_else(|| anyhow!("inscription {inscription} in an output without address"))?,
)?
.require_network(wallet.chain().network())?
} else if let Some(output) = self.output {
wallet.chain().address_from_script(
&wallet
.utxos()
.get(&output)
.ok_or_else(|| anyhow!("output {output} has no address"))?
.script_pubkey,
)?
} else {
unreachable!()
};

let message = if let Some(message) = &self.message {
message.as_bytes()
Expand All @@ -40,7 +74,7 @@ impl Sign {
unreachable!()
};

let to_spend = bip322::create_to_spend(address, message)?;
let to_spend = bip322::create_to_spend(&address, message)?;

let to_sign = bip322::create_to_sign(&to_spend, None)?;

Expand All @@ -64,6 +98,8 @@ impl Sign {

Ok(Some(Box::new(Output {
address: address.as_unchecked().clone(),
inscription: self.inscription,
output: self.output,
message: self.message.clone(),
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
witness: general_purpose::STANDARD.encode(buffer),
})))
Expand Down
60 changes: 60 additions & 0 deletions tests/wallet/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,63 @@ fn sign_file() {
.stderr_regex("error: Invalid signature.*")
.run_and_extract_stdout();
}

#[test]
fn sign_for_inscription() {
let core = mockcore::spawn();

let ord = TestServer::spawn_with_server_args(&core, &[], &[]);

create_wallet(&core, &ord);

let (inscription, _reveal) = inscribe(&core, &ord);

core.mine_blocks(1);

let addresses = CommandBuilder::new("wallet addresses")
.core(&core)
.ord(&ord)
.run_and_deserialize_output::<BTreeMap<Address<NetworkUnchecked>, Vec<AddressesOutput>>>();

let message = "HelloWorld";

let sign = CommandBuilder::new(format!(
"wallet sign --inscription {inscription} --message {message}",
))
.core(&core)
.ord(&ord)
.run_and_deserialize_output::<SignOutput>();

assert!(addresses.contains_key(&sign.address));
assert_eq!(message, &sign.message.unwrap());
assert_eq!(inscription, sign.inscription.unwrap());
}

#[test]
fn sign_for_output() {
let core = mockcore::spawn();

let ord = TestServer::spawn_with_server_args(&core, &[], &[]);

create_wallet(&core, &ord);

core.mine_blocks(1);

let addresses = CommandBuilder::new("wallet addresses")
.core(&core)
.ord(&ord)
.run_and_deserialize_output::<BTreeMap<Address<NetworkUnchecked>, Vec<AddressesOutput>>>();

let output = addresses.first_key_value().unwrap().1[0].output;

let message = "HelloWorld";

let sign = CommandBuilder::new(format!("wallet sign --output {output} --message {message}",))
.core(&core)
.ord(&ord)
.run_and_deserialize_output::<SignOutput>();

assert!(addresses.contains_key(&sign.address));
assert_eq!(message, &sign.message.unwrap());
assert_eq!(output, sign.output.unwrap());
}
Loading