Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
Merge branch 'no-labels-kv-store' into v3.30-release
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjors committed Mar 29, 2017
2 parents 16646ea + c99bedb commit 8e70c5b
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 647 deletions.
22 changes: 6 additions & 16 deletions src/blockchain-wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,9 @@ Wallet.prototype.metadata = function (typeId) {
return Metadata.fromMasterHDNode(masterhdnode, typeId);
};

// This sets:
// * wallet.external (buy-sell, KV Store type 3)
// * wallet.labels (not yet using KV Store)
Wallet.prototype.loadMetadata = function (optionalPayloads, magicHashes) {
optionalPayloads = optionalPayloads || {};

Expand All @@ -857,21 +860,8 @@ Wallet.prototype.loadMetadata = function (optionalPayloads, magicHashes) {
};

var fetchLabels = function () {
var success = function (labels) {
self._labels = labels;
};

var error = function (message) {
console.warn('wallet.labels not set:', message);
self._labels = null;
return Promise.resolve();
};

if (optionalPayloads.labels) {
return Labels.fromJSON(this, optionalPayloads.labels, magicHashes.labels, MyWallet.syncWallet).then(success);
} else {
return Labels.fetch(this, MyWallet.syncWallet).then(success).catch(error);
}
this._labels = new Labels(this, MyWallet.syncWallet);
return Promise.resolve();
};

let promises = [];
Expand All @@ -881,7 +871,7 @@ Wallet.prototype.loadMetadata = function (optionalPayloads, magicHashes) {
promises.push(fetchExternal.call(this));
}

// Falls back to read-only based on wallet payload if metadata is disabled
// Labels currently don't use the KV Store, so this should never fail.
promises.push(fetchLabels.call(this));

return Promise.all(promises);
Expand Down
25 changes: 5 additions & 20 deletions src/hd-account.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ function HDAccount (object) {
this._xpub = obj.xpub;
this._network = obj.network || Bitcoin.networks.bitcoin;

// Prevent deleting address_labels field when saving wallet:
// * backwards compatibility with mobile (we'll keep one entry for the highest label)
// * if user has 2nd password enabled and doesn't enter it during migration step
this._address_labels_backup = obj.address_labels;
this._address_labels = obj.address_labels;

// computed properties
this._keyRing = new KeyRing(obj.xpub, obj.cache);
Expand Down Expand Up @@ -94,10 +91,10 @@ Object.defineProperties(HDAccount.prototype, {
configurable: false,
get: function () {
let maxLabeledReceiveIndex = null;
if (MyWallet.wallet.labels) {
if (MyWallet.wallet.labels) { // May not be set yet
maxLabeledReceiveIndex = MyWallet.wallet.labels.maxLabeledReceiveIndex(this.index);
} else if (this._address_labels_backup && this._address_labels_backup.length) {
maxLabeledReceiveIndex = this._address_labels_backup[this._address_labels_backup.length - 1].index;
} else if (this._address_labels && this._address_labels.length) {
maxLabeledReceiveIndex = this._address_labels[this._address_labels.length - 1].index;
}
return Math.max(
this.lastUsedReceiveIndex === null ? -1 : this.lastUsedReceiveIndex,
Expand Down Expand Up @@ -211,24 +208,12 @@ HDAccount.factory = function (o) {
// JSON SERIALIZER

HDAccount.prototype.toJSON = function () {
let labelsJSON = this._address_labels_backup;

// Hold on to the backup until labels are saved in KV store.
if (MyWallet.wallet.labels && !MyWallet.wallet.labels.readOnly && !MyWallet.wallet.labels.dirty) {
// Use placeholder entry to prevent address reuse:
labelsJSON = [];
let max = MyWallet.wallet.labels.maxLabeledReceiveIndex(this._index);
if (max > -1) {
labelsJSON.push({index: max, label: ''});
}
}

var hdaccount = {
label: this._label,
archived: this._archived,
xpriv: this._xpriv,
xpub: this._xpub,
address_labels: labelsJSON,
address_labels: this._address_labels,
cache: this._keyRing
};

Expand Down
Loading

0 comments on commit 8e70c5b

Please sign in to comment.