Make safe transaction signing the default

This commit is contained in:
Greg Fitzgerald 2019-03-14 15:55:28 -06:00
parent 7b4568b9bf
commit f8bf9ca218
3 changed files with 9 additions and 11 deletions

View File

@ -180,7 +180,7 @@ impl Transaction {
Hash::default(), Hash::default(),
fee, fee,
); );
transaction.sign_checked(&[from_keypair], recent_blockhash); transaction.sign(&[from_keypair], recent_blockhash);
transaction transaction
} }
pub fn new_unsigned<T: Serialize>( pub fn new_unsigned<T: Serialize>(
@ -229,7 +229,7 @@ impl Transaction {
program_ids, program_ids,
instructions, instructions,
}; };
tx.sign_checked(from_keypairs, recent_blockhash); tx.sign(from_keypairs, recent_blockhash);
tx tx
} }
pub fn data(&self, instruction_index: usize) -> &[u8] { pub fn data(&self, instruction_index: usize) -> &[u8] {
@ -280,7 +280,7 @@ impl Transaction {
} }
/// Sign this transaction. /// Sign this transaction.
pub fn sign<T: KeypairUtil>(&mut self, keypairs: &[&T], recent_blockhash: Hash) { pub fn sign_unchecked<T: KeypairUtil>(&mut self, keypairs: &[&T], recent_blockhash: Hash) {
self.recent_blockhash = recent_blockhash; self.recent_blockhash = recent_blockhash;
let message = self.message(); let message = self.message();
self.signatures = keypairs self.signatures = keypairs
@ -291,14 +291,14 @@ impl Transaction {
/// Check keys and keypair lengths, then sign this transaction. /// Check keys and keypair lengths, then sign this transaction.
/// Note: this presumes signatures.capacity() was set to the number of required signatures. /// Note: this presumes signatures.capacity() was set to the number of required signatures.
pub fn sign_checked<T: KeypairUtil>(&mut self, keypairs: &[&T], recent_blockhash: Hash) { pub fn sign<T: KeypairUtil>(&mut self, keypairs: &[&T], recent_blockhash: Hash) {
let signed_keys = &self.account_keys[0..self.signatures.capacity()]; let signed_keys = &self.account_keys[0..self.signatures.capacity()];
for (i, keypair) in keypairs.iter().enumerate() { for (i, keypair) in keypairs.iter().enumerate() {
assert_eq!(keypair.pubkey(), signed_keys[i], "keypair-pubkey mismatch"); assert_eq!(keypair.pubkey(), signed_keys[i], "keypair-pubkey mismatch");
} }
assert_eq!(keypairs.len(), signed_keys.len(), "not enough keypairs"); assert_eq!(keypairs.len(), signed_keys.len(), "not enough keypairs");
self.sign(keypairs, recent_blockhash); self.sign_unchecked(keypairs, recent_blockhash);
} }
/// Verify only the transaction signature. /// Verify only the transaction signature.

View File

@ -108,7 +108,7 @@ impl TransactionBuilder {
/// Return a signed transaction. /// Return a signed transaction.
pub fn sign<T: KeypairUtil>(&self, keypairs: &[&T], recent_blockhash: Hash) -> Transaction { pub fn sign<T: KeypairUtil>(&self, keypairs: &[&T], recent_blockhash: Hash) -> Transaction {
let mut tx = self.compile(); let mut tx = self.compile();
tx.sign_checked(keypairs, recent_blockhash); tx.sign(keypairs, recent_blockhash);
tx tx
} }
} }

View File

@ -1726,17 +1726,15 @@ mod tests {
let blockhash = Hash::default(); let blockhash = Hash::default();
let mut tx = SystemTransaction::new_account(&key, &to, 50, blockhash, 0); let mut tx = SystemTransaction::new_account(&key, &to, 50, blockhash, 0);
let signer = Keypair::new(); let result = send_and_confirm_transaction(&rpc_client, &mut tx, &key);
let result = send_and_confirm_transaction(&rpc_client, &mut tx, &signer);
result.unwrap(); result.unwrap();
let rpc_client = RpcClient::new("account_in_use".to_string()); let rpc_client = RpcClient::new("account_in_use".to_string());
let result = send_and_confirm_transaction(&rpc_client, &mut tx, &signer); let result = send_and_confirm_transaction(&rpc_client, &mut tx, &key);
assert!(result.is_err()); assert!(result.is_err());
let rpc_client = RpcClient::new("fails".to_string()); let rpc_client = RpcClient::new("fails".to_string());
let result = send_and_confirm_transaction(&rpc_client, &mut tx, &signer); let result = send_and_confirm_transaction(&rpc_client, &mut tx, &key);
assert!(result.is_err()); assert!(result.is_err());
} }