sp-statement-store 26.0.0

A crate which contains primitives related to the statement store
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// 	http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![cfg_attr(not(feature = "std"), no_std)]
#![warn(missing_docs)]

//! A crate which contains statement-store primitives.

extern crate alloc;

use alloc::vec::Vec;
use codec::{Compact, Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
use core::ops::Deref;
use scale_info::{build::Fields, Path, Type, TypeInfo};
use sp_application_crypto::RuntimeAppPublic;
#[cfg(feature = "std")]
use sp_core::Pair;

/// Statement topic.
///
/// A 32-byte topic identifier that serializes as a hex string (like `sp_core::Bytes`).
#[derive(
	Clone,
	Copy,
	Debug,
	Default,
	PartialEq,
	Eq,
	PartialOrd,
	Ord,
	Hash,
	Encode,
	Decode,
	DecodeWithMemTracking,
	MaxEncodedLen,
	TypeInfo,
)]
pub struct Topic(pub [u8; 32]);

#[cfg(feature = "serde")]
impl serde::Serialize for Topic {
	fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
	where
		S: serde::Serializer,
	{
		sp_core::bytes::serialize(&self.0, serializer)
	}
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for Topic {
	fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
	where
		D: serde::Deserializer<'de>,
	{
		let mut arr = [0u8; 32];
		sp_core::bytes::deserialize_check_len(
			deserializer,
			sp_core::bytes::ExpectedLen::Exact(&mut arr[..]),
		)?;
		Ok(Topic(arr))
	}
}

impl From<[u8; 32]> for Topic {
	fn from(inner: [u8; 32]) -> Self {
		Topic(inner)
	}
}

impl From<Topic> for [u8; 32] {
	fn from(topic: Topic) -> Self {
		topic.0
	}
}

impl AsRef<[u8; 32]> for Topic {
	fn as_ref(&self) -> &[u8; 32] {
		&self.0
	}
}

impl AsRef<[u8]> for Topic {
	fn as_ref(&self) -> &[u8] {
		&self.0
	}
}

impl Deref for Topic {
	type Target = [u8; 32];

	fn deref(&self) -> &Self::Target {
		&self.0
	}
}

/// Decryption key identifier.
pub type DecryptionKey = [u8; 32];
/// Statement hash.
pub type Hash = [u8; 32];
/// Block hash.
pub type BlockHash = [u8; 32];
/// Account id
pub type AccountId = [u8; 32];
/// Statement channel.
pub type Channel = [u8; 32];

/// Total number of topic fields allowed in a statement and in `MatchAll` filters.
pub const MAX_TOPICS: usize = 4;
/// `MatchAny` allows to provide a list of topics match against. This is the maximum number of
/// topics allowed.
pub const MAX_ANY_TOPICS: usize = 128;

/// Statement allowance limits for an account.
#[derive(Clone, Default, PartialEq, Eq, Encode, Decode, DecodeWithMemTracking, Debug, TypeInfo)]
pub struct StatementAllowance {
	/// Maximum number of statements allowed
	pub max_count: u32,
	/// Maximum total size of statements in bytes
	pub max_size: u32,
}

impl StatementAllowance {
	/// Create a new statement allowance.
	pub fn new(max_count: u32, max_size: u32) -> Self {
		Self { max_count, max_size }
	}

	/// Saturating addition of statement allowances.
	pub const fn saturating_add(self, rhs: StatementAllowance) -> StatementAllowance {
		StatementAllowance {
			max_count: self.max_count.saturating_add(rhs.max_count),
			max_size: self.max_size.saturating_add(rhs.max_size),
		}
	}

	/// Saturating subtraction of statement allowances.
	pub const fn saturating_sub(self, rhs: StatementAllowance) -> StatementAllowance {
		StatementAllowance {
			max_count: self.max_count.saturating_sub(rhs.max_count),
			max_size: self.max_size.saturating_sub(rhs.max_size),
		}
	}

