Skip to content

Commit e1d1c65

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

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

stack/src/lib.rs

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,4 +872,122 @@ mod tests {
872872
}
873873
));
874874
}
875+
876+
#[test]
877+
fn test_execute_script_atomic_swap() {
878+
let secret = vec![1, 2, 3, 4];
879+
let hash_secret = calculate_sha256(&secret);
880+
let pk_1 = PublicKey::from_bytes([1; 33]);
881+
let pk_2 = PublicKey::from_bytes([2; 33]);
882+
883+
let ks_1 = ks_from_pk(pk_1.clone());
884+
let ks_2 = ks_from_pk(pk_2.clone());
885+
886+
// 1 can spend after timelock
887+
let s = vec![
888+
// Witness script
889+
Item::Value(MyValue::Signature(ks_1.to_pb_bytes().unwrap())),
890+
Item::Value(MyValue::Boolean(true)),
891+
// Redeem script
892+
Item::Operator(MyOperator::If),
893+
Item::Value(MyValue::Integer(10_000)),
894+
Item::Operator(MyOperator::CheckTimeLock),
895+
Item::Operator(MyOperator::Verify),
896+
Item::Value(MyValue::Bytes(pk_1.pkh().bytes().to_vec())),
897+
Item::Operator(MyOperator::CheckSig),
898+
Item::Operator(MyOperator::Verify),
899+
Item::Operator(MyOperator::Else),
900+
Item::Operator(MyOperator::Sha256),
901+
Item::Value(MyValue::Bytes(hash_secret.as_ref().to_vec())),
902+
Item::Operator(MyOperator::Equal),
903+
Item::Operator(MyOperator::Verify),
904+
Item::Value(MyValue::Bytes(pk_2.pkh().bytes().to_vec())),
905+
Item::Operator(MyOperator::CheckSig),
906+
Item::Operator(MyOperator::Verify),
907+
Item::Operator(MyOperator::EndIf),
908+
];
909+
assert!(execute_script(
910+
s,
911+
&ScriptContext {
912+
block_timestamp: 20_000
913+
}
914+
));
915+
916+
// 1 cannot spend before timelock
917+
let s = vec![
918+
// Witness script
919+
Item::Value(MyValue::Signature(ks_1.to_pb_bytes().unwrap())),
920+
Item::Value(MyValue::Boolean(true)),
921+
// Redeem script
922+
Item::Operator(MyOperator::If),
923+
Item::Value(MyValue::Integer(10_000)),
924+
Item::Operator(MyOperator::CheckTimeLock),
925+
Item::Operator(MyOperator::Verify),
926+
Item::Value(MyValue::Bytes(pk_1.pkh().bytes().to_vec())),
927+
Item::Operator(MyOperator::CheckSig),
928+
Item::Operator(MyOperator::Verify),
929+
Item::Operator(MyOperator::Else),
930+
Item::Operator(MyOperator::Sha256),
931+
Item::Value(MyValue::Bytes(hash_secret.as_ref().to_vec())),
932+
Item::Operator(MyOperator::Equal),
933+
Item::Operator(MyOperator::Verify),
934+
Item::Value(MyValue::Bytes(pk_2.pkh().bytes().to_vec())),
935+
Item::Operator(MyOperator::CheckSig),
936+
Item::Operator(MyOperator::Verify),
937+
Item::Operator(MyOperator::EndIf),
938+
];
939+
assert!(!execute_script(s, &ScriptContext { block_timestamp: 0 }));
940+
941+
// 2 can spend with secret
942+
let s = vec![
943+
// Witness script
944+
Item::Value(MyValue::Signature(ks_2.to_pb_bytes().unwrap())),
945+
Item::Value(MyValue::Bytes(secret)),
946+
Item::Value(MyValue::Boolean(false)),
947+
// Redeem script
948+
Item::Operator(MyOperator::If),
949+
Item::Value(MyValue::Integer(10_000)),
950+
Item::Operator(MyOperator::CheckTimeLock),
951+
Item::Operator(MyOperator::Verify),
952+
Item::Value(MyValue::Bytes(pk_1.pkh().bytes().to_vec())),
953+
Item::Operator(MyOperator::CheckSig),
954+
Item::Operator(MyOperator::Verify),
955+
Item::Operator(MyOperator::Else),
956+
Item::Operator(MyOperator::Sha256),
957+
Item::Value(MyValue::Bytes(hash_secret.as_ref().to_vec())),
958+
Item::Operator(MyOperator::Equal),
959+
Item::Operator(MyOperator::Verify),
960+
Item::Value(MyValue::Bytes(pk_2.pkh().bytes().to_vec())),
961+
Item::Operator(MyOperator::CheckSig),
962+
Item::Operator(MyOperator::Verify),
963+
Item::Operator(MyOperator::EndIf),
964+
];
965+
assert!(execute_script(s, &ScriptContext { block_timestamp: 0 }));
966+
967+
// 2 cannot spend with a wrong secret
968+
let s = vec![
969+
// Witness script
970+
Item::Value(MyValue::Signature(ks_2.to_pb_bytes().unwrap())),
971+
Item::Value(MyValue::Bytes(vec![0, 0, 0, 0])),
972+
Item::Value(MyValue::Boolean(false)),
973+
// Redeem script
974+
Item::Operator(MyOperator::If),
975+
Item::Value(MyValue::Integer(10_000)),
976+
Item::Operator(MyOperator::CheckTimeLock),
977+
Item::Operator(MyOperator::Verify),
978+
Item::Value(MyValue::Bytes(pk_1.pkh().bytes().to_vec())),
979+
Item::Operator(MyOperator::CheckSig),
980+
Item::Operator(MyOperator::Verify),
981+
Item::Operator(MyOperator::Else),
982+
Item::Operator(MyOperator::Sha256),
983+
Item::Value(MyValue::Bytes(hash_secret.as_ref().to_vec())),
984+
Item::Operator(MyOperator::Equal),
985+
Item::Operator(MyOperator::Verify),
986+
Item::Value(MyValue::Bytes(pk_2.pkh().bytes().to_vec())),
987+
Item::Operator(MyOperator::CheckSig),
988+
Item::Operator(MyOperator::Verify),
989+
Item::Operator(MyOperator::EndIf),
990+
];
991+
assert!(!execute_script(s, &ScriptContext { block_timestamp: 0 }));
992+
}
875993
}

0 commit comments

Comments
 (0)