-
Notifications
You must be signed in to change notification settings - Fork 11.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Ernesto García <ernestognw@gmail.com>
- Loading branch information
1 parent
281ab15
commit 692dbc5
Showing
6 changed files
with
133 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'openzeppelin-solidity': minor | ||
--- | ||
|
||
`Base64`: Add `encodeURL` following section 5 of RFC4648 for URL encoding |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -euo pipefail | ||
|
||
_encode() { | ||
# - Print the input to stdout | ||
# - Remove the first two characters | ||
# - Convert from hex to binary | ||
# - Convert from binary to base64 | ||
# - Remove newlines from `base64` output | ||
echo -n "$1" | cut -c 3- | xxd -r -p | base64 | tr -d \\n | ||
} | ||
|
||
encode() { | ||
# - Convert from base64 to hex | ||
# - Remove newlines from `xxd` output | ||
_encode "$1" | xxd -p | tr -d \\n | ||
} | ||
|
||
encodeURL() { | ||
# - Remove padding from `base64` output | ||
# - Replace `+` with `-` | ||
# - Replace `/` with `_` | ||
# - Convert from base64 to hex | ||
# - Remove newlines from `xxd` output | ||
_encode "$1" | sed 's/=//g' | sed 's/+/-/g' | sed 's/\//_/g' | xxd -p | tr -d \\n | ||
} | ||
|
||
# $1: function name | ||
# $2: input | ||
$1 $2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.20; | ||
|
||
import {Test} from "forge-std/Test.sol"; | ||
|
||
import {Base64} from "@openzeppelin/contracts/utils/Base64.sol"; | ||
|
||
/// NOTE: This test requires `ffi` to be enabled. It does not run in the CI | ||
/// environment given `ffi` is not recommended. | ||
/// See: https://github.com/foundry-rs/foundry/issues/6744 | ||
contract Base64Test is Test { | ||
function testEncode(bytes memory input) external { | ||
string memory output = Base64.encode(input); | ||
assertEq(output, _base64Ffi(input, "encode")); | ||
} | ||
|
||
function testEncodeURL(bytes memory input) external { | ||
string memory output = Base64.encodeURL(input); | ||
assertEq(output, _base64Ffi(input, "encodeURL")); | ||
} | ||
|
||
function _base64Ffi(bytes memory input, string memory fn) internal returns (string memory) { | ||
string[] memory command = new string[](4); | ||
command[0] = "bash"; | ||
command[1] = "scripts/tests/base64.sh"; | ||
command[2] = fn; | ||
command[3] = vm.toString(input); | ||
bytes memory retData = vm.ffi(command); | ||
return string(retData); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters