1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#![cfg(feature = "full")]
use crate::{
pubkey::Pubkey,
signature::{Signature, Signer, SignerError},
};
pub trait Signers {
fn pubkeys(&self) -> Vec<Pubkey>;
fn try_pubkeys(&self) -> Result<Vec<Pubkey>, SignerError>;
fn sign_message(&self, message: &[u8]) -> Vec<Signature>;
fn try_sign_message(&self, message: &[u8]) -> Result<Vec<Signature>, SignerError>;
}
macro_rules! default_keypairs_impl {
() => {
fn pubkeys(&self) -> Vec<Pubkey> {
self.iter().map(|keypair| keypair.pubkey()).collect()
}
fn try_pubkeys(&self) -> Result<Vec<Pubkey>, SignerError> {
let mut pubkeys = Vec::new();
for keypair in self.iter() {
pubkeys.push(keypair.try_pubkey()?);
}
Ok(pubkeys)
}
fn sign_message(&self, message: &[u8]) -> Vec<Signature> {
self.iter()
.map(|keypair| keypair.sign_message(message))
.collect()
}
fn try_sign_message(&self, message: &[u8]) -> Result<Vec<Signature>, SignerError> {
let mut signatures = Vec::new();
for keypair in self.iter() {
signatures.push(keypair.try_sign_message(message)?);
}
Ok(signatures)
}
};
}
impl<T: Signer> Signers for [&T] {
default_keypairs_impl!();
}
impl Signers for [Box<dyn Signer>] {
default_keypairs_impl!();
}
impl Signers for Vec<Box<dyn Signer>> {
default_keypairs_impl!();
}
impl Signers for Vec<&dyn Signer> {
default_keypairs_impl!();
}
impl Signers for [&dyn Signer] {
default_keypairs_impl!();
}
impl Signers for [&dyn Signer; 0] {
default_keypairs_impl!();
}
impl Signers for [&dyn Signer; 1] {
default_keypairs_impl!();
}
impl Signers for [&dyn Signer; 2] {
default_keypairs_impl!();
}
impl Signers for [&dyn Signer; 3] {
default_keypairs_impl!();
}
impl Signers for [&dyn Signer; 4] {
default_keypairs_impl!();
}
impl<T: Signer> Signers for [&T; 0] {
default_keypairs_impl!();
}
impl<T: Signer> Signers for [&T; 1] {
default_keypairs_impl!();
}
impl<T: Signer> Signers for [&T; 2] {
default_keypairs_impl!();
}
impl<T: Signer> Signers for [&T; 3] {
default_keypairs_impl!();
}
impl<T: Signer> Signers for [&T; 4] {
default_keypairs_impl!();
}
impl<T: Signer> Signers for Vec<&T> {
default_keypairs_impl!();
}
#[cfg(test)]
mod tests {
use super::*;
struct Foo;
impl Signer for Foo {
fn try_pubkey(&self) -> Result<Pubkey, SignerError> {
Ok(Pubkey::default())
}
fn try_sign_message(&self, _message: &[u8]) -> Result<Signature, SignerError> {
Ok(Signature::default())
}
}
struct Bar;
impl Signer for Bar {
fn try_pubkey(&self) -> Result<Pubkey, SignerError> {
Ok(Pubkey::default())
}
fn try_sign_message(&self, _message: &[u8]) -> Result<Signature, SignerError> {
Ok(Signature::default())
}
}
#[test]
fn test_dyn_keypairs_compile() {
let xs: Vec<Box<dyn Signer>> = vec![Box::new(Foo {}), Box::new(Bar {})];
assert_eq!(
xs.sign_message(b""),
vec![Signature::default(), Signature::default()],
);
let xs_ref: &[Box<dyn Signer>] = &xs;
assert_eq!(
Signers::sign_message(xs_ref, b""),
vec![Signature::default(), Signature::default()],
);
}
#[test]
#[allow(clippy::blacklisted_name)]
fn test_dyn_keypairs_by_ref_compile() {
let foo = Foo {};
let bar = Bar {};
let xs: Vec<&dyn Signer> = vec![&foo, &bar];
assert_eq!(
xs.sign_message(b""),
vec![Signature::default(), Signature::default()],
);
let xs_ref: &[&dyn Signer] = &xs;
assert_eq!(
Signers::sign_message(xs_ref, b""),
vec![Signature::default(), Signature::default()],
);
}
}