Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it("can load format version 1 profile with password", async () => {
const db = levelup(MemDownConstructor());
// Storage data created with IOV-Core 0.14 cli
await db.put("format_version", userprofileData.serializations.version1.db.format_version);
await db.put("created_at", userprofileData.serializations.version1.db.created_at);
await db.put("keyring", userprofileData.serializations.version1.db.keyring);
const loaded = await UserProfile.loadFrom(db, userprofileData.serializations.version1.password);
expect(loaded.createdAt).toEqual(new ReadonlyDate("2019-05-27T16:40:44.522Z"));
expect(loaded.wallets.value.length).toEqual(2);
expect(loaded.wallets.value[0].label).toEqual("ed");
expect(loaded.wallets.value[1].label).toEqual("secp");
expect(loaded.printableSecret(loaded.wallets.value[0].id)).toEqual(
"degree tackle suggest window test behind mesh extra cover prepare oak script",
);
expect(loaded.printableSecret(loaded.wallets.value[1].id)).toEqual(
options.db = txdown(db._levelup, options.createLock)
location = db.location
} else if (isFunction(db.sublevel) && isFunction(db.levelup)) {
// db is sublevelup, get its levelup
this._levelup = db.levelup()
options.db = txdown(db.levelup(), options.createLock)
location = db.location
} else {
// db is LevelUP, wrap txdown
this._levelup = db
options.db = txdown(db)
location = ''
}
// LevelUP.call(this, options.db(location), options)
LevelUP.call(this, location, options)
var self = this
this.once('closed', function () {
self.emit('end', self.db._error)
self.emit('release', self.db._error)
})
}
constructor (db: any, root: Uint8Array = EMPTY_ROOT_U8A, hashing: HashFn = keccakAsU8a) {
this.putRaw = this._putRaw;
this.semaphore = semaphore(1);
this.dbDown = db || memdown();
this.db = levelup(encoder(this.dbDown));
this._getDBs = [this.db];
this._putDBs = [this.db];
this.hashing = hashing;
this.nodeFactory = createFactory(hashing);
this.root = root;
// l.debug(() => ['Created BaseTrie', typeof db, u8aToHex(root)]);
}
let db;
if (store) {
db = await levelup(store as any, levelupOptions);
} else {
let directory = this.options.dbPath;
if (!directory) {
const dirInfo = await dir(tmpOptions);
directory = dirInfo.path;
this._cleanupDirectory = dirInfo.cleanup;
// don't continue if we closed while we were waiting for the dir
if (this.closed) return this._cleanup();
}
this.directory = directory;
const store = encode(leveldown(directory), levelupOptions);
db = await levelup(store, {});
}
// don't continue if we closed while we were waiting for the db
if (this.closed) return this._cleanup();
const open = db.open();
this.trie = sub(db, "trie", levelupOptions);
this.db = db;
await open;
// don't continue if we closed while we were waiting for it to open
if (this.closed) return this._cleanup();
this.blocks = sub(db, "blocks", levelupOptions);
this.transactions = sub(db, "transactions", levelupOptions);
self.store.get(DB.PREFIXES.VERSION, options, function(err, buffer) {
var version;
if (err instanceof levelup.errors.NotFoundError) {
// The initial version (1) of the database didn't store the version number
version = 1;
} else if (err) {
return callback(err);
} else {
version = buffer.readUInt32BE();
}
if (self.version !== version) {
var helpUrl = 'https://github.com/bitpay/bitcore-node/blob/master/docs/services/db.md#how-to-reindex';
return callback(new Error(
'The version of the database "' + version + '" does not match the expected version "' +
self.version + '". A recreation of "' + self.dataPath + '" (can take several hours) is ' +
'required or to switch versions of software to match. Please see ' + helpUrl +
' for more information.'
));
}
// mostly from level-packager
const levelMore = (location, options) => {
if (typeof options !== 'object' || options === null) options = {}
const store = options.store || leveldown
delete options.store
;['destroy', 'repair'].forEach(function (m) {
if (typeof store[m] === 'function') {
levelMore[m] = () => store[m].apply(store, arguments)
}
})
return levelup(encode(store(location), options), options)
}
levelMore.errors = levelup.errors
function levelPlugin (fastify, opts, next) {
if (!opts.name && (!opts.options || !opts.options.store)) {
return next(new Error('Missing database name'))
}
opts.options = opts.options || {}
fastify
.decorate('level', levelMore(opts.name, opts.options))
.addHook('onClose', close)
next()
}
function close (fastify, done) {
fastify.level.close(done)
self.store.get(DB.PREFIXES.TIP, options, function(err) {
if (err instanceof levelup.errors.NotFoundError) {
// The database is brand new and doesn't have a tip stored
// we can skip version checking
return callback();
} else if (err) {
return callback(err);
}
self.store.get(DB.PREFIXES.VERSION, options, function(err, buffer) {
var version;
if (err instanceof levelup.errors.NotFoundError) {
// The initial version (1) of the database didn't store the version number
version = 1;
} else if (err) {
return callback(err);
} else {
version = buffer.readUInt32BE();
}
public constructor(dbPath: string) {
// Make sure that the DB directory exists.
const directory = path.dirname(dbPath);
mkdirp.sync(directory);
// Open/create the blocks LevelDB database.
this.db = levelup.default(leveldown(dbPath));
}
function Db (location) {
// Initialize necessary methods/properties from levelup in this instance
levelup.call(this, location)
}
function collect(records) {
if (!records || !records.length) {
return cb(new NotFoundError("Key not found in database [" + key + "]"))
}
var r = records[0]
// TODO other options?
if (options && options.valueEncoding == "json") r.value = JSON.parse(r.value)
return cb(null, r.value, r.version)
}