	/// Check if the statement allowance is depleted.
	pub fn is_depleted(&self) -> bool {
		self.max_count == 0 || self.max_size == 0
	}
}

/// Storage key prefix for per-account statement allowances.
pub const STATEMENT_ALLOWANCE_PREFIX: &[u8] = b":statement_allowance:";

/// Constructs a per-account statement allowance storage key.
///
/// # Arguments
/// * `account_id` - Account identifier as byte slice
///
/// # Returns
/// Storage key: `":statement_allowance:" ++ account_id`
pub fn statement_allowance_key(account_id: impl AsRef<[u8]>) -> Vec<u8> {
	let mut key = STATEMENT_ALLOWANCE_PREFIX.to_vec();
	key.extend_from_slice(account_id.as_ref());
	key
}

/// Increase the statement allowance by the given amount.
pub fn increase_allowance_by(account_id: impl AsRef<[u8]>, by: StatementAllowance) {
	let key = statement_allowance_key(account_id);
	let mut allowance: StatementAllowance = frame_support::storage::unhashed::get_or_default(&key);
	allowance = allowance.saturating_add(by);
	frame_support::storage::unhashed::put(&key, &allowance);
}

/// Decrease the statement allowance by the given amount.
pub fn decrease_allowance_by(account_id: impl AsRef<[u8]>, by: StatementAllowance) {
	let key = statement_allowance_key(account_id);
	let mut allowance: StatementAllowance = frame_support::storage::unhashed::get_or_default(&key);
	allowance = allowance.saturating_sub(by);
	if allowance.is_depleted() {
		frame_support::storage::unhashed::kill(&key);
	} else {
		frame_support::storage::unhashed::put(&key, &allowance);
	}
}

/// Get the statement allowance for the given account.
pub fn get_allowance(account_id: impl AsRef<[u8]>) -> StatementAllowance {
	let key = statement_allowance_key(account_id);
	frame_support::storage::unhashed::get_or_default(&key)
}

#[cfg(feature = "std")]
pub use store_api::{
	Error, FilterDecision, InvalidReason, OptimizedTopicFilter, RejectionReason, Result,
	StatementEvent, StatementSource, StatementStore, SubmitResult, TopicFilter,
};

#[cfg(feature = "std")]
mod ecies;
pub mod runtime_api;
#[cfg(feature = "std")]
mod store_api;

mod sr25519 {
	mod app_sr25519 {
		use sp_application_crypto::{app_crypto, key_types::STATEMENT, sr25519};
		app_crypto!(sr25519, STATEMENT);
	}
	pub type Public = app_sr25519::Public;
}

/// Statement-store specific ed25519 crypto primitives.
pub mod ed25519 {
	mod app_ed25519 {
		use sp_application_crypto::{app_crypto, ed25519, key_types::STATEMENT};
		app_crypto!(ed25519, STATEMENT);
	}
	/// Statement-store specific ed25519 public key.
	pub type Public = app_ed25519::Public;
	/// Statement-store specific ed25519 key pair.
	#[cfg(feature = "std")]
	pub type Pair = app_ed25519::Pair;
}

mod ecdsa {
	mod app_ecdsa {
		use sp_application_crypto::{app_crypto, ecdsa, key_types::STATEMENT};
		app_crypto!(ecdsa, STATEMENT);
	}
	pub type Public = app_ecdsa::Public;
}

/// Returns blake2-256 hash for the encoded statement.
#[cfg(feature = "std")]
pub fn hash_encoded(data: &[u8]) -> [u8; 32] {
	sp_crypto_hashing::blake2_256(data)
}

/// Statement proof.
#[derive(
	Encode, Decode, DecodeWithMemTracking, MaxEncodedLen, TypeInfo, Debug, Clone, PartialEq, Eq,
)]
pub enum Proof {
	/// Sr25519 Signature.
	Sr25519 {
		/// Signature.
		signature: [u8; 64],
		/// Public key.
		signer: [u8; 32],
	},
	/// Ed25519 Signature.
	Ed25519 {
		/// Signature.
		signature: [u8; 64],
		/// Public key.
		signer: [u8; 32],
	},
	/// Secp256k1 Signature.
	Secp256k1Ecdsa {
		/// Signature.
		signature: [u8; 65],
		/// Public key.
		signer: [u8; 33],
	},
	/// On-chain event proof.
	OnChain {
		/// Account identifier associated with the event.
		who: AccountId,
		/// Hash of block that contains the event.
		block_hash: BlockHash,
		/// Index of the event in the event list.
		event_index: u64,
	},
}

impl Proof {
	/// Return account id for the proof creator.
	pub fn account_id(&self) -> AccountId {
		match self {
			Proof::Sr25519 { signer, .. } => *signer,
			Proof::Ed25519 { signer, .. } => *signer,
			Proof::Secp256k1Ecdsa { signer, .. } => {
				<sp_runtime::traits::BlakeTwo256 as sp_core::Hasher>::hash(signer).into()
			},
			Proof::OnChain { who, .. } => *who,
		}
	}
}

/// Statement attributes. Each statement is a list of 0 or more fields. Fields may only appear once
/// and in the order declared here.
#[derive(Encode, Decode, TypeInfo, Debug, Clone, PartialEq, Eq)]
#[repr(u8)]
pub enum Field {
	/// Statement proof.
	AuthenticityProof(Proof) = 0,
	/// An identifier for the key that `Data` field may be decrypted with.
	DecryptionKey(DecryptionKey) = 1,
	/// Expiry of the statement. See [`Statement::expiry`] for details on the format.
	Expiry(u64) = 2,
	/// Account channel to use. Only one message per `(account, channel)` pair is allowed.
	Channel(Channel) = 3,
	/// First statement topic.
	Topic1(Topic) = 4,
	/// Second statement topic.
	Topic2(Topic) = 5,
	/// Third statement topic.
	Topic3(Topic) = 6,
	/// Fourth statement topic.
	Topic4(Topic) = 7,
	/// Additional data.
	Data(Vec<u8>) = 8,
}

impl Field {
	fn discriminant(&self) -> u8 {
		// This is safe for repr(u8)
		// see https://doc.rust-lang.org/reference/items/enumerations.html#pointer-casting
		unsafe { *(self as *const Self as *const u8) }
	}
}

/// Statement structure.
#[derive(DecodeWithMemTracking, Debug, Clone, PartialEq, Eq, Default)]
pub struct Statement {
	/// Proof used for authorizing the statement.
	proof: Option<Proof>,
	/// An identifier for the key that `Data` field may be decrypted with.
	#[deprecated(note = "Experimental feature, may be removed/changed in future releases")]
	decryption_key: Option<DecryptionKey>,
	/// Used for identifying a distinct communication channel, only a message per channel is
	/// stored.
	///
	/// This can be used to implement message replacement, submitting a new message with a
	/// different topic/data on the same channel and a greater expiry replaces the previous one.
	///
	/// If the new statement data is bigger than the old one, submitting a statement with the same
	/// channel does not guarantee that **ONLY** the old one will be replaced, as it might not fit
	/// in the account quota. In that case, other statements from the same account with the lowest
	/// expiry will be removed.
	channel: Option<Channel>,
	/// Message expiry, used for determining which statements to keep.
	///
	/// The most significant 32 bits represents the expiration timestamp (in seconds since
	/// UNIX epoch) after which the statement gets removed. These ensure that statements with a
	/// higher expiration time have a higher priority.
	/// The lower 32 bits represents an arbitrary sequence number used to order statements with the
	/// same expiration time.
	///
	/// Higher values indicate a higher priority.
	/// This is used in two cases:
	/// 1) When an account exceeds its quota and some statements need to be removed. Statements
	///    with the lowest `expiry` are removed first.
	/// 2) When multiple statements are submitted on the same channel, the one with the highest
	///    expiry replaces the one with the same channel.
	expiry: u64,
	/// Number of topics present.
	num_topics: u8,
	/// Topics, used for querying and filtering statements.
	topics: [Topic; MAX_TOPICS],
	/// Statement data.
	data: Option<Vec<u8>>,
}

/// Note: The `TypeInfo` implementation reflects the actual encoding format (`Vec<Field>`)
/// rather than the struct fields, since `Statement` has custom `Encode`/`Decode` implementations.
impl TypeInfo for Statement {
	type Identity = Self;

	fn type_info() -> Type {
		// Statement encodes as Vec<Field>, so we report the same type info
		Type::builder()
			.path(Path::new("Statement", module_path!()))
			.docs(&["Statement structure"])
			.composite(Fields::unnamed().field(|f| f.ty::<Vec<Field>>()))
	}
}

impl Decode for Statement {
	fn decode<I: codec::Input>(input: &mut I) -> core::result::Result<Self, codec::Error> {
		// Encoding matches that of Vec<Field>. Basically this just means accepting that there
		// will be a prefix of vector length.
		let num_fields: codec::Compact<u32> = Decode::decode(input)?;
		let mut tag = 0;
		let mut statement = Statement::new();
		for i in 0..num_fields.into() {
			let field: Field = Decode::decode(input)?;
			if i > 0 && field.discriminant() <= tag {
				return Err("Invalid field order or duplicate fields".into());
			}
			tag = field.discriminant();
			match field {
				Field::AuthenticityProof(p) => statement.set_proof(p),
				Field::DecryptionKey(key) => statement.set_decryption_key(key),
				Field::Expiry(p) => statement.set_expiry(p),
				Field::Channel(c) => statement.set_channel(c),
				Field::Topic1(t) => statement.set_topic(0, t),
				Field::Topic2(t) => statement.set_topic(1, t),
				Field::Topic3(t) => statement.set_topic(2, t),
				Field::Topic4(t) => statement.set_topic(3, t),
				Field::Data(data) => statement.set_plain_data(data),
			}
		}
		Ok(statement)
	}
}

impl Encode for Statement {
	fn encode(&self) -> Vec<u8> {
		self.encoded(false)
	}
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
/// Result returned by `Statement::verify_signature`
pub enum SignatureVerificationResult {
	/// Signature is valid and matches this account id.
	Valid(AccountId),
	/// Signature has failed verification.
	Invalid,
	/// No signature in the proof or no proof.
	NoSignature,
}

impl Statement {
	/// Create a new empty statement with no proof.
	pub fn new() -> Statement {
		Default::default()
	}

	/// Create a new statement with a proof.
	pub fn new_with_proof(proof: Proof) -> Statement {
		let mut statement = Self::new();
		statement.set_proof(proof);
		statement
	}

	/// Sign with a key that matches given public key in the keystore.
	///
	/// Returns `true` if signing worked (private key present etc).
	///
	/// NOTE: This can only be called from the runtime.
	pub fn sign_sr25519_public(&mut self, key: &sr25519::Public) -> bool {
		let to_sign = self.signature_material();
		if let Some(signature) = key.sign(&to_sign) {
			let proof = Proof::Sr25519 {
				signature: signature.into_inner().into(),
				signer: key.clone().into_inner().into(),
			};
			self.set_proof(proof);
			true
		} else {
			false
		}
	}

	/// Returns slice of all topics set in the statement.
	pub fn topics(&self) -> &[Topic] {
		&self.topics[..self.num_topics as usize]
	}

	/// Sign with a given private key and add the signature proof field.
	#[cfg(feature = "std")]
	pub fn sign_sr25519_private(&mut self, key: &sp_core::sr25519::Pair) {
		let to_sign = self.signature_material();
		let proof =
			Proof::Sr25519 { signature: key.sign(&to_sign).into(), signer: key.public().into() };
		self.set_proof(proof);
	}

	/// Sign with a key that matches given public key in the keystore.
	///
	/// Returns `true` if signing worked (private key present etc).
	///
	/// NOTE: This can only be called from the runtime.
	pub fn sign_ed25519_public(&mut self, key: &ed25519::Public) -> bool {
		let to_sign = self.signature_material();
		if let Some(signature) = key.sign(&to_sign) {
			let proof = Proof::Ed25519 {
				signature: signature.into_inner().into(),
				signer: key.clone().into_inner().into(),
			};
			self.set_proof(proof);
			true
		} else {
			false
		}
	}

	/// Sign with a given private key and add the signature proof field.
	#[cfg(feature = "std")]
	pub fn sign_ed25519_private(&mut self, key: &sp_core::ed25519::Pair) {
		let to_sign = self.signature_material();
		let proof =
			Proof::Ed25519 { signature: key.sign(&to_sign).into(), signer: key.public().into() };
		self.set_proof(proof);
	}

	/// Sign with a key that matches given public key in the keystore.
	///
	/// Returns `true` if signing worked (private key present etc).
	///
	/// NOTE: This can only be called from the runtime.
	///
	/// Returns `true` if signing worked (private key present etc).
	///
	/// NOTE: This can only be called from the runtime.
	pub fn sign_ecdsa_public(&mut self, key: &ecdsa::Public) -> bool {
		let to_sign = self.signature_material();
		if let Some(signature) = key.sign(&to_sign) {
			let proof = Proof::Secp256k1Ecdsa {
				signature: signature.into_inner().into(),
				signer: key.clone().into_inner().0,
			};
			self.set_proof(proof);
			true
		} else {
			false
		}
	}

	/// Sign with a given private key and add the signature proof field.
	#[cfg(feature = "std")]
	pub fn sign_ecdsa_private(&mut self, key: &sp_core::ecdsa::Pair) {
		let to_sign = self.signature_material();
		let proof =
			Proof::Secp256k1Ecdsa { signature: key.sign(&to_sign).into(), signer: key.public().0 };
		self.set_proof(proof);
	}

	/// Check proof signature, if any.
	pub fn verify_signature(&self) -> SignatureVerificationResult {
		use sp_runtime::traits::Verify;

		match self.proof() {
			Some(Proof::OnChain { .. }) | None => SignatureVerificationResult::NoSignature,
			Some(Proof::Sr25519 { signature, signer }) => {
				let to_sign = self.signature_material();
				let signature = sp_core::sr25519::Signature::from(*signature);
				let public = sp_core::sr25519::Public::from(*signer);
				if signature.verify(to_sign.as_slice(), &public) {
					SignatureVerificationResult::Valid(*signer)
				} else {
					SignatureVerificationResult::Invalid
				}
			},
			Some(Proof::Ed25519 { signature, signer }) => {
				let to_sign = self.signature_material();
				let signature = sp_core::ed25519::Signature::from(*signature);
				let public = sp_core::ed25519::Public::from(*signer);
				if signature.verify(to_sign.as_slice(), &public) {
					SignatureVerificationResult::Valid(*signer)
				} else {
					SignatureVerificationResult::Invalid
				}
			},
			Some(Proof::Secp256k1Ecdsa { signature, signer }) => {
				let to_sign = self.signature_material();
				let signature = sp_core::ecdsa::Signature::from(*signature);
				let public = sp_core::ecdsa::Public::from(*signer);
				if signature.verify(to_sign.as_slice(), &public) {
					let sender_hash =
						<sp_runtime::traits::BlakeTwo256 as sp_core::Hasher>::hash(signer);
					SignatureVerificationResult::Valid(sender_hash.into())
				} else {
					SignatureVerificationResult::Invalid
				}
			},
		}
	}

	/// Calculate statement hash.
	#[cfg(feature = "std")]
	pub fn hash(&self) -> [u8; 32] {
		self.using_encoded(hash_encoded)
	}

	/// Returns a topic by topic index.
	pub fn topic(&self, index: usize) -> Option<Topic> {
		if index < self.num_topics as usize {
			Some(self.topics[index])
		} else {
			None
		}
	}

	/// Returns decryption key if any.
	#[allow(deprecated)]
	pub fn decryption_key(&self) -> Option<DecryptionKey> {
		self.decryption_key
	}

	/// Convert to internal data.
	pub fn into_data(self) -> Option<Vec<u8>> {
		self.data
	}

	/// Get a reference to the statement proof, if any.
	pub fn proof(&self) -> Option<&Proof> {
		self.proof.as_ref()
	}

	/// Get proof account id, if any
	pub fn account_id(&self) -> Option<AccountId> {
		self.proof.as_ref().map(Proof::account_id)
	}

	/// Get plain data.
	pub fn data(&self) -> Option<&Vec<u8>> {
		self.data.as_ref()
	}

