Skip to content

Commit

Permalink
DB BIP44 Wallet
Browse files Browse the repository at this point in the history
For saving and getting a BIP44 Wallet (bip44-wallet) from the database.
  • Loading branch information
ryanxcharles committed Nov 20, 2015
1 parent bc9bb4f commit 24a56a5
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
55 changes: 55 additions & 0 deletions core/db-bip44-wallet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* DB BIP44 Wallet
* ===============
*
* For storing a bip44wallet to the db.
*/
'use strict'
let BIP44Wallet = require('./bip44-wallet')
let Struct = require('fullnode/lib/struct')
let asink = require('asink')

function DBBIP44Wallet (db, bip44wallet) {
if (!(this instanceof DBBIP44Wallet)) {
return new DBBIP44Wallet(db, bip44wallet)
}
this.fromObject({db, bip44wallet})
}

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

DBBIP44Wallet.prototype.asyncSave = function (bip44wallet) {
return asink(function *() {
if (!bip44wallet) {
bip44wallet = this.bip44wallet
}
this.bip44wallet = bip44wallet
let _rev
try {
// We need to try retrieving the doc first so we can save it with the
// correct _rev if it already exists.
let doc = yield this.db.asyncGet('bip44wallet')
_rev = doc._rev
} catch (err) {
if (err.message !== 'missing') {
throw err
}
}
let doc = {
_id: 'bip44wallet',
_rev: _rev,
bip44wallet: bip44wallet.toJSON()
}
return this.db.put(doc)
}.bind(this))
}

DBBIP44Wallet.prototype.asyncGet = function () {
return asink(function *() {
let doc = yield this.db.asyncGet('bip44wallet')
return BIP44Wallet().fromJSON(doc.bip44wallet)
}.bind(this))
}

module.exports = DBBIP44Wallet
45 changes: 45 additions & 0 deletions test/core/db-bip44-wallet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* global describe,it,before,after */
'use strict'
let BIP44Wallet = require('../../core/bip44-wallet')
let DB = require('../../core/db')
let DBBIP44Wallet = require('../../core/db-bip44-wallet')
let asink = require('asink')
let should = require('should')

describe('DBBIP44Wallet', function () {
let db = DB('datt-testdatabase')

before(function () {
return db.asyncInitialize()
})

after(function () {
return db.asyncDestroy()
})

it('should exist', function () {
should.exist(DBBIP44Wallet)
should.exist(DBBIP44Wallet())
})

describe('#asyncSave', function () {
it('should save this new wallet and not throw an error', function () {
return asink(function *() {
let bip44wallet = yield BIP44Wallet().asyncFromRandom()
yield bip44wallet.asyncGetNewAddress(0)
yield bip44wallet.asyncGetNewAddress(0)
yield bip44wallet.asyncGetNewChangeAddress(0)
return DBBIP44Wallet(db).asyncSave(bip44wallet)
})
})
})

describe('#asyncGet', function () {
it('should get the previously saved wallet', function () {
return asink(function *() {
let bip44wallet = yield DBBIP44Wallet(db).asyncGet()
;(bip44wallet instanceof BIP44Wallet).should.equal(true)
})
})
})
})

0 comments on commit 24a56a5

Please sign in to comment.