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
This commit is contained in:
prathamesh0 2023-02-14 11:27:36 +05:30 committed by GitHub
parent 197aec74aa
commit 0d22915d59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 94 additions and 73 deletions

View File

@ -2,7 +2,7 @@
"packages": [ "packages": [
"packages/*" "packages/*"
], ],
"version": "0.2.27", "version": "0.2.28",
"npmClient": "yarn", "npmClient": "yarn",
"useWorkspaces": true, "useWorkspaces": true,
"command": { "command": {

View File

@ -1,6 +1,6 @@
{ {
"name": "@cerc-io/address-watcher", "name": "@cerc-io/address-watcher",
"version": "0.2.27", "version": "0.2.28",
"description": "Address Watcher", "description": "Address Watcher",
"private": true, "private": true,
"scripts": { "scripts": {
@ -26,11 +26,11 @@
"homepage": "https://github.com/cerc-io/watcher-ts#readme", "homepage": "https://github.com/cerc-io/watcher-ts#readme",
"dependencies": { "dependencies": {
"@apollo/client": "^3.3.19", "@apollo/client": "^3.3.19",
"@cerc-io/cache": "^0.2.27", "@cerc-io/cache": "^0.2.28",
"@cerc-io/ipld-eth-client": "^0.2.27", "@cerc-io/ipld-eth-client": "^0.2.28",
"@cerc-io/solidity-mapper": "^0.2.27", "@cerc-io/solidity-mapper": "^0.2.28",
"@cerc-io/tracing-client": "^0.2.27", "@cerc-io/tracing-client": "^0.2.28",
"@cerc-io/util": "^0.2.27", "@cerc-io/util": "^0.2.28",
"@types/lodash": "^4.14.168", "@types/lodash": "^4.14.168",
"debug": "^4.3.1", "debug": "^4.3.1",
"ethers": "^5.4.4", "ethers": "^5.4.4",

View File

@ -1,6 +1,6 @@
{ {
"name": "@cerc-io/cache", "name": "@cerc-io/cache",
"version": "0.2.27", "version": "0.2.28",
"description": "Generic object cache", "description": "Generic object cache",
"main": "dist/index.js", "main": "dist/index.js",
"scripts": { "scripts": {

View File

@ -1,6 +1,6 @@
{ {
"name": "@cerc-io/cli", "name": "@cerc-io/cli",
"version": "0.2.27", "version": "0.2.28",
"main": "dist/index.js", "main": "dist/index.js",
"license": "AGPL-3.0", "license": "AGPL-3.0",
"scripts": { "scripts": {
@ -11,8 +11,8 @@
"chat": "node dist/chat.js" "chat": "node dist/chat.js"
}, },
"dependencies": { "dependencies": {
"@cerc-io/peer": "^0.2.27", "@cerc-io/peer": "^0.2.28",
"@cerc-io/util": "^0.2.27", "@cerc-io/util": "^0.2.28",
"@ethersproject/providers": "^5.4.4", "@ethersproject/providers": "^5.4.4",
"@graphql-tools/utils": "^9.1.1", "@graphql-tools/utils": "^9.1.1",
"@ipld/dag-cbor": "^8.0.0", "@ipld/dag-cbor": "^8.0.0",

View File

@ -1,6 +1,6 @@
{ {
"name": "@cerc-io/codegen", "name": "@cerc-io/codegen",
"version": "0.2.27", "version": "0.2.28",
"description": "Code generator", "description": "Code generator",
"private": true, "private": true,
"main": "index.js", "main": "index.js",
@ -20,7 +20,7 @@
}, },
"homepage": "https://github.com/cerc-io/watcher-ts#readme", "homepage": "https://github.com/cerc-io/watcher-ts#readme",
"dependencies": { "dependencies": {
"@cerc-io/util": "^0.2.27", "@cerc-io/util": "^0.2.28",
"@graphql-tools/load-files": "^6.5.2", "@graphql-tools/load-files": "^6.5.2",
"@poanet/solidity-flattener": "https://github.com/vulcanize/solidity-flattener.git", "@poanet/solidity-flattener": "https://github.com/vulcanize/solidity-flattener.git",
"@solidity-parser/parser": "^0.13.2", "@solidity-parser/parser": "^0.13.2",

View File

@ -41,12 +41,12 @@
"dependencies": { "dependencies": {
"@apollo/client": "^3.3.19", "@apollo/client": "^3.3.19",
"@ethersproject/providers": "^5.4.4", "@ethersproject/providers": "^5.4.4",
"@cerc-io/cli": "^0.2.27", "@cerc-io/cli": "^0.2.28",
"@cerc-io/ipld-eth-client": "^0.2.27", "@cerc-io/ipld-eth-client": "^0.2.28",
"@cerc-io/solidity-mapper": "^0.2.27", "@cerc-io/solidity-mapper": "^0.2.28",
"@cerc-io/util": "^0.2.27", "@cerc-io/util": "^0.2.28",
{{#if (subgraphPath)}} {{#if (subgraphPath)}}
"@cerc-io/graph-node": "^0.2.27", "@cerc-io/graph-node": "^0.2.28",
{{/if}} {{/if}}
"apollo-type-bigint": "^0.1.3", "apollo-type-bigint": "^0.1.3",
"debug": "^4.3.1", "debug": "^4.3.1",

View File

@ -1,6 +1,6 @@
{ {
"name": "@cerc-io/eden-watcher", "name": "@cerc-io/eden-watcher",
"version": "0.2.27", "version": "0.2.28",
"description": "eden-watcher", "description": "eden-watcher",
"private": true, "private": true,
"main": "dist/index.js", "main": "dist/index.js",
@ -38,11 +38,11 @@
"homepage": "https://github.com/cerc-io/watcher-ts#readme", "homepage": "https://github.com/cerc-io/watcher-ts#readme",
"dependencies": { "dependencies": {
"@apollo/client": "^3.3.19", "@apollo/client": "^3.3.19",
"@cerc-io/cli": "^0.2.27", "@cerc-io/cli": "^0.2.28",
"@cerc-io/graph-node": "^0.2.27", "@cerc-io/graph-node": "^0.2.28",
"@cerc-io/ipld-eth-client": "^0.2.27", "@cerc-io/ipld-eth-client": "^0.2.28",
"@cerc-io/solidity-mapper": "^0.2.27", "@cerc-io/solidity-mapper": "^0.2.28",
"@cerc-io/util": "^0.2.27", "@cerc-io/util": "^0.2.28",
"@ethersproject/providers": "^5.4.4", "@ethersproject/providers": "^5.4.4",
"apollo-type-bigint": "^0.1.3", "apollo-type-bigint": "^0.1.3",
"debug": "^4.3.1", "debug": "^4.3.1",

View File

@ -1,6 +1,6 @@
{ {
"name": "@cerc-io/erc20-watcher", "name": "@cerc-io/erc20-watcher",
"version": "0.2.27", "version": "0.2.28",
"description": "ERC20 Watcher", "description": "ERC20 Watcher",
"private": true, "private": true,
"main": "dist/index.js", "main": "dist/index.js",
@ -42,10 +42,10 @@
"homepage": "https://github.com/cerc-io/watcher-ts#readme", "homepage": "https://github.com/cerc-io/watcher-ts#readme",
"dependencies": { "dependencies": {
"@apollo/client": "^3.3.19", "@apollo/client": "^3.3.19",
"@cerc-io/cli": "^0.2.27", "@cerc-io/cli": "^0.2.28",
"@cerc-io/ipld-eth-client": "^0.2.27", "@cerc-io/ipld-eth-client": "^0.2.28",
"@cerc-io/solidity-mapper": "^0.2.27", "@cerc-io/solidity-mapper": "^0.2.28",
"@cerc-io/util": "^0.2.27", "@cerc-io/util": "^0.2.28",
"@ethersproject/providers": "^5.4.4", "@ethersproject/providers": "^5.4.4",
"apollo-type-bigint": "^0.1.3", "apollo-type-bigint": "^0.1.3",
"debug": "^4.3.1", "debug": "^4.3.1",

View File

@ -1,6 +1,6 @@
{ {
"name": "@cerc-io/erc721-watcher", "name": "@cerc-io/erc721-watcher",
"version": "0.2.27", "version": "0.2.28",
"description": "erc721-watcher", "description": "erc721-watcher",
"private": true, "private": true,
"main": "dist/index.js", "main": "dist/index.js",
@ -47,10 +47,10 @@
"homepage": "https://github.com/cerc-io/watcher-ts#readme", "homepage": "https://github.com/cerc-io/watcher-ts#readme",
"dependencies": { "dependencies": {
"@apollo/client": "^3.3.19", "@apollo/client": "^3.3.19",
"@cerc-io/cli": "^0.2.27", "@cerc-io/cli": "^0.2.28",
"@cerc-io/ipld-eth-client": "^0.2.27", "@cerc-io/ipld-eth-client": "^0.2.28",
"@cerc-io/solidity-mapper": "^0.2.27", "@cerc-io/solidity-mapper": "^0.2.28",
"@cerc-io/util": "^0.2.27", "@cerc-io/util": "^0.2.28",
"@ethersproject/providers": "^5.4.4", "@ethersproject/providers": "^5.4.4",
"apollo-type-bigint": "^0.1.3", "apollo-type-bigint": "^0.1.3",
"debug": "^4.3.1", "debug": "^4.3.1",

View File

@ -1,10 +1,10 @@
{ {
"name": "@cerc-io/graph-node", "name": "@cerc-io/graph-node",
"version": "0.2.27", "version": "0.2.28",
"main": "dist/index.js", "main": "dist/index.js",
"license": "AGPL-3.0", "license": "AGPL-3.0",
"devDependencies": { "devDependencies": {
"@cerc-io/solidity-mapper": "^0.2.27", "@cerc-io/solidity-mapper": "^0.2.28",
"@ethersproject/providers": "^5.4.4", "@ethersproject/providers": "^5.4.4",
"@graphprotocol/graph-ts": "^0.22.0", "@graphprotocol/graph-ts": "^0.22.0",
"@nomiclabs/hardhat-ethers": "^2.0.2", "@nomiclabs/hardhat-ethers": "^2.0.2",
@ -50,9 +50,9 @@
"dependencies": { "dependencies": {
"@apollo/client": "^3.3.19", "@apollo/client": "^3.3.19",
"@cerc-io/assemblyscript": "0.19.10-watcher-ts-0.1.2", "@cerc-io/assemblyscript": "0.19.10-watcher-ts-0.1.2",
"@cerc-io/cache": "^0.2.27", "@cerc-io/cache": "^0.2.28",
"@cerc-io/ipld-eth-client": "^0.2.27", "@cerc-io/ipld-eth-client": "^0.2.28",
"@cerc-io/util": "^0.2.27", "@cerc-io/util": "^0.2.28",
"@types/json-diff": "^0.5.2", "@types/json-diff": "^0.5.2",
"bn.js": "^4.11.9", "bn.js": "^4.11.9",
"debug": "^4.3.1", "debug": "^4.3.1",

View File

@ -1,6 +1,6 @@
{ {
"name": "@cerc-io/graph-test-watcher", "name": "@cerc-io/graph-test-watcher",
"version": "0.2.27", "version": "0.2.28",
"description": "graph-test-watcher", "description": "graph-test-watcher",
"private": true, "private": true,
"main": "dist/index.js", "main": "dist/index.js",
@ -38,11 +38,11 @@
"homepage": "https://github.com/cerc-io/watcher-ts#readme", "homepage": "https://github.com/cerc-io/watcher-ts#readme",
"dependencies": { "dependencies": {
"@apollo/client": "^3.3.19", "@apollo/client": "^3.3.19",
"@cerc-io/cli": "^0.2.27", "@cerc-io/cli": "^0.2.28",
"@cerc-io/graph-node": "^0.2.27", "@cerc-io/graph-node": "^0.2.28",
"@cerc-io/ipld-eth-client": "^0.2.27", "@cerc-io/ipld-eth-client": "^0.2.28",
"@cerc-io/solidity-mapper": "^0.2.27", "@cerc-io/solidity-mapper": "^0.2.28",
"@cerc-io/util": "^0.2.27", "@cerc-io/util": "^0.2.28",
"@ethersproject/providers": "^5.4.4", "@ethersproject/providers": "^5.4.4",
"apollo-type-bigint": "^0.1.3", "apollo-type-bigint": "^0.1.3",
"debug": "^4.3.1", "debug": "^4.3.1",

View File

@ -1,6 +1,6 @@
{ {
"name": "@cerc-io/ipld-eth-client", "name": "@cerc-io/ipld-eth-client",
"version": "0.2.27", "version": "0.2.28",
"description": "IPLD ETH Client", "description": "IPLD ETH Client",
"main": "dist/index.js", "main": "dist/index.js",
"scripts": { "scripts": {
@ -20,7 +20,7 @@
"homepage": "https://github.com/cerc-io/watcher-ts#readme", "homepage": "https://github.com/cerc-io/watcher-ts#readme",
"dependencies": { "dependencies": {
"@apollo/client": "^3.7.1", "@apollo/client": "^3.7.1",
"@cerc-io/cache": "^0.2.27", "@cerc-io/cache": "^0.2.28",
"cross-fetch": "^3.1.4", "cross-fetch": "^3.1.4",
"debug": "^4.3.1", "debug": "^4.3.1",
"ethers": "^5.4.4", "ethers": "^5.4.4",

View File

@ -1,6 +1,6 @@
{ {
"name": "@cerc-io/mobymask-watcher", "name": "@cerc-io/mobymask-watcher",
"version": "0.2.27", "version": "0.2.28",
"description": "mobymask-watcher", "description": "mobymask-watcher",
"private": true, "private": true,
"main": "dist/index.js", "main": "dist/index.js",
@ -37,10 +37,10 @@
"homepage": "https://github.com/cerc-io/watcher-ts#readme", "homepage": "https://github.com/cerc-io/watcher-ts#readme",
"dependencies": { "dependencies": {
"@apollo/client": "^3.3.19", "@apollo/client": "^3.3.19",
"@cerc-io/cli": "^0.2.27", "@cerc-io/cli": "^0.2.28",
"@cerc-io/ipld-eth-client": "^0.2.27", "@cerc-io/ipld-eth-client": "^0.2.28",
"@cerc-io/solidity-mapper": "^0.2.27", "@cerc-io/solidity-mapper": "^0.2.28",
"@cerc-io/util": "^0.2.27", "@cerc-io/util": "^0.2.28",
"@ethersproject/providers": "^5.4.4", "@ethersproject/providers": "^5.4.4",
"apollo-type-bigint": "^0.1.3", "apollo-type-bigint": "^0.1.3",
"debug": "^4.3.1", "debug": "^4.3.1",

View File

@ -1,6 +1,6 @@
{ {
"name": "@cerc-io/peer", "name": "@cerc-io/peer",
"version": "0.2.27", "version": "0.2.28",
"description": "libp2p module", "description": "libp2p module",
"main": "dist/index.js", "main": "dist/index.js",
"exports": "./dist/index.js", "exports": "./dist/index.js",
@ -34,6 +34,7 @@
"@libp2p/peer-id-factory": "^2.0.0", "@libp2p/peer-id-factory": "^2.0.0",
"@libp2p/pubsub-peer-discovery": "^8.0.0", "@libp2p/pubsub-peer-discovery": "^8.0.0",
"@multiformats/multiaddr": "^11.1.4", "@multiformats/multiaddr": "^11.1.4",
"buffer": "^6.0.3",
"debug": "^4.3.1", "debug": "^4.3.1",
"it-length-prefixed": "^8.0.4", "it-length-prefixed": "^8.0.4",
"it-map": "^2.0.0", "it-map": "^2.0.0",

View File

@ -2,27 +2,19 @@
// Copyright 2022 Vulcanize, Inc. // Copyright 2022 Vulcanize, Inc.
// //
import assert from 'assert';
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
import { hideBin } from 'yargs/helpers'; import { hideBin } from 'yargs/helpers';
import yargs from 'yargs'; import yargs from 'yargs';
import { createEd25519PeerId } from '@libp2p/peer-id-factory'; import { createPeerId } from './index.js';
interface Arguments { interface Arguments {
file: string; file: string;
} }
async function main (): Promise<void> { async function main (): Promise<void> {
const peerId = await createEd25519PeerId(); const obj = await createPeerId();
assert(peerId.privateKey);
const obj = {
id: peerId.toString(),
privKey: Buffer.from(peerId.privateKey).toString('base64'),
pubKey: Buffer.from(peerId.publicKey).toString('base64')
};
const argv: Arguments = _getArgv(); const argv: Arguments = _getArgv();
if (argv.file) { if (argv.file) {
@ -34,7 +26,7 @@ async function main (): Promise<void> {
} }
fs.writeFileSync(exportFilePath, JSON.stringify(obj, null, 2)); 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 { } else {
console.log(obj); console.log(obj);
} }

View File

@ -6,6 +6,7 @@ import { createLibp2p, Libp2p } from '@cerc-io/libp2p';
// For nodejs. // For nodejs.
import wrtc from 'wrtc'; import wrtc from 'wrtc';
import assert from 'assert'; import assert from 'assert';
import { Buffer } from 'buffer';
import { pipe } from 'it-pipe'; import { pipe } from 'it-pipe';
import * as lp from 'it-length-prefixed'; import * as lp from 'it-length-prefixed';
import map from 'it-map'; 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 { PeerInfo } from '@libp2p/interface-peer-info';
import type { Message } from '@libp2p/interface-pubsub'; import type { Message } from '@libp2p/interface-pubsub';
import type { PeerId } from '@libp2p/interface-peer-id'; import type { PeerId } from '@libp2p/interface-peer-id';
import { createFromJSON, createEd25519PeerId } from '@libp2p/peer-id-factory';
import { multiaddr, Multiaddr } from '@multiformats/multiaddr'; import { multiaddr, Multiaddr } from '@multiformats/multiaddr';
import { floodsub } from '@libp2p/floodsub'; import { floodsub } from '@libp2p/floodsub';
import { pubsubPeerDiscovery } from '@libp2p/pubsub-peer-discovery'; 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'; export const ERR_PROTOCOL_SELECTION = 'protocol selection failed';
type PeerIdObj = {
id: string
privKey: string
pubKey: string
};
export class Peer { export class Peer {
_node?: Libp2p _node?: Libp2p
_peerHeartbeatChecker?: PeerHearbeatChecker _peerHeartbeatChecker?: PeerHearbeatChecker
@ -71,9 +79,18 @@ export class Peer {
return this._node; return this._node;
} }
async init (maxRelayConnections = DEFAULT_MAX_RELAY_CONNECTIONS): Promise<void> { async init (
peerIdObj?: PeerIdObj,
maxRelayConnections = DEFAULT_MAX_RELAY_CONNECTIONS
): Promise<void> {
try { try {
let peerId: PeerId | undefined;
if (peerIdObj) {
peerId = await createFromJSON(peerIdObj);
}
this._node = await createLibp2p({ this._node = await createLibp2p({
peerId,
addresses: { addresses: {
// Use existing protocol id in multiaddr to listen through signalling channel to relay node // 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) // 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<PeerIdObj> {
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')
};
}

View File

@ -1,10 +1,10 @@
{ {
"name": "@cerc-io/solidity-mapper", "name": "@cerc-io/solidity-mapper",
"version": "0.2.27", "version": "0.2.28",
"main": "dist/index.js", "main": "dist/index.js",
"license": "AGPL-3.0", "license": "AGPL-3.0",
"devDependencies": { "devDependencies": {
"@cerc-io/ipld-eth-client": "^0.2.27", "@cerc-io/ipld-eth-client": "^0.2.28",
"@ethersproject/abi": "^5.3.0", "@ethersproject/abi": "^5.3.0",
"@nomiclabs/hardhat-ethers": "^2.0.2", "@nomiclabs/hardhat-ethers": "^2.0.2",
"@nomiclabs/hardhat-waffle": "^2.0.1", "@nomiclabs/hardhat-waffle": "^2.0.1",

View File

@ -1,6 +1,6 @@
{ {
"name": "@cerc-io/test", "name": "@cerc-io/test",
"version": "0.2.27", "version": "0.2.28",
"main": "dist/index.js", "main": "dist/index.js",
"license": "AGPL-3.0", "license": "AGPL-3.0",
"private": true, "private": true,

View File

@ -1,6 +1,6 @@
{ {
"name": "@cerc-io/tracing-client", "name": "@cerc-io/tracing-client",
"version": "0.2.27", "version": "0.2.28",
"description": "ETH VM tracing client", "description": "ETH VM tracing client",
"private": true, "private": true,
"main": "dist/index.js", "main": "dist/index.js",

View File

@ -1,11 +1,11 @@
{ {
"name": "@cerc-io/util", "name": "@cerc-io/util",
"version": "0.2.27", "version": "0.2.28",
"main": "dist/index.js", "main": "dist/index.js",
"license": "AGPL-3.0", "license": "AGPL-3.0",
"dependencies": { "dependencies": {
"@apollo/utils.keyvaluecache": "^1.0.1", "@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", "@ethersproject/providers": "^5.4.4",
"@graphql-tools/schema": "^9.0.10", "@graphql-tools/schema": "^9.0.10",
"@graphql-tools/utils": "^9.1.1", "@graphql-tools/utils": "^9.1.1",
@ -37,8 +37,8 @@
"yargs": "^17.0.1" "yargs": "^17.0.1"
}, },
"devDependencies": { "devDependencies": {
"@cerc-io/cache": "^0.2.27", "@cerc-io/cache": "^0.2.28",
"@cerc-io/ipld-eth-client": "^0.2.27", "@cerc-io/ipld-eth-client": "^0.2.28",
"@nomiclabs/hardhat-waffle": "^2.0.1", "@nomiclabs/hardhat-waffle": "^2.0.1",
"@types/express": "^4.17.14", "@types/express": "^4.17.14",
"@types/fs-extra": "^9.0.11", "@types/fs-extra": "^9.0.11",