-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: add function to mask address #12
Merged
+132
−0
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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,48 @@ | ||
package account | ||
|
||
import "fmt" | ||
|
||
var ( | ||
ErrInvalidAddress = fmt.Errorf("invalid Ethereum address") | ||
ErrInvalidNumVisibleChars = fmt.Errorf("number of visible characters must be even") | ||
ErrInvalidMaskedChars = fmt.Errorf("invalid number of masked characters") | ||
) | ||
|
||
// MaskAddress masks the middle part of an Ethereum address with a number of masked characters and returns the masked address. | ||
// The number of visible characters must be even. | ||
// The number of masked characters must be greater than 0. | ||
// The masked character can be any character. | ||
// @param address: The Ethereum address to mask | ||
// @param numVisibleChars: The number of visible characters to keep at the start and end of the address | ||
// @param maskedCharacter: The character to use for masking the middle part of the address | ||
// @return The masked address and error if any | ||
func MaskAddress(address string, numVisibleChars int, maskedCharacter rune) (string, error) { | ||
if !IsValidAddress(address) { | ||
return "", ErrInvalidAddress | ||
} | ||
|
||
if numVisibleChars%2 != 0 || numVisibleChars <= 0 { | ||
return "", ErrInvalidNumVisibleChars | ||
} | ||
|
||
prefixLength := len("0x") + numVisibleChars/2 // The number of characters to keep at the start (0x + first some hex digits) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm, sometimes go formating is weird https://go.screenpal.com/watch/cZifeCV8zdQ |
||
suffixLength := numVisibleChars / 2 // The number of characters to keep at the end (last some hex digits) | ||
|
||
addressLength := len(address) | ||
maskedCharacterCount := addressLength - prefixLength - suffixLength | ||
|
||
if maskedCharacterCount <= 0 { | ||
return "", ErrInvalidMaskedChars | ||
} | ||
|
||
// Create the masked part | ||
maskedPart := make([]rune, maskedCharacterCount) | ||
for i := range maskedPart { | ||
maskedPart[i] = maskedCharacter | ||
} | ||
|
||
// Construct the masked address | ||
maskedAddress := address[:prefixLength] + string(maskedPart) + address[addressLength-suffixLength:] | ||
|
||
return maskedAddress, nil | ||
} |
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,84 @@ | ||
package account | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMaskAddress(t *testing.T) { | ||
type args struct { | ||
address string | ||
numVisibleChars int | ||
maskedCharacter rune | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
wantErr error | ||
}{ | ||
{ | ||
name: "valid address", | ||
args: args{ | ||
address: "0x1234567890abcdef1234567890abcdef12345678", | ||
numVisibleChars: 8, | ||
maskedCharacter: 'x', | ||
}, | ||
want: "0x1234xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx5678", | ||
wantErr: nil, | ||
}, | ||
{ | ||
name: "invalid address", | ||
args: args{ | ||
address: "0x1234567890abcdef1234567890abcdef1234567", | ||
numVisibleChars: 8, | ||
maskedCharacter: 'x', | ||
}, | ||
want: "", | ||
wantErr: ErrInvalidAddress, | ||
}, | ||
{ | ||
name: "invalid number of visible characters", | ||
args: args{ | ||
address: "0x1234567890abcdef1234567890abcdef12345678", | ||
numVisibleChars: 7, | ||
maskedCharacter: 'x', | ||
}, | ||
want: "", | ||
wantErr: ErrInvalidNumVisibleChars, | ||
}, | ||
{ | ||
name: "invalid number of visible characters", | ||
args: args{ | ||
address: "0x1234567890abcdef1234567890abcdef12345678", | ||
numVisibleChars: -1, | ||
maskedCharacter: 'x', | ||
}, | ||
want: "", | ||
wantErr: ErrInvalidNumVisibleChars, | ||
}, | ||
{ | ||
name: "invalid number of masked characters", | ||
args: args{ | ||
address: "0x1234567890abcdef1234567890abcdef12345678", | ||
numVisibleChars: 40, | ||
maskedCharacter: 'x', | ||
}, | ||
want: "", | ||
wantErr: ErrInvalidMaskedChars, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := MaskAddress(tt.args.address, tt.args.numVisibleChars, tt.args.maskedCharacter) | ||
if err != nil { | ||
assert.ErrorIsf(t, err, tt.wantErr, "MaskAddress() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
|
||
assert.Equalf(t, tt.want, got, "MaskAddress() = %v, want %v", got, tt.want) | ||
}) | ||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with negative numVisibleChars, this method will throw panic
panic: runtime error: slice bounds out of range [44:42] [recovered]
panic: runtime error: slice bounds out of range [44:42]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, good catch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I pushed a commit to fix this