Use marshalling from libp2p-peer library as it is expected in libp2p comonents

This commit is contained in:
Dmitry 2018-07-13 14:21:49 +03:00
parent 66df000be0
commit becd46a4dd
4 changed files with 145 additions and 11 deletions

96
Gopkg.lock generated
View File

@ -1,6 +1,18 @@
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
[[projects]]
branch = "master"
digest = "1:5abd22a5ff4bf419426ec8cef91cb9620a6f527bcd13b8382f8859f990b66e6a"
name = "github.com/agl/ed25519"
packages = [
".",
"edwards25519",
"extra25519",
]
pruneopts = "T"
revision = "5312a61534124124185d41f09206b9fef1d88403"
[[projects]]
branch = "master"
digest = "1:cafb561ce87d0eaa309ad6853380d437df3c1142561c5afa700311825aa38df1"
@ -45,6 +57,14 @@
revision = "259ab82a6cad3992b4e21ff5cac294ccb06474bc"
version = "v1.7.0"
[[projects]]
digest = "1:2a1db9bae44464f781d3637b67df38e896c6e1b9c902e27d24ee9037cb50f23b"
name = "github.com/gogo/protobuf"
packages = ["proto"]
pruneopts = "T"
revision = "1adfc126b41513cc696b209667c8656ea7aac67c"
version = "v1.0.0"
[[projects]]
branch = "master"
digest = "1:968462840e6d86b12990015ac6ab297c022ccde102953040724be1df0e9e6c96"
@ -77,6 +97,19 @@
pruneopts = "T"
revision = "1395d1447324cbea88d249fbfcfd70ea878fdfca"
[[projects]]
digest = "1:dd959f2e5dcedd3ea55b7a02af1de22d27a7390896e33f32cb6f6cabe8918f84"
name = "github.com/ipfs/go-log"
packages = [
".",
"tracer",
"tracer/wire",
"writer",
]
pruneopts = "T"
revision = "5dc2060baaf8db344f31dafd852340b93811d03f"
version = "v1.5.3"
[[projects]]
digest = "1:32b82e71cf24f8b78323e0d7903c4b90278486283965aa2a19b1ea13763b8f34"
name = "github.com/jackpal/go-nat-pmp"
@ -85,6 +118,41 @@
revision = "c9cfead9f2a36ddf3daa40ba269aa7f4bbba6b62"
version = "v1.0.1"
[[projects]]
digest = "1:ab1a5449c35caf516f8a96576e73d0c2fc71a9061b0321c0e605396adcf73e6c"
name = "github.com/libp2p/go-libp2p-crypto"
packages = [
".",
"pb",
]
pruneopts = "T"
revision = "50487e0f0b4842e9e657317e689fbb8209fab2dc"
version = "v1.6.2"
[[projects]]
digest = "1:df084505ce8713372daaa6bb0b1f8cff204bbccdb900a8846df74d47c2177a63"
name = "github.com/libp2p/go-libp2p-peer"
packages = ["."]
pruneopts = "T"
revision = "a26c4b782bfe3b2570c539f69dc7777a45117a90"
version = "v2.3.5"
[[projects]]
digest = "1:01add77c3ed14d47fbf1d2a2182fd7737b4774959147700213066479465c280a"
name = "github.com/mattn/go-colorable"
packages = ["."]
pruneopts = "T"
revision = "167de6bfdfba052fa6b2d3664c8f5272e23c9072"
version = "v0.0.9"
[[projects]]
digest = "1:d4d17353dbd05cb52a2a52b7fe1771883b682806f68db442b436294926bbfafb"
name = "github.com/mattn/go-isatty"
packages = ["."]
pruneopts = "T"
revision = "0360b2af4f38e8d38c7fce2a9f4e702702d73a39"
version = "v0.0.3"
[[projects]]
branch = "master"
digest = "1:130cefe87d7eeefc824978dcb78e35672d4c49a11f25c153fbf0cfd952756fa3"
@ -124,6 +192,18 @@
revision = "8be2a682ab9f254311de1375145a2f78a809b07d"
version = "v1.0.8"
[[projects]]
digest = "1:fc7601e8a28dc9b963f9ad0367a6bf6a3467ea55553d4a1c31d728e2eb517a80"
name = "github.com/opentracing/opentracing-go"
packages = [
".",
"ext",
"log",
]
pruneopts = "T"
revision = "1949ddbfd147afd4d964a9f00b24eb291e0e7c38"
version = "v1.0.2"
[[projects]]
digest = "1:22aa691fe0213cb5c07d103f9effebcb7ad04bee45a0ce5fe5369d0ca2ec3a1f"
name = "github.com/pmezard/go-difflib"
@ -172,6 +252,14 @@
pruneopts = "T"
revision = "c4c61651e9e37fa117f53c5a906d3b63090d8445"
[[projects]]
branch = "master"
digest = "1:f3a0bbb2fc733a0e66554756f9afd14cbcad48c2a3aa80af3cb225e1b380139a"
name = "github.com/whyrusleeping/go-logging"
packages = ["."]
pruneopts = "T"
revision = "0457bb6b88fc1973573aaf6b5145d8d3ae972390"
[[projects]]
branch = "master"
digest = "1:2c4971d2da7bb27fa225a119dc96af2119dd096869c1228438a0b5fda5f6fe15"
@ -188,6 +276,7 @@
digest = "1:d6898619e5c42d9ce801d56c471250879946ae80dfd5940beff172126a3e8ad1"
name = "golang.org/x/net"
packages = [
"context",
"html",
"html/atom",
"html/charset",
@ -199,7 +288,10 @@
branch = "master"
digest = "1:cf05848629661fe016ffdf4b18febeb06986760cb4b70df246b16bc81f9a302e"
name = "golang.org/x/sys"
packages = ["cpu"]
packages = [
"cpu",
"unix",
]
pruneopts = "T"
revision = "1b2967e3c290b7c545b3db0deeda16e9be4f98a2"
@ -235,6 +327,8 @@
input-imports = [
"github.com/ethereum/go-ethereum/crypto",
"github.com/ethereum/go-ethereum/p2p/discover",
"github.com/libp2p/go-libp2p-crypto",
"github.com/libp2p/go-libp2p-peer",
"github.com/multiformats/go-multiaddr",
"github.com/stretchr/testify/require",
]

View File

@ -36,3 +36,11 @@
[[constraint]]
name = "github.com/ethereum/go-ethereum"
version = "1.8.12"
[[constraint]]
name = "github.com/libp2p/go-libp2p-peer"
version = "2.3.5"
[[constraint]]
name = "github.com/libp2p/go-libp2p-crypto"
version = "1.6.2"

View File

@ -1,8 +1,14 @@
package ethv4
import (
"crypto/ecdsa"
"errors"
"github.com/ethereum/go-ethereum/p2p/discover"
crypto "github.com/libp2p/go-libp2p-crypto"
peer "github.com/libp2p/go-libp2p-peer"
ma "github.com/multiformats/go-multiaddr"
mh "github.com/multiformats/go-multihash"
)
const (
@ -10,7 +16,7 @@ const (
)
func init() {
if err := ma.AddProtocol(ma.Protocol{P_ETHv4, discover.NodeIDBits, "ethv4", ma.CodeToVarint(P_ETHv4), false, TranscoderETHv4}); err != nil {
if err := ma.AddProtocol(ma.Protocol{P_ETHv4, 39 * 8, "ethv4", ma.CodeToVarint(P_ETHv4), false, TranscoderETHv4}); err != nil {
panic(err)
}
}
@ -18,17 +24,34 @@ func init() {
var TranscoderETHv4 = ma.NewTranscoderFromFunctions(ethv4StB, ethv4BtS)
func ethv4StB(s string) ([]byte, error) {
id, err := discover.HexID(s)
id, err := mh.FromB58String(s)
if err != nil {
return nil, err
}
return id.Bytes(), err
return id, err
}
func ethv4BtS(b []byte) (string, error) {
id, err := discover.BytesID(b)
id, err := mh.Cast(b)
if err != nil {
return "", err
}
return id.String(), err
return id.B58String(), err
}
// PeerIDToNodeID casts peer.ID (b58 encoded string) to discover.NodeID
func PeerIDToNodeID(pid string) (n discover.NodeID, err error) {
nodeid, err := peer.IDB58Decode(pid)
if err != nil {
return n, err
}
pubkey, err := nodeid.ExtractPublicKey()
if err != nil {
return n, err
}
seckey, ok := pubkey.(*crypto.Secp256k1PublicKey)
if !ok {
return n, errors.New("public key is not on the secp256k1 curve")
}
return discover.PubkeyID((*ecdsa.PublicKey)(seckey)), nil
}

View File

@ -1,22 +1,31 @@
package ethv4
import (
"crypto/ecdsa"
"math/rand"
"testing"
crypto "github.com/libp2p/go-libp2p-crypto"
peer "github.com/libp2p/go-libp2p-peer"
"github.com/stretchr/testify/require"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p/discover"
ma "github.com/multiformats/go-multiaddr"
)
func TestETHv4(t *testing.T) {
key, _ := crypto.GenerateKey()
addr, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/34012/ethv4/" + discover.PubkeyID(&key.PublicKey).String())
pkey, pubkey, err := crypto.GenerateKeyPairWithReader(crypto.Secp256k1, 2048, rand.New(rand.NewSource(1)))
require.NoError(t, err)
pid, err := peer.IDFromPublicKey(pkey.GetPublic())
require.NoError(t, err)
addr, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/34012/ethv4/" + pid.Pretty())
require.NoError(t, err)
rst, err := addr.ValueForProtocol(P_ETHv4)
require.NoError(t, err)
nodeid, err := discover.HexID(rst)
nid, err := PeerIDToNodeID(rst)
require.NoError(t, err)
require.Equal(t, discover.PubkeyID(&key.PublicKey), nodeid)
require.Equal(t,
discover.PubkeyID((*ecdsa.PublicKey)(pubkey.(*crypto.Secp256k1PublicKey))),
nid,
)
}