Support transaction signing by heterogenous lists of keypairs (#8342)

automerge
This commit is contained in:
Greg Fitzgerald
2020-02-20 13:13:23 -07:00
committed by GitHub
parent 1720fe6a46
commit e8124324ff
16 changed files with 196 additions and 81 deletions

97
sdk/src/signers.rs Normal file
View File

@@ -0,0 +1,97 @@
use crate::{
pubkey::Pubkey,
signature::{KeypairUtil, Signature},
};
pub trait Signers {
fn pubkeys(&self) -> Vec<Pubkey>;
fn sign_message(&self, message: &[u8]) -> Vec<Signature>;
}
macro_rules! default_keypairs_impl {
() => (
fn pubkeys(&self) -> Vec<Pubkey> {
self.iter().map(|keypair| keypair.pubkey()).collect()
}
fn sign_message(&self, message: &[u8]) -> Vec<Signature> {
self.iter()
.map(|keypair| keypair.sign_message(message))
.collect()
}
);
}
impl<T: KeypairUtil> Signers for [&T] {
default_keypairs_impl!();
}
impl Signers for [Box<dyn KeypairUtil>] {
default_keypairs_impl!();
}
impl<T: KeypairUtil> Signers for [&T; 0] {
default_keypairs_impl!();
}
impl<T: KeypairUtil> Signers for [&T; 1] {
default_keypairs_impl!();
}
impl<T: KeypairUtil> Signers for [&T; 2] {
default_keypairs_impl!();
}
impl<T: KeypairUtil> Signers for [&T; 3] {
default_keypairs_impl!();
}
impl<T: KeypairUtil> Signers for [&T; 4] {
default_keypairs_impl!();
}
impl<T: KeypairUtil> Signers for Vec<&T> {
default_keypairs_impl!();
}
#[cfg(test)]
mod tests {
use super::*;
use std::error;
struct Foo;
impl KeypairUtil for Foo {
fn try_pubkey(&self) -> Result<Pubkey, Box<dyn error::Error>> {
Ok(Pubkey::default())
}
fn try_sign_message(&self, _message: &[u8]) -> Result<Signature, Box<dyn error::Error>> {
Ok(Signature::default())
}
}
struct Bar;
impl KeypairUtil for Bar {
fn try_pubkey(&self) -> Result<Pubkey, Box<dyn error::Error>> {
Ok(Pubkey::default())
}
fn try_sign_message(&self, _message: &[u8]) -> Result<Signature, Box<dyn error::Error>> {
Ok(Signature::default())
}
}
#[test]
fn test_dyn_keypairs_compile() {
let xs: Vec<Box<dyn KeypairUtil>> = vec![Box::new(Foo {}), Box::new(Bar {})];
assert_eq!(
xs.sign_message(b""),
vec![Signature::default(), Signature::default()],
);
// Same as above, but less compiler magic.
let xs_ref: &[Box<dyn KeypairUtil>] = &xs;
assert_eq!(
Signers::sign_message(xs_ref, b""),
vec![Signature::default(), Signature::default()],
);
}
}