ab_system_contract_simple_wallet_base/
seal.rs1use crate::{SIGNING_CONTEXT, Seal};
4use ab_contracts_common::ContractError;
5use ab_core_primitives::transaction::{
6 SerializedTransactionLengths, TransactionHeader, TransactionSlot,
7};
8use ab_io_type::trivial_type::TrivialType;
9use blake3::{Hasher, OUT_LEN};
10use core::mem::offset_of;
11use schnorrkel::context::SigningContext;
12use schnorrkel::{Keypair, PublicKey, Signature};
13
14pub fn hash_transaction(
28 header: &TransactionHeader,
29 read_slots: &[TransactionSlot],
30 write_slots: &[TransactionSlot],
31 payload: &[u128],
32 nonce: u64,
33) -> Option<[u8; OUT_LEN]> {
34 const {
35 assert!(offset_of!(Seal, signature) + size_of::<[u8; 64]>() == size_of::<Seal>());
37 }
38
39 let lengths = SerializedTransactionLengths {
40 read_slots: u16::try_from(read_slots.len()).ok()?,
41 write_slots: u16::try_from(write_slots.len()).ok()?,
42 payload: u32::try_from(size_of_val(payload)).ok()?,
43 seal: Seal::SIZE,
44 padding: [0; _],
45 };
46
47 let mut hasher = Hasher::new();
48 hasher.update(header.as_bytes());
49 hasher.update(lengths.as_bytes());
50 hasher.update(TransactionSlot::slice_as_bytes(read_slots));
51 hasher.update(TransactionSlot::slice_as_bytes(write_slots));
52 hasher.update(u128::slice_as_bytes(payload));
53 let seal = Seal {
54 nonce,
55 signature: [0; _],
56 };
57 hasher.update(&seal.as_bytes()[..offset_of!(Seal, signature)]);
58 Some(hasher.finalize().into())
59}
60
61pub fn sign(keypair: &Keypair, tx_hash: &[u8; OUT_LEN]) -> Signature {
68 let signing_context = SigningContext::new(SIGNING_CONTEXT);
69 keypair.sign(signing_context.bytes(tx_hash))
70}
71
72pub fn hash_and_sign(
78 keypair: &Keypair,
79 header: &TransactionHeader,
80 read_slots: &[TransactionSlot],
81 write_slots: &[TransactionSlot],
82 payload: &[u128],
83 nonce: u64,
84) -> Option<Seal> {
85 let tx_hash = hash_transaction(header, read_slots, write_slots, payload, nonce)?;
86 let signature = sign(keypair, &tx_hash).to_bytes();
87
88 Some(Seal { nonce, signature })
89}
90
91pub fn verify(
98 public_key: &PublicKey,
99 expected_nonce: u64,
100 tx_hash: &[u8; OUT_LEN],
101 signature: &Signature,
102 nonce: u64,
103) -> Result<(), ContractError> {
104 if nonce != expected_nonce {
105 return Err(ContractError::BadInput);
106 }
107
108 let signing_context = SigningContext::new(SIGNING_CONTEXT);
109 public_key
110 .verify(signing_context.bytes(tx_hash.as_bytes()), signature)
111 .map_err(|_error| ContractError::Forbidden)
112}
113
114pub fn hash_and_verify(
120 public_key: &PublicKey,
121 expected_nonce: u64,
122 header: &TransactionHeader,
123 read_slots: &[TransactionSlot],
124 write_slots: &[TransactionSlot],
125 payload: &[u128],
126 seal: &Seal,
127) -> Result<(), ContractError> {
128 if seal.nonce != expected_nonce {
129 return Err(ContractError::BadInput);
130 }
131
132 let tx_hash = hash_transaction(header, read_slots, write_slots, payload, seal.nonce)
133 .ok_or(ContractError::BadInput)?;
134 let signature =
135 Signature::from_bytes(&seal.signature).map_err(|_error| ContractError::BadInput)?;
136
137 verify(public_key, expected_nonce, &tx_hash, &signature, seal.nonce)
138}