-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
Copy pathmod.rs
1671 lines (1568 loc) · 61.3 KB
/
mod.rs
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Atomically-committed sequences of instructions.
//!
//! While [`Instruction`]s are the basic unit of computation in Solana, they are
//! submitted by clients in [`Transaction`]s containing one or more
//! instructions, and signed by one or more [`Signer`]s. Solana executes the
//! instructions in a transaction in order, and only commits any changes if all
//! instructions terminate without producing an error or exception.
//!
//! Transactions do not directly contain their instructions but instead include
//! a [`Message`], a precompiled representation of a sequence of instructions.
//! `Message`'s constructors handle the complex task of reordering the
//! individual lists of accounts required by each instruction into a single flat
//! list of deduplicated accounts required by the Solana runtime. The
//! `Transaction` type has constructors that build the `Message` so that clients
//! don't need to interact with them directly.
//!
//! Prior to submission to the network, transactions must be signed by one or or
//! more keypairs, and this signing is typically performed by an abstract
//! [`Signer`], which may be a [`Keypair`] but may also be other types of
//! signers including remote wallets, such as Ledger devices, as represented by
//! the [`RemoteKeypair`] type in the [`solana-remote-wallet`] crate.
//!
//! [`Signer`]: crate::signer::Signer
//! [`Keypair`]: crate::signer::keypair::Keypair
//! [`solana-remote-wallet`]: https://docs.rs/solana-remote-wallet/latest/
//! [`RemoteKeypair`]: https://docs.rs/solana-remote-wallet/latest/solana_remote_wallet/remote_keypair/struct.RemoteKeypair.html
//!
//! Every transaction must be signed by a fee-paying account, the account from
//! which the cost of executing the transaction is withdrawn. Other required
//! signatures are determined by the requirements of the programs being executed
//! by each instruction, and are conventionally specified by that program's
//! documentation.
//!
//! When signing a transaction, a recent blockhash must be provided (which can
//! be retrieved with [`RpcClient::get_latest_blockhash`]). This allows
//! validators to drop old but unexecuted transactions; and to distinguish
//! between accidentally duplicated transactions and intentionally duplicated
//! transactions — any identical transactions will not be executed more
//! than once, so updating the blockhash between submitting otherwise identical
//! transactions makes them unique. If a client must sign a transaction long
//! before submitting it to the network, then it can use the _[durable
//! transaction nonce]_ mechanism instead of a recent blockhash to ensure unique
//! transactions.
//!
//! [`RpcClient::get_latest_blockhash`]: https://docs.rs/solana-rpc-client/latest/solana_rpc_client/rpc_client/struct.RpcClient.html#method.get_latest_blockhash
//! [durable transaction nonce]: https://docs.solana.com/implemented-proposals/durable-tx-nonces
//!
//! # Examples
//!
//! This example uses the [`solana_rpc_client`] and [`anyhow`] crates.
//!
//! [`solana_rpc_client`]: https://docs.rs/solana-rpc-client
//! [`anyhow`]: https://docs.rs/anyhow
//!
//! ```
//! # use solana_sdk::example_mocks::solana_rpc_client;
//! use anyhow::Result;
//! use borsh::{BorshSerialize, BorshDeserialize};
//! use solana_rpc_client::rpc_client::RpcClient;
//! use solana_sdk::{
//! instruction::Instruction,
//! message::Message,
//! pubkey::Pubkey,
//! signature::{Keypair, Signer},
//! transaction::Transaction,
//! };
//!
//! // A custom program instruction. This would typically be defined in
//! // another crate so it can be shared between the on-chain program and
//! // the client.
//! #[derive(BorshSerialize, BorshDeserialize)]
//! enum BankInstruction {
//! Initialize,
//! Deposit { lamports: u64 },
//! Withdraw { lamports: u64 },
//! }
//!
//! fn send_initialize_tx(
//! client: &RpcClient,
//! program_id: Pubkey,
//! payer: &Keypair
//! ) -> Result<()> {
//!
//! let bank_instruction = BankInstruction::Initialize;
//!
//! let instruction = Instruction::new_with_borsh(
//! program_id,
//! &bank_instruction,
//! vec![],
//! );
//!
//! let blockhash = client.get_latest_blockhash()?;
//! let mut tx = Transaction::new_signed_with_payer(
//! &[instruction],
//! Some(&payer.pubkey()),
//! &[payer],
//! blockhash,
//! );
//! client.send_and_confirm_transaction(&tx)?;
//!
//! Ok(())
//! }
//! #
//! # let client = RpcClient::new(String::new());
//! # let program_id = Pubkey::new_unique();
//! # let payer = Keypair::new();
//! # send_initialize_tx(&client, program_id, &payer)?;
//! #
//! # Ok::<(), anyhow::Error>(())
//! ```
#![cfg(feature = "full")]
use {
crate::{
hash::Hash,
instruction::{CompiledInstruction, Instruction},
message::Message,
nonce::NONCED_TX_MARKER_IX_INDEX,
precompiles::verify_if_precompile,
program_utils::limited_deserialize,
pubkey::Pubkey,
sanitize::{Sanitize, SanitizeError},
short_vec,
signature::{Signature, SignerError},
signers::Signers,
wasm_bindgen,
},
serde::Serialize,
solana_program::{system_instruction::SystemInstruction, system_program},
solana_sdk::feature_set,
std::result,
};
mod error;
mod sanitized;
mod versioned;
pub use {error::*, sanitized::*, versioned::*};
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub enum TransactionVerificationMode {
HashOnly,
HashAndVerifyPrecompiles,
FullVerification,
}
pub type Result<T> = result::Result<T, TransactionError>;
/// An atomically-commited sequence of instructions.
///
/// While [`Instruction`]s are the basic unit of computation in Solana,
/// they are submitted by clients in [`Transaction`]s containing one or
/// more instructions, and signed by one or more [`Signer`]s.
///
/// [`Signer`]: crate::signer::Signer
///
/// See the [module documentation] for more details about transactions.
///
/// [module documentation]: self
///
/// Some constructors accept an optional `payer`, the account responsible for
/// paying the cost of executing a transaction. In most cases, callers should
/// specify the payer explicitly in these constructors. In some cases though,
/// the caller is not _required_ to specify the payer, but is still allowed to:
/// in the [`Message`] structure, the first account is always the fee-payer, so
/// if the caller has knowledge that the first account of the constructed
/// transaction's `Message` is both a signer and the expected fee-payer, then
/// redundantly specifying the fee-payer is not strictly required.
#[wasm_bindgen]
#[frozen_abi(digest = "FZtncnS1Xk8ghHfKiXE5oGiUbw2wJhmfXQuNgQR3K6Mc")]
#[derive(Debug, PartialEq, Default, Eq, Clone, Serialize, Deserialize, AbiExample)]
pub struct Transaction {
/// A set of signatures of a serialized [`Message`], signed by the first
/// keys of the `Message`'s [`account_keys`], where the number of signatures
/// is equal to [`num_required_signatures`] of the `Message`'s
/// [`MessageHeader`].
///
/// [`account_keys`]: Message::account_keys
/// [`MessageHeader`]: crate::message::MessageHeader
/// [`num_required_signatures`]: crate::message::MessageHeader::num_required_signatures
// NOTE: Serialization-related changes must be paired with the direct read at sigverify.
#[wasm_bindgen(skip)]
#[serde(with = "short_vec")]
pub signatures: Vec<Signature>,
/// The message to sign.
#[wasm_bindgen(skip)]
pub message: Message,
}
impl Sanitize for Transaction {
fn sanitize(&self) -> std::result::Result<(), SanitizeError> {
if self.message.header.num_required_signatures as usize > self.signatures.len() {
return Err(SanitizeError::IndexOutOfBounds);
}
if self.signatures.len() > self.message.account_keys.len() {
return Err(SanitizeError::IndexOutOfBounds);
}
self.message.sanitize()
}
}
impl Transaction {
/// Create an unsigned transaction from a [`Message`].
///
/// # Examples
///
/// This example uses the [`solana_rpc_client`] and [`anyhow`] crates.
///
/// [`solana_rpc_client`]: https://docs.rs/solana-rpc-client
/// [`anyhow`]: https://docs.rs/anyhow
///
/// ```
/// # use solana_sdk::example_mocks::solana_rpc_client;
/// use anyhow::Result;
/// use borsh::{BorshSerialize, BorshDeserialize};
/// use solana_rpc_client::rpc_client::RpcClient;
/// use solana_sdk::{
/// instruction::Instruction,
/// message::Message,
/// pubkey::Pubkey,
/// signature::{Keypair, Signer},
/// transaction::Transaction,
/// };
///
/// // A custom program instruction. This would typically be defined in
/// // another crate so it can be shared between the on-chain program and
/// // the client.
/// #[derive(BorshSerialize, BorshDeserialize)]
/// enum BankInstruction {
/// Initialize,
/// Deposit { lamports: u64 },
/// Withdraw { lamports: u64 },
/// }
///
/// fn send_initialize_tx(
/// client: &RpcClient,
/// program_id: Pubkey,
/// payer: &Keypair
/// ) -> Result<()> {
///
/// let bank_instruction = BankInstruction::Initialize;
///
/// let instruction = Instruction::new_with_borsh(
/// program_id,
/// &bank_instruction,
/// vec![],
/// );
///
/// let message = Message::new(
/// &[instruction],
/// Some(&payer.pubkey()),
/// );
///
/// let mut tx = Transaction::new_unsigned(message);
/// let blockhash = client.get_latest_blockhash()?;
/// tx.sign(&[payer], blockhash);
/// client.send_and_confirm_transaction(&tx)?;
///
/// Ok(())
/// }
/// #
/// # let client = RpcClient::new(String::new());
/// # let program_id = Pubkey::new_unique();
/// # let payer = Keypair::new();
/// # send_initialize_tx(&client, program_id, &payer)?;
/// #
/// # Ok::<(), anyhow::Error>(())
/// ```
pub fn new_unsigned(message: Message) -> Self {
Self {
signatures: vec![Signature::default(); message.header.num_required_signatures as usize],
message,
}
}
/// Create a fully-signed transaction from a [`Message`].
///
/// # Panics
///
/// Panics when signing fails. See [`Transaction::try_sign`] and
/// [`Transaction::try_partial_sign`] for a full description of failure
/// scenarios.
///
/// # Examples
///
/// This example uses the [`solana_rpc_client`] and [`anyhow`] crates.
///
/// [`solana_rpc_client`]: https://docs.rs/solana-rpc-client
/// [`anyhow`]: https://docs.rs/anyhow
///
/// ```
/// # use solana_sdk::example_mocks::solana_rpc_client;
/// use anyhow::Result;
/// use borsh::{BorshSerialize, BorshDeserialize};
/// use solana_rpc_client::rpc_client::RpcClient;
/// use solana_sdk::{
/// instruction::Instruction,
/// message::Message,
/// pubkey::Pubkey,
/// signature::{Keypair, Signer},
/// transaction::Transaction,
/// };
///
/// // A custom program instruction. This would typically be defined in
/// // another crate so it can be shared between the on-chain program and
/// // the client.
/// #[derive(BorshSerialize, BorshDeserialize)]
/// enum BankInstruction {
/// Initialize,
/// Deposit { lamports: u64 },
/// Withdraw { lamports: u64 },
/// }
///
/// fn send_initialize_tx(
/// client: &RpcClient,
/// program_id: Pubkey,
/// payer: &Keypair
/// ) -> Result<()> {
///
/// let bank_instruction = BankInstruction::Initialize;
///
/// let instruction = Instruction::new_with_borsh(
/// program_id,
/// &bank_instruction,
/// vec![],
/// );
///
/// let message = Message::new(
/// &[instruction],
/// Some(&payer.pubkey()),
/// );
///
/// let blockhash = client.get_latest_blockhash()?;
/// let mut tx = Transaction::new(&[payer], message, blockhash);
/// client.send_and_confirm_transaction(&tx)?;
///
/// Ok(())
/// }
/// #
/// # let client = RpcClient::new(String::new());
/// # let program_id = Pubkey::new_unique();
/// # let payer = Keypair::new();
/// # send_initialize_tx(&client, program_id, &payer)?;
/// #
/// # Ok::<(), anyhow::Error>(())
/// ```
pub fn new<T: Signers>(
from_keypairs: &T,
message: Message,
recent_blockhash: Hash,
) -> Transaction {
let mut tx = Self::new_unsigned(message);
tx.sign(from_keypairs, recent_blockhash);
tx
}
/// Create an unsigned transaction from a list of [`Instruction`]s.
///
/// `payer` is the account responsible for paying the cost of executing the
/// transaction. It is typically provided, but is optional in some cases.
/// See the [`Transaction`] docs for more.
///
/// # Examples
///
/// This example uses the [`solana_rpc_client`] and [`anyhow`] crates.
///
/// [`solana_rpc_client`]: https://docs.rs/solana-rpc-client
/// [`anyhow`]: https://docs.rs/anyhow
///
/// ```
/// # use solana_sdk::example_mocks::solana_rpc_client;
/// use anyhow::Result;
/// use borsh::{BorshSerialize, BorshDeserialize};
/// use solana_rpc_client::rpc_client::RpcClient;
/// use solana_sdk::{
/// instruction::Instruction,
/// message::Message,
/// pubkey::Pubkey,
/// signature::{Keypair, Signer},
/// transaction::Transaction,
/// };
///
/// // A custom program instruction. This would typically be defined in
/// // another crate so it can be shared between the on-chain program and
/// // the client.
/// #[derive(BorshSerialize, BorshDeserialize)]
/// enum BankInstruction {
/// Initialize,
/// Deposit { lamports: u64 },
/// Withdraw { lamports: u64 },
/// }
///
/// fn send_initialize_tx(
/// client: &RpcClient,
/// program_id: Pubkey,
/// payer: &Keypair
/// ) -> Result<()> {
///
/// let bank_instruction = BankInstruction::Initialize;
///
/// let instruction = Instruction::new_with_borsh(
/// program_id,
/// &bank_instruction,
/// vec![],
/// );
///
/// let mut tx = Transaction::new_with_payer(&[instruction], Some(&payer.pubkey()));
/// let blockhash = client.get_latest_blockhash()?;
/// tx.sign(&[payer], blockhash);
/// client.send_and_confirm_transaction(&tx)?;
///
/// Ok(())
/// }
/// #
/// # let client = RpcClient::new(String::new());
/// # let program_id = Pubkey::new_unique();
/// # let payer = Keypair::new();
/// # send_initialize_tx(&client, program_id, &payer)?;
/// #
/// # Ok::<(), anyhow::Error>(())
/// ```
pub fn new_with_payer(instructions: &[Instruction], payer: Option<&Pubkey>) -> Self {
let message = Message::new(instructions, payer);
Self::new_unsigned(message)
}
/// Create a fully-signed transaction from a list of [`Instruction`]s.
///
/// `payer` is the account responsible for paying the cost of executing the
/// transaction. It is typically provided, but is optional in some cases.
/// See the [`Transaction`] docs for more.
///
/// # Panics
///
/// Panics when signing fails. See [`Transaction::try_sign`] and
/// [`Transaction::try_partial_sign`] for a full description of failure
/// scenarios.
///
/// # Examples
///
/// This example uses the [`solana_rpc_client`] and [`anyhow`] crates.
///
/// [`solana_rpc_client`]: https://docs.rs/solana-rpc-client
/// [`anyhow`]: https://docs.rs/anyhow
///
/// ```
/// # use solana_sdk::example_mocks::solana_rpc_client;
/// use anyhow::Result;
/// use borsh::{BorshSerialize, BorshDeserialize};
/// use solana_rpc_client::rpc_client::RpcClient;
/// use solana_sdk::{
/// instruction::Instruction,
/// message::Message,
/// pubkey::Pubkey,
/// signature::{Keypair, Signer},
/// transaction::Transaction,
/// };
///
/// // A custom program instruction. This would typically be defined in
/// // another crate so it can be shared between the on-chain program and
/// // the client.
/// #[derive(BorshSerialize, BorshDeserialize)]
/// enum BankInstruction {
/// Initialize,
/// Deposit { lamports: u64 },
/// Withdraw { lamports: u64 },
/// }
///
/// fn send_initialize_tx(
/// client: &RpcClient,
/// program_id: Pubkey,
/// payer: &Keypair
/// ) -> Result<()> {
///
/// let bank_instruction = BankInstruction::Initialize;
///
/// let instruction = Instruction::new_with_borsh(
/// program_id,
/// &bank_instruction,
/// vec![],
/// );
///
/// let blockhash = client.get_latest_blockhash()?;
/// let mut tx = Transaction::new_signed_with_payer(
/// &[instruction],
/// Some(&payer.pubkey()),
/// &[payer],
/// blockhash,
/// );
/// client.send_and_confirm_transaction(&tx)?;
///
/// Ok(())
/// }
/// #
/// # let client = RpcClient::new(String::new());
/// # let program_id = Pubkey::new_unique();
/// # let payer = Keypair::new();
/// # send_initialize_tx(&client, program_id, &payer)?;
/// #
/// # Ok::<(), anyhow::Error>(())
/// ```
pub fn new_signed_with_payer<T: Signers>(
instructions: &[Instruction],
payer: Option<&Pubkey>,
signing_keypairs: &T,
recent_blockhash: Hash,
) -> Self {
let message = Message::new(instructions, payer);
Self::new(signing_keypairs, message, recent_blockhash)
}
/// Create a fully-signed transaction from pre-compiled instructions.
///
/// # Arguments
///
/// * `from_keypairs` - The keys used to sign the transaction.
/// * `keys` - The keys for the transaction. These are the program state
/// instances or lamport recipient keys.
/// * `recent_blockhash` - The PoH hash.
/// * `program_ids` - The keys that identify programs used in the `instruction` vector.
/// * `instructions` - Instructions that will be executed atomically.
///
/// # Panics
///
/// Panics when signing fails. See [`Transaction::try_sign`] and for a full
/// description of failure conditions.
pub fn new_with_compiled_instructions<T: Signers>(
from_keypairs: &T,
keys: &[Pubkey],
recent_blockhash: Hash,
program_ids: Vec<Pubkey>,
instructions: Vec<CompiledInstruction>,
) -> Self {
let mut account_keys = from_keypairs.pubkeys();
let from_keypairs_len = account_keys.len();
account_keys.extend_from_slice(keys);
account_keys.extend(&program_ids);
let message = Message::new_with_compiled_instructions(
from_keypairs_len as u8,
0,
program_ids.len() as u8,
account_keys,
Hash::default(),
instructions,
);
Transaction::new(from_keypairs, message, recent_blockhash)
}
/// Get the data for an instruction at the given index.
///
/// The `instruction_index` corresponds to the [`instructions`] vector of
/// the `Transaction`'s [`Message`] value.
///
/// [`instructions`]: Message::instructions
///
/// # Panics
///
/// Panics if `instruction_index` is greater than or equal to the number of
/// instructions in the transaction.
pub fn data(&self, instruction_index: usize) -> &[u8] {
&self.message.instructions[instruction_index].data
}
fn key_index(&self, instruction_index: usize, accounts_index: usize) -> Option<usize> {
self.message
.instructions
.get(instruction_index)
.and_then(|instruction| instruction.accounts.get(accounts_index))
.map(|&account_keys_index| account_keys_index as usize)
}
/// Get the `Pubkey` of an account required by one of the instructions in
/// the transaction.
///
/// The `instruction_index` corresponds to the [`instructions`] vector of
/// the `Transaction`'s [`Message`] value; and the `account_index` to the
/// [`accounts`] vector of the message's [`CompiledInstruction`]s.
///
/// [`instructions`]: Message::instructions
/// [`accounts`]: CompiledInstruction::accounts
/// [`CompiledInstruction`]: CompiledInstruction
///
/// Returns `None` if `instruction_index` is greater than or equal to the
/// number of instructions in the transaction; or if `accounts_index` is
/// greater than or equal to the number of accounts in the instruction.
pub fn key(&self, instruction_index: usize, accounts_index: usize) -> Option<&Pubkey> {
self.key_index(instruction_index, accounts_index)
.and_then(|account_keys_index| self.message.account_keys.get(account_keys_index))
}
/// Get the `Pubkey` of a signing account required by one of the
/// instructions in the transaction.
///
/// The transaction does not need to be signed for this function to return a
/// signing account's pubkey.
///
/// Returns `None` if the indexed account is not required to sign the
/// transaction. Returns `None` if the [`signatures`] field does not contain
/// enough elements to hold a signature for the indexed account (this should
/// only be possible if `Transaction` has been manually constructed).
///
/// [`signatures`]: Transaction::signatures
///
/// Returns `None` if `instruction_index` is greater than or equal to the
/// number of instructions in the transaction; or if `accounts_index` is
/// greater than or equal to the number of accounts in the instruction.
pub fn signer_key(&self, instruction_index: usize, accounts_index: usize) -> Option<&Pubkey> {
match self.key_index(instruction_index, accounts_index) {
None => None,
Some(signature_index) => {
if signature_index >= self.signatures.len() {
return None;
}
self.message.account_keys.get(signature_index)
}
}
}
/// Return the message containing all data that should be signed.
pub fn message(&self) -> &Message {
&self.message
}
/// Return the serialized message data to sign.
pub fn message_data(&self) -> Vec<u8> {
self.message().serialize()
}
/// Sign the transaction.
///
/// This method fully signs a transaction with all required signers, which
/// must be present in the `keypairs` slice. To sign with only some of the
/// required signers, use [`Transaction::partial_sign`].
///
/// If `recent_blockhash` is different than recorded in the transaction message's
/// [`recent_blockhash`] field, then the message's `recent_blockhash` will be updated
/// to the provided `recent_blockhash`, and any prior signatures will be cleared.
///
/// [`recent_blockhash`]: Message::recent_blockhash
///
/// # Panics
///
/// Panics when signing fails. Use [`Transaction::try_sign`] to handle the
/// error. See the documentation for [`Transaction::try_sign`] for a full description of
/// failure conditions.
///
/// # Examples
///
/// This example uses the [`solana_rpc_client`] and [`anyhow`] crates.
///
/// [`solana_rpc_client`]: https://docs.rs/solana-rpc-client
/// [`anyhow`]: https://docs.rs/anyhow
///
/// ```
/// # use solana_sdk::example_mocks::solana_rpc_client;
/// use anyhow::Result;
/// use borsh::{BorshSerialize, BorshDeserialize};
/// use solana_rpc_client::rpc_client::RpcClient;
/// use solana_sdk::{
/// instruction::Instruction,
/// message::Message,
/// pubkey::Pubkey,
/// signature::{Keypair, Signer},
/// transaction::Transaction,
/// };
///
/// // A custom program instruction. This would typically be defined in
/// // another crate so it can be shared between the on-chain program and
/// // the client.
/// #[derive(BorshSerialize, BorshDeserialize)]
/// enum BankInstruction {
/// Initialize,
/// Deposit { lamports: u64 },
/// Withdraw { lamports: u64 },
/// }
///
/// fn send_initialize_tx(
/// client: &RpcClient,
/// program_id: Pubkey,
/// payer: &Keypair
/// ) -> Result<()> {
///
/// let bank_instruction = BankInstruction::Initialize;
///
/// let instruction = Instruction::new_with_borsh(
/// program_id,
/// &bank_instruction,
/// vec![],
/// );
///
/// let mut tx = Transaction::new_with_payer(&[instruction], Some(&payer.pubkey()));
/// let blockhash = client.get_latest_blockhash()?;
/// tx.sign(&[payer], blockhash);
/// client.send_and_confirm_transaction(&tx)?;
///
/// Ok(())
/// }
/// #
/// # let client = RpcClient::new(String::new());
/// # let program_id = Pubkey::new_unique();
/// # let payer = Keypair::new();
/// # send_initialize_tx(&client, program_id, &payer)?;
/// #
/// # Ok::<(), anyhow::Error>(())
/// ```
pub fn sign<T: Signers>(&mut self, keypairs: &T, recent_blockhash: Hash) {
if let Err(e) = self.try_sign(keypairs, recent_blockhash) {
panic!("Transaction::sign failed with error {e:?}");
}
}
/// Sign the transaction with a subset of required keys.
///
/// Unlike [`Transaction::sign`], this method does not require all keypairs
/// to be provided, allowing a transaction to be signed in multiple steps.
///
/// It is permitted to sign a transaction with the same keypair multiple
/// times.
///
/// If `recent_blockhash` is different than recorded in the transaction message's
/// [`recent_blockhash`] field, then the message's `recent_blockhash` will be updated
/// to the provided `recent_blockhash`, and any prior signatures will be cleared.
///
/// [`recent_blockhash`]: Message::recent_blockhash
///
/// # Panics
///
/// Panics when signing fails. Use [`Transaction::try_partial_sign`] to
/// handle the error. See the documentation for
/// [`Transaction::try_partial_sign`] for a full description of failure
/// conditions.
pub fn partial_sign<T: Signers>(&mut self, keypairs: &T, recent_blockhash: Hash) {
if let Err(e) = self.try_partial_sign(keypairs, recent_blockhash) {
panic!("Transaction::partial_sign failed with error {e:?}");
}
}
/// Sign the transaction with a subset of required keys.
///
/// This places each of the signatures created from `keypairs` in the
/// corresponding position, as specified in the `positions` vector, in the
/// transactions [`signatures`] field. It does not verify that the signature
/// positions are correct.
///
/// [`signatures`]: Transaction::signatures
///
/// # Panics
///
/// Panics if signing fails. Use [`Transaction::try_partial_sign_unchecked`]
/// to handle the error.
pub fn partial_sign_unchecked<T: Signers>(
&mut self,
keypairs: &T,
positions: Vec<usize>,
recent_blockhash: Hash,
) {
if let Err(e) = self.try_partial_sign_unchecked(keypairs, positions, recent_blockhash) {
panic!("Transaction::partial_sign_unchecked failed with error {e:?}");
}
}
/// Sign the transaction, returning any errors.
///
/// This method fully signs a transaction with all required signers, which
/// must be present in the `keypairs` slice. To sign with only some of the
/// required signers, use [`Transaction::try_partial_sign`].
///
/// If `recent_blockhash` is different than recorded in the transaction message's
/// [`recent_blockhash`] field, then the message's `recent_blockhash` will be updated
/// to the provided `recent_blockhash`, and any prior signatures will be cleared.
///
/// [`recent_blockhash`]: Message::recent_blockhash
///
/// # Errors
///
/// Signing will fail if some required signers are not provided in
/// `keypairs`; or, if the transaction has previously been partially signed,
/// some of the remaining required signers are not provided in `keypairs`.
/// In other words, the transaction must be fully signed as a result of
/// calling this function. The error is [`SignerError::NotEnoughSigners`].
///
/// Signing will fail for any of the reasons described in the documentation
/// for [`Transaction::try_partial_sign`].
///
/// # Examples
///
/// This example uses the [`solana_rpc_client`] and [`anyhow`] crates.
///
/// [`solana_rpc_client`]: https://docs.rs/solana-rpc-client
/// [`anyhow`]: https://docs.rs/anyhow
///
/// ```
/// # use solana_sdk::example_mocks::solana_rpc_client;
/// use anyhow::Result;
/// use borsh::{BorshSerialize, BorshDeserialize};
/// use solana_rpc_client::rpc_client::RpcClient;
/// use solana_sdk::{
/// instruction::Instruction,
/// message::Message,
/// pubkey::Pubkey,
/// signature::{Keypair, Signer},
/// transaction::Transaction,
/// };
///
/// // A custom program instruction. This would typically be defined in
/// // another crate so it can be shared between the on-chain program and
/// // the client.
/// #[derive(BorshSerialize, BorshDeserialize)]
/// enum BankInstruction {
/// Initialize,
/// Deposit { lamports: u64 },
/// Withdraw { lamports: u64 },
/// }
///
/// fn send_initialize_tx(
/// client: &RpcClient,
/// program_id: Pubkey,
/// payer: &Keypair
/// ) -> Result<()> {
///
/// let bank_instruction = BankInstruction::Initialize;
///
/// let instruction = Instruction::new_with_borsh(
/// program_id,
/// &bank_instruction,
/// vec![],
/// );
///
/// let mut tx = Transaction::new_with_payer(&[instruction], Some(&payer.pubkey()));
/// let blockhash = client.get_latest_blockhash()?;
/// tx.try_sign(&[payer], blockhash)?;
/// client.send_and_confirm_transaction(&tx)?;
///
/// Ok(())
/// }
/// #
/// # let client = RpcClient::new(String::new());
/// # let program_id = Pubkey::new_unique();
/// # let payer = Keypair::new();
/// # send_initialize_tx(&client, program_id, &payer)?;
/// #
/// # Ok::<(), anyhow::Error>(())
/// ```
pub fn try_sign<T: Signers>(
&mut self,
keypairs: &T,
recent_blockhash: Hash,
) -> result::Result<(), SignerError> {
self.try_partial_sign(keypairs, recent_blockhash)?;
if !self.is_signed() {
Err(SignerError::NotEnoughSigners)
} else {
Ok(())
}
}
/// Sign the transaction with a subset of required keys, returning any errors.
///
/// Unlike [`Transaction::try_sign`], this method does not require all
/// keypairs to be provided, allowing a transaction to be signed in multiple
/// steps.
///
/// It is permitted to sign a transaction with the same keypair multiple
/// times.
///
/// If `recent_blockhash` is different than recorded in the transaction message's
/// [`recent_blockhash`] field, then the message's `recent_blockhash` will be updated
/// to the provided `recent_blockhash`, and any prior signatures will be cleared.
///
/// [`recent_blockhash`]: Message::recent_blockhash
///
/// # Errors
///
/// Signing will fail if
///
/// - The transaction's [`Message`] is malformed such that the number of
/// required signatures recorded in its header
/// ([`num_required_signatures`]) is greater than the length of its
/// account keys ([`account_keys`]). The error is
/// [`SignerError::TransactionError`] where the interior
/// [`TransactionError`] is [`TransactionError::InvalidAccountIndex`].
/// - Any of the provided signers in `keypairs` is not a required signer of
/// the message. The error is [`SignerError::KeypairPubkeyMismatch`].
/// - Any of the signers is a [`Presigner`], and its provided signature is
/// incorrect. The error is [`SignerError::PresignerError`] where the
/// interior [`PresignerError`] is
/// [`PresignerError::VerificationFailure`].
/// - The signer is a [`RemoteKeypair`] and
/// - It does not understand the input provided ([`SignerError::InvalidInput`]).
/// - The device cannot be found ([`SignerError::NoDeviceFound`]).
/// - The user cancels the signing ([`SignerError::UserCancel`]).
/// - An error was encountered connecting ([`SignerError::Connection`]).
/// - Some device-specific protocol error occurs ([`SignerError::Protocol`]).
/// - Some other error occurs ([`SignerError::Custom`]).
///
/// See the documentation for the [`solana-remote-wallet`] crate for details
/// on the operation of [`RemoteKeypair`] signers.
///
/// [`num_required_signatures`]: crate::message::MessageHeader::num_required_signatures
/// [`account_keys`]: Message::account_keys
/// [`Presigner`]: crate::signer::presigner::Presigner
/// [`PresignerError`]: crate::signer::presigner::PresignerError
/// [`PresignerError::VerificationFailure`]: crate::signer::presigner::PresignerError::VerificationFailure
/// [`solana-remote-wallet`]: https://docs.rs/solana-remote-wallet/latest/
/// [`RemoteKeypair`]: https://docs.rs/solana-remote-wallet/latest/solana_remote_wallet/remote_keypair/struct.RemoteKeypair.html
pub fn try_partial_sign<T: Signers>(
&mut self,
keypairs: &T,
recent_blockhash: Hash,
) -> result::Result<(), SignerError> {
let positions = self.get_signing_keypair_positions(&keypairs.pubkeys())?;
if positions.iter().any(|pos| pos.is_none()) {
return Err(SignerError::KeypairPubkeyMismatch);
}
let positions: Vec<usize> = positions.iter().map(|pos| pos.unwrap()).collect();
self.try_partial_sign_unchecked(keypairs, positions, recent_blockhash)
}
/// Sign the transaction with a subset of required keys, returning any
/// errors.
///
/// This places each of the signatures created from `keypairs` in the
/// corresponding position, as specified in the `positions` vector, in the
/// transactions [`signatures`] field. It does not verify that the signature
/// positions are correct.
///
/// [`signatures`]: Transaction::signatures
///
/// # Errors
///
/// Returns an error if signing fails.
pub fn try_partial_sign_unchecked<T: Signers>(
&mut self,
keypairs: &T,
positions: Vec<usize>,
recent_blockhash: Hash,
) -> result::Result<(), SignerError> {
// if you change the blockhash, you're re-signing...
if recent_blockhash != self.message.recent_blockhash {
self.message.recent_blockhash = recent_blockhash;
self.signatures
.iter_mut()
.for_each(|signature| *signature = Signature::default());
}
let signatures = keypairs.try_sign_message(&self.message_data())?;
for i in 0..positions.len() {
self.signatures[positions[i]] = signatures[i];
}
Ok(())
}
/// Returns a signature that is not valid for signing this transaction.
pub fn get_invalid_signature() -> Signature {
Signature::default()
}
/// Verifies that all signers have signed the message.
///
/// # Errors
///
/// Returns [`TransactionError::SignatureFailure`] on error.
pub fn verify(&self) -> Result<()> {
let message_bytes = self.message_data();
if !self
._verify_with_results(&message_bytes)
.iter()
.all(|verify_result| *verify_result)
{
Err(TransactionError::SignatureFailure)
} else {
Ok(())
}
}
/// Verify the transaction and hash its message.
///
/// # Errors
///
/// Returns [`TransactionError::SignatureFailure`] on error.
pub fn verify_and_hash_message(&self) -> Result<Hash> {
let message_bytes = self.message_data();
if !self
._verify_with_results(&message_bytes)
.iter()
.all(|verify_result| *verify_result)
{
Err(TransactionError::SignatureFailure)
} else {
Ok(Message::hash_raw_message(&message_bytes))
}
}
/// Verifies that all signers have signed the message.
///
/// Returns a vector with the length of required signatures, where each
/// element is either `true` if that signer has signed, or `false` if not.