-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For saving and getting a BIP44 Wallet (bip44-wallet) from the database.
- Loading branch information
1 parent
bc9bb4f
commit 24a56a5
Showing
2 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) | ||
}) |