Skip to content

Commit

Permalink
feat(filters): support on-demand owner hashing PE-4214
Browse files Browse the repository at this point in the history
In order to simplify filter construction, if owner_address is set in a
filter, but only owner is present on the matchable item (L1 TXs don't
include the address), hash owner on-demand to produce and owner_address
and match against that.
  • Loading branch information
djwhitt committed Jul 17, 2023
1 parent 66181c7 commit d3e9457
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
18 changes: 16 additions & 2 deletions src/filters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function getTx(id: string) {

const TX_ID = '----LT69qUmuIeC4qb0MZHlxVp7UxLu_14rEkA_9n6w';
const TX = getTx(TX_ID);
const TX_OWNER_ADDRESS = 'Th825IP80n4i9F3Rc4cBFh767CGqiV4n7S-Oy5lGLjc';

describe('AlwaysMatch', () => {
const alwaysMatch = new AlwaysMatch();
Expand Down Expand Up @@ -178,12 +179,25 @@ describe('MatchAttributes', () => {

const matchAttributes = new MatchAttributes(attributes);

delete TX.owner;
const tx = JSON.parse(JSON.stringify(TX));
delete tx.owner;

const result = await matchAttributes.match(TX);
const result = await matchAttributes.match(tx);

expect(result).to.be.false;
});

it('should match owner given an owner address', async () => {
const attributes = {
owner_address: TX_OWNER_ADDRESS,
};

const matchAttributes = new MatchAttributes(attributes);

const result = await matchAttributes.match(TX);

expect(result).to.be.true;
});
});

describe('createFilter', () => {
Expand Down
8 changes: 7 additions & 1 deletion src/filters.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { b64UrlToUtf8 } from './lib/encoding.js';
import { b64UrlToUtf8, fromB64Url, sha256B64Url } from './lib/encoding.js';
import { ItemFilter, MatchableItem } from './types.js';

export class AlwaysMatch implements ItemFilter {
Expand Down Expand Up @@ -108,6 +108,12 @@ export class MatchAttributes implements ItemFilter {
for (const [name, value] of Object.entries(this.attributes)) {
if (item?.[name as keyof MatchableItem] === value) {
matches.add(name);
} else if (name === 'owner_address' && item['owner'] !== undefined) {
const ownerBuffer = fromB64Url(item['owner']);
const ownerAddress = sha256B64Url(ownerBuffer);
if (ownerAddress === value) {
matches.add(name);
}
}
}

Expand Down

0 comments on commit d3e9457

Please sign in to comment.