Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PeerId config to commonwealth, add create-peer-id.js #9353

Merged
merged 5 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion libs/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@
"lint-diff": "NODE_OPTIONS=\"--max-old-space-size=4096\" eslint -c ../../.eslintrc-diff.cjs './src/**/*.{ts,tsx}'"
},
"dependencies": {
"@canvas-js/core": "^0.10.10",
"@canvas-js/chain-cosmos": "^0.10.10",
"@canvas-js/chain-ethereum": "^0.10.10",
"@canvas-js/chain-solana": "^0.10.10",
"@canvas-js/chain-substrate": "^0.10.10",
"@canvas-js/core": "^0.10.10",
"@canvas-js/gossiplog": "^0.10.10",
"@canvas-js/interfaces": "^0.10.10",
"@canvas-js/signatures": "^0.10.10",
"@ipld/dag-json": "^10.2.0",
"@libp2p/peer-id-factory": "^4.2.4",
"moment": "^2.23.0",
"safe-stable-stringify": "^2.4.2"
},
Expand Down
13 changes: 11 additions & 2 deletions libs/shared/src/canvas/runtime/node.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { Canvas } from '@canvas-js/core';
import {
createEd25519PeerId,
createFromProtobuf,
} from '@libp2p/peer-id-factory';

import { getSessionSigners } from '../signers';
import { contract, contractTopic } from './contract';

export const CANVAS_TOPIC = contractTopic;

export const startCanvasNode = async () => {
export const startCanvasNode = async (config: { PEER_ID?: string }) => {
const path =
process.env.FEDERATION_POSTGRES_DB_URL ??
(process.env.APP_ENV === 'local'
Expand All @@ -16,7 +20,12 @@ export const startCanvasNode = async () => {
const listen =
process.env.FEDERATION_LISTEN_ADDRESS ?? '/ip4/127.0.0.1/tcp/8090/ws';

const peerId = config.PEER_ID
? await createFromProtobuf(Buffer.from(config.PEER_ID, 'base64'))
: await createEd25519PeerId();

const app = await Canvas.initialize({
peerId,
topic: contractTopic,
path,
contract,
Expand All @@ -26,7 +35,7 @@ export const startCanvasNode = async () => {
listen: [listen],
});

if (process.env.START_LIBP2P) {
if (config.PEER_ID) {
await app.libp2p.start();
}

Expand Down
1 change: 1 addition & 0 deletions packages/commonwealth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
"@knocklabs/react": "^0.2.15",
"@knocklabs/react-notification-feed": "^0.8.15",
"@lexical/rich-text": "^0.17.0",
"@libp2p/peer-id-factory": "^4.2.4",
"@magic-ext/cosmos": "^12.1.3",
"@magic-ext/oauth": "^11.1.1",
"@magic-sdk/admin": "^2.4.0",
Expand Down
7 changes: 7 additions & 0 deletions packages/commonwealth/scripts/create-peer-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env node
timolegros marked this conversation as resolved.
Show resolved Hide resolved

import { createEd25519PeerId, exportToProtobuf } from '@libp2p/peer-id-factory';

const id = await createEd25519PeerId();
console.log(`# ${id}`);
raykyri marked this conversation as resolved.
Show resolved Hide resolved
console.log(`PEER_ID=${Buffer.from(exportToProtobuf(id)).toString('base64')}`);
3 changes: 3 additions & 0 deletions packages/commonwealth/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const {
EVM_CE_POLL_INTERVAL,
CF_ZONE_ID,
CF_API_KEY,
PEER_ID,
} = process.env;

const NO_PRERENDER = _NO_PRERENDER;
Expand Down Expand Up @@ -108,6 +109,7 @@ export const config = configure(
10,
),
},
PEER_ID,
},
z.object({
NO_PRERENDER: z.boolean(),
Expand Down Expand Up @@ -207,5 +209,6 @@ export const config = configure(
MESSAGE_RELAYER_PREFETCH: z.number().int().positive(),
EVM_CE_POLL_INTERVAL_MS: z.number().int().positive(),
}),
PEER_ID: z.string().optional(),
}),
);
3 changes: 2 additions & 1 deletion packages/commonwealth/server/federation/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { logger } from '@hicommonwealth/core';
import { CanvasSignedData, startCanvasNode } from '@hicommonwealth/shared';
import { parse } from '@ipld/dag-json';
import { config } from '../config';

const log = logger(import.meta);
export const canvas = await startCanvasNode();
export const canvas = await startCanvasNode(config);

log.info(
'canvas: started libp2p with multiaddrs: ' +
Expand Down
7 changes: 7 additions & 0 deletions packages/network-explorer/create-peer-id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env node
timolegros marked this conversation as resolved.
Show resolved Hide resolved

import { createEd25519PeerId, exportToProtobuf } from '@libp2p/peer-id-factory';

const id = await createEd25519PeerId();
console.log(`# ${id}`);
console.log(`PEER_ID=${Buffer.from(exportToProtobuf(id)).toString('base64')}`);
Loading
Loading