mirror of
https://github.com/wharfkit/example-p2pclient.git
synced 2026-07-21 18:03:36 +00:00
Initial commit
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"root": true,
|
||||
"ignorePatterns": ["node_modules/**"],
|
||||
"extends": [
|
||||
"eslint:recommended",
|
||||
"plugin:@typescript-eslint/eslint-recommended",
|
||||
"plugin:@typescript-eslint/recommended",
|
||||
"plugin:prettier/recommended"
|
||||
],
|
||||
"rules": {
|
||||
"prettier/prettier": "warn",
|
||||
"no-console": "warn",
|
||||
"sort-imports": [
|
||||
"warn",
|
||||
{
|
||||
"ignoreCase": true,
|
||||
"ignoreDeclarationSort": true
|
||||
}
|
||||
],
|
||||
"@typescript-eslint/no-empty-interface": "off", // TODO: This should be removed before PR #1
|
||||
"@typescript-eslint/explicit-module-boundary-types": "off",
|
||||
"@typescript-eslint/no-explicit-any": "off",
|
||||
"@typescript-eslint/no-namespace": "off",
|
||||
"@typescript-eslint/no-non-null-assertion": "off",
|
||||
"@typescript-eslint/no-empty-function": "warn",
|
||||
"no-inner-declarations": "off"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
node_modules/
|
||||
@@ -0,0 +1,8 @@
|
||||
arrowParens: "always"
|
||||
bracketSpacing: false
|
||||
endOfLine: "lf"
|
||||
printWidth: 100
|
||||
semi: false
|
||||
singleQuote: true
|
||||
tabWidth: 4
|
||||
trailingComma: "es5"
|
||||
@@ -0,0 +1,5 @@
|
||||
# @wharfkit/example-p2pclient
|
||||
|
||||
A simple (and somewhat dirty) script for nodejs that establishes a p2pclient and listens to activity on the jungle network.
|
||||
|
||||
It can be run with `yarn install` and then `node index.js`.
|
||||
@@ -0,0 +1,82 @@
|
||||
const {Socket} = require('net')
|
||||
const {
|
||||
APIClient,
|
||||
Checksum256,
|
||||
FetchProvider,
|
||||
P2P,
|
||||
P2PClient,
|
||||
PrivateKey,
|
||||
SimpleEnvelopeP2PProvider,
|
||||
} = require('@wharfkit/antelope')
|
||||
const fetch = require('node-fetch')
|
||||
|
||||
const socket = new Socket()
|
||||
socket.connect(9876, 'jungle4.greymass.com')
|
||||
|
||||
const client = new P2PClient({
|
||||
provider: new SimpleEnvelopeP2PProvider(socket),
|
||||
})
|
||||
|
||||
// Establish API Client and embedding fetch for nodejs below v18
|
||||
const fetchProvider = new FetchProvider('https://jungle4.api.eosnation.io', {fetch})
|
||||
const apiClient = new APIClient({provider: fetchProvider})
|
||||
|
||||
async function run() {
|
||||
// Request current chain state via get_info call to sync from this point forward
|
||||
const info = await apiClient.v1.chain.get_info()
|
||||
const token = Checksum256.hash(info.head_block_time.value.byteArray)
|
||||
|
||||
// Generate a key pair for usage in our messages
|
||||
const privateKey = PrivateKey.generate('K1')
|
||||
const publicKey = privateKey.toPublic()
|
||||
|
||||
// Assemble the P2P.HandshakeMessage
|
||||
const handshake = P2P.HandshakeMessage.from({
|
||||
networkVersion: 0xfe,
|
||||
chainId: info.chain_id,
|
||||
nodeId: Checksum256.hash(publicKey.data),
|
||||
key: publicKey,
|
||||
time: info.head_block_time.value,
|
||||
token,
|
||||
sig: privateKey.signDigest(token),
|
||||
p2pAddress: 'none',
|
||||
lastIrreversibleBlockNumber: info.last_irreversible_block_num,
|
||||
lastIrreversibleBlockId: info.last_irreversible_block_id,
|
||||
headNum: info.head_block_num,
|
||||
headId: info.head_block_id,
|
||||
os: 'nodejs',
|
||||
agent: 'wharfkit/antelope',
|
||||
generation: 4,
|
||||
})
|
||||
|
||||
// Send the connected client the message
|
||||
client.send(handshake)
|
||||
|
||||
client.on('message', (msg) => {
|
||||
// Each message received has a type and data
|
||||
const {value} = msg
|
||||
// For the sake of seeing socket activity, dump everything to the console
|
||||
console.log(value)
|
||||
// Switch based on message type
|
||||
switch (value.constructor) {
|
||||
// If we receive a time_message...
|
||||
case P2P.TimeMessage: {
|
||||
// Assemble a response using the current time
|
||||
const payload = P2P.TimeMessage.from({
|
||||
org: Date.now(),
|
||||
rec: 0,
|
||||
xmt: 0,
|
||||
dst: 0,
|
||||
})
|
||||
// Respond to the peer to let them know this connection is alive
|
||||
client.send(payload)
|
||||
break
|
||||
}
|
||||
default: {
|
||||
break
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
run()
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"name": "@wharfkit/example-p2pclient",
|
||||
"version": "1.0.0",
|
||||
"description": "An example using the P2PClient from the @wharfkit/antelope library in a nodejs application",
|
||||
"main": "index.js",
|
||||
"repository": "https://github.com/wharfkit/example-p2pclient",
|
||||
"author": "Aaron Cox <aaron@greymass.com>",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@wharfkit/antelope": "^0.7.3",
|
||||
"node-fetch": "2.6.1"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
|
||||
# yarn lockfile v1
|
||||
|
||||
|
||||
"@wharfkit/antelope@^0.7.3":
|
||||
version "0.7.3"
|
||||
resolved "https://registry.yarnpkg.com/@wharfkit/antelope/-/antelope-0.7.3.tgz#408f6c587f4f5990d4b55596c10be2e976798641"
|
||||
integrity sha512-pyUmuXUpLQh1RVpJVIcbVUHTwV/DQ+MI0nlfWDBHIICdYf6XidZtQmaHB7JEXiFzlS8T7S9Xc5VOTOQU8dnl3Q==
|
||||
dependencies:
|
||||
bn.js "^4.11.9"
|
||||
brorand "^1.1.0"
|
||||
elliptic "^6.5.4"
|
||||
hash.js "^1.0.0"
|
||||
tslib "^2.0.3"
|
||||
|
||||
bn.js@^4.11.9:
|
||||
version "4.12.0"
|
||||
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.12.0.tgz#775b3f278efbb9718eec7361f483fb36fbbfea88"
|
||||
integrity sha512-c98Bf3tPniI+scsdk237ku1Dc3ujXQTSgyiPUDEOe7tRkhrqridvh8klBv0HCEso1OLOYcHuCv/cS6DNxKH+ZA==
|
||||
|
||||
brorand@^1.1.0:
|
||||
version "1.1.0"
|
||||
resolved "https://registry.yarnpkg.com/brorand/-/brorand-1.1.0.tgz#12c25efe40a45e3c323eb8675a0a0ce57b22371f"
|
||||
integrity sha512-cKV8tMCEpQs4hK/ik71d6LrPOnpkpGBR0wzxqr68g2m/LB2GxVYQroAjMJZRVM1Y4BCjCKc3vAamxSzOY2RP+w==
|
||||
|
||||
elliptic@^6.5.4:
|
||||
version "6.5.4"
|
||||
resolved "https://registry.yarnpkg.com/elliptic/-/elliptic-6.5.4.tgz#da37cebd31e79a1367e941b592ed1fbebd58abbb"
|
||||
integrity sha512-iLhC6ULemrljPZb+QutR5TQGB+pdW6KGD5RSegS+8sorOZT+rdQFbsQFJgvN3eRqNALqJer4oQ16YvJHlU8hzQ==
|
||||
dependencies:
|
||||
bn.js "^4.11.9"
|
||||
brorand "^1.1.0"
|
||||
hash.js "^1.0.0"
|
||||
hmac-drbg "^1.0.1"
|
||||
inherits "^2.0.4"
|
||||
minimalistic-assert "^1.0.1"
|
||||
minimalistic-crypto-utils "^1.0.1"
|
||||
|
||||
hash.js@^1.0.0, hash.js@^1.0.3:
|
||||
version "1.1.7"
|
||||
resolved "https://registry.yarnpkg.com/hash.js/-/hash.js-1.1.7.tgz#0babca538e8d4ee4a0f8988d68866537a003cf42"
|
||||
integrity sha512-taOaskGt4z4SOANNseOviYDvjEJinIkRgmp7LbKP2YTTmVxWBl87s/uzK9r+44BclBSp2X7K1hqeNfz9JbBeXA==
|
||||
dependencies:
|
||||
inherits "^2.0.3"
|
||||
minimalistic-assert "^1.0.1"
|
||||
|
||||
hmac-drbg@^1.0.1:
|
||||
version "1.0.1"
|
||||
resolved "https://registry.yarnpkg.com/hmac-drbg/-/hmac-drbg-1.0.1.tgz#d2745701025a6c775a6c545793ed502fc0c649a1"
|
||||
integrity sha512-Tti3gMqLdZfhOQY1Mzf/AanLiqh1WTiJgEj26ZuYQ9fbkLomzGchCws4FyrSd4VkpBfiNhaE1On+lOz894jvXg==
|
||||
dependencies:
|
||||
hash.js "^1.0.3"
|
||||
minimalistic-assert "^1.0.0"
|
||||
minimalistic-crypto-utils "^1.0.1"
|
||||
|
||||
inherits@^2.0.3, inherits@^2.0.4:
|
||||
version "2.0.4"
|
||||
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c"
|
||||
integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==
|
||||
|
||||
minimalistic-assert@^1.0.0, minimalistic-assert@^1.0.1:
|
||||
version "1.0.1"
|
||||
resolved "https://registry.yarnpkg.com/minimalistic-assert/-/minimalistic-assert-1.0.1.tgz#2e194de044626d4a10e7f7fbc00ce73e83e4d5c7"
|
||||
integrity sha512-UtJcAD4yEaGtjPezWuO9wC4nwUnVH/8/Im3yEHQP4b67cXlD/Qr9hdITCU1xDbSEXg2XKNaP8jsReV7vQd00/A==
|
||||
|
||||
minimalistic-crypto-utils@^1.0.1:
|
||||
version "1.0.1"
|
||||
resolved "https://registry.yarnpkg.com/minimalistic-crypto-utils/-/minimalistic-crypto-utils-1.0.1.tgz#f6c00c1c0b082246e5c4d99dfb8c7c083b2b582a"
|
||||
integrity sha512-JIYlbt6g8i5jKfJ3xz7rF0LXmv2TkDxBLUkiBeZ7bAx4GnnNMr8xFpGnOxn6GhTEHx3SjRrZEoU+j04prX1ktg==
|
||||
|
||||
node-fetch@2.6.1:
|
||||
version "2.6.1"
|
||||
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052"
|
||||
integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==
|
||||
|
||||
tslib@^2.0.3:
|
||||
version "2.6.1"
|
||||
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.6.1.tgz#fd8c9a0ff42590b25703c0acb3de3d3f4ede0410"
|
||||
integrity sha512-t0hLfiEKfMUoqhG+U1oid7Pva4bbDPHYfJNiB7BiIjRkj1pyC++4N3huJfqY6aRH6VTB0rvtzQwjM4K6qpfOig==
|
||||
Reference in New Issue
Block a user