Skip to content

Commit b32e02a

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

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

bpb/src/main.rs

Lines changed: 29 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
_ => {
@@ -155,6 +162,28 @@ fn verify_commit() -> Result<(), Error> {
155162
Ok(())
156163
}
157164

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

0 commit comments

Comments
 (0)