Skip to content

Commit

Permalink
update canvas initialisation code, update code to generate peer ids t…
Browse files Browse the repository at this point in the history
…o use new libp2p api
  • Loading branch information
rjwebb committed Oct 9, 2024
1 parent 2986ed8 commit f621073
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
25 changes: 10 additions & 15 deletions libs/shared/src/canvas/runtime/node.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { Canvas } from '@canvas-js/core';
import {
createEd25519PeerId,
createFromProtobuf,
} from '@libp2p/peer-id-factory';
import { generateKeyPair, privateKeyFromProtobuf } from '@libp2p/crypto/keys';
import { ConnectionConfig } from 'pg';

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

export const CANVAS_TOPIC = contractTopic;
Expand All @@ -21,9 +17,9 @@ export const startCanvasNode = async (config: { PEER_ID?: string }) => {
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 privateKey = config.PEER_ID
? privateKeyFromProtobuf(Buffer.from(config.PEER_ID, 'base64'))
: await generateKeyPair('Ed25519');

let pgConnectionConfig: ConnectionConfig | undefined = undefined;

Expand All @@ -47,19 +43,18 @@ export const startCanvasNode = async (config: { PEER_ID?: string }) => {
}

const app = await Canvas.initialize({
peerId,
topic: contractTopic,
path: pgConnectionConfig!,
contract,
signers: getSessionSigners(),
bootstrapList: [],
});

await app.startLibp2p({
announce: [announce],
listen: [listen],
bootstrapList: [],
privateKey,
start: true,
});

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

return app;
};
14 changes: 10 additions & 4 deletions packages/commonwealth/scripts/create-peer-id.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { logger } from '@hicommonwealth/core';
import { createEd25519PeerId, exportToProtobuf } from '@libp2p/peer-id-factory';
import { generateKeyPair, privateKeyToProtobuf } from '@libp2p/crypto/keys';
import { peerIdFromPrivateKey } from '@libp2p/peer-id';

const log = logger(import.meta);

const id = await createEd25519PeerId();
log.info(`# ${id}`);
log.info(`PEER_ID=${Buffer.from(exportToProtobuf(id)).toString('base64')}`);
const privateKey = await generateKeyPair('Ed25519');

const peerId = peerIdFromPrivateKey(privateKey);

log.info(`# ${peerId}`);
log.info(
`PEER_ID=${Buffer.from(privateKeyToProtobuf(privateKey)).toString('base64')}`,
);

0 comments on commit f621073

Please sign in to comment.