diff --git a/src/trust_is_risk.js b/src/trust_is_risk.js index 07d79c8..367bbee 100644 --- a/src/trust_is_risk.js +++ b/src/trust_is_risk.js @@ -228,17 +228,18 @@ class TrustIsRisk { return nextTrust; } - // Looks for direct trust outputs that originate from a sender in an array of bitcoin outputs. - // Returns a list of the corresponding DirectTrust objects. + // Looks for direct trust outputs that originate from a sender in a transaction. + // Returns an array of the corresponding DirectTrust objects. // If the recipient parameter is set, it will limit the results only to the outputs being sent to // the recipient. searchForDirectTrustOutputs(tx : bcoin$TX, origin : Entity, recipient : ?Entity) : DirectTrust[] { var directTrusts = tx.outputs.map((output, outputIndex) => this.parseOutputAsDirectTrust(tx, outputIndex, origin) - ).filter(Boolean); + ).filter(Boolean); // filter out nulls if (recipient) { - directTrusts.filter((trust) => trust.dest === recipient); + directTrusts = directTrusts.filter((trust) => + helpers.pubKeyToEntity(trust.dest) === recipient); } return directTrusts; diff --git a/test/trust_is_risk.js b/test/trust_is_risk.js index a4085fa..2040784 100644 --- a/test/trust_is_risk.js +++ b/test/trust_is_risk.js @@ -150,6 +150,16 @@ describe("TrustIsRisk", () => { tir.getDirectTrust(alice, bob).should.equal(20 * COIN); }); + it("decreases trust to zero for trust decreasing transactions with a wrong recipient", () => { + // By changing the trust recipient from bob to charlie, we make the transaction a + // nullifying trust transaction. + trustDecreasingMTX.outputs[0] = + testHelpers.getOneOfTwoMultisigOutput(addr.alice.pubKey, addr.charlie.pubKey, 20 * COIN); + + tir.addTX(trustDecreasingMTX.toTX()); + tir.getDirectTrust(alice, bob).should.equal(0); + }); + it("which has a second input decreases trust to zero", () => { trustDecreasingMTX.inputs.push(testHelpers.getP2PKHInput(addr.alice.pubKey)); tir.addTX(trustDecreasingMTX.toTX());