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

Do ext checks after translation. #556

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sanket1729
Copy link
Member

Fixes #238

Also adds some basic tests

Also adds some basic tests
@apoelstra
Copy link
Member

I think the CI failure is something real, though maybe it means that we need to update the test.

@tcharding
Copy link
Member

The bug is in the test I believe, just change the match statement in ms_attributes_test to be:

        match (ms, valid) {
            (Ok(ms), true) => {
                assert_eq!(format!("{:x}", ms.encode()), expected_hex);
                assert_eq!(ms.ty.mall.non_malleable, non_mal);
                assert_eq!(ms.ty.mall.safe, need_sig);
                assert_eq!(ms.ext.ops.op_count().unwrap(), ops);
            }
            (Err(_), false) => {}
            (Ok(_), false) => panic!("false positive"),
            (Err(_), true) => panic!("false negative"),
        }

@tcharding
Copy link
Member

Here is a refactor of the new unit test I did while debugging if you want it:

    // Sanity test for `translate_fails_for_duplicate_key`.
    #[test]
    fn can_translate_different_keys() {
        let ms = Miniscript::<String, Segwitv0>::from_str("and_b(pk(A),a:pk(B))").unwrap();
        let mut t = StrKeyTranslator::new();

        let key0 = bitcoin::PublicKey::from_str(
            "0238e6a8035e67d46f2a350f748a9c2dd45ba467f12432e5f9371ca91fb9831086",
        ).unwrap();
        // insecure complement of key0
        let key1 = bitcoin::PublicKey::from_str(
            "03a8e6a8035e67d46f2a350f748a9c2dd45ba467f12432e5f9371ca91fb9831086",
        ).unwrap();

        t.pk_map.insert(String::from("A"), key0);
        t.pk_map.insert(String::from("B"), key1);
        ms.translate_pk(&mut t).expect("translation failed");
    }

    #[test]
    fn translate_fails_for_duplicate_key() {
        let ms = Miniscript::<String, Segwitv0>::from_str("and_b(pk(A),a:pk(B))").unwrap();
        let mut t = StrKeyTranslator::new();

        let key = bitcoin::PublicKey::from_str(
            "0238e6a8035e67d46f2a350f748a9c2dd45ba467f12432e5f9371ca91fb9831086",
        ).unwrap();

        t.pk_map.insert(String::from("A"), key);
        t.pk_map.insert(String::from("B"), key);
        match ms.translate_pk(&mut t) {
            Err(TranslateErr::OuterError(Error::AnalysisError(AnalysisError::RepeatedPubkeys))) => {},
            Err(_) => panic!("Unexpected error"),
            Ok(_) => panic!("False positive"),
        }
    }

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.

Key Translation Duplicate Check Safety Hole
3 participants