Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
2c505efd
Commit
2c505efd
authored
Mar 02, 2015
by
Felix Lange
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
p2p/discover: add NodeID.Pubkey
parent
d344054e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
0 deletions
+33
-0
node.go
p2p/discover/node.go
+15
-0
node_test.go
p2p/discover/node_test.go
+18
-0
No files found.
p2p/discover/node.go
View file @
2c505efd
...
...
@@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"io"
"math/big"
"math/rand"
"net"
"net/url"
...
...
@@ -14,6 +15,7 @@ import (
"strings"
"time"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
"github.com/ethereum/go-ethereum/rlp"
)
...
...
@@ -187,6 +189,19 @@ func PubkeyID(pub *ecdsa.PublicKey) NodeID {
return
id
}
// Pubkey returns the public key represented by the node ID.
// It returns an error if the ID is not a point on the curve.
func
(
id
NodeID
)
Pubkey
()
(
*
ecdsa
.
PublicKey
,
error
)
{
p
:=
&
ecdsa
.
PublicKey
{
Curve
:
crypto
.
S256
(),
X
:
new
(
big
.
Int
),
Y
:
new
(
big
.
Int
)}
half
:=
len
(
id
)
/
2
p
.
X
.
SetBytes
(
id
[
:
half
])
p
.
Y
.
SetBytes
(
id
[
half
:
])
if
!
p
.
Curve
.
IsOnCurve
(
p
.
X
,
p
.
Y
)
{
return
nil
,
errors
.
New
(
"not a point on the S256 curve"
)
}
return
p
,
nil
}
// recoverNodeID computes the public key used to sign the
// given hash from the signature.
func
recoverNodeID
(
hash
,
sig
[]
byte
)
(
id
NodeID
,
err
error
)
{
...
...
p2p/discover/node_test.go
View file @
2c505efd
...
...
@@ -133,6 +133,24 @@ func TestNodeID_recover(t *testing.T) {
if
pub
!=
recpub
{
t
.
Errorf
(
"recovered wrong pubkey:
\n
got: %v
\n
want: %v"
,
recpub
,
pub
)
}
ecdsa
,
err
:=
pub
.
Pubkey
()
if
err
!=
nil
{
t
.
Errorf
(
"Pubkey error: %v"
,
err
)
}
if
!
reflect
.
DeepEqual
(
ecdsa
,
&
prv
.
PublicKey
)
{
t
.
Errorf
(
"Pubkey mismatch:
\n
got: %#v
\n
want: %#v"
,
ecdsa
,
&
prv
.
PublicKey
)
}
}
func
TestNodeID_pubkeyBad
(
t
*
testing
.
T
)
{
ecdsa
,
err
:=
NodeID
{}
.
Pubkey
()
if
err
==
nil
{
t
.
Error
(
"expected error for zero ID"
)
}
if
ecdsa
!=
nil
{
t
.
Error
(
"expected nil result"
)
}
}
func
TestNodeID_distcmp
(
t
*
testing
.
T
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment