Implement multiaddr format compatible with scheme used in ethereum

This commit is contained in:
Dmitry 2018-07-12 13:34:18 +03:00
commit 66df000be0
6 changed files with 352 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
vendor/

242
Gopkg.lock generated Normal file
View File

@ -0,0 +1,242 @@
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
[[projects]]
branch = "master"
digest = "1:cafb561ce87d0eaa309ad6853380d437df3c1142561c5afa700311825aa38df1"
name = "github.com/btcsuite/btcd"
packages = ["btcec"]
pruneopts = "T"
revision = "fdfc19097e7ac6b57035062056f5b7b4638b8898"
[[projects]]
digest = "1:3aa953edddec96fd00285789ccd4a31efaff0a2979a3e35b77f5c19d5eaa37f7"
name = "github.com/davecgh/go-spew"
packages = ["spew"]
pruneopts = "T"
revision = "346938d642f2ec3594ed81d874461961cd0faa76"
version = "v1.1.0"
[[projects]]
digest = "1:68ab6afdca4a2b711034ccb97ba084e05de6e4469ae9969ce7a855954db54551"
name = "github.com/ethereum/go-ethereum"
packages = [
"common",
"common/hexutil",
"common/math",
"crypto",
"crypto/secp256k1",
"crypto/sha3",
"log",
"p2p/discover",
"p2p/nat",
"p2p/netutil",
"rlp",
]
pruneopts = "T"
revision = "37685930d953bcbe023f9bc65b135a8d8b8f1488"
version = "v1.8.12"
[[projects]]
digest = "1:c4a2528ccbcabf90f9f3c464a5fc9e302d592861bbfd0b7135a7de8a943d0406"
name = "github.com/go-stack/stack"
packages = ["."]
pruneopts = "T"
revision = "259ab82a6cad3992b4e21ff5cac294ccb06474bc"
version = "v1.7.0"
[[projects]]
branch = "master"
digest = "1:968462840e6d86b12990015ac6ab297c022ccde102953040724be1df0e9e6c96"
name = "github.com/golang/snappy"
packages = ["."]
pruneopts = "T"
revision = "2e65f85255dbc3072edf28d6b5b8efc472979f5a"
[[projects]]
branch = "master"
digest = "1:2b7df6363912cb5b639ecc36a0324399f8a62dff3d831cadd7ea15d834240a54"
name = "github.com/gxed/hashland"
packages = ["keccakpg"]
pruneopts = "T"
revision = "d9f6b97f8db22dd1e090fd0bbbe98f09cc7dd0a8"
[[projects]]
branch = "master"
digest = "1:003733378951572c9172596bbc9634b6b55754718926886089999fc164cb2dea"
name = "github.com/huin/goupnp"
packages = [
".",
"dcps/internetgateway1",
"dcps/internetgateway2",
"httpu",
"scpd",
"soap",
"ssdp",
]
pruneopts = "T"
revision = "1395d1447324cbea88d249fbfcfd70ea878fdfca"
[[projects]]
digest = "1:32b82e71cf24f8b78323e0d7903c4b90278486283965aa2a19b1ea13763b8f34"
name = "github.com/jackpal/go-nat-pmp"
packages = ["."]
pruneopts = "T"
revision = "c9cfead9f2a36ddf3daa40ba269aa7f4bbba6b62"
version = "v1.0.1"
[[projects]]
branch = "master"
digest = "1:130cefe87d7eeefc824978dcb78e35672d4c49a11f25c153fbf0cfd952756fa3"
name = "github.com/minio/blake2b-simd"
packages = ["."]
pruneopts = "T"
revision = "3f5f724cb5b182a5c278d6d3d55b40e7f8c2efb4"
[[projects]]
branch = "master"
digest = "1:17e048d1af8833670439d1ad0c92305fc1ad503e5d03f37ee667c0074df0fd03"
name = "github.com/minio/sha256-simd"
packages = ["."]
pruneopts = "T"
revision = "ad98a36ba0da87206e3378c556abbfeaeaa98668"
[[projects]]
branch = "master"
digest = "1:d69d2d7bf5f2894c77b54eaffe6af81d94fe481104092d73c2fd93aca0c1e137"
name = "github.com/mr-tron/base58"
packages = ["base58"]
pruneopts = "T"
revision = "4df4dc6e86a912614d09719d10cad427b087cbfb"
[[projects]]
digest = "1:6b479546b26e436cda3601673ccfd7f355a1ffb84c78ca748d9b706fcd8f3788"
name = "github.com/multiformats/go-multiaddr"
packages = ["."]
pruneopts = "T"
revision = "f36800afeb9c141e1adb7da099e6f010dfd4c419"
[[projects]]
digest = "1:e2fb0a8b4205e9714e6e1224414209a969db1383d37b99be5b2a88c8b29b1d27"
name = "github.com/multiformats/go-multihash"
packages = ["."]
pruneopts = "T"
revision = "8be2a682ab9f254311de1375145a2f78a809b07d"
version = "v1.0.8"
[[projects]]
digest = "1:22aa691fe0213cb5c07d103f9effebcb7ad04bee45a0ce5fe5369d0ca2ec3a1f"
name = "github.com/pmezard/go-difflib"
packages = ["difflib"]
pruneopts = "T"
revision = "792786c7400a136282c1664665ae0a8db921c6c2"
version = "v1.0.0"
[[projects]]
digest = "1:919bb3aa6d9d0b67648c219fa4925312bc3c2872da19e818fa769e9c97a2b643"
name = "github.com/spaolacci/murmur3"
packages = ["."]
pruneopts = "T"
revision = "9f5d223c60793748f04a9d5b4b4eacddfc1f755d"
version = "v1.1"
[[projects]]
digest = "1:c7f05297d9ad389d81e6d764388d97c4b6a64665eff9fd2550fbdd8545430b80"
name = "github.com/stretchr/testify"
packages = [
"assert",
"require",
]
pruneopts = "T"
revision = "f35b8ab0b5a2cef36673838d662e249dd9c94686"
version = "v1.2.2"
[[projects]]
branch = "master"
digest = "1:9c39a878048f4a5468675b814fb7d2528d622f8c3612511ff0b5e2a48d451ad2"
name = "github.com/syndtr/goleveldb"
packages = [
"leveldb",
"leveldb/cache",
"leveldb/comparer",
"leveldb/errors",
"leveldb/filter",
"leveldb/iterator",
"leveldb/journal",
"leveldb/memdb",
"leveldb/opt",
"leveldb/storage",
"leveldb/table",
"leveldb/util",
]
pruneopts = "T"
revision = "c4c61651e9e37fa117f53c5a906d3b63090d8445"
[[projects]]
branch = "master"
digest = "1:2c4971d2da7bb27fa225a119dc96af2119dd096869c1228438a0b5fda5f6fe15"
name = "golang.org/x/crypto"
packages = [
"blake2s",
"sha3",
]
pruneopts = "T"
revision = "a49355c7e3f8fe157a85be2f77e6e269a0f89602"
[[projects]]
branch = "master"
digest = "1:d6898619e5c42d9ce801d56c471250879946ae80dfd5940beff172126a3e8ad1"
name = "golang.org/x/net"
packages = [
"html",
"html/atom",
"html/charset",
]
pruneopts = "T"
revision = "cffdcf672aee934982473246bc7e9a8ba446aa9b"
[[projects]]
branch = "master"
digest = "1:cf05848629661fe016ffdf4b18febeb06986760cb4b70df246b16bc81f9a302e"
name = "golang.org/x/sys"
packages = ["cpu"]
pruneopts = "T"
revision = "1b2967e3c290b7c545b3db0deeda16e9be4f98a2"
[[projects]]
digest = "1:24db346d9931fe01f1e9a02aba78ba22c1ecd55bf0f79dd10ba5169719cf002d"
name = "golang.org/x/text"
packages = [
"encoding",
"encoding/charmap",
"encoding/htmlindex",
"encoding/internal",
"encoding/internal/identifier",
"encoding/japanese",
"encoding/korean",
"encoding/simplifiedchinese",
"encoding/traditionalchinese",
"encoding/unicode",
"internal/gen",
"internal/tag",
"internal/utf8internal",
"language",
"runes",
"transform",
"unicode/cldr",
]
pruneopts = "T"
revision = "f21a4dfb5e38f5895301dc265a8def02365cc3d0"
version = "v0.3.0"
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
input-imports = [
"github.com/ethereum/go-ethereum/crypto",
"github.com/ethereum/go-ethereum/p2p/discover",
"github.com/multiformats/go-multiaddr",
"github.com/stretchr/testify/require",
]
solver-name = "gps-cdcl"
solver-version = 1

38
Gopkg.toml Normal file
View File

@ -0,0 +1,38 @@
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true
# 1.2.7
[[constraint]]
name = "github.com/multiformats/go-multiaddr"
revision = "f36800afeb9c141e1adb7da099e6f010dfd4c419"
[prune]
go-tests = true
[[constraint]]
name = "github.com/ethereum/go-ethereum"
version = "1.8.12"

15
README.md Normal file
View File

@ -0,0 +1,15 @@
ETHv4 multiaddr library
=======================
ETHv4 mutliaddr format adds a spec that is compatible with enode definition, for example:
peer with identity 436cc6f674928fdc9a9f7990f2944002b685d1c37f025c1be425185b5b1f0900feaf1ccc2a6130268f9901be4a7d252f37302c8335a2c1a62736e9232691cc3a ip 174.138.105.243 and tcp port 30404 can be defined as enode:
```
enode://436cc6f674928fdc9a9f7990f2944002b685d1c37f025c1be425185b5b1f0900feaf1ccc2a6130268f9901be4a7d252f37302c8335a2c1a62736e9232691cc3a@174.138.105.243:30404
```
or as multiaddr
```
/ip4/174.138.105.243/tcp/30404/ethv4/436cc6f674928fdc9a9f7990f2944002b685d1c37f025c1be425185b5b1f0900feaf1ccc2a6130268f9901be4a7d252f37302c8335a2c1a62736e9232691cc3a
```

34
protocol.go Normal file
View File

@ -0,0 +1,34 @@
package ethv4
import (
"github.com/ethereum/go-ethereum/p2p/discover"
ma "github.com/multiformats/go-multiaddr"
)
const (
P_ETHv4 = 0x01EA
)
func init() {
if err := ma.AddProtocol(ma.Protocol{P_ETHv4, discover.NodeIDBits, "ethv4", ma.CodeToVarint(P_ETHv4), false, TranscoderETHv4}); err != nil {
panic(err)
}
}
var TranscoderETHv4 = ma.NewTranscoderFromFunctions(ethv4StB, ethv4BtS)
func ethv4StB(s string) ([]byte, error) {
id, err := discover.HexID(s)
if err != nil {
return nil, err
}
return id.Bytes(), err
}
func ethv4BtS(b []byte) (string, error) {
id, err := discover.BytesID(b)
if err != nil {
return "", err
}
return id.String(), err
}

22
protocol_test.go Normal file
View File

@ -0,0 +1,22 @@
package ethv4
import (
"testing"
"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())
require.NoError(t, err)
rst, err := addr.ValueForProtocol(P_ETHv4)
require.NoError(t, err)
nodeid, err := discover.HexID(rst)
require.NoError(t, err)
require.Equal(t, discover.PubkeyID(&key.PublicKey), nodeid)
}