	/// Get plain data len.
	pub fn data_len(&self) -> usize {
		self.data().map_or(0, Vec::len)
	}

	/// Get channel, if any.
	pub fn channel(&self) -> Option<Channel> {
		self.channel
	}

	/// Get expiry.
	pub fn expiry(&self) -> u64 {
		self.expiry
	}

	/// Get expiration timestamp in seconds.
	///
	/// The expiration timestamp in seconds is stored in the most significant 32 bits of the expiry
	/// field.
	pub fn get_expiration_timestamp_secs(&self) -> u32 {
		(self.expiry >> 32) as u32
	}

	/// Return encoded fields that can be signed to construct or verify a proof
	fn signature_material(&self) -> Vec<u8> {
		self.encoded(true)
	}

	/// Remove the proof of this statement.
	pub fn remove_proof(&mut self) {
		self.proof = None;
	}

	/// Set statement proof. Any existing proof is overwritten.
	pub fn set_proof(&mut self, proof: Proof) {
		self.proof = Some(proof)
	}

	/// Set statement expiry.
	pub fn set_expiry(&mut self, expiry: u64) {
		self.expiry = expiry;
	}

	/// Set statement expiry from its parts. See [`Statement::expiry`] for details on the format.
	pub fn set_expiry_from_parts(&mut self, expiration_timestamp_secs: u32, sequence_number: u32) {
		self.expiry = (expiration_timestamp_secs as u64) << 32 | sequence_number as u64;
	}

	/// Set statement channel.
	pub fn set_channel(&mut self, channel: Channel) {
		self.channel = Some(channel)
	}

	/// Set topic by index. Does noting if index is over `MAX_TOPICS`.
	pub fn set_topic(&mut self, index: usize, topic: Topic) {
		if index < MAX_TOPICS {
			self.topics[index] = topic;
			self.num_topics = self.num_topics.max(index as u8 + 1);
		}
	}

	/// Set decryption key.
	#[allow(deprecated)]
	pub fn set_decryption_key(&mut self, key: DecryptionKey) {
		self.decryption_key = Some(key);
	}

	/// Set unencrypted statement data.
	pub fn set_plain_data(&mut self, data: Vec<u8>) {
		self.data = Some(data)
	}

	/// Estimate the encoded size for preallocation.
	///
	/// Returns a close approximation of the SCALE-encoded size without actually performing the
	/// encoding. Uses max_encoded_len() for type sizes:
	/// - Compact length prefix: max_encoded_len() bytes
	/// - Proof field: 1 (tag) + max_encoded_len()
	/// - DecryptionKey: 1 (tag) + max_encoded_len()
	/// - Expiry: 1 (tag) + max_encoded_len()
	/// - Channel: 1 (tag) + max_encoded_len()
	/// - Each topic: 1 (tag) + max_encoded_len()
	/// - Data: 1 (tag) + max_encoded_len() (compact len) + data.len()
	#[allow(deprecated)]
	fn estimated_encoded_size(&self, for_signing: bool) -> usize {
		let proof_size =
			if !for_signing && self.proof.is_some() { 1 + Proof::max_encoded_len() } else { 0 };
		let decryption_key_size =
			if self.decryption_key.is_some() { 1 + DecryptionKey::max_encoded_len() } else { 0 };
		let expiry_size = 1 + u64::max_encoded_len();
		let channel_size = if self.channel.is_some() { 1 + Channel::max_encoded_len() } else { 0 };
		let topics_size = self.num_topics as usize * (1 + Topic::max_encoded_len());
		let data_size = self
			.data
			.as_ref()
			.map_or(0, |d| 1 + Compact::<u32>::max_encoded_len() + d.len());
		let compact_prefix_size = if !for_signing { Compact::<u32>::max_encoded_len() } else { 0 };

		compact_prefix_size +
			proof_size +
			decryption_key_size +
			expiry_size +
			channel_size +
			topics_size +
			data_size
	}

	#[allow(deprecated)]
	fn encoded(&self, for_signing: bool) -> Vec<u8> {
		// Encoding matches that of Vec<Field>. Basically this just means accepting that there
		// will be a prefix of vector length.
		// Expiry field is always present.
		let num_fields = if !for_signing && self.proof.is_some() { 2 } else { 1 } +
			if self.decryption_key.is_some() { 1 } else { 0 } +
			if self.channel.is_some() { 1 } else { 0 } +
			if self.data.is_some() { 1 } else { 0 } +
			self.num_topics as u32;

		let mut output = Vec::with_capacity(self.estimated_encoded_size(for_signing));
		// When encoding signature payload, the length prefix is omitted.
		// This is so that the signature for encoded statement can potentially be derived without
		// needing to re-encode the statement.
		if !for_signing {
			let compact_len = codec::Compact::<u32>(num_fields);
			compact_len.encode_to(&mut output);

			if let Some(proof) = &self.proof {
				0u8.encode_to(&mut output);
				proof.encode_to(&mut output);
			}
		}
		if let Some(decryption_key) = &self.decryption_key {
			1u8.encode_to(&mut output);
			decryption_key.encode_to(&mut output);
		}

		2u8.encode_to(&mut output);
		self.expiry().encode_to(&mut output);

		if let Some(channel) = &self.channel {
			3u8.encode_to(&mut output);
			channel.encode_to(&mut output);
		}
		for t in 0..self.num_topics {
			(4u8 + t).encode_to(&mut output);
			self.topics[t as usize].encode_to(&mut output);
		}
		if let Some(data) = &self.data {
			8u8.encode_to(&mut output);
			data.encode_to(&mut output);
		}
		output
	}

