Skip to content

Commit a31ed40

Browse files
committed
bpb sign-hex 0xfooo
QoL helped for tea gpg rewards wallets
1 parent d37bf7a commit a31ed40

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

bpb/src/main.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ fn main() -> Result<(), Error> {
3535
Some("print") => print_public_key(),
3636
Some("fingerprint") => print_fingerprint(),
3737
Some("key-id") => print_key_id(),
38+
Some("sign-hex") => {
39+
if let Some(hex) = args.next() {
40+
sign_from_hex(hex)
41+
} else {
42+
bail!("Must specify a hex string to sign, e.g.: `bpb sign-hex 1234abcd`")
43+
}
44+
}
3845
Some("--help") => print_help_message(),
3946
Some(arg) if gpg_sign_arg(arg) => verify_commit(),
4047
_ => {
@@ -60,6 +67,7 @@ fn print_help_message() -> Result<(), Error> {
6067
println!(" print: Print public key in OpenPGP format.");
6168
println!(" fingerprint: Print the fingerprint of the public key.");
6269
println!(" key-id: Print the key ID of the public key.\n");
70+
println!(" sign-hex <hex>: Sign a hex string and print the signature and public key.");
6371
println!("See https://github.com/pkgxdev/bpb for more information.");
6472
Ok(())
6573
}
@@ -155,6 +163,28 @@ fn verify_commit() -> Result<(), Error> {
155163
Ok(())
156164
}
157165

166+
// Signs a hex string and prints the signature
167+
fn sign_from_hex(hex: String) -> Result<(), Error> {
168+
let config = Config::load()?;
169+
let service = config.service();
170+
let account = config.user_id();
171+
let secret_str = get_keychain_item(service, account)?;
172+
let secret = to_32_bytes(&secret_str)?;
173+
174+
let keypair = KeyData::load(&config, secret)?;
175+
// remove any leading 0x prefix
176+
let hex = hex.trim_start_matches("0x").trim_end_matches(' ');
177+
let data = hex::decode(hex)?;
178+
179+
let signed = keypair.sign(&data)?;
180+
let signature = hex::encode(signed.as_bytes());
181+
182+
let public_key = hex::encode_upper(keypair.public().as_bytes());
183+
println!("signature:\n\n{signature}\n");
184+
println!("public key:\n\n{public_key}\n");
185+
Ok(())
186+
}
187+
158188
fn delegate() -> ! {
159189
use std::process;
160190

0 commit comments

Comments
 (0)