Skip to content

Commit 5b8c089

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

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

data_structures/src/transaction.rs

+15-5
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)]

src/cli/node/json_rpc_client.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -904,21 +904,17 @@ pub fn sign_tx(
904904

905905
match tx {
906906
Transaction::ValueTransfer(ref mut vtt) => {
907-
let signature_bytes = signature.to_pb_bytes()?;
908-
// TODO: this only works if the witness field represents a script
909-
// It could also represent a signature in the case of normal value transfer
910-
// transactions. It would be nice to also support signing normal transactions here
907+
let signature_bytes = MyValue::from_signature(&signature);
908+
// This also works with normal transactions, because the signature is encoded as a
909+
// script with one element.
911910
let mut script = witnet_data_structures::stack::decode(&vtt.witness[input_index])?;
912911

913912
println!(
914913
"-----------------------\nPrevious witness:\n-----------------------\n{}",
915914
witnet_data_structures::stack::parser::script_to_string(&script)
916915
);
917916

918-
script.insert(
919-
signature_position_in_witness,
920-
Item::Value(MyValue::Bytes(signature_bytes)),
921-
);
917+
script.insert(signature_position_in_witness, Item::Value(signature_bytes));
922918

923919
println!(
924920
"-----------------------\nNew witness:\n-----------------------\n{}",

validations/src/validations.rs

+1-1
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)