diff --git a/lib/wallet/txdb.js b/lib/wallet/txdb.js index 3c45aacc7..8f46e9b82 100644 --- a/lib/wallet/txdb.js +++ b/lib/wallet/txdb.js @@ -22,7 +22,7 @@ const rules = require('../covenants/rules'); const NameState = require('../covenants/namestate'); const NameUndo = require('../covenants/undo'); const {TXRecord} = records; -const {types} = rules; +const {types, typesByVal} = rules; /* * Constants @@ -79,6 +79,63 @@ class TXDB { this.wallet.emit(event, data, details); } + /** + * Emit events for each type of covenant. + * @private + * @param {TXRecord} wtx + * @param {Details} details + */ + + async emitCovenants(tx, details, view, height) { + const network = this.wdb.network; + if (height < 0) + height = null; + + const {outputs} = tx; + for (const output of outputs) { + const {covenant} = output; + if (!covenant.isName()) + continue; + + // view accumulates state in connectNames + const nameHash = covenant.get(0); + const ns = await view.getNameState(this, nameHash); + + // only look up the name if it isn't already populated + if (EMPTY.equals(ns.name) || !ns.name) { + switch (covenant.type) { + case types.CLAIM: + case types.OPEN: + case types.BID: + case types.FINALIZE: { + ns.name = covenant.get(2); + break; + } + case types.REVEAL: + case types.REDEEM: + case types.REGISTER: + case types.UPDATE: + case types.RENEW: + case types.TRANSFER: + case types.REVOKE: { + const nameState = await this.wallet.getNameState(nameHash); + ns.name = nameState.name; + break; + } + } + } + + if (ns.name) + ns.name = ns.name.toString(); + + const nameState = ns.getJSON(height, network); + const {type} = covenant; + const action = typesByVal[type].toLowerCase(); + + this.emit(`${action} covenant`, nameState, details); + } + } + /** * Get wallet path for output. * @param {Output} output @@ -1064,6 +1121,7 @@ class TXDB { // successfully written to disk. this.emit('tx', tx, details); this.emit('balance', balance); + this.emitCovenants(tx, details, view, height); return details; }