DAOBRAINS / dbrains

https://dbrains.vercel.app
0 stars 0 forks source link

Ceramic ComposeDB #1

Closed bomanaps closed 1 year ago

bomanaps commented 1 year ago

const Ceramic = require('@ceramicnetwork/http-client').default const { Ed25519Provider } = require('key-did-provider-ed25519') const { TileDocument } = require('@ceramicnetwork/stream-tile') const { ComposeClient } = require('@ceramicnetwork/compose') const bcrypt = require('bcrypt')

// Set up Ceramic client and provider const ceramic = new Ceramic('http://localhost:7007') // replace with your own node's endpoint const seed = Uint8Array.from('my seed phrase', c => c.charCodeAt(0)) const provider = new Ed25519Provider(seed) await ceramic.setDIDProvider(provider)

// Set up ComposeClient and define the schema for the LoginDB const composeClient = new ComposeClient(ceramic) const loginSchema = { $schema: 'http://json-schema.org/draft-07/schema#', title: 'Login', type: 'object', properties: { username: { type: 'string' }, password: { type: 'string' }, usertag: { type: 'string' }, ethereumAddress: { type: 'string' }, name: { type: 'string' }, email: { type: 'string' } }, required: ['username', 'password', 'usertag', 'ethereumAddress', 'name', 'email'] }

// Define the LoginDB with hash and salt for password security const saltRounds = 10 // Number of rounds of salting/hashing to apply const LoginDB = composeClient.createDatabase('LoginDB', { username: { type: 'string', primary: true }, password: { type: 'string', beforeCreate: async (value) => await bcrypt.hash(value, saltRounds) }, usertag: { type: 'string' }, ethereumAddress: { type: 'string' }, name: { type: 'string' }, email: { type: 'string' } })

// Save the LoginDB schema to Ceramic and retrieve the database instance await composeClient.saveSchema(loginSchema) const loginDB = await composeClient.getDatabase('LoginDB')

// Publish the LoginDB to the Ceramic network const tileDoc = await TileDocument.create(ceramic, { content: loginDB.serialize(), metadata: { controllers: [provider.getPublicKey().toString()], schema: loginSchema.$id } }) console.log(LoginDB published with stream ID ${tileDoc.id.toString()})

brunolune commented 1 year ago

not an issue