Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(stack): implement control flow and checktimelock #2217

Closed
wants to merge 10 commits into from

Conversation

tmpolaczyk
Copy link
Contributor

No description provided.

stack/src/lib.rs Outdated Show resolved Hide resolved
@tmpolaczyk tmpolaczyk changed the title feat(stack): implement control flow feat(stack): implement control flow and checktimelock Jun 8, 2022
@aesedepece
Copy link
Member

This is looking great. Let's address the TODOs and go for it.

Comment on lines +941 to +992
// 2 can spend with secret
let s = vec![
// Witness script
Item::Value(MyValue::Signature(ks_2.to_pb_bytes().unwrap())),
Item::Value(MyValue::Bytes(secret)),
Item::Value(MyValue::Boolean(false)),
// Redeem script
Item::Operator(MyOperator::If),
Item::Value(MyValue::Integer(10_000)),
Item::Operator(MyOperator::CheckTimeLock),
Item::Operator(MyOperator::Verify),
Item::Value(MyValue::Bytes(pk_1.pkh().bytes().to_vec())),
Item::Operator(MyOperator::CheckSig),
Item::Operator(MyOperator::Verify),
Item::Operator(MyOperator::Else),
Item::Operator(MyOperator::Sha256),
Item::Value(MyValue::Bytes(hash_secret.as_ref().to_vec())),
Item::Operator(MyOperator::Equal),
Item::Operator(MyOperator::Verify),
Item::Value(MyValue::Bytes(pk_2.pkh().bytes().to_vec())),
Item::Operator(MyOperator::CheckSig),
Item::Operator(MyOperator::Verify),
Item::Operator(MyOperator::EndIf),
];
assert!(execute_script(s, &ScriptContext { block_timestamp: 0 }));

// 2 cannot spend with a wrong secret
let s = vec![
// Witness script
Item::Value(MyValue::Signature(ks_2.to_pb_bytes().unwrap())),
Item::Value(MyValue::Bytes(vec![0, 0, 0, 0])),
Item::Value(MyValue::Boolean(false)),
// Redeem script
Item::Operator(MyOperator::If),
Item::Value(MyValue::Integer(10_000)),
Item::Operator(MyOperator::CheckTimeLock),
Item::Operator(MyOperator::Verify),
Item::Value(MyValue::Bytes(pk_1.pkh().bytes().to_vec())),
Item::Operator(MyOperator::CheckSig),
Item::Operator(MyOperator::Verify),
Item::Operator(MyOperator::Else),
Item::Operator(MyOperator::Sha256),
Item::Value(MyValue::Bytes(hash_secret.as_ref().to_vec())),
Item::Operator(MyOperator::Equal),
Item::Operator(MyOperator::Verify),
Item::Value(MyValue::Bytes(pk_2.pkh().bytes().to_vec())),
Item::Operator(MyOperator::CheckSig),
Item::Operator(MyOperator::Verify),
Item::Operator(MyOperator::EndIf),
];
assert!(!execute_script(s, &ScriptContext { block_timestamp: 0 }));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check that 2 cannot spend after timelock even if the right secret is provided? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this script they can, if that's a requirement we can add another test for that case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that, ideally, they shouldn't. It is a sort of assumption for an atomic swap that only 1 party can spend at a time.

stack/src/lib.rs Outdated
];
assert!(!execute_script(s, &ScriptContext { block_timestamp: 0 }));

// 2 cannot spend before timelock
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 2 cannot spend before timelock
// 2 cannot spend after timelock

@tmpolaczyk
Copy link
Contributor Author

Now the tests are failing because of a stack.pop() on an empty stack, this is fixed by aesedepece/scriptful#6

@tmpolaczyk
Copy link
Contributor Author

Closing, will continue in #2240.

@tmpolaczyk tmpolaczyk closed this Jul 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants