Skip to content

Commit 6d0d530

Browse files
committed
feat(stack): implement CheckSig operator
1 parent 9eb710e commit 6d0d530

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

stack/src/lib.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub enum MyOperator {
2121
Equal,
2222
Hash160,
2323
Sha256,
24+
CheckSig,
2425
CheckMultiSig,
2526
CheckTimeLock,
2627
/// Stop script execution if top-most element of stack is not "true"
@@ -75,6 +76,14 @@ fn sha_256_operator(stack: &mut Stack<MyValue>) {
7576
}
7677
}
7778

79+
fn check_sig_operator(stack: &mut Stack<MyValue>) {
80+
let pkh = stack.pop();
81+
let keyed_signature = stack.pop();
82+
// CheckSig operator is validated as a 1-of-1 multisig
83+
let res = check_multi_sig(vec![pkh], vec![keyed_signature]);
84+
stack.push(MyValue::Boolean(res));
85+
}
86+
7887
fn check_multisig_operator(stack: &mut Stack<MyValue>) {
7988
let m = stack.pop();
8089
match m {
@@ -202,6 +211,7 @@ fn my_operator_system(
202211
MyOperator::Equal => equal_operator(stack),
203212
MyOperator::Hash160 => hash_160_operator(stack),
204213
MyOperator::Sha256 => sha_256_operator(stack),
214+
MyOperator::CheckSig => check_sig_operator(stack),
205215
MyOperator::CheckMultiSig => check_multisig_operator(stack),
206216
MyOperator::CheckTimeLock => check_timelock_operator(stack, context.block_timestamp),
207217
MyOperator::Verify => {
@@ -632,6 +642,40 @@ mod tests {
632642
public_key: pk,
633643
}
634644
}
645+
646+
#[test]
647+
fn test_check_sig() {
648+
let pk_1 = PublicKey::from_bytes([1; 33]);
649+
let pk_2 = PublicKey::from_bytes([2; 33]);
650+
651+
let ks_1 = ks_from_pk(pk_1.clone());
652+
let ks_2 = ks_from_pk(pk_2.clone());
653+
654+
let witness = vec![Item::Value(MyValue::Signature(ks_1.to_pb_bytes().unwrap()))];
655+
let redeem_script = vec![
656+
Item::Value(MyValue::Bytes(pk_1.pkh().bytes().to_vec())),
657+
Item::Operator(MyOperator::CheckSig),
658+
];
659+
assert!(execute_redeem_script(
660+
&encode(witness).unwrap(),
661+
&encode(redeem_script).unwrap(),
662+
&ScriptContext::default(),
663+
)
664+
.unwrap());
665+
666+
let invalid_witness = vec![Item::Value(MyValue::Signature(ks_2.to_pb_bytes().unwrap()))];
667+
let redeem_script = vec![
668+
Item::Value(MyValue::Bytes(pk_1.pkh().bytes().to_vec())),
669+
Item::Operator(MyOperator::CheckSig),
670+
];
671+
assert!(!execute_redeem_script(
672+
&encode(invalid_witness).unwrap(),
673+
&encode(redeem_script).unwrap(),
674+
&ScriptContext::default(),
675+
)
676+
.unwrap());
677+
}
678+
635679
#[test]
636680
fn test_check_multisig() {
637681
let pk_1 = PublicKey::from_bytes([1; 33]);

0 commit comments

Comments
 (0)