<commit_before>/**
 * DattCore
 * ========
 *
 * This is the entry point into the DattCore application. The way to use it is
 * like this:
 *
 * let config = { [insert config here] }
 * let dattcore = DattCore.create(config)
 * // dattcore now exists, and you need to initialize it:
 * return dattcore.init().then( [handle datt after it is initialized] )
 *
 * A word on architecture of this file: This file, dattcore, is intended to be
 * a window into the p2p connections and database. It is NOT intended to host
 * application logic - but just provide an API to the code contained in other
 * files in lib/*.js. If the methods here have too many lines of code and
 * access the database and peers, something has gone wrong. All methods here
 * should be very simple and only access other objects/methods from other
 * files.
 */
'use strict'
let AsyncCrypto = require('./async-crypto')
let DB = require('./db')
let Struct = require('fullnode/lib/struct')
let User = require('./user')
let CoreUser = require('./core-user')
let BR = require('fullnode/lib/br')

function DattCore (config, db, coreuser) {
  if (!(this instanceof DattCore)) {
    return new DattCore(config, db, coreuser)
  }
  this.initialize()
  this.fromObject({
    config: config,
    db: db,
    coreuser: coreuser
  })
}

DattCore.prototype = Object.create(Struct.prototype)
DattCore.prototype.constructor = Struct.prototype.constructor

/**
 * Synchronous initialization to set default values.
 */
DattCore.prototype.initialize = function () {
  this.config = {}
  return this
}

/**
 * Asynchronous initialization method to prepare the database and network
 * connections. Run this to turn dattcore on.
 */
DattCore.prototype.init = function () {
  if (!this.db) {
    let name = this.config.dbName
    let basePath = this.config.dbBasePath
    this.db = DB(name, basePath)
  }

  this.coreuser = CoreUser(this.db)

  return this.db.init().then(() => {
    return this.coreuser.init()
  })
}

DattCore.prototype.close = function () {
  return this.db.close()
}

/**
 * Create a new dattcore.
 */
DattCore.create = function (config) {
  let dattcore = DattCore(config)
  return dattcore
}

/**
 * Get cached global dattcore, or else make a new one. Note that the config
 * is only used if a new dattcore needs to be created.
 */
DattCore.getGlobal = function (config) {
  if (global.dattcore) {
    return global.dattcore
  } else {
    let dattcore = DattCore.create(config)
    global.dattcore = dattcore
    return dattcore
  }
}

DattCore.AsyncCrypto = AsyncCrypto
DattCore.DB = DB
DattCore.User = User

/**
 * User
 * ====
 */

/**
 * TODO: Move all this logic to yet-to-exist core-user module and save to
 * database.
 */
DattCore.prototype.setUserName = function (name) {
  let blockhashbuf
  let blockheightnum
  return this.getLatestBlockInfo().then(info => {
    blockhashbuf = info.hashbuf
    blockheightnum = info.blockheightnum
    return this.coreuser.asyncSetName(name)
  }).then(() => {
    return this.coreuser.asyncGetMsgAuth(blockhashbuf, blockheightnum)
  }).then(msgauth => {
    // TODO: save msgauth.contentauth to db
    // TODO: broadcast msgauth
    return this
  })
}

DattCore.prototype.getUserName = function () {
  return Promise.resolve(this.coreuser.user.name)
}

DattCore.prototype.getUserMnemonic = function () {
  return Promise.resolve(this.coreuser.user.mnemonic)
}

/**
 * Bitcoin
 * =======
 */

/**
 * Return information about the latest block, including the id and height.
 * TODO: Make this actually return the latest block info instead of a
 * pre-programmed value.
 */
DattCore.prototype.getLatestBlockInfo = function () {
  let idhex = '000000000000000010bf939fcce01f8f896d107febe519de2ebc75a4a29fef11'<commit_msg>save contentauth to db after setting name<commit_after>/**
 * DattCore
 * ========
 *
 * This is the entry point into the DattCore application. The way to use it is
 * like this:
 *
 * let config = { [insert config here] }
 * let dattcore = DattCore.create(config)
 * // dattcore now exists, and you need to initialize it:
 * return dattcore.init().then( [handle datt after it is initialized] )
 *
 * A word on architecture of this file: This file, dattcore, is intended to be
 * a window into the p2p connections and database. It is NOT intended to host
 * application logic - but just provide an API to the code contained in other
 * files in lib/*.js. If the methods here have too many lines of code and
 * access the database and peers, something has gone wrong. All methods here
 * should be very simple and only access other objects/methods from other
 * files.
 */
'use strict'
let AsyncCrypto = require('./async-crypto')
let DB = require('./db')
let DBContentAuth = require('./db-content-auth')
let Struct = require('fullnode/lib/struct')
let User = require('./user')
let CoreUser = require('./core-user')
let BR = require('fullnode/lib/br')

function DattCore (config, db, coreuser) {
  if (!(this instanceof DattCore)) {
    return new DattCore(config, db, coreuser)
  }
  this.initialize()
  this.fromObject({
    config: config,
    db: db,
    coreuser: coreuser
  })
}

DattCore.prototype = Object.create(Struct.prototype)
DattCore.prototype.constructor = Struct.prototype.constructor

/**
 * Synchronous initialization to set default values.
 */
DattCore.prototype.initialize = function () {
  this.config = {}
  return this
}

/**
 * Asynchronous initialization method to prepare the database and network
 * connections. Run this to turn dattcore on.
 */
DattCore.prototype.init = function () {
  if (!this.db) {
    let name = this.config.dbName
    let basePath = this.config.dbBasePath
    this.db = DB(name, basePath)
  }

  this.coreuser = CoreUser(this.db)

  return this.db.init().then(() => {
    return this.coreuser.init()
  })
}

DattCore.prototype.close = function () {
  return this.db.close()
}

/**
 * Create a new dattcore.
 */
DattCore.create = function (config) {
  let dattcore = DattCore(config)
  return dattcore
}

/**
 * Get cached global dattcore, or else make a new one. Note that the config
 * is only used if a new dattcore needs to be created.
 */
DattCore.getGlobal = function (config) {
  if (global.dattcore) {
    return global.dattcore
  } else {
    let dattcore = DattCore.create(config)
    global.dattcore = dattcore
    return dattcore
  }
}

DattCore.AsyncCrypto = AsyncCrypto
DattCore.DB = DB
DattCore.User = User

/**
 * User
 * ====
 */

/**
 * TODO: Move all this logic to yet-to-exist core-user module and save to
 * database.
 */
DattCore.prototype.setUserName = function (name) {
  let blockhashbuf
  let blockheightnum
  return this.getLatestBlockInfo().then(info => {
    blockhashbuf = info.hashbuf
    blockheightnum = info.blockheightnum
    return this.coreuser.asyncSetName(name)
  }).then(() => {
    return this.coreuser.asyncGetMsgAuth(blockhashbuf, blockheightnum)
  }).then(msgauth => {
    return DBContentAuth(this.db, msgauth.contentauth).save()
  }).then(() => {
    // TODO: broadcast msgauth
    return this
  })
}

DattCore.prototype.getUserName = function () {
  return Promise.resolve(this.coreuser.user.name)
}

DattCore.prototype.getUserMnemonic = function () {
  return Promise.resolve(this.coreuser.user.mnemonic)
}

/**
 * Bitcoin
 * =======
 */

/**
 * Return information about the latest block, including the id and height.
 * TODO: Make this actually return the latest block info instead of a
 * pre-programmed value.
 */
DattCore.prototype.getLatestBlockInfo = function () {
  let idhex = '000000000000000010bf939fcce01f8f896d107febe519de2ebc75a4a29fef11'
{'fraction_non_alphanumeric': 0.11660730774304182, 'fraction_numerical': 0.0110803324099723, 'mean_word_length': 3.6007281553398056, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2790771', 'n_tokens_mistral': 2425, 'n_tokens_neox': 2360, 'n_words': 727}