good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
bor
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
open
bor
Commits
d42a56af
Commit
d42a56af
authored
Mar 2, 2017
by
Felix Lange
Browse files
Options
Downloads
Patches
Plain Diff
common: add UnprefixedHash, UnprefixedAddress
parent
b4547a56
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
common/types.go
+30
-14
30 additions, 14 deletions
common/types.go
with
30 additions
and
14 deletions
common/types.go
+
30
−
14
View file @
d42a56af
...
@@ -17,6 +17,7 @@
...
@@ -17,6 +17,7 @@
package
common
package
common
import
(
import
(
"encoding/hex"
"fmt"
"fmt"
"math/big"
"math/big"
"math/rand"
"math/rand"
...
@@ -30,13 +31,8 @@ const (
...
@@ -30,13 +31,8 @@ const (
AddressLength
=
20
AddressLength
=
20
)
)
type
(
// Hash represents the 32 byte Keccak256 hash of arbitrary data.
// Hash represents the 32 byte Keccak256 hash of arbitrary data.
Hash
[
HashLength
]
byte
type
Hash
[
HashLength
]
byte
// Address represents the 20 byte address of an Ethereum account.
Address
[
AddressLength
]
byte
)
func
BytesToHash
(
b
[]
byte
)
Hash
{
func
BytesToHash
(
b
[]
byte
)
Hash
{
var
h
Hash
var
h
Hash
...
@@ -113,7 +109,24 @@ func EmptyHash(h Hash) bool {
...
@@ -113,7 +109,24 @@ func EmptyHash(h Hash) bool {
return
h
==
Hash
{}
return
h
==
Hash
{}
}
}
// UnprefixedHash allows marshaling a Hash without 0x prefix.
type
UnprefixedHash
Hash
// UnmarshalText decodes the hash from hex. The 0x prefix is optional.
func
(
h
*
UnprefixedHash
)
UnmarshalText
(
input
[]
byte
)
error
{
return
hexutil
.
UnmarshalFixedUnprefixedText
(
"UnprefixedHash"
,
input
,
h
[
:
])
}
// MarshalText encodes the hash as hex.
func
(
h
UnprefixedHash
)
MarshalText
()
([]
byte
,
error
)
{
return
[]
byte
(
hex
.
EncodeToString
(
h
[
:
])),
nil
}
/////////// Address
/////////// Address
// Address represents the 20 byte address of an Ethereum account.
type
Address
[
AddressLength
]
byte
func
BytesToAddress
(
b
[]
byte
)
Address
{
func
BytesToAddress
(
b
[]
byte
)
Address
{
var
a
Address
var
a
Address
a
.
SetBytes
(
b
)
a
.
SetBytes
(
b
)
...
@@ -181,12 +194,15 @@ func (a *Address) UnmarshalText(input []byte) error {
...
@@ -181,12 +194,15 @@ func (a *Address) UnmarshalText(input []byte) error {
return
hexutil
.
UnmarshalFixedText
(
"Address"
,
input
,
a
[
:
])
return
hexutil
.
UnmarshalFixedText
(
"Address"
,
input
,
a
[
:
])
}
}
// PP Pretty Prints a byte slice in the following format:
// UnprefixedHash allows marshaling an Address without 0x prefix.
// hex(value[:4])...(hex[len(value)-4:])
type
UnprefixedAddress
Address
func
PP
(
value
[]
byte
)
string
{
if
len
(
value
)
<=
8
{
// UnmarshalText decodes the address from hex. The 0x prefix is optional.
return
Bytes2Hex
(
value
)
func
(
a
*
UnprefixedAddress
)
UnmarshalText
(
input
[]
byte
)
error
{
return
hexutil
.
UnmarshalFixedUnprefixedText
(
"UnprefixedAddress"
,
input
,
a
[
:
])
}
}
return
fmt
.
Sprintf
(
"%x...%x"
,
value
[
:
4
],
value
[
len
(
value
)
-
4
])
// MarshalText encodes the address as hex.
func
(
a
UnprefixedAddress
)
MarshalText
()
([]
byte
,
error
)
{
return
[]
byte
(
hex
.
EncodeToString
(
a
[
:
])),
nil
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment