From 0d22915d59bbc5a9a7ba3448aeec70a3a83ebc67 Mon Sep 17 00:00:00 2001 From: prathamesh0 <42446521+prathamesh0@users.noreply.github.com> Date: Tue, 14 Feb 2023 11:27:36 +0530 Subject: [PATCH] Add support to use stored peer id (#318) * Add support to use stored peer id * Upgrade package version * Add a type for peer id object * Use Buffer from buffer package for creating peer id --- lerna.json | 2 +- packages/address-watcher/package.json | 12 ++++---- packages/cache/package.json | 2 +- packages/cli/package.json | 6 ++-- packages/codegen/package.json | 4 +-- .../src/templates/package-template.handlebars | 10 +++---- packages/eden-watcher/package.json | 12 ++++---- packages/erc20-watcher/package.json | 10 +++---- packages/erc721-watcher/package.json | 10 +++---- packages/graph-node/package.json | 10 +++---- packages/graph-test-watcher/package.json | 12 ++++---- packages/ipld-eth-client/package.json | 4 +-- packages/mobymask-watcher/package.json | 10 +++---- packages/peer/package.json | 3 +- packages/peer/src/create-peer.ts | 14 ++------- packages/peer/src/index.ts | 30 ++++++++++++++++++- packages/solidity-mapper/package.json | 4 +-- packages/test/package.json | 2 +- packages/tracing-client/package.json | 2 +- packages/util/package.json | 8 ++--- 20 files changed, 94 insertions(+), 73 deletions(-) diff --git a/lerna.json b/lerna.json index 9fa6cd7f..83e20a22 100644 --- a/lerna.json +++ b/lerna.json @@ -2,7 +2,7 @@ "packages": [ "packages/*" ], - "version": "0.2.27", + "version": "0.2.28", "npmClient": "yarn", "useWorkspaces": true, "command": { diff --git a/packages/address-watcher/package.json b/packages/address-watcher/package.json index ea6a6825..0d80badc 100644 --- a/packages/address-watcher/package.json +++ b/packages/address-watcher/package.json @@ -1,6 +1,6 @@ { "name": "@cerc-io/address-watcher", - "version": "0.2.27", + "version": "0.2.28", "description": "Address Watcher", "private": true, "scripts": { @@ -26,11 +26,11 @@ "homepage": "https://github.com/cerc-io/watcher-ts#readme", "dependencies": { "@apollo/client": "^3.3.19", - "@cerc-io/cache": "^0.2.27", - "@cerc-io/ipld-eth-client": "^0.2.27", - "@cerc-io/solidity-mapper": "^0.2.27", - "@cerc-io/tracing-client": "^0.2.27", - "@cerc-io/util": "^0.2.27", + "@cerc-io/cache": "^0.2.28", + "@cerc-io/ipld-eth-client": "^0.2.28", + "@cerc-io/solidity-mapper": "^0.2.28", + "@cerc-io/tracing-client": "^0.2.28", + "@cerc-io/util": "^0.2.28", "@types/lodash": "^4.14.168", "debug": "^4.3.1", "ethers": "^5.4.4", diff --git a/packages/cache/package.json b/packages/cache/package.json index 21cb7ab8..ff59b251 100644 --- a/packages/cache/package.json +++ b/packages/cache/package.json @@ -1,6 +1,6 @@ { "name": "@cerc-io/cache", - "version": "0.2.27", + "version": "0.2.28", "description": "Generic object cache", "main": "dist/index.js", "scripts": { diff --git a/packages/cli/package.json b/packages/cli/package.json index 4e07f707..7b3ce9e4 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -1,6 +1,6 @@ { "name": "@cerc-io/cli", - "version": "0.2.27", + "version": "0.2.28", "main": "dist/index.js", "license": "AGPL-3.0", "scripts": { @@ -11,8 +11,8 @@ "chat": "node dist/chat.js" }, "dependencies": { - "@cerc-io/peer": "^0.2.27", - "@cerc-io/util": "^0.2.27", + "@cerc-io/peer": "^0.2.28", + "@cerc-io/util": "^0.2.28", "@ethersproject/providers": "^5.4.4", "@graphql-tools/utils": "^9.1.1", "@ipld/dag-cbor": "^8.0.0", diff --git a/packages/codegen/package.json b/packages/codegen/package.json index 5f83ae6e..3a62a3a9 100644 --- a/packages/codegen/package.json +++ b/packages/codegen/package.json @@ -1,6 +1,6 @@ { "name": "@cerc-io/codegen", - "version": "0.2.27", + "version": "0.2.28", "description": "Code generator", "private": true, "main": "index.js", @@ -20,7 +20,7 @@ }, "homepage": "https://github.com/cerc-io/watcher-ts#readme", "dependencies": { - "@cerc-io/util": "^0.2.27", + "@cerc-io/util": "^0.2.28", "@graphql-tools/load-files": "^6.5.2", "@poanet/solidity-flattener": "https://github.com/vulcanize/solidity-flattener.git", "@solidity-parser/parser": "^0.13.2", diff --git a/packages/codegen/src/templates/package-template.handlebars b/packages/codegen/src/templates/package-template.handlebars index 134866a9..31d8087b 100644 --- a/packages/codegen/src/templates/package-template.handlebars +++ b/packages/codegen/src/templates/package-template.handlebars @@ -41,12 +41,12 @@ "dependencies": { "@apollo/client": "^3.3.19", "@ethersproject/providers": "^5.4.4", - "@cerc-io/cli": "^0.2.27", - "@cerc-io/ipld-eth-client": "^0.2.27", - "@cerc-io/solidity-mapper": "^0.2.27", - "@cerc-io/util": "^0.2.27", + "@cerc-io/cli": "^0.2.28", + "@cerc-io/ipld-eth-client": "^0.2.28", + "@cerc-io/solidity-mapper": "^0.2.28", + "@cerc-io/util": "^0.2.28", {{#if (subgraphPath)}} - "@cerc-io/graph-node": "^0.2.27", + "@cerc-io/graph-node": "^0.2.28", {{/if}} "apollo-type-bigint": "^0.1.3", "debug": "^4.3.1", diff --git a/packages/eden-watcher/package.json b/packages/eden-watcher/package.json index df76eb70..f1877bfd 100644 --- a/packages/eden-watcher/package.json +++ b/packages/eden-watcher/package.json @@ -1,6 +1,6 @@ { "name": "@cerc-io/eden-watcher", - "version": "0.2.27", + "version": "0.2.28", "description": "eden-watcher", "private": true, "main": "dist/index.js", @@ -38,11 +38,11 @@ "homepage": "https://github.com/cerc-io/watcher-ts#readme", "dependencies": { "@apollo/client": "^3.3.19", - "@cerc-io/cli": "^0.2.27", - "@cerc-io/graph-node": "^0.2.27", - "@cerc-io/ipld-eth-client": "^0.2.27", - "@cerc-io/solidity-mapper": "^0.2.27", - "@cerc-io/util": "^0.2.27", + "@cerc-io/cli": "^0.2.28", + "@cerc-io/graph-node": "^0.2.28", + "@cerc-io/ipld-eth-client": "^0.2.28", + "@cerc-io/solidity-mapper": "^0.2.28", + "@cerc-io/util": "^0.2.28", "@ethersproject/providers": "^5.4.4", "apollo-type-bigint": "^0.1.3", "debug": "^4.3.1", diff --git a/packages/erc20-watcher/package.json b/packages/erc20-watcher/package.json index 9a409689..43dd0271 100644 --- a/packages/erc20-watcher/package.json +++ b/packages/erc20-watcher/package.json @@ -1,6 +1,6 @@ { "name": "@cerc-io/erc20-watcher", - "version": "0.2.27", + "version": "0.2.28", "description": "ERC20 Watcher", "private": true, "main": "dist/index.js", @@ -42,10 +42,10 @@ "homepage": "https://github.com/cerc-io/watcher-ts#readme", "dependencies": { "@apollo/client": "^3.3.19", - "@cerc-io/cli": "^0.2.27", - "@cerc-io/ipld-eth-client": "^0.2.27", - "@cerc-io/solidity-mapper": "^0.2.27", - "@cerc-io/util": "^0.2.27", + "@cerc-io/cli": "^0.2.28", + "@cerc-io/ipld-eth-client": "^0.2.28", + "@cerc-io/solidity-mapper": "^0.2.28", + "@cerc-io/util": "^0.2.28", "@ethersproject/providers": "^5.4.4", "apollo-type-bigint": "^0.1.3", "debug": "^4.3.1", diff --git a/packages/erc721-watcher/package.json b/packages/erc721-watcher/package.json index 470fa67d..90b332f6 100644 --- a/packages/erc721-watcher/package.json +++ b/packages/erc721-watcher/package.json @@ -1,6 +1,6 @@ { "name": "@cerc-io/erc721-watcher", - "version": "0.2.27", + "version": "0.2.28", "description": "erc721-watcher", "private": true, "main": "dist/index.js", @@ -47,10 +47,10 @@ "homepage": "https://github.com/cerc-io/watcher-ts#readme", "dependencies": { "@apollo/client": "^3.3.19", - "@cerc-io/cli": "^0.2.27", - "@cerc-io/ipld-eth-client": "^0.2.27", - "@cerc-io/solidity-mapper": "^0.2.27", - "@cerc-io/util": "^0.2.27", + "@cerc-io/cli": "^0.2.28", + "@cerc-io/ipld-eth-client": "^0.2.28", + "@cerc-io/solidity-mapper": "^0.2.28", + "@cerc-io/util": "^0.2.28", "@ethersproject/providers": "^5.4.4", "apollo-type-bigint": "^0.1.3", "debug": "^4.3.1", diff --git a/packages/graph-node/package.json b/packages/graph-node/package.json index 66af0279..1bf3c71e 100644 --- a/packages/graph-node/package.json +++ b/packages/graph-node/package.json @@ -1,10 +1,10 @@ { "name": "@cerc-io/graph-node", - "version": "0.2.27", + "version": "0.2.28", "main": "dist/index.js", "license": "AGPL-3.0", "devDependencies": { - "@cerc-io/solidity-mapper": "^0.2.27", + "@cerc-io/solidity-mapper": "^0.2.28", "@ethersproject/providers": "^5.4.4", "@graphprotocol/graph-ts": "^0.22.0", "@nomiclabs/hardhat-ethers": "^2.0.2", @@ -50,9 +50,9 @@ "dependencies": { "@apollo/client": "^3.3.19", "@cerc-io/assemblyscript": "0.19.10-watcher-ts-0.1.2", - "@cerc-io/cache": "^0.2.27", - "@cerc-io/ipld-eth-client": "^0.2.27", - "@cerc-io/util": "^0.2.27", + "@cerc-io/cache": "^0.2.28", + "@cerc-io/ipld-eth-client": "^0.2.28", + "@cerc-io/util": "^0.2.28", "@types/json-diff": "^0.5.2", "bn.js": "^4.11.9", "debug": "^4.3.1", diff --git a/packages/graph-test-watcher/package.json b/packages/graph-test-watcher/package.json index 753897ca..cf21b02c 100644 --- a/packages/graph-test-watcher/package.json +++ b/packages/graph-test-watcher/package.json @@ -1,6 +1,6 @@ { "name": "@cerc-io/graph-test-watcher", - "version": "0.2.27", + "version": "0.2.28", "description": "graph-test-watcher", "private": true, "main": "dist/index.js", @@ -38,11 +38,11 @@ "homepage": "https://github.com/cerc-io/watcher-ts#readme", "dependencies": { "@apollo/client": "^3.3.19", - "@cerc-io/cli": "^0.2.27", - "@cerc-io/graph-node": "^0.2.27", - "@cerc-io/ipld-eth-client": "^0.2.27", - "@cerc-io/solidity-mapper": "^0.2.27", - "@cerc-io/util": "^0.2.27", + "@cerc-io/cli": "^0.2.28", + "@cerc-io/graph-node": "^0.2.28", + "@cerc-io/ipld-eth-client": "^0.2.28", + "@cerc-io/solidity-mapper": "^0.2.28", + "@cerc-io/util": "^0.2.28", "@ethersproject/providers": "^5.4.4", "apollo-type-bigint": "^0.1.3", "debug": "^4.3.1", diff --git a/packages/ipld-eth-client/package.json b/packages/ipld-eth-client/package.json index 6de8faa0..365176c2 100644 --- a/packages/ipld-eth-client/package.json +++ b/packages/ipld-eth-client/package.json @@ -1,6 +1,6 @@ { "name": "@cerc-io/ipld-eth-client", - "version": "0.2.27", + "version": "0.2.28", "description": "IPLD ETH Client", "main": "dist/index.js", "scripts": { @@ -20,7 +20,7 @@ "homepage": "https://github.com/cerc-io/watcher-ts#readme", "dependencies": { "@apollo/client": "^3.7.1", - "@cerc-io/cache": "^0.2.27", + "@cerc-io/cache": "^0.2.28", "cross-fetch": "^3.1.4", "debug": "^4.3.1", "ethers": "^5.4.4", diff --git a/packages/mobymask-watcher/package.json b/packages/mobymask-watcher/package.json index 1214a099..d9695a46 100644 --- a/packages/mobymask-watcher/package.json +++ b/packages/mobymask-watcher/package.json @@ -1,6 +1,6 @@ { "name": "@cerc-io/mobymask-watcher", - "version": "0.2.27", + "version": "0.2.28", "description": "mobymask-watcher", "private": true, "main": "dist/index.js", @@ -37,10 +37,10 @@ "homepage": "https://github.com/cerc-io/watcher-ts#readme", "dependencies": { "@apollo/client": "^3.3.19", - "@cerc-io/cli": "^0.2.27", - "@cerc-io/ipld-eth-client": "^0.2.27", - "@cerc-io/solidity-mapper": "^0.2.27", - "@cerc-io/util": "^0.2.27", + "@cerc-io/cli": "^0.2.28", + "@cerc-io/ipld-eth-client": "^0.2.28", + "@cerc-io/solidity-mapper": "^0.2.28", + "@cerc-io/util": "^0.2.28", "@ethersproject/providers": "^5.4.4", "apollo-type-bigint": "^0.1.3", "debug": "^4.3.1", diff --git a/packages/peer/package.json b/packages/peer/package.json index 8bde6d92..73286833 100644 --- a/packages/peer/package.json +++ b/packages/peer/package.json @@ -1,6 +1,6 @@ { "name": "@cerc-io/peer", - "version": "0.2.27", + "version": "0.2.28", "description": "libp2p module", "main": "dist/index.js", "exports": "./dist/index.js", @@ -34,6 +34,7 @@ "@libp2p/peer-id-factory": "^2.0.0", "@libp2p/pubsub-peer-discovery": "^8.0.0", "@multiformats/multiaddr": "^11.1.4", + "buffer": "^6.0.3", "debug": "^4.3.1", "it-length-prefixed": "^8.0.4", "it-map": "^2.0.0", diff --git a/packages/peer/src/create-peer.ts b/packages/peer/src/create-peer.ts index e07e45e2..50d2ec4a 100644 --- a/packages/peer/src/create-peer.ts +++ b/packages/peer/src/create-peer.ts @@ -2,27 +2,19 @@ // Copyright 2022 Vulcanize, Inc. // -import assert from 'assert'; import fs from 'fs'; import path from 'path'; import { hideBin } from 'yargs/helpers'; import yargs from 'yargs'; -import { createEd25519PeerId } from '@libp2p/peer-id-factory'; +import { createPeerId } from './index.js'; interface Arguments { file: string; } async function main (): Promise { - const peerId = await createEd25519PeerId(); - assert(peerId.privateKey); - - const obj = { - id: peerId.toString(), - privKey: Buffer.from(peerId.privateKey).toString('base64'), - pubKey: Buffer.from(peerId.publicKey).toString('base64') - }; + const obj = await createPeerId(); const argv: Arguments = _getArgv(); if (argv.file) { @@ -34,7 +26,7 @@ async function main (): Promise { } fs.writeFileSync(exportFilePath, JSON.stringify(obj, null, 2)); - console.log(`Peer id ${peerId.toString()} exported to file ${exportFilePath}`); + console.log(`Peer id ${obj.id.toString()} exported to file ${exportFilePath}`); } else { console.log(obj); } diff --git a/packages/peer/src/index.ts b/packages/peer/src/index.ts index de7c8147..190f213f 100644 --- a/packages/peer/src/index.ts +++ b/packages/peer/src/index.ts @@ -6,6 +6,7 @@ import { createLibp2p, Libp2p } from '@cerc-io/libp2p'; // For nodejs. import wrtc from 'wrtc'; import assert from 'assert'; +import { Buffer } from 'buffer'; import { pipe } from 'it-pipe'; import * as lp from 'it-length-prefixed'; import map from 'it-map'; @@ -21,6 +22,7 @@ import type { Stream as P2PStream, Connection } from '@libp2p/interface-connecti import type { PeerInfo } from '@libp2p/interface-peer-info'; import type { Message } from '@libp2p/interface-pubsub'; import type { PeerId } from '@libp2p/interface-peer-id'; +import { createFromJSON, createEd25519PeerId } from '@libp2p/peer-id-factory'; import { multiaddr, Multiaddr } from '@multiformats/multiaddr'; import { floodsub } from '@libp2p/floodsub'; import { pubsubPeerDiscovery } from '@libp2p/pubsub-peer-discovery'; @@ -33,6 +35,12 @@ export const CHAT_PROTOCOL = '/chat/1.0.0'; export const ERR_PROTOCOL_SELECTION = 'protocol selection failed'; +type PeerIdObj = { + id: string + privKey: string + pubKey: string +}; + export class Peer { _node?: Libp2p _peerHeartbeatChecker?: PeerHearbeatChecker @@ -71,9 +79,18 @@ export class Peer { return this._node; } - async init (maxRelayConnections = DEFAULT_MAX_RELAY_CONNECTIONS): Promise { + async init ( + peerIdObj?: PeerIdObj, + maxRelayConnections = DEFAULT_MAX_RELAY_CONNECTIONS + ): Promise { try { + let peerId: PeerId | undefined; + if (peerIdObj) { + peerId = await createFromJSON(peerIdObj); + } + this._node = await createLibp2p({ + peerId, addresses: { // Use existing protocol id in multiaddr to listen through signalling channel to relay node // Allows direct webrtc connection to a peer if possible (eg. peers on a same network) @@ -474,3 +491,14 @@ export class Peer { }); } } + +export async function createPeerId (): Promise { + const peerId = await createEd25519PeerId(); + assert(peerId.privateKey); + + return { + id: peerId.toString(), + privKey: Buffer.from(peerId.privateKey).toString('base64'), + pubKey: Buffer.from(peerId.publicKey).toString('base64') + }; +} diff --git a/packages/solidity-mapper/package.json b/packages/solidity-mapper/package.json index 7dabf493..791c73d6 100644 --- a/packages/solidity-mapper/package.json +++ b/packages/solidity-mapper/package.json @@ -1,10 +1,10 @@ { "name": "@cerc-io/solidity-mapper", - "version": "0.2.27", + "version": "0.2.28", "main": "dist/index.js", "license": "AGPL-3.0", "devDependencies": { - "@cerc-io/ipld-eth-client": "^0.2.27", + "@cerc-io/ipld-eth-client": "^0.2.28", "@ethersproject/abi": "^5.3.0", "@nomiclabs/hardhat-ethers": "^2.0.2", "@nomiclabs/hardhat-waffle": "^2.0.1", diff --git a/packages/test/package.json b/packages/test/package.json index 501ecd7c..f535bbd8 100644 --- a/packages/test/package.json +++ b/packages/test/package.json @@ -1,6 +1,6 @@ { "name": "@cerc-io/test", - "version": "0.2.27", + "version": "0.2.28", "main": "dist/index.js", "license": "AGPL-3.0", "private": true, diff --git a/packages/tracing-client/package.json b/packages/tracing-client/package.json index a038fec6..5caa485e 100644 --- a/packages/tracing-client/package.json +++ b/packages/tracing-client/package.json @@ -1,6 +1,6 @@ { "name": "@cerc-io/tracing-client", - "version": "0.2.27", + "version": "0.2.28", "description": "ETH VM tracing client", "private": true, "main": "dist/index.js", diff --git a/packages/util/package.json b/packages/util/package.json index f2737dfd..264fee83 100644 --- a/packages/util/package.json +++ b/packages/util/package.json @@ -1,11 +1,11 @@ { "name": "@cerc-io/util", - "version": "0.2.27", + "version": "0.2.28", "main": "dist/index.js", "license": "AGPL-3.0", "dependencies": { "@apollo/utils.keyvaluecache": "^1.0.1", - "@cerc-io/solidity-mapper": "^0.2.27", + "@cerc-io/solidity-mapper": "^0.2.28", "@ethersproject/providers": "^5.4.4", "@graphql-tools/schema": "^9.0.10", "@graphql-tools/utils": "^9.1.1", @@ -37,8 +37,8 @@ "yargs": "^17.0.1" }, "devDependencies": { - "@cerc-io/cache": "^0.2.27", - "@cerc-io/ipld-eth-client": "^0.2.27", + "@cerc-io/cache": "^0.2.28", + "@cerc-io/ipld-eth-client": "^0.2.28", "@nomiclabs/hardhat-waffle": "^2.0.1", "@types/express": "^4.17.14", "@types/fs-extra": "^9.0.11",