This commit is contained in:
2023-07-22 19:37:46 +01:00
parent 74a3cd7754
commit b1f93c9fd8
65 changed files with 1115 additions and 1029 deletions

View File

@ -1,62 +1,62 @@
import { EventExt } from "../src/EventExt";
describe("NIP-10", () => {
it("should extract thread", () => {
const a = {
content: "This is the problem with Lightning....",
id: "868187063f...",
kind: 1,
created_at: 1,
pubkey: "test",
sig: "test",
"tags": [
["e", "cbf2375078..."],
["e", "977ac5d3b6..."],
["e", "8f99ca1363..."],
]
}
it("should extract thread", () => {
const a = {
content: "This is the problem with Lightning....",
id: "868187063f...",
kind: 1,
created_at: 1,
pubkey: "test",
sig: "test",
tags: [
["e", "cbf2375078..."],
["e", "977ac5d3b6..."],
["e", "8f99ca1363..."],
],
};
const b = {
"content": "This is a good point, but your ...",
"id": "434ad4a646...",
kind: 1,
created_at: 1,
pubkey: "test",
sig: "test",
"tags": [
["e", "cbf2375078..."],
["e", "868187063f..."],
["e", "6834ffc491..."],
]
}
const b = {
content: "This is a good point, but your ...",
id: "434ad4a646...",
kind: 1,
created_at: 1,
pubkey: "test",
sig: "test",
tags: [
["e", "cbf2375078..."],
["e", "868187063f..."],
["e", "6834ffc491..."],
],
};
const c = {
"content": "There is some middle ground ...",
"id": "6834ffc491...",
kind: 1,
created_at: 1,
pubkey: "test",
sig: "test",
"tags": [
["e", "cbf2375078...", "", "root"],
["e", "868187063f...", "", "reply"],
]
}
const c = {
content: "There is some middle ground ...",
id: "6834ffc491...",
kind: 1,
created_at: 1,
pubkey: "test",
sig: "test",
tags: [
["e", "cbf2375078...", "", "root"],
["e", "868187063f...", "", "reply"],
],
};
expect(EventExt.extractThread(a)).toMatchObject({
root: { key: "e", value: "cbf2375078...", marker: "root" },
replyTo: { key: "e", value: "8f99ca1363...", marker: "reply" },
mentions: [{ key: "e", value: "977ac5d3b6...", marker: "mention" }]
})
expect(EventExt.extractThread(b)).toMatchObject({
root: { key: "e", value: "cbf2375078...", marker: "root" },
replyTo: { key: "e", value: "6834ffc491...", marker: "reply" },
mentions: [{ key: "e", value: "868187063f...", marker: "mention" }]
})
expect(EventExt.extractThread(c)).toMatchObject({
root: { key: "e", value: "cbf2375078...", relay: "", marker: "root" },
replyTo: { key: "e", value: "868187063f...", relay: "", marker: "reply" },
mentions: []
})
})
})
expect(EventExt.extractThread(a)).toMatchObject({
root: { key: "e", value: "cbf2375078...", marker: "root" },
replyTo: { key: "e", value: "8f99ca1363...", marker: "reply" },
mentions: [{ key: "e", value: "977ac5d3b6...", marker: "mention" }],
});
expect(EventExt.extractThread(b)).toMatchObject({
root: { key: "e", value: "cbf2375078...", marker: "root" },
replyTo: { key: "e", value: "6834ffc491...", marker: "reply" },
mentions: [{ key: "e", value: "868187063f...", marker: "mention" }],
});
expect(EventExt.extractThread(c)).toMatchObject({
root: { key: "e", value: "cbf2375078...", relay: "", marker: "root" },
replyTo: { key: "e", value: "868187063f...", relay: "", marker: "reply" },
mentions: [],
});
});
});

View File

