Skip to content

Commit 5368669

Browse files
tmpolaczyklrubiorod
andcommitted
feat(stack): Create MyValue::Signature type to differ from MyValue::Bytes
Co-authored-by: Luis Rubio <l.rubiorod@gmail.com>
1 parent f3f43da commit 5368669

3 files changed

Lines changed: 18 additions & 21 deletions

File tree

src/cli/node/json_rpc_client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ pub fn sign_tx(addr: SocketAddr, hex: String, dry_run: bool) -> Result<(), failu
855855
let mut script = witnet_stack::decode(&vtt.witness[0]);
856856

857857
println!("Previous script:\n{:?}", script);
858-
script.push(Item::Value(MyValue::Bytes(signature_bytes)));
858+
script.push(Item::Value(MyValue::Signature(signature_bytes)));
859859

860860
println!("Post script:\n{:?}", script);
861861
let encoded_script = witnet_stack::encode(script);

stack/src/lib.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ pub enum MyValue {
3333
String(String),
3434
/// Bytes.
3535
Bytes(Vec<u8>),
36+
/// Signature.
37+
Signature(Vec<u8>),
3638
}
3739

3840
fn equal_operator(stack: &mut Stack<MyValue>) {
@@ -43,17 +45,13 @@ fn equal_operator(stack: &mut Stack<MyValue>) {
4345

4446
fn hash_160_operator(stack: &mut Stack<MyValue>) {
4547
let a = stack.pop();
46-
match a {
47-
MyValue::Boolean(_) => {}
48-
MyValue::Float(_) => {}
49-
MyValue::Integer(_) => {}
50-
MyValue::String(_) => {}
51-
MyValue::Bytes(bytes) => {
52-
let mut pkh = [0; 20];
53-
let Sha256(h) = calculate_sha256(&bytes);
54-
pkh.copy_from_slice(&h[..20]);
55-
stack.push(MyValue::Bytes(pkh.as_ref().to_vec()));
56-
}
48+
if let MyValue::Bytes(bytes) = a {
49+
let mut pkh = [0; 20];
50+
let Sha256(h) = calculate_sha256(&bytes);
51+
pkh.copy_from_slice(&h[..20]);
52+
stack.push(MyValue::Bytes(pkh.as_ref().to_vec()));
53+
} else {
54+
// TODO: hash other types?
5755
}
5856
}
5957

@@ -94,7 +92,7 @@ fn check_multi_sig(bytes_pkhs: Vec<MyValue>, bytes_keyed_signatures: Vec<MyValue
9492
let mut signed_pkhs = vec![];
9593
for signature in bytes_keyed_signatures {
9694
match signature {
97-
MyValue::Bytes(bytes) => {
95+
MyValue::Signature(bytes) => {
9896
// TODO Handle unwrap
9997
let ks: KeyedSignature = KeyedSignature::from_pb_bytes(&bytes).unwrap();
10098
let signed_pkh = ks.public_key.pkh();
@@ -364,8 +362,8 @@ mod tests {
364362
let ks_3 = ks_from_pk(pk_3.clone());
365363

366364
let witness = vec![
367-
Item::Value(MyValue::Bytes(ks_1.to_pb_bytes().unwrap())),
368-
Item::Value(MyValue::Bytes(ks_2.to_pb_bytes().unwrap())),
365+
Item::Value(MyValue::Signature(ks_1.to_pb_bytes().unwrap())),
366+
Item::Value(MyValue::Signature(ks_2.to_pb_bytes().unwrap())),
369367
];
370368
let redeem_script = vec![
371369
Item::Value(MyValue::Integer(2)),
@@ -381,8 +379,8 @@ mod tests {
381379
));
382380

383381
let other_valid_witness = vec![
384-
Item::Value(MyValue::Bytes(ks_1.to_pb_bytes().unwrap())),
385-
Item::Value(MyValue::Bytes(ks_3.to_pb_bytes().unwrap())),
382+
Item::Value(MyValue::Signature(ks_1.to_pb_bytes().unwrap())),
383+
Item::Value(MyValue::Signature(ks_3.to_pb_bytes().unwrap())),
386384
];
387385
let redeem_script = vec![
388386
Item::Value(MyValue::Integer(2)),
@@ -400,8 +398,8 @@ mod tests {
400398
let pk_4 = PublicKey::from_bytes([4; 33]);
401399
let ks_4 = ks_from_pk(pk_4);
402400
let invalid_witness = vec![
403-
Item::Value(MyValue::Bytes(ks_1.to_pb_bytes().unwrap())),
404-
Item::Value(MyValue::Bytes(ks_4.to_pb_bytes().unwrap())),
401+
Item::Value(MyValue::Signature(ks_1.to_pb_bytes().unwrap())),
402+
Item::Value(MyValue::Signature(ks_4.to_pb_bytes().unwrap())),
405403
];
406404
let redeem_script = vec![
407405
Item::Value(MyValue::Integer(2)),

validations/src/validations.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,8 +1279,7 @@ pub fn validate_transaction_signatures(
12791279
// The witness field must have at least one signature
12801280
for item in witness_script {
12811281
match item {
1282-
// TODO: use MyValue::Signature
1283-
Item::Value(MyValue::Bytes(bytes)) => {
1282+
Item::Value(MyValue::Signature(bytes)) => {
12841283
let keyed_signature = KeyedSignature::from_pb_bytes(&bytes).unwrap();
12851284

12861285
// Validate the actual signature

0 commit comments

Comments
 (0)