Add whitelistPolicy
This commit is contained in:
parent
817519c132
commit
254b0f1f11
15
src/policies/whitelist-policy.test.ts
Normal file
15
src/policies/whitelist-policy.test.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import { assert } from '../deps.ts';
|
||||
import { buildEvent, buildInputMessage } from '../test.ts';
|
||||
|
||||
import whitelistPolicy from './whitelist-policy.ts';
|
||||
|
||||
Deno.test('allows only whitelisted pubkeys', async () => {
|
||||
const msgA = buildInputMessage({ event: buildEvent({ pubkey: 'A' }) });
|
||||
const msgB = buildInputMessage({ event: buildEvent({ pubkey: 'B' }) });
|
||||
const msgC = buildInputMessage({ event: buildEvent({ pubkey: 'C' }) });
|
||||
|
||||
assert((await whitelistPolicy(msgA, [])).action === 'reject');
|
||||
assert((await whitelistPolicy(msgA, ['A'])).action === 'accept');
|
||||
assert((await whitelistPolicy(msgC, ['B', 'A'])).action === 'reject');
|
||||
assert((await whitelistPolicy(msgB, ['B', 'A'])).action === 'accept');
|
||||
});
|
32
src/policies/whitelist-policy.ts
Normal file
32
src/policies/whitelist-policy.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import type { Policy } from '../types.ts';
|
||||
|
||||
/**
|
||||
* Allows only the listed pubkeys to post to the relay. All other events are rejected.
|
||||
* Pass an array of pubkeys or an iterable, making it efficient to load pubkeys from a large file.
|
||||
*/
|
||||
const whitelistPolicy: Policy<Iterable<string>> = ({ event: { id, pubkey } }, pubkeys = []) => {
|
||||
let isMatch = false;
|
||||
|
||||
for (const p of pubkeys) {
|
||||
if (p === pubkey) {
|
||||
isMatch = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (isMatch) {
|
||||
return {
|
||||
id,
|
||||
action: 'accept',
|
||||
msg: '',
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
id,
|
||||
action: 'reject',
|
||||
msg: 'Only certain pubkeys are allowed.',
|
||||
};
|
||||
};
|
||||
|
||||
export default whitelistPolicy;
|
Loading…
Reference in New Issue
Block a user