@ -1,37 +1,33 @@
import { splitAllByWriteRelays } from "../src/GossipModel"
import { splitAllByWriteRelays } from "../src/GossipModel";
describe("GossipModel", () => {
it("should not output empty", () => {
const Relays = {
getFromCache: (pk?: string) => {
if (pk) {
return {
pubkey: pk,
created_at: 0,
relays: []
};
}
}
it("should not output empty", () => {
const Relays = {
getFromCache: (pk?: string) => {
if (pk) {
return {
pubkey: pk,
created_at: 0,
relays: [],
};
}
const a = [{
"until": 1686651693,
"limit": 200,
"kinds": [
1,
6,
6969
],
"authors": [
"3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"
]
}];
},
};
const a = [
{
until: 1686651693,
limit: 200,
kinds: [1, 6, 6969],
authors: ["3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"],
},
];
const output = splitAllByWriteRelays(Relays, a);
expect(output).toEqual([
{
relay: "",
filters: a
}
])
})
})
const output = splitAllByWriteRelays(Relays, a);
expect(output).toEqual([
{
relay: "",
filters: a,
},
]);
});
});

View File

@ -1,4 +1,3 @@
import { schnorr, secp256k1 } from "@noble/curves/secp256k1";
import { Nip4WebCryptoEncryptor } from "../src/impl/nip4";
import { Nip44Encryptor } from "../src/impl/nip44";
@ -10,36 +9,36 @@ const bKey = secp256k1.utils.randomPrivateKey();
const bPubKey = schnorr.getPublicKey(bKey);
describe("NIP-04", () => {
it("should encrypt/decrypt", async () => {
const msg = "test hello, 123";
const enc = new Nip4WebCryptoEncryptor();
const sec = enc.getSharedSecret(bytesToHex(aKey), bytesToHex(bPubKey));
it("should encrypt/decrypt", async () => {
const msg = "test hello, 123";
const enc = new Nip4WebCryptoEncryptor();
const sec = enc.getSharedSecret(bytesToHex(aKey), bytesToHex(bPubKey));
const ciphertext = await enc.encryptData(msg, sec);
expect(ciphertext).toMatch(/^.*\?iv=.*$/i);
const ciphertext = await enc.encryptData(msg, sec);
expect(ciphertext).toMatch(/^.*\?iv=.*$/i);
const dec = new Nip4WebCryptoEncryptor();
const sec2 = enc.getSharedSecret(bytesToHex(bKey), bytesToHex(aPubKey));
const plaintext = await dec.decryptData(ciphertext, sec2);
expect(plaintext).toEqual(msg);
})
})
const dec = new Nip4WebCryptoEncryptor();
const sec2 = enc.getSharedSecret(bytesToHex(bKey), bytesToHex(aPubKey));
const plaintext = await dec.decryptData(ciphertext, sec2);
expect(plaintext).toEqual(msg);
});
});
describe("NIP-44", () => {
it("should encrypt/decrypt", () => {
const msg = "test hello, 123";
const enc = new Nip44Encryptor();
const sec = enc.getSharedSecret(bytesToHex(aKey), bytesToHex(bPubKey));
it("should encrypt/decrypt", () => {
const msg = "test hello, 123";
const enc = new Nip44Encryptor();
const sec = enc.getSharedSecret(bytesToHex(aKey), bytesToHex(bPubKey));
const ciphertext = enc.encryptData(msg, sec);
const jObj = JSON.parse(ciphertext);
expect(jObj).toHaveProperty("ciphertext")
expect(jObj).toHaveProperty("nonce")
expect(jObj.v).toBe(1);
const ciphertext = enc.encryptData(msg, sec);
const jObj = JSON.parse(ciphertext);
expect(jObj).toHaveProperty("ciphertext");
expect(jObj).toHaveProperty("nonce");
expect(jObj.v).toBe(1);
const dec = new Nip44Encryptor();
const sec2 = enc.getSharedSecret(bytesToHex(bKey), bytesToHex(aPubKey));
const plaintext = dec.decryptData(ciphertext, sec2);
expect(plaintext).toEqual(msg);
})
})
const dec = new Nip44Encryptor();
const sec2 = enc.getSharedSecret(bytesToHex(bKey), bytesToHex(aPubKey));
const plaintext = dec.decryptData(ciphertext, sec2);
expect(plaintext).toEqual(msg);
});
});

View File

@ -20,7 +20,7 @@ const DummyCache = {
write: true,
},
},
]
],
};
},
} as RelayCache;
@ -181,23 +181,24 @@ describe("build diff, large follow list", () => {
const start = unixNowMs();
const a = rb.build(DummyCache);
expect(a).toEqual(f.map(a => {
return {
strategy: RequestStrategy.AuthorsRelays,
relay: `wss://${a}.com/`,
filters: [
{
kinds: [1, 6, 10002, 3, 6969],
authors: [a],
}
],
}
}));
expect(a).toEqual(
f.map(a => {
return {
strategy: RequestStrategy.AuthorsRelays,
relay: `wss://${a}.com/`,
filters: [
{
kinds: [1, 6, 10002, 3, 6969],
authors: [a],
},
],
};
})
);
expect(unixNowMs() - start).toBeLessThan(500);
const start2 = unixNowMs();
const b = rb.buildDiff(DummyCache, rb.buildRaw().flatMap(expandFilter));
expect(b).toEqual([]);
expect(unixNowMs() - start2).toBeLessThan(100);
})
});

View File

@ -108,50 +108,50 @@ describe("flatMerge", () => {
});
});
describe('canMerge', () => {
describe("canMerge", () => {
it("should have 0 distance", () => {
const a = {
ids: "a",
keys: 1
keys: 1,
};
const b = {
ids: "a",
keys: 1
keys: 1,
};
expect(canMergeFilters(a, b)).toEqual(true);
});
it("should have 1 distance", () => {
const a = {
ids: "a",
keys: 1
keys: 1,
};
const b = {
ids: "b",
keys: 1
keys: 1,
};
expect(canMergeFilters(a, b)).toEqual(true);
});
it("should have 10 distance", () => {
const a = {
ids: "a",
keys: 1
keys: 1,
};
const b = {
ids: "a",
kinds: 1,
keys: 2
keys: 2,
};
expect(canMergeFilters(a, b)).toEqual(false);
});
it("should have 11 distance", () => {
const a = {
ids: "a",
keys: 1
keys: 1,
};
const b = {
ids: "b",
kinds: 1,
keys: 2
keys: 2,
};
expect(canMergeFilters(a, b)).toEqual(false);
});
@ -160,13 +160,13 @@ describe('canMerge', () => {
since: 1,
until: 100,
kinds: [1],
authors: ["kieran", "snort", "c", "d", "e"]
authors: ["kieran", "snort", "c", "d", "e"],
};
const b = {
since: 1,
until: 100,
kinds: [6969],
authors: ["kieran", "snort", "c", "d", "e"]
authors: ["kieran", "snort", "c", "d", "e"],
};
expect(canMergeFilters(a, b)).toEqual(true);
});
@ -175,14 +175,14 @@ describe('canMerge', () => {
since: 1,
until: 100,
kinds: [1],
authors: ["f", "kieran", "snort", "c", "d"]
authors: ["f", "kieran", "snort", "c", "d"],
};
const b = {
since: 1,
until: 100,
kinds: [1],
authors: ["kieran", "snort", "c", "d", "e"]
authors: ["kieran", "snort", "c", "d", "e"],
};
expect(canMergeFilters(a, b)).toEqual(true);
});
})
});

View File

@ -2,4 +2,4 @@ import { TextEncoder, TextDecoder } from "util";
import { Crypto } from "@peculiar/webcrypto";
Object.assign(global, { TextDecoder, TextEncoder });
Object.assign(globalThis.window.crypto, new Crypto());
Object.assign(globalThis.window.crypto, new Crypto());