	/// Encrypt give data with given key and store both in the statements.
	#[allow(deprecated)]
	#[cfg(feature = "std")]
	pub fn encrypt(
		&mut self,
		data: &[u8],
		key: &sp_core::ed25519::Public,
	) -> core::result::Result<(), ecies::Error> {
		let encrypted = ecies::encrypt_ed25519(key, data)?;
		self.data = Some(encrypted);
		self.decryption_key = Some((*key).into());
		Ok(())
	}

	/// Decrypt data (if any) with the given private key.
	#[cfg(feature = "std")]
	pub fn decrypt_private(
		&self,
		key: &sp_core::ed25519::Pair,
	) -> core::result::Result<Option<Vec<u8>>, ecies::Error> {
		self.data.as_ref().map(|d| ecies::decrypt_ed25519(key, d)).transpose()
	}
}

#[cfg(test)]
mod test {
	use crate::{
		hash_encoded, Field, Proof, SignatureVerificationResult, Statement, Topic, MAX_TOPICS,
	};
	use codec::{Decode, Encode};
	use scale_info::{MetaType, TypeInfo};
	use sp_application_crypto::Pair;
	use sp_core::sr25519;

	#[test]
	fn statement_encoding_matches_vec() {
		let mut statement = Statement::new();
		assert!(statement.proof().is_none());
		let proof = Proof::OnChain { who: [42u8; 32], block_hash: [24u8; 32], event_index: 66 };

		let decryption_key = [0xde; 32];
		let topic1: Topic = [0x01; 32].into();
		let topic2: Topic = [0x02; 32].into();
		let data = vec![55, 99];
		let expiry = 999;
		let channel = [0xcc; 32];

		statement.set_proof(proof.clone());
		statement.set_decryption_key(decryption_key);
		statement.set_expiry(expiry);
		statement.set_channel(channel);
		statement.set_topic(0, topic1);
		statement.set_topic(1, topic2);
		statement.set_plain_data(data.clone());

		statement.set_topic(5, [0x55; 32].into());
		assert_eq!(statement.topic(5), None);

		let fields = vec![
			Field::AuthenticityProof(proof.clone()),
			Field::DecryptionKey(decryption_key),
			Field::Expiry(expiry),
			Field::Channel(channel),
			Field::Topic1(topic1),
			Field::Topic2(topic2),
			Field::Data(data.clone()),
		];

		let encoded = statement.encode();
		assert_eq!(statement.hash(), hash_encoded(&encoded));
		assert_eq!(encoded, fields.encode());

		let decoded = Statement::decode(&mut encoded.as_slice()).unwrap();
		assert_eq!(decoded, statement);
	}

	#[test]
	fn decode_checks_fields() {
		let topic1: Topic = [0x01; 32].into();
		let topic2: Topic = [0x02; 32].into();
		let priority = 999;

		let dup_topic1 = vec![
			Field::Expiry(priority),
			Field::Topic1(topic1),
			Field::Topic1(topic1),
			Field::Topic2(topic2),
		]
		.encode();
		assert!(Statement::decode(&mut dup_topic1.as_slice()).is_err());

		let topic1_before_expiry =
			vec![Field::Topic1(topic1), Field::Expiry(priority), Field::Topic2(topic2)].encode();
		assert!(Statement::decode(&mut topic1_before_expiry.as_slice()).is_err());

		let dup_expiry = vec![Field::Expiry(1), Field::Expiry(2)].encode();
		assert!(Statement::decode(&mut dup_expiry.as_slice()).is_err());

		let dup_data = vec![Field::Data(vec![1]), Field::Data(vec![2])].encode();
		assert!(Statement::decode(&mut dup_data.as_slice()).is_err());

		let data_before_expiry = vec![Field::Data(vec![1]), Field::Expiry(42)].encode();
		assert!(Statement::decode(&mut data_before_expiry.as_slice()).is_err());

		let channel_before_expiry = vec![Field::Channel([0; 32]), Field::Expiry(1)].encode();
		assert!(Statement::decode(&mut channel_before_expiry.as_slice()).is_err());

		let topic2_before_topic1 =
			vec![Field::Expiry(1), Field::Topic2(topic1), Field::Topic1(topic2)].encode();
		assert!(Statement::decode(&mut topic2_before_topic1.as_slice()).is_err());
	}

	#[test]
	fn decode_rejects_malformed_bytes() {
		assert!(Statement::decode(&mut &[][..]).is_err());

		// Take a valid encoded statement and corrupt it in different ways
		let valid = vec![Field::Expiry(42)].encode();
		let decoded = Statement::decode(&mut valid.as_slice()).unwrap();
		assert_eq!(decoded.expiry(), 42);

		// Truncate to just the length prefix
		assert!(Statement::decode(&mut &valid[..1][..]).is_err());

		// Replace field discriminant with invalid value (Field only has 0..=8)
		let mut invalid_discriminant = valid.clone();
		invalid_discriminant[1] = 9;
		assert!(Statement::decode(&mut invalid_discriminant.as_slice()).is_err());

		invalid_discriminant[1] = 255;
		assert!(Statement::decode(&mut invalid_discriminant.as_slice()).is_err());

		// Truncate the Expiry payload (need 8 bytes for u64, provide fewer)
		assert!(Statement::decode(&mut &valid[..5][..]).is_err());

		// Encode a statement with Proof, then corrupt the Proof variant
		let with_proof = vec![
			Field::AuthenticityProof(Proof::OnChain {
				who: [0u8; 32],
				block_hash: [0u8; 32],
				event_index: 0,
			}),
			Field::Expiry(42),
		]
		.encode();
		assert!(Statement::decode(&mut with_proof.as_slice()).is_ok());

		let mut invalid_proof_variant = with_proof.clone();
		invalid_proof_variant[2] = 99;
		assert!(Statement::decode(&mut invalid_proof_variant.as_slice()).is_err());

		// Truncate the Proof payload
		assert!(Statement::decode(&mut &with_proof[..6][..]).is_err());

		// Claim more fields than actually present
		let mut inflated_count = valid.clone();
		inflated_count[0] = 5 << 2; // change field count from 1 to 5
		assert!(Statement::decode(&mut inflated_count.as_slice()).is_err());
	}

	#[test]
	fn sign_and_verify() {
		let mut statement = Statement::new();
		statement.set_plain_data(vec![42]);

		let sr25519_kp = sp_core::sr25519::Pair::from_string("//Alice", None).unwrap();
		let ed25519_kp = sp_core::ed25519::Pair::from_string("//Alice", None).unwrap();
		let secp256k1_kp = sp_core::ecdsa::Pair::from_string("//Alice", None).unwrap();

		statement.sign_sr25519_private(&sr25519_kp);
		assert_eq!(
			statement.verify_signature(),
			SignatureVerificationResult::Valid(sr25519_kp.public().0)
		);

		statement.sign_ed25519_private(&ed25519_kp);
		assert_eq!(
			statement.verify_signature(),
			SignatureVerificationResult::Valid(ed25519_kp.public().0)
		);

		statement.sign_ecdsa_private(&secp256k1_kp);
		assert_eq!(
			statement.verify_signature(),
			SignatureVerificationResult::Valid(sp_crypto_hashing::blake2_256(
				&secp256k1_kp.public().0
			))
		);

		// set an invalid Sr25519 signature
		statement.set_proof(Proof::Sr25519 { signature: [0u8; 64], signer: [0u8; 32] });
		assert_eq!(statement.verify_signature(), SignatureVerificationResult::Invalid);

		// set an invalid Ed25519 signature
		statement.set_proof(Proof::Ed25519 { signature: [0xAB; 64], signer: [0xCD; 32] });
		assert_eq!(statement.verify_signature(), SignatureVerificationResult::Invalid);

		// set an invalid Secp256k1Ecdsa signature
		statement.set_proof(Proof::Secp256k1Ecdsa { signature: [0u8; 65], signer: [0u8; 33] });
		assert_eq!(statement.verify_signature(), SignatureVerificationResult::Invalid);

		statement.remove_proof();
		assert_eq!(statement.verify_signature(), SignatureVerificationResult::NoSignature);
	}

	#[test]
	fn encrypt_decrypt() {
		let mut statement = Statement::new();
		let (pair, _) = sp_core::ed25519::Pair::generate();
		let plain = b"test data".to_vec();

		// let sr25519_kp = sp_core::sr25519::Pair::from_string("//Alice", None).unwrap();
		statement.encrypt(&plain, &pair.public()).unwrap();
		assert_ne!(plain.as_slice(), statement.data().unwrap().as_slice());

		let decrypted = statement.decrypt_private(&pair).unwrap();
		assert_eq!(decrypted, Some(plain));
	}

	#[test]
	fn check_matches() {
		let mut statement = Statement::new();
		let topic1: Topic = [0x01; 32].into();
		let topic2: Topic = [0x02; 32].into();
		let topic3: Topic = [0x03; 32].into();

		statement.set_topic(0, topic1);
		statement.set_topic(1, topic2);

		let filter_any = crate::OptimizedTopicFilter::Any;
		assert!(filter_any.matches(&statement));

		let filter_all =
			crate::OptimizedTopicFilter::MatchAll([topic1, topic2].iter().cloned().collect());
		assert!(filter_all.matches(&statement));

		let filter_all_fail =
			crate::OptimizedTopicFilter::MatchAll([topic1, topic3].iter().cloned().collect());
		assert!(!filter_all_fail.matches(&statement));

		let filter_any_match =
			crate::OptimizedTopicFilter::MatchAny([topic2, topic3].iter().cloned().collect());
		assert!(filter_any_match.matches(&statement));

		let filter_any_fail =
			crate::OptimizedTopicFilter::MatchAny([topic3].iter().cloned().collect());
		assert!(!filter_any_fail.matches(&statement));
	}

	#[test]
	fn statement_type_info_matches_encoding() {
		// Statement has custom Encode/Decode that encodes as Vec<Field>.
		// Verify that TypeInfo reflects this by containing a reference to Vec<Field>.
		let statement_type = Statement::type_info();
		let vec_field_meta = MetaType::new::<Vec<Field>>();

		// The Statement type should be a composite with one unnamed field of type Vec<Field>
		match statement_type.type_def {
			scale_info::TypeDef::Composite(composite) => {
				assert_eq!(composite.fields.len(), 1, "Statement should have exactly one field");
				let field = &composite.fields[0];
				assert!(field.name.is_none(), "Field should be unnamed (newtype pattern)");
				assert_eq!(field.ty, vec_field_meta, "Statement's inner type should be Vec<Field>");
			},
			_ => panic!("Statement TypeInfo should be a Composite"),
		}
	}

	#[test]
	fn measure_hash_30_000_statements() {
		use std::time::Instant;
		const NUM_STATEMENTS: usize = 30_000;
		let (keyring, _) = sr25519::Pair::generate();

		// Create 2000 statements with varying data
		let statements: Vec<Statement> = (0..NUM_STATEMENTS)
			.map(|i| {
				let mut statement = Statement::new();

				statement.set_expiry(i as u64);
				statement.set_topic(0, [(i % 256) as u8; 32].into());
				statement.set_plain_data(vec![i as u8; 512]);
				statement.sign_sr25519_private(&keyring);

				statement.sign_sr25519_private(&keyring);
				statement
			})
			.collect();
		// Measure time to hash all statements
		let start = Instant::now();
		let hashes: Vec<[u8; 32]> = statements.iter().map(|s| s.hash()).collect();
		let elapsed = start.elapsed();
		println!("Time to hash {} statements: {:?}", NUM_STATEMENTS, elapsed);
		println!("Average time per statement: {:?}", elapsed / NUM_STATEMENTS as u32);
		// Verify hashes are unique
		let unique_hashes: std::collections::HashSet<_> = hashes.iter().collect();
		assert_eq!(unique_hashes.len(), NUM_STATEMENTS);
	}

	#[test]
	fn estimated_encoded_size_is_sufficient() {
		// Allow some overhead due to using max_encoded_len() approximations.
		const MAX_ACCEPTED_OVERHEAD: usize = 33;

		let proof = Proof::OnChain { who: [42u8; 32], block_hash: [24u8; 32], event_index: 66 };
		let decryption_key = [0xde; 32];
		let data = vec![55; 1000];
		let expiry = 999;
		let channel = [0xcc; 32];

		// Test with all fields populated
		let mut statement = Statement::new();
		statement.set_proof(proof);
		statement.set_decryption_key(decryption_key);
		statement.set_expiry(expiry);
		statement.set_channel(channel);
		for i in 0..MAX_TOPICS {
			statement.set_topic(i, [i as u8; 32].into());
		}
		statement.set_plain_data(data);

		let encoded = statement.encode();
		let estimated = statement.estimated_encoded_size(false);
		assert!(
			estimated >= encoded.len(),
			"estimated_encoded_size ({}) should be >= actual encoded length ({})",
			estimated,
			encoded.len()
		);
		let overhead = estimated - encoded.len();
		assert!(
			overhead <= MAX_ACCEPTED_OVERHEAD,
			"estimated overhead ({}) should be small, estimated: {}, actual: {}",
			overhead,
			estimated,
			encoded.len()
		);

		// Test for_signing = true (no proof, no compact prefix)
		let signing_payload = statement.encoded(true);
		let signing_estimated = statement.estimated_encoded_size(true);
		assert!(
			signing_estimated >= signing_payload.len(),
			"estimated_encoded_size for signing ({}) should be >= actual signing payload length ({})",
			signing_estimated,
			signing_payload.len()
		);
		let signing_overhead = signing_estimated - signing_payload.len();
		assert!(
			signing_overhead <= MAX_ACCEPTED_OVERHEAD,
			"signing overhead ({}) should be small, estimated: {}, actual: {}",
			signing_overhead,
			signing_estimated,
			signing_payload.len()
		);

		// Test with minimal statement (empty)
		let empty_statement = Statement::new();
		let empty_encoded = empty_statement.encode();
		let empty_estimated = empty_statement.estimated_encoded_size(false);
		assert!(
			empty_estimated >= empty_encoded.len(),
			"estimated_encoded_size for empty ({}) should be >= actual encoded length ({})",
			empty_estimated,
			empty_encoded.len()
		);
		let empty_overhead = empty_estimated - empty_encoded.len();
		assert!(
			empty_overhead <= MAX_ACCEPTED_OVERHEAD,
			"empty overhead ({}) should be minimal, estimated: {}, actual: {}",
			empty_overhead,
			empty_estimated,
			empty_encoded.len()
		);
	}
}