Generalize Transaction::new to accept anything that implements KeypairUtil

This commit is contained in:
Greg Fitzgerald
2019-01-29 16:57:48 -07:00
parent 34c8b2cc2f
commit c741a960b9
8 changed files with 26 additions and 25 deletions

View File

@ -97,11 +97,11 @@ pub struct Transaction {
}
impl Transaction {
pub fn new<T: Serialize>(
from_keypair: &Keypair,
pub fn new<S: Serialize, T: KeypairUtil>(
from_keypair: &T,
transaction_keys: &[Pubkey],
program_id: Pubkey,
userdata: &T,
userdata: &S,
last_id: Hash,
fee: u64,
) -> Self {
@ -130,7 +130,14 @@ impl Transaction {
let instructions = vec![Instruction::new(0, userdata, accounts)];
let mut keys = vec![*from_pubkey];
keys.extend_from_slice(transaction_keys);
Self::new_with_instructions(&[], &keys[..], last_id, fee, program_ids, instructions)
Self::new_with_instructions::<Keypair>(
&[],
&keys[..],
last_id,
fee,
program_ids,
instructions,
)
}
/// Create a signed transaction
/// * `from_keypair` - The key used to sign the transaction. This key is stored as keys[0]
@ -140,8 +147,8 @@ impl Transaction {
/// * `fee` - The transaction fee.
/// * `program_ids` - The keys that identify programs used in the `instruction` vector.
/// * `instructions` - The programs and their arguments that the transaction will execute atomically
pub fn new_with_instructions(
from_keypairs: &[&Keypair],
pub fn new_with_instructions<T: KeypairUtil>(
from_keypairs: &[&T],
keys: &[Pubkey],
last_id: Hash,
fee: u64,
@ -211,7 +218,7 @@ impl Transaction {
}
/// Sign this transaction.
pub fn sign(&mut self, keypairs: &[&Keypair], last_id: Hash) {
pub fn sign<T: KeypairUtil>(&mut self, keypairs: &[&T], last_id: Hash) {
self.last_id = last_id;
let message = self.message();
self.signatures = keypairs

View File

@ -10,7 +10,7 @@ use crate::vote_program::{self, Vote, VoteInstruction};
use bincode::deserialize;
pub trait VoteTransaction {
fn vote_new(vote_account: &Pubkey, vote: Vote, last_id: Hash, fee: u64) -> Self;
fn vote_new(vote_account: &Pubkey, tick_height: u64, last_id: Hash, fee: u64) -> Self;
fn vote_account_new(
validator_id: &Keypair,
vote_account_id: Pubkey,
@ -23,7 +23,8 @@ pub trait VoteTransaction {
}
impl VoteTransaction for Transaction {
fn vote_new(vote_account: &Pubkey, vote: Vote, last_id: Hash, fee: u64) -> Self {
fn vote_new(vote_account: &Pubkey, tick_height: u64, last_id: Hash, fee: u64) -> Self {
let vote = Vote { tick_height };
let instruction = VoteInstruction::NewVote(vote);
Transaction::new_unsigned(
vote_account,