Skip to content

Commit

Permalink
Add (PublicKey).Short method
Browse files Browse the repository at this point in the history
  • Loading branch information
gagliardetto committed Feb 1, 2022
1 parent 4a1d072 commit d205f44
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,25 @@ func (p PublicKey) String() string {
return base58.Encode(p[:])
}

// Short returns a shortened pubkey string,
// only including the first n chars, ellipsis, and the last n characters.
// NOTE: this is ONLY for visual representation for humans,
// and cannot be used for anything else.
func (p PublicKey) Short(n int) string {
return formatShortPubkey(n, p)
}

func formatShortPubkey(n int, pubkey PublicKey) string {
if n > 10 {
n = 10
}
if n < 3 {
n = 3
}
str := pubkey.String()
return str[:n] + "..." + str[len(str)-n:]
}

type PublicKeySlice []PublicKey

// UniqueAppend appends the provided pubkey only if it is not
Expand Down

0 comments on commit d205f44

Please sign in to comment.