Skip to content

Commit 7338cb5

Browse files
committed
feat: encode legacy signatures as script with one element
1 parent e6adc69 commit 7338cb5

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

data_structures/src/transaction.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use serde::{Deserialize, Serialize};
22

3+
use crate::stack::{Item, MyValue, ScriptError};
34
use crate::{
45
chain::{
56
Block, Bn256PublicKey, DataRequestOutput, Epoch, Hash, Hashable, Input, KeyedSignature,
@@ -199,13 +200,22 @@ pub fn mint(tx: &Transaction) -> Option<&MintTransaction> {
199200
}
200201

201202
pub fn vtt_signature_to_witness(ks: &KeyedSignature) -> Vec<u8> {
202-
// TODO: it would be nice to encode KeyedSignature as a script
203-
// This way vtt.witness is always a valid script
204-
ks.to_pb_bytes().unwrap()
203+
let script = vec![Item::Value(MyValue::from_signature(ks))];
204+
205+
crate::stack::encode(&script).unwrap()
205206
}
206207

207-
pub fn vtt_witness_to_signature(witness: &[u8]) -> KeyedSignature {
208-
KeyedSignature::from_pb_bytes(witness).unwrap()
208+
pub fn vtt_witness_to_signature(witness: &[u8]) -> Result<KeyedSignature, ScriptError> {
209+
let script = crate::stack::decode(witness)?;
210+
211+
if script.len() != 1 {
212+
return Err(ScriptError::InvalidSignature);
213+
}
214+
215+
match &script[0] {
216+
Item::Value(value) => value.to_signature(),
217+
_ => Err(ScriptError::InvalidSignature),
218+
}
209219
}
210220

211221
#[derive(Debug, Default, Eq, PartialEq, Clone, Serialize, Deserialize, ProtobufConvert, Hash)]

validations/src/validations.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1243,7 +1243,7 @@ pub fn validate_transaction_signatures(
12431243
if input.redeem_script.is_empty() {
12441244
// Validate that public key hash of the pointed output matches public
12451245
// key in the provided signature
1246-
let keyed_signature = vtt_witness_to_signature(witness);
1246+
let keyed_signature = vtt_witness_to_signature(witness)?;
12471247
validate_pkh_signature(input, &keyed_signature, utxo_set).map_err(fte)?;
12481248

12491249
// Validate the actual signature

0 commit comments

Comments
 (0)