Skip to content

Commit

Permalink
wallet: emit covenant related events
Browse files Browse the repository at this point in the history
Add a new method `emitCovenants` that emits
an event for each auction related output in
a transaction that is indexed in the walletdb.
  • Loading branch information
Mark Tyneway committed Jun 22, 2019
1 parent e47bd6c commit 9722b5c
Showing 1 changed file with 59 additions and 1 deletion.
60 changes: 59 additions & 1 deletion lib/wallet/txdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 9722b5c

Please sign in to comment.