wallet id. wallet db.

This commit is contained in:
Christopher Jeffrey 2016-03-02 14:00:53 -08:00
parent 8d8952add7
commit 689db5632e
2 changed files with 22 additions and 61 deletions

View File

@ -85,6 +85,8 @@ function Wallet(options) {
: this.master.deriveAccount44(this.accountIndex); : this.master.deriveAccount44(this.accountIndex);
} }
this.id = this.getID();
this.addKey(this.accountKey); this.addKey(this.accountKey);
(options.keys || []).forEach(function(key) { (options.keys || []).forEach(function(key) {
@ -100,13 +102,8 @@ Wallet.prototype._init = function _init() {
var addr, i; var addr, i;
assert(!this._initialized); assert(!this._initialized);
this._initialized = true; this._initialized = true;
this.id = this.getID();
this.emit('init');
if (Object.keys(this.addressMap).length === 0) { if (Object.keys(this.addressMap).length === 0) {
for (i = 0; i < this.receiveDepth - 1; i++) for (i = 0; i < this.receiveDepth - 1; i++)
this.deriveReceive(i); this.deriveReceive(i);
@ -129,12 +126,6 @@ Wallet.prototype._init = function _init() {
assert(this.changeAddress.change); assert(this.changeAddress.change);
}; };
Wallet.prototype.onInit = function onInit(callback) {
if (this._initialized)
return callback();
this.once('init', callback);
};
Wallet.prototype.addKey = function addKey(key) { Wallet.prototype.addKey = function addKey(key) {
var has, i; var has, i;
@ -234,13 +225,21 @@ Wallet.prototype._finalizeKeys = function _finalizeKeys(key) {
}; };
// Wallet ID: // Wallet ID:
// bip45: Purpose key address // bip45: Purpose key "address" (prefix: WLT)
// bip44: Account key address // bip44: Account key "address" (prefix: WLT)
Wallet.prototype.getID = function getID() { Wallet.prototype.getID = function getID() {
var publicKey = this.accountKey.publicKey;
var id;
if (this.options.id) if (this.options.id)
return this.options.id; return this.options.id;
return bcoin.address.compileData(this.accountKey.publicKey); id = new Buffer(27);
utils.copy(new Buffer([0x03, 0xbe, 0x04]), id, 0);
utils.copy(utils.ripesha(publicKey), id, 3);
utils.copy(utils.checksum(id.slice(0, 23)), id, 23);
return utils.toBase58(id);
}; };
Wallet.prototype.createReceive = function createReceive() { Wallet.prototype.createReceive = function createReceive() {
@ -426,7 +425,7 @@ Wallet.prototype.ownInput = function ownInput(tx, index) {
}; };
Wallet.prototype.ownOutput = function ownOutput(tx, index) { Wallet.prototype.ownOutput = function ownOutput(tx, index) {
if ((tx instanceof bcoin.output) || (tx instanceof bcoin.coin)) if (tx instanceof bcoin.output)
return tx.test(this.addressMap); return tx.test(this.addressMap);
return tx.testOutputs(this.addressMap, index); return tx.testOutputs(this.addressMap, index);

View File

@ -291,15 +291,8 @@ WalletDB.prototype.save = function save(options, callback) {
options.on('add address', self._onAddress(options, options.id)); options.on('add address', self._onAddress(options, options.id));
options.provider = self; options.provider = self;
} }
options.onInit(function() { if (options instanceof bcoin.wallet)
if (options instanceof bcoin.wallet) options = options.toJSON();
options = options.toJSON();
self.saveJSON(options.id, options, function(err) {
if (err)
self.emit('error', err);
});
});
return callback(null, options);
} }
this.saveJSON(options.id, options, callback); this.saveJSON(options.id, options, callback);
@ -353,13 +346,7 @@ WalletDB.prototype.create = function create(options, callback) {
} else { } else {
options.provider = self; options.provider = self;
wallet = new bcoin.wallet(options); wallet = new bcoin.wallet(options);
wallet.onInit(function() { self.saveJSON(wallet.id, wallet.toJSON(), done);
self.saveJSON(wallet.id, wallet.toJSON(), function(err) {
if (err)
self.emit('error', err);
});
});
return done();
} }
function done(err) { function done(err) {
@ -415,6 +402,7 @@ WalletDB.prototype.getAll = function getAll(id, callback) {
return this.getAddresses(id, function(err, addresses) { return this.getAddresses(id, function(err, addresses) {
if (err) if (err)
return callback(err); return callback(err);
return self.tx.getAllByAddress(addresses, callback); return self.tx.getAllByAddress(addresses, callback);
}); });
}; };
@ -425,36 +413,7 @@ WalletDB.prototype.getUnspent = function getUnspent(id, callback) {
if (err) if (err)
return callback(err); return callback(err);
return self.tx.getUnspentByAddress(addresses, function(err, unspent) { return self.tx.getUnspentByAddress(addresses, callback);
if (err)
return callback(err);
if (unspent.length === 0)
return callback(null, unspent);
// Warning: won't work with encrypted wallets
self.get(id, null, function(err, wallet) {
var res = false;
if (!wallet)
return callback(null, unspent);
unspent.forEach(function(coin) {
if (wallet.syncOutputDepth(coin))
res = true;
});
if (!res)
return callback(null, wallet);
self.save(wallet, function(err) {
if (err)
return callback(err);
return callback(null, unspent);
});
});
});
}); });
}; };
@ -463,6 +422,7 @@ WalletDB.prototype.getPending = function getPending(id, callback) {
return this.getAddresses(id, function(err, addresses) { return this.getAddresses(id, function(err, addresses) {
if (err) if (err)
return callback(err); return callback(err);
return self.tx.getPendingByAddress(addresses, callback); return self.tx.getPendingByAddress(addresses, callback);
}); });
}; };
@ -472,6 +432,7 @@ WalletDB.prototype.getBalance = function getBalance(id, callback) {
return this.getAddresses(id, function(err, addresses) { return this.getAddresses(id, function(err, addresses) {
if (err) if (err)
return callback(err); return callback(err);
return self.tx.getBalanceByAddress(addresses, callback); return self.tx.getBalanceByAddress(addresses, callback);
}); });
}; };
@ -481,6 +442,7 @@ WalletDB.prototype.getLast = function getLast(id, callback) {
return this.getAddresses(id, function(err, addresses) { return this.getAddresses(id, function(err, addresses) {
if (err) if (err)
return callback(err); return callback(err);
return self.tx.getLast(addresses, callback); return self.tx.getLast(addresses, callback);
}); });
}; };