JavaScript library for the Metaverse Blockchain P2P network
Install
Install base package
npm install metaversejs-p2p
Install a transport plugin
npm install metaversejs-transport-tcpip
Usage
Please see the examples directory for more complete examples.
Pool
Create a pool
const Pool =const P2PTransporterTCPIP =const pool = {}
Receive Messages
To access the incomming messages you can subscribe to $ingress
pool$ingress
Get Pool Actions
Pool actions are available on the $egress subject
pool$egress
Number Of Connected Peers
const q = pool
Peer
Connect to peer
It will automatically send the version message and respond with verack if version messages are received.
const Peer =const P2PTransportTCPIP =const transport = host: '198.199.84.199' port: 5251const peer =transporthost: '198.199.84.199'port: 5251relay: true
Peer Status
The current status is avalable as a variable.
const status = peercurrentStatus
You can also subscibe to status changes on $status
peer$status
Send Messages
The sendMessage function can send messages. The following example will send a Ping.
const PingMessage =peer
You can access all outgoing messages by subscribing to $egress
peer$egress
Receive
The received messages are available on the $ingress subject
peer$ingress
Messages
Messages are transferred as binary data.
length | content | |
---|---|---|
4 byte | NETWORK_MAGIC | mainnet value: 4d56534d |
12 byte | COMMAND | ascii encoded command |
4 byte | PAYLOAD LENGTH | Int32LE encoded |
4 byte | PAYLOAD CHECKSUM | first 4 bytes of sha256sha256(payload) |
rest | PAYLOAD |
To make message handling easier there are classes for the supported messages.
Ping
To keep a connection open you should send ping messages to the other peer.
const PingMessage =const message =nonce?: Buffer // if not defined a random nonce will be generated
Pong
Send pong messages as a response of another peer's ping message.
const PongMessage =const message =nonce: Buffer // must be the nonce of the previous ping received from the peer
Verack
After receiving a version message from another peer you need to answer with a verack message.
const VerackMessage =const message =
Version
The version message is the first message of a peer connecting to another peer. The version message contains information about both peers and the own services and state.
const VersionkMessage =const message =relay: true // receive unconfirmed transactionsversion: 70012 // versionservices: 1 // services your node providesstartHeight: 3514519 // your current bestblockheightsubversion: "/metaverse:0.9.0/" // subversion namenonce?: Buffer // optional - default: random buffertimestamp: "2020-03-29T19:11:25.000Z" // optional - default: current timestampaddrMe:ip: // works without providing real ip but nodes should try to provide real ipv4: '95.116.169.107' // your ipv4 addressv6: '0000:0000:0000:0000:0000:ffff:5f74:a96b' // your ipv6 addressport: 58616 // port numberservices: 1 // provided servicesaddrYou: // similar to addMeip:v4: '198.199.84.199'v6: '0000:0000:0000:0000:0000:ffff:c6c7:54c7'port: 5251services: 0
Inventory
Peers exchange information about each others transactions and blocks via inventory messages. The messages only contain the hashes of the transactions and blocks.
const InventoryMessage InventoryTx InventoryBlock =// can contain transactionsconst message =inventory:'3550047ed23a5e68c5fca0f6cb1ddd0df69998c73e0318dc9c57adb376784849''bfee531c274585ca3e222bc39e0fbad94b8e1a9b2cda09892d5e27d02caa3db9'// or blocksconst message =inventory:'a2e5e162a0b6d0faeb71ced40836b6fdf3715e4950d7ac9dd02bd80daea33de7''8f3cc621407f2b749bb807d855db6c0787a543d6956148684c0804279c35e8ce'
GetAddr
Ask the other peer for a list of other connected peers.
const GetAddrMessage =const message =
MemPool
Ask the other peer to send the inventory of the mempool.
const MemPoolMessage =const message =
Addr
Answer to the GetAddr message. It contains a list of addresses of the connected peers.
const AddrMessage =const message =addresses:timestamp:services: 1 // list of services that peer providesip:v6?: '0000:0000:0000:0000:0000:ffff:1b25:4427'v4?: '27.37.68.39'port: 5251// ... can and in most cases will contain more addresses
License
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.