-
Notifications
You must be signed in to change notification settings - Fork 1
Feature: Add isWeb3Checksummed function #62
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
Open
athegaul
wants to merge
7
commits into
main
Choose a base branch
from
feature/check-web3-address
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4cb43a1
Feature: Add isWeb3Checksummed function
athegaul 569907f
Merge branch 'main' into feature/check-web3-address
polds 9646edd
Update functions/is_web3_checksum.go
polds fb9e9e3
Update functions/is_web3_checksum.go
polds e0668b5
Update functions/is_web3_checksum.go
polds d49db6e
Update functions/is_web3_checksum.go
polds 50d6ee4
Merge branch 'main' into feature/check-web3-address
polds File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,115 @@ | ||
| // Copyright 2024 Peter Olds <me@polds.dev> | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package functions | ||
|
|
||
| import ( | ||
| "encoding/hex" | ||
| "fmt" | ||
| "reflect" | ||
| "strings" | ||
|
|
||
| "github.com/ethereum/go-ethereum/common" | ||
| "github.com/ethereum/go-ethereum/crypto" | ||
| "github.com/expr-lang/expr" | ||
| ) | ||
|
|
||
| // IsWeb3Checksummed is a function that checks whether the given address (or list of addresses) is checksummed. It is provided as an Expr function. | ||
| // It supports the following types: | ||
| // - string | ||
| // - []any (which should contain only string elements) | ||
|
|
||
| // Examples: | ||
| // - isWeb3Checksummed("0xb0F001C7F6C665b7b8e12F29EDC1107613fe980D") | ||
| // - isWeb3Checksummed(["0xb0F001C7F6C665b7b8e12F29EDC1107613fe980D", "0x3106E2e148525b3DB36795b04691D444c24972fB"]) | ||
| func IsWeb3Checksummed() expr.Option { | ||
| return expr.Function("isWeb3Checksummed", func(params ...any) (any, error) { | ||
| return isWeb3Checksummed(params[0]) | ||
| }, | ||
| new(func([]any) (bool, error)), | ||
| new(func(string) (bool, error)), | ||
| ) | ||
| } | ||
|
|
||
| func isWeb3Checksummed(v any) (any, error) { | ||
| if v == nil { | ||
| return false, nil | ||
| } | ||
|
|
||
| switch t := v.(type) { | ||
| case []any: | ||
| return arrayChecksummed(t) | ||
| case string: | ||
| return checksummed(t) | ||
| default: | ||
| return false, fmt.Errorf("type %s is not supported", reflect.TypeOf(v)) | ||
| } | ||
| } | ||
|
|
||
| func arrayChecksummed(v []any) (bool, error) { | ||
| switch t := v[0].(type) { | ||
| case string: | ||
| for _, address := range v { | ||
| res, err := checksummed(address.(string)) | ||
| if err != nil || !res { | ||
| return res, err | ||
| } | ||
| } | ||
| return true, nil | ||
| default: | ||
| return false, fmt.Errorf("unsupported type %T", t) | ||
| } | ||
| } | ||
|
|
||
| func checksummed(address string) (bool, error) { | ||
| if len(address) != 42 { | ||
| return false, fmt.Errorf("Address needs to be 42 characters long") | ||
polds marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| if !strings.HasPrefix(address, "0x") { | ||
| return false, fmt.Errorf("Address needs to start with 0x") | ||
polds marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return common.IsHexAddress(address) && checksumAddress(address) == address, nil | ||
| } | ||
|
|
||
| // Algorithm for checksumming a web3 address: | ||
| // - Convert the address to lowercase | ||
| // - Hash the address usinga keccak256 | ||
polds marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // - Take 40 characters of the hash, drop the rest (40 because of the address length) | ||
| // - Iterate through each character in the original address | ||
| // - If the checksum character >= 8 and character in the original address at the same idx is [a, f] then capitalize | ||
| // - Otherwise, add character | ||
| // | ||
| // For visualization, you can watch the following video: https://www.youtube.com/watch?v=2vH_CQ_rvbc | ||
| func checksumAddress(address string) string { | ||
| if strings.HasPrefix(address, "0x") { | ||
| address = address[2:] | ||
| } | ||
|
|
||
| lowercaseAddress := strings.ToLower(address) | ||
| hashedAddress := crypto.Keccak256([]byte(lowercaseAddress)) | ||
| checksum := hex.EncodeToString(hashedAddress)[:40] | ||
|
|
||
| var checksumAddress strings.Builder | ||
| for idx, char := range lowercaseAddress { | ||
| if checksum[idx] >= '8' && (char >= 'a' && char <= 'f') { | ||
| checksumAddress.WriteRune(char - 32) | ||
| } else { | ||
| checksumAddress.WriteRune(char) | ||
| } | ||
| } | ||
|
|
||
| return "0x" + checksumAddress.String() | ||
| } | ||
This file contains hidden or 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,118 @@ | ||
| // Copyright 2024 Peter Olds <me@polds.dev> | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package functions | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/expr-lang/expr" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestIsWeb3Checksummed(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| expr string | ||
| want bool | ||
| wantCompileErr bool | ||
| wantRuntimeErr bool | ||
| }{ | ||
| { | ||
| name: "nil", | ||
| expr: `isWeb3Checksummed(nil)`, | ||
| want: false, | ||
| }, | ||
| { | ||
| name: "string - not checksummed", | ||
| expr: `isWeb3Checksummed('0x30F4283a3d6302f968909Ff7c02ceCB2ac6C27Ac')`, | ||
| want: false, | ||
| }, | ||
| { | ||
| name: "string - checksummed", | ||
| expr: `isWeb3Checksummed('0x30D873664Ba766C983984C7AF9A921ccE36D34e1')`, | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "string slice - checksummed", | ||
| expr: `isWeb3Checksummed(['0x55028780918330FD00a34a61D9a7Efd3f43ca845', '0xAA95A3e367b427477bAdAB3d104f7D04ba158895'])`, | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "string slice - checksummed", | ||
| expr: `isWeb3Checksummed(['0x869C8ADA0fb9AfC753159b7D6D72Cc8bf58e6987', '0x2a92BCecd6e702702864E134821FD2DE73C3e180'])`, | ||
| want: false, | ||
| }, | ||
| { | ||
| name: "address needs to start with 0x", | ||
| expr: `isWeb3Checksummed('0034B03Cb9086d7D758AC55af71584F81A598759FE')`, | ||
| wantRuntimeErr: true, | ||
| }, | ||
| { | ||
| name: "address needs to be 42 characters long", | ||
| expr: `isWeb3Checksummed('34B03Cb9086d7D758AC55af71584F81A598759FE')`, | ||
| wantRuntimeErr: true, | ||
| }, | ||
| { | ||
| name: "unsupported type int", | ||
| expr: `isWeb3Checksummed(0)`, | ||
| wantCompileErr: true, | ||
| }, | ||
| { | ||
| name: "unsupported type int", | ||
| expr: `isWeb3Checksummed([0])`, | ||
| wantRuntimeErr: true, | ||
| }, | ||
| { | ||
| name: "not enough arguments", | ||
| expr: `isWeb3Checksummed()`, | ||
| wantCompileErr: true, | ||
| }, | ||
| } | ||
|
|
||
| opts := []expr.Option{ | ||
| expr.AsBool(), | ||
| expr.DisableAllBuiltins(), | ||
| IsWeb3Checksummed(), | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| program, err := expr.Compile(tc.expr, opts...) | ||
| if tc.wantCompileErr && err == nil { | ||
| require.Error(t, err) | ||
| } | ||
| if !tc.wantCompileErr && err != nil { | ||
| require.NoError(t, err) | ||
| } | ||
| if tc.wantCompileErr { | ||
| return | ||
| } | ||
|
|
||
| got, err := expr.Run(program, nil) | ||
| if tc.wantRuntimeErr && err == nil { | ||
| require.Error(t, err) | ||
| } | ||
| if !tc.wantRuntimeErr && err != nil { | ||
| require.NoError(t, err) | ||
| } | ||
| if tc.wantRuntimeErr { | ||
| return | ||
| } | ||
| assert.IsType(t, tc.want, got) | ||
| assert.Equal(t, tc.want, got) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.