Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created August 9, 2020 16:15
Show Gist options
  • Save rust-play/48ecc1455bf2d6e9f15278b37db15c3a to your computer and use it in GitHub Desktop.
Save rust-play/48ecc1455bf2d6e9f15278b37db15c3a to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
/// The trait interesting for everyone wanting to sign PSBTs, be it using their
/// own code or ours
pub trait PsbtSigner {
type Error: Debug;
fn verify(_psbt: &Psbt) -> Result<(), Self::Error> {
// Some default validation code
Ok(())
}
fn sign(&self, psbt: Psbt) -> Result<Psbt, Self::Error>;
}
/// We alos supply a default signer with an interface similar to what you
/// initially proposed
pub struct DefaultPsbtSigner {
pub sign_fn: Box<dyn Fn(Message, DerivationPath) -> Result<Option<Signature>, DefaultPsbtSignerError>>,
}
#[derive(Debug)]
pub enum DefaultPsbtSignerError {}
impl PsbtSigner for DefaultPsbtSigner {
type Error = DefaultPsbtSignerError;
#[allow(unused_mut)]
fn sign(&self, mut psbt: Psbt) -> Result<Psbt, Self::Error> {
Self::verify(&psbt)?;
// do things with psbt, call sign_fn, add signature to psbt
Ok(psbt)
}
}
/// But we can even extend our default signer to hold its own secrets
pub struct SecretPsbtSigner {
signer: DefaultPsbtSigner,
}
impl PsbtSigner for SecretPsbtSigner {
type Error = DefaultPsbtSignerError;
#[allow(unused_mut)]
fn sign(&self, mut psbt: Psbt) -> Result<Psbt, Self::Error> {
self.signer.sign(psbt)
}
}
impl SecretPsbtSigner {
pub fn from_xkey(master: XPrivKey) -> Self {
SecretPsbtSigner {
signer: DefaultPsbtSigner {
sign_fn: Box::new(move |_msg, _path| {
// sign using XPrivKey
let _ = master;
// could return Ok(None) if it doesn't have the key
Ok(Some(Signature {}))
})
}
}
}
}
use std::fmt::Debug;
pub struct Psbt {}
pub struct Signature {}
pub struct Message {}
pub struct DerivationPath {}
pub struct XPrivKey {}
fn main() {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment