accounts, p2p, rpc: make CGO_ENABLED=0 build again (#19593)

* p2p: remove direct import of cgo-library

* accounts, rpc: more nocgo alternatives

* rpc: move unix path constant into separate file

* accounts/scwallet: address review concerns, remove copy-pasta
This commit is contained in:
Martin Holst Swende 2019-05-26 00:07:10 +02:00 committed by Péter Szilágyi
parent 9efc1a847e
commit fec3b56f7f
6 changed files with 71 additions and 34 deletions

View File

@ -37,7 +37,6 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
pcsc "github.com/gballet/go-libpcsclite" pcsc "github.com/gballet/go-libpcsclite"
"github.com/status-im/keycard-go/derivationpath" "github.com/status-im/keycard-go/derivationpath"
@ -1050,33 +1049,25 @@ func (s *Session) sign(path accounts.DerivationPath, hash []byte) ([]byte, error
// determinePublicKey uses a signature and the X component of a public key to // determinePublicKey uses a signature and the X component of a public key to
// recover the entire public key. // recover the entire public key.
func determinePublicKey(sig, pubkeyX []byte) ([]byte, error) { func determinePublicKey(sig, pubkeyX []byte) ([]byte, error) {
for v := 0; v < 2; v++ { return makeRecoverableSignature(DerivationSignatureHash[:], sig, pubkeyX)
sig[64] = byte(v)
pubkey, err := crypto.Ecrecover(DerivationSignatureHash[:], sig)
if err == nil {
if bytes.Equal(pubkey, pubkeyX) {
return pubkey, nil
}
} else if v == 1 || err != secp256k1.ErrRecoverFailed {
return nil, err
}
}
return nil, ErrPubkeyMismatch
} }
// makeRecoverableSignature uses a signature and an expected public key to // makeRecoverableSignature uses a signature and an expected public key to
// recover the v value and produce a recoverable signature. // recover the v value and produce a recoverable signature.
func makeRecoverableSignature(hash, sig, expectedPubkey []byte) ([]byte, error) { func makeRecoverableSignature(hash, sig, expectedPubkey []byte) ([]byte, error) {
var libraryError error
for v := 0; v < 2; v++ { for v := 0; v < 2; v++ {
sig[64] = byte(v) sig[64] = byte(v)
pubkey, err := crypto.Ecrecover(hash, sig) if pubkey, err := crypto.Ecrecover(hash, sig); err == nil {
if err == nil {
if bytes.Equal(pubkey, expectedPubkey) { if bytes.Equal(pubkey, expectedPubkey) {
return sig, nil return sig, nil
} }
} else if v == 1 || err != secp256k1.ErrRecoverFailed { } else {
return nil, err libraryError = err
} }
} }
if libraryError != nil {
return nil, libraryError
}
return nil, ErrPubkeyMismatch return nil, ErrPubkeyMismatch
} }

View File

@ -25,7 +25,6 @@ import (
"github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
"github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethereum/go-ethereum/p2p/enode"
) )
@ -64,7 +63,7 @@ func (e encPubkey) id() enode.ID {
// recoverNodeKey computes the public key used to sign the // recoverNodeKey computes the public key used to sign the
// given hash from the signature. // given hash from the signature.
func recoverNodeKey(hash, sig []byte) (key encPubkey, err error) { func recoverNodeKey(hash, sig []byte) (key encPubkey, err error) {
pubkey, err := secp256k1.RecoverPubkey(hash, sig) pubkey, err := crypto.Ecrecover(hash, sig)
if err != nil { if err != nil {
return key, err return key, err
} }

View File

@ -38,7 +38,6 @@ import (
"github.com/ethereum/go-ethereum/common/bitutil" "github.com/ethereum/go-ethereum/common/bitutil"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/ecies" "github.com/ethereum/go-ethereum/crypto/ecies"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/golang/snappy" "github.com/golang/snappy"
"golang.org/x/crypto/sha3" "golang.org/x/crypto/sha3"
@ -400,7 +399,7 @@ func (h *encHandshake) handleAuthMsg(msg *authMsgV4, prv *ecdsa.PrivateKey) erro
return err return err
} }
signedMsg := xor(token, h.initNonce) signedMsg := xor(token, h.initNonce)
remoteRandomPub, err := secp256k1.RecoverPubkey(signedMsg, msg.Signature[:]) remoteRandomPub, err := crypto.Ecrecover(signedMsg, msg.Signature[:])
if err != nil { if err != nil {
return err return err
} }

33
rpc/constants_unix.go Normal file
View File

@ -0,0 +1,33 @@
// Copyright 2019 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
package rpc
/*
#include <sys/un.h>
int max_socket_path_size() {
struct sockaddr_un s;
return sizeof(s.sun_path);
}
*/
import "C"
var (
max_path_size = C.max_socket_path_size()
)

View File

@ -0,0 +1,25 @@
// Copyright 2019 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// +build !cgo,!windows
package rpc
var (
// On Linux, sun_path is 108 bytes in size
// see http://man7.org/linux/man-pages/man7/unix.7.html
max_path_size = 108
)

View File

@ -1,4 +1,4 @@
// Copyright 2015 The go-ethereum Authors // Copyright 2019 The go-ethereum Authors
// This file is part of the go-ethereum library. // This file is part of the go-ethereum library.
// //
// The go-ethereum library is free software: you can redistribute it and/or modify // The go-ethereum library is free software: you can redistribute it and/or modify
@ -28,20 +28,10 @@ import (
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
) )
/*
#include <sys/un.h>
int max_socket_path_size() {
struct sockaddr_un s;
return sizeof(s.sun_path);
}
*/
import "C"
// ipcListen will create a Unix socket on the given endpoint. // ipcListen will create a Unix socket on the given endpoint.
func ipcListen(endpoint string) (net.Listener, error) { func ipcListen(endpoint string) (net.Listener, error) {
if len(endpoint) > int(C.max_socket_path_size()) { if len(endpoint) > int(max_path_size) {
log.Warn(fmt.Sprintf("The ipc endpoint is longer than %d characters. ", C.max_socket_path_size()), log.Warn(fmt.Sprintf("The ipc endpoint is longer than %d characters. ", max_path_size),
"endpoint", endpoint) "endpoint", endpoint)
} }