<commit_before>'use strict';

define(['bitcoinjs-lib', 'model/pocket/base'], function(Bitcoin, BasePocket) {

/**
 * Read Only pocket for watching addresses
 * @param {Object} store Store for the object.
 * @param {Object} pockets Pockets parent object
 * @constructor
 */
function ReadOnlyPocket(store, pockets) {
    BasePocket.call(this, pockets);
    this.store = store;
    this.name = store.id;
}

ReadOnlyPocket.prototype = Object.create(BasePocket.prototype);

// Pocket definition
ReadOnlyPocket.prototype.type = 'readonly';
ReadOnlyPocket.prototype.types = ['readonly'];
ReadOnlyPocket.prototype.autoCreate = true;
// Mark as read only
ReadOnlyPocket.prototype.readOnly = true;

/**
 * Get the index for some address in this pocket
 */
ReadOnlyPocket.prototype.getIndex = function(walletAddress) {
    // indexes are like 'r', pocketIndex
    return walletAddress.index[0].split(":")[1];
};

/**
 * Add an address to its pocket
 * @param {Object} walletAddress Address we're adding. See {@link Wallet#getWalletAddress}.
 */
ReadOnlyPocket.prototype.addToPocket = function(walletAddress) {
    this.addresses.push(walletAddress.address);
};

/**
 * Create a read only address
 */
ReadOnlyPocket.prototype.createAddress = function(data) {
    var seq = ['readonly:'+this.name, data.address];
    var oldAddress = this.getMyWallet().pubKeys[seq];
    if (!oldAddress) {
        var walletAddress = {
                        'index': seq,
                        'label': data.label || data.address,
                        'type': 'readonly',
                        'address': data.address,
                        'balance': 0,
                        'nOutputs': 0,
                        'height': 0,
                        'pubKey': false };

        // Add to Wallet
        this.getMyWallet().addToWallet(walletAddress);
        return walletAddress;
    }
};

/**
 * Destroy this pocket and cleanup all related addresses.
 */
ReadOnlyPocket.prototype.destroy = function() {
    var pocketId = this.name;
    var wallet = this.getMyWallet();
    // First delete all addresses
    var addresses = this.getAllAddresses();
    addresses.forEach(function(address) {
        var seq = ['readonly:'+pocketId, address];
        if (wallet.pubKeys[seq]) {
            wallet.deleteAddress(seq, true);
        }
    });
    // Now delete our index in the wallet
    delete wallet.pockets.pockets.readonly[pocketId];
}

/**
 * Create a read only addresses into this pocket from the given contact
 */
ReadOnlyPocket.prototype.fromContact = function(contact) {
    var self = this;
    var created = [];
    // Now add all keys
    contact.pubKeys.forEach(function(pubKey) {
        if (pubKey.address && pubKey.type !== 'stealth') {
            var walletAddress = self.createAddress(pubKey);
            if (walletAddress) {
                created.push(walletAddress);
            }
        }
    });
    return created;
};


return ReadOnlyPocket;
});
<commit_msg>Clear the addresses array when destroying.<commit_after>'use strict';

define(['bitcoinjs-lib', 'model/pocket/base'], function(Bitcoin, BasePocket) {

/**
 * Read Only pocket for watching addresses
 * @param {Object} store Store for the object.
 * @param {Object} pockets Pockets parent object
 * @constructor
 */
function ReadOnlyPocket(store, pockets) {
    BasePocket.call(this, pockets);
    this.store = store;
    this.name = store.id;
}

ReadOnlyPocket.prototype = Object.create(BasePocket.prototype);

// Pocket definition
ReadOnlyPocket.prototype.type = 'readonly';
ReadOnlyPocket.prototype.types = ['readonly'];
ReadOnlyPocket.prototype.autoCreate = true;
// Mark as read only
ReadOnlyPocket.prototype.readOnly = true;

/**
 * Get the index for some address in this pocket
 */
ReadOnlyPocket.prototype.getIndex = function(walletAddress) {
    // indexes are like 'r', pocketIndex
    return walletAddress.index[0].split(":")[1];
};

/**
 * Add an address to its pocket
 * @param {Object} walletAddress Address we're adding. See {@link Wallet#getWalletAddress}.
 */
ReadOnlyPocket.prototype.addToPocket = function(walletAddress) {
    this.addresses.push(walletAddress.address);
};

/**
 * Create a read only address
 */
ReadOnlyPocket.prototype.createAddress = function(data) {
    var seq = ['readonly:'+this.name, data.address];
    var walletAddress = this.getMyWallet().pubKeys[seq];
    if (!walletAddress) {
        walletAddress = {
                        'index': seq,
                        'label': data.label || data.address,
                        'type': 'readonly',
                        'address': data.address,
                        'balance': 0,
                        'nOutputs': 0,
                        'height': 0,
                        'pubKey': false };

        // Add to Wallet
        this.getMyWallet().addToWallet(walletAddress);
    }
    return walletAddress;
};

/**
 * Destroy this pocket and cleanup all related addresses.
 */
ReadOnlyPocket.prototype.destroy = function() {
    var pocketId = this.name;
    var wallet = this.getMyWallet();
    // First delete all addresses
    var addresses = this.getAllAddresses();
    addresses.forEach(function(address) {
        var seq = ['readonly:'+pocketId, address];
        if (wallet.pubKeys[seq]) {
            wallet.deleteAddress(seq, true);
        }
    });
    // Now delete our index in the wallet
    delete wallet.pockets.pockets.readonly[pocketId];
    this.addresses = [];
}

/**
 * Create a read only addresses into this pocket from the given contact
 */
ReadOnlyPocket.prototype.fromContact = function(contact) {
    var self = this;
    var created = [];
    // Now add all keys
    contact.pubKeys.forEach(function(pubKey) {
        if (pubKey.address && pubKey.type !== 'stealth') {
            var walletAddress = self.createAddress(pubKey);
            if (walletAddress) {
                created.push(walletAddress);
            }
        }
    });
    return created;
};


return ReadOnlyPocket;
});

{'fraction_non_alphanumeric': 0.12408272181454302, 'fraction_numerical': 0.001667778519012675, 'mean_word_length': 2.5953237410071943, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6059095', 'n_tokens_mistral': 1681, 'n_tokens_neox': 1680, 'n_words': 419}