Skip to content

Commit a32492f

Browse files
committed
test(stack): add another atomic swap test case
1 parent e1d1c65 commit a32492f

File tree

1 file changed

+141
-1
lines changed

1 file changed

+141
-1
lines changed

stack/src/lib.rs

Lines changed: 141 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ fn check_multi_sig(bytes_pkhs: Vec<MyValue>, bytes_keyed_signatures: Vec<MyValue
129129
}
130130
_ => {
131131
// TODO change panic by error
132-
unreachable!("check_multi_sig should pick only bytes");
132+
//unreachable!("check_multi_sig should pick only bytes");
133+
return false;
133134
}
134135
}
135136
}
@@ -990,4 +991,143 @@ mod tests {
990991
];
991992
assert!(!execute_script(s, &ScriptContext { block_timestamp: 0 }));
992993
}
994+
995+
#[test]
996+
fn test_execute_script_atomic_swap_2() {
997+
let secret = vec![1, 2, 3, 4];
998+
let hash_secret = calculate_sha256(&secret);
999+
let pk_1 = PublicKey::from_bytes([1; 33]);
1000+
let pk_2 = PublicKey::from_bytes([2; 33]);
1001+
1002+
let ks_1 = ks_from_pk(pk_1.clone());
1003+
let ks_2 = ks_from_pk(pk_2.clone());
1004+
1005+
// 1 can spend after timelock
1006+
let s = vec![
1007+
// Witness script
1008+
Item::Value(MyValue::Signature(ks_1.to_pb_bytes().unwrap())),
1009+
// Redeem script
1010+
Item::Value(MyValue::Integer(10_000)),
1011+
Item::Operator(MyOperator::CheckTimeLock),
1012+
Item::Operator(MyOperator::If),
1013+
Item::Value(MyValue::Bytes(pk_1.pkh().bytes().to_vec())),
1014+
Item::Operator(MyOperator::CheckSig),
1015+
Item::Operator(MyOperator::Verify),
1016+
Item::Operator(MyOperator::Else),
1017+
Item::Operator(MyOperator::Sha256),
1018+
Item::Value(MyValue::Bytes(hash_secret.as_ref().to_vec())),
1019+
Item::Operator(MyOperator::Equal),
1020+
Item::Operator(MyOperator::Verify),
1021+
Item::Value(MyValue::Bytes(pk_2.pkh().bytes().to_vec())),
1022+
Item::Operator(MyOperator::CheckSig),
1023+
Item::Operator(MyOperator::Verify),
1024+
Item::Operator(MyOperator::EndIf),
1025+
];
1026+
assert!(execute_script(
1027+
s,
1028+
&ScriptContext {
1029+
block_timestamp: 20_000
1030+
}
1031+
));
1032+
1033+
// 1 cannot spend before timelock
1034+
let s = vec![
1035+
// Witness script
1036+
Item::Value(MyValue::Signature(ks_1.to_pb_bytes().unwrap())),
1037+
// Redeem script
1038+
Item::Value(MyValue::Integer(10_000)),
1039+
Item::Operator(MyOperator::CheckTimeLock),
1040+
Item::Operator(MyOperator::If),
1041+
Item::Value(MyValue::Bytes(pk_1.pkh().bytes().to_vec())),
1042+
Item::Operator(MyOperator::CheckSig),
1043+
Item::Operator(MyOperator::Verify),
1044+
Item::Operator(MyOperator::Else),
1045+
Item::Operator(MyOperator::Sha256),
1046+
Item::Value(MyValue::Bytes(hash_secret.as_ref().to_vec())),
1047+
Item::Operator(MyOperator::Equal),
1048+
Item::Operator(MyOperator::Verify),
1049+
Item::Value(MyValue::Bytes(pk_2.pkh().bytes().to_vec())),
1050+
Item::Operator(MyOperator::CheckSig),
1051+
Item::Operator(MyOperator::Verify),
1052+
Item::Operator(MyOperator::EndIf),
1053+
];
1054+
assert!(!execute_script(s, &ScriptContext { block_timestamp: 0 }));
1055+
1056+
// 2 can spend with secret
1057+
let s = vec![
1058+
// Witness script
1059+
Item::Value(MyValue::Signature(ks_2.to_pb_bytes().unwrap())),
1060+
Item::Value(MyValue::Bytes(secret.clone())),
1061+
// Redeem script
1062+
Item::Value(MyValue::Integer(10_000)),
1063+
Item::Operator(MyOperator::CheckTimeLock),
1064+
Item::Operator(MyOperator::If),
1065+
Item::Value(MyValue::Bytes(pk_1.pkh().bytes().to_vec())),
1066+
Item::Operator(MyOperator::CheckSig),
1067+
Item::Operator(MyOperator::Verify),
1068+
Item::Operator(MyOperator::Else),
1069+
Item::Operator(MyOperator::Sha256),
1070+
Item::Value(MyValue::Bytes(hash_secret.as_ref().to_vec())),
1071+
Item::Operator(MyOperator::Equal),
1072+
Item::Operator(MyOperator::Verify),
1073+
Item::Value(MyValue::Bytes(pk_2.pkh().bytes().to_vec())),
1074+
Item::Operator(MyOperator::CheckSig),
1075+
Item::Operator(MyOperator::Verify),
1076+
Item::Operator(MyOperator::EndIf),
1077+
];
1078+
assert!(execute_script(s, &ScriptContext { block_timestamp: 0 }));
1079+
1080+
// 2 cannot spend with a wrong secret
1081+
let s = vec![
1082+
// Witness script
1083+
Item::Value(MyValue::Signature(ks_2.to_pb_bytes().unwrap())),
1084+
Item::Value(MyValue::Bytes(vec![0, 0, 0, 0])),
1085+
// Redeem script
1086+
Item::Value(MyValue::Integer(10_000)),
1087+
Item::Operator(MyOperator::CheckTimeLock),
1088+
Item::Operator(MyOperator::If),
1089+
Item::Value(MyValue::Bytes(pk_1.pkh().bytes().to_vec())),
1090+
Item::Operator(MyOperator::CheckSig),
1091+
Item::Operator(MyOperator::Verify),
1092+
Item::Operator(MyOperator::Else),
1093+
Item::Operator(MyOperator::Sha256),
1094+
Item::Value(MyValue::Bytes(hash_secret.as_ref().to_vec())),
1095+
Item::Operator(MyOperator::Equal),
1096+
Item::Operator(MyOperator::Verify),
1097+
Item::Value(MyValue::Bytes(pk_2.pkh().bytes().to_vec())),
1098+
Item::Operator(MyOperator::CheckSig),
1099+
Item::Operator(MyOperator::Verify),
1100+
Item::Operator(MyOperator::EndIf),
1101+
];
1102+
assert!(!execute_script(s, &ScriptContext { block_timestamp: 0 }));
1103+
1104+
// 2 cannot spend after timelock
1105+
let s = vec![
1106+
// Witness script
1107+
Item::Value(MyValue::Signature(ks_2.to_pb_bytes().unwrap())),
1108+
Item::Value(MyValue::Bytes(secret)),
1109+
// Redeem script
1110+
Item::Value(MyValue::Integer(10_000)),
1111+
Item::Operator(MyOperator::CheckTimeLock),
1112+
Item::Operator(MyOperator::If),
1113+
Item::Value(MyValue::Bytes(pk_1.pkh().bytes().to_vec())),
1114+
Item::Operator(MyOperator::CheckSig),
1115+
Item::Operator(MyOperator::Verify),
1116+
Item::Operator(MyOperator::Else),
1117+
Item::Operator(MyOperator::Sha256),
1118+
Item::Value(MyValue::Bytes(hash_secret.as_ref().to_vec())),
1119+
Item::Operator(MyOperator::Equal),
1120+
Item::Operator(MyOperator::Verify),
1121+
Item::Value(MyValue::Bytes(pk_2.pkh().bytes().to_vec())),
1122+
Item::Operator(MyOperator::CheckSig),
1123+
Item::Operator(MyOperator::Verify),
1124+
Item::Operator(MyOperator::EndIf),
1125+
];
1126+
assert!(!execute_script(
1127+
s,
1128+
&ScriptContext {
1129+
block_timestamp: 20_000
1130+
}
1131+
));
1132+
}
9931133
}

0 commit comments

Comments
 (0)