Got keychain working for first time

This commit is contained in:
Chris Troutner
2024-01-28 08:03:41 -08:00
parent d41d20cc81
commit 2724b500ee
3 changed files with 35 additions and 26 deletions
+7 -6
View File
@@ -25,6 +25,7 @@
"glob": "7.1.6", "glob": "7.1.6",
"helia": "2.1.0", "helia": "2.1.0",
"helia-coord": "1.2.23", "helia-coord": "1.2.23",
"interface-datastore": "8.2.10",
"jsonrpc-lite": "2.2.0", "jsonrpc-lite": "2.2.0",
"jsonwebtoken": "8.5.1", "jsonwebtoken": "8.5.1",
"jwt-bch-lib": "1.3.0", "jwt-bch-lib": "1.3.0",
@@ -8704,9 +8705,9 @@
} }
}, },
"node_modules/interface-datastore": { "node_modules/interface-datastore": {
"version": "8.2.9", "version": "8.2.10",
"resolved": "https://registry.npmjs.org/interface-datastore/-/interface-datastore-8.2.9.tgz", "resolved": "https://registry.npmjs.org/interface-datastore/-/interface-datastore-8.2.10.tgz",
"integrity": "sha512-J/8PN8TnB5xxCRtgu9Vx3zExdOzcTU5/DBF2dlU41deX1GW6/SPpbJo5DRNSnvzfjmwJ7YhUOIFXyccUp8nuAA==", "integrity": "sha512-D8RuxMdjOPB+j6WMDJ+I2aXTDzUT6DIVjgzo1E+ODL7w8WrSFl9FXD2SYmgj6vVzdb7Kb5qmAI9pEnDZJz7ifg==",
"dependencies": { "dependencies": {
"interface-store": "^5.0.0", "interface-store": "^5.0.0",
"uint8arrays": "^5.0.0" "uint8arrays": "^5.0.0"
@@ -25576,9 +25577,9 @@
} }
}, },
"interface-datastore": { "interface-datastore": {
"version": "8.2.9", "version": "8.2.10",
"resolved": "https://registry.npmjs.org/interface-datastore/-/interface-datastore-8.2.9.tgz", "resolved": "https://registry.npmjs.org/interface-datastore/-/interface-datastore-8.2.10.tgz",
"integrity": "sha512-J/8PN8TnB5xxCRtgu9Vx3zExdOzcTU5/DBF2dlU41deX1GW6/SPpbJo5DRNSnvzfjmwJ7YhUOIFXyccUp8nuAA==", "integrity": "sha512-D8RuxMdjOPB+j6WMDJ+I2aXTDzUT6DIVjgzo1E+ODL7w8WrSFl9FXD2SYmgj6vVzdb7Kb5qmAI9pEnDZJz7ifg==",
"requires": { "requires": {
"interface-store": "^5.0.0", "interface-store": "^5.0.0",
"uint8arrays": "^5.0.0" "uint8arrays": "^5.0.0"
+1
View File
@@ -40,6 +40,7 @@
"glob": "7.1.6", "glob": "7.1.6",
"helia": "2.1.0", "helia": "2.1.0",
"helia-coord": "1.2.23", "helia-coord": "1.2.23",
"interface-datastore": "8.2.10",
"jsonrpc-lite": "2.2.0", "jsonrpc-lite": "2.2.0",
"jsonwebtoken": "8.5.1", "jsonwebtoken": "8.5.1",
"jwt-bch-lib": "1.3.0", "jwt-bch-lib": "1.3.0",
+27 -20
View File
@@ -26,8 +26,9 @@ import { webSockets } from '@libp2p/websockets'
import publicIp from 'public-ip' import publicIp from 'public-ip'
import { multiaddr } from '@multiformats/multiaddr' import { multiaddr } from '@multiformats/multiaddr'
import { webRTC } from '@libp2p/webrtc' import { webRTC } from '@libp2p/webrtc'
// import { keychain } from '@libp2p/keychain' import { keychain } from '@libp2p/keychain'
// import { defaultLogger } from '@libp2p/logger' import { defaultLogger } from '@libp2p/logger'
import {Key} from 'interface-datastore/key'
// Local libraries // Local libraries
import config from '../../../config/index.js' import config from '../../../config/index.js'
@@ -41,9 +42,9 @@ const __dirname = url.fileURLToPath(new URL('.', import.meta.url))
const ROOT_DIR = `${__dirname}../../../` const ROOT_DIR = `${__dirname}../../../`
const IPFS_DIR = `${__dirname}../../../.ipfsdata/ipfs` const IPFS_DIR = `${__dirname}../../../.ipfsdata/ipfs`
// const keychainInit = { const keychainInit = {
// pass: 'very long, very secure password' pass: 'very long, very secure password'
// } }
class IpfsAdapter { class IpfsAdapter {
constructor (localConfig) { constructor (localConfig) {
@@ -116,16 +117,20 @@ class IpfsAdapter {
const datastore = new FsDatastore(`${IPFS_DIR}/datastore`) const datastore = new FsDatastore(`${IPFS_DIR}/datastore`)
// Create an identity // Create an identity
// let peerId let peerId
// const chain = keychain(keychainInit)({ const chain = keychain(keychainInit)({
// datastore, datastore,
// logger: defaultLogger() logger: defaultLogger()
// }) })
// const selfKey = new Key('/pkcs8/self') const selfKey = new Key('/pkcs8/self')
// if (await datastore.has(selfKey)) { if (await datastore.has(selfKey)) {
// // load the peer id from the keychain // load the peer id from the keychain
// peerId = await chain.exportPeerId('self') peerId = await chain.exportPeerId('self')
// } } else {
// await chain.createKey('myKey', 'RSA', 4096)
peerId = await chain.exportPeerId('myKey')
}
console.log(`Created peer ID: `, peerId)
// Configure services // Configure services
const services = { const services = {
@@ -164,7 +169,7 @@ class IpfsAdapter {
// libp2p is the networking layer that underpins Helia // libp2p is the networking layer that underpins Helia
const libp2p = await this.createLibp2p({ const libp2p = await this.createLibp2p({
// peerId, peerId,
datastore, datastore,
addresses: { addresses: {
listen: [ listen: [
@@ -194,10 +199,12 @@ class IpfsAdapter {
services services
}) })
// console.log(`Node started with id ${libp2p.peerId.toString()}`) // Save the identity if it was newly created.
// console.log('Listening on:') if (peerId != null && !await datastore.has(selfKey)) {
// libp2p.getMultiaddrs().forEach((ma) => console.log(ma.toString())) console.log(`Saving key for peer ID ${libp2p.peerId}`)
// a new PeerId would have been generated so store it in the keychain for next time
await chain.importPeer('self', libp2p.peerId)
}
// create a Helia node // create a Helia node
const helia = await createHelia({ const helia = await createHelia({
blockstore, blockstore,