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

[framework] base64 support url decode #3114

Merged
merged 5 commits into from
Dec 28, 2024

Conversation

baicaiyihao
Copy link
Contributor

Summary

I added a logic check to handle the decoding of both STANDARD and URL_SAFE base64 encoding formats. Please check if there are any issues.

Here is the test code for base64 decode:

/*
/// Module: testrooch
module testrooch::testrooch {
}
*/
module testrooch::testrooch {
    use std::vector;
    use moveos_std::base64::{ decode };

    const E_DECODE_FAILED: u64 = 1;

    #[test]
    fun test_decode() {
        let encoded_input = b"/////w==";
        let decoded = decode(&encoded_input);
        let v = vector::empty<u8>();

        vector::push_back(&mut v, 255);
        vector::push_back(&mut v, 255);
        vector::push_back(&mut v, 255);
        vector::push_back(&mut v, 255);
        decode(&encoded_input);

        assert!(v == decoded, E_DECODE_FAILED);
    }

    #[test]
    fun test_url_safe_decode() {
        let encoded_input = b"_____w";
        let decoded = decode(&encoded_input);
        let v = vector::empty<u8>();

        vector::push_back(&mut v, 255);
        vector::push_back(&mut v, 255);
        vector::push_back(&mut v, 255);
        vector::push_back(&mut v, 255);
        decode(&encoded_input);

        assert!(v == decoded, E_DECODE_FAILED);
    }


    #[test]
    fun test_decode1() {
        let encoded_input = b"aHR0cHM6Ly9naXRodWIuY29tL3Jvb2NoLW5ldHdvcmsvcm9vY2gvYmxvYi8zZTdjNjNjZjMyMmMzZGZkMmU2ZTE0MTkzOTM4OTdlYjEzY2M0Mjk0L2ZyYW1ld29ya3MvbW92ZW9zLXN0ZGxpYi9zcmMvbmF0aXZlcy9tb3Zlb3Nfc3RkbGliL2Jhc2U2NC5ycw==";
        let decoded = decode(&encoded_input);
        let expected = b"https://github.com/rooch-network/rooch/blob/3e7c63cf322c3dfd2e6e1419393897eb13cc4294/frameworks/moveos-stdlib/src/natives/moveos_stdlib/base64.rs";

        assert!(decoded == expected, E_DECODE_FAILED);
    }

    #[test]
    fun test_url_safe_decode1() {
        let encoded_input = b"aHR0cHM6Ly9naXRodWIuY29tL3Jvb2NoLW5ldHdvcmsvcm9vY2gvYmxvYi8zZTdjNjNjZjMyMmMzZGZkMmU2ZTE0MTkzOTM4OTdlYjEzY2M0Mjk0L2ZyYW1ld29ya3MvbW92ZW9zLXN0ZGxpYi9zcmMvbmF0aXZlcy9tb3Zlb3Nfc3RkbGliL2Jhc2U2NC5ycw";
        let decoded = decode(&encoded_input);
        let expected = b"https://github.com/rooch-network/rooch/blob/3e7c63cf322c3dfd2e6e1419393897eb13cc4294/frameworks/moveos-stdlib/src/natives/moveos_stdlib/base64.rs";

        assert!(decoded == expected, E_DECODE_FAILED);
    }
}

result

rooch move test
INCLUDING DEPENDENCY MoveStdlib
INCLUDING DEPENDENCY MoveosStdlib
INCLUDING DEPENDENCY RoochFramework
BUILDING testrooch
Running Move unit tests
[ PASS    ] 0xfe88a84f5c2b508331475ae552259d1319f873e5680b5fdd650c41730dced646::testrooch::test_decode
[ PASS    ] 0xfe88a84f5c2b508331475ae552259d1319f873e5680b5fdd650c41730dced646::testrooch::test_decode1
[ PASS    ] 0xfe88a84f5c2b508331475ae552259d1319f873e5680b5fdd650c41730dced646::testrooch::test_url_safe_decode
[ PASS    ] 0xfe88a84f5c2b508331475ae552259d1319f873e5680b5fdd650c41730dced646::testrooch::test_url_safe_decode1
Test result: OK. Total tests: 4; passed: 4; failed: 0

Please review if there are any issues.

Copy link

vercel bot commented Dec 27, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
rooch-portal-v2.1 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 28, 2024 3:07am
test-portal ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 28, 2024 3:07am
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
rooch ⬜️ Ignored (Inspect) Visit Preview Dec 28, 2024 3:07am

@jolestar
Copy link
Contributor

Please put the unit test in the framework code.

@baicaiyihao
Copy link
Contributor Author

Add base64_tests.move
frameworks/moveos-stdlib/tests/base64_tests.move

result:
image

Please review @jolestar

Choose a reason for hiding this comment

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

Copilot reviewed 1 out of 2 changed files in this pull request and generated no comments.

Files not reviewed (1)
  • frameworks/moveos-stdlib/tests/base64_tests.move: Language not supported
Comments suppressed due to low confidence (1)

frameworks/moveos-stdlib/src/natives/moveos_stdlib/base64.rs:64

  • The variable 'bytes_ref' is defined but not used directly. It is redundant and should be removed.
let bytes_ref = encoded_input.as_bytes_ref();
@jolestar jolestar merged commit ec45754 into rooch-network:main Dec 28, 2024
10 checks passed
@baicaiyihao baicaiyihao deleted the feature/my-new-feature branch December 30, 2024 02:31
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.

[framework] base64 support url decode
2 participants