@ghostlytools/sdk 0.0.0-stage → 1.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -2
- package/dist/app.js +72 -0
- package/dist/app.js.map +1 -0
- package/dist/core.js +207 -0
- package/dist/core.js.map +1 -0
- package/dist/fakes-JOzOFxn3.js +536 -0
- package/dist/fakes-JOzOFxn3.js.map +1 -0
- package/dist/fakes.js +2 -0
- package/dist/index.js +179 -0
- package/dist/index.js.map +1 -0
- package/dist/keet-identity-key-BLli1rE4.js +76483 -0
- package/dist/keet-identity-key-BLli1rE4.js.map +1 -0
- package/dist/src-BqHTnRWH.js +31296 -0
- package/dist/src-BqHTnRWH.js.map +1 -0
- package/dist/testing.js +330 -0
- package/dist/testing.js.map +1 -0
- package/dist/types/browser/src/engine/paymentAdapters/providers/contractSuite.d.ts +41 -0
- package/dist/types/browser/src/engine/paymentAdapters/providers/lightning.d.ts +103 -0
- package/dist/types/browser/src/engine/paymentAdapters/providers/onchain.d.ts +88 -0
- package/dist/types/browser/src/engine/paymentAdapters/providers/testing.d.ts +89 -0
- package/dist/types/browser/src/engine/paymentAdapters/providers/types.d.ts +151 -0
- package/dist/types/browser/src/plugins/bundled.d.ts +8 -0
- package/dist/types/browser/src/plugins/registry.d.ts +57 -0
- package/dist/types/browser/src/proofs/contract.d.ts +262 -0
- package/dist/types/browser/src/proofs/contractSuite.d.ts +27 -0
- package/dist/types/browser/src/proofs/testing.d.ts +36 -0
- package/dist/types/browser/src/proofs/verify.d.ts +23 -0
- package/dist/types/browser/src/shared/mints.d.ts +50 -0
- package/dist/types/core/src/appBundle.d.ts +173 -0
- package/dist/types/core/src/appStatements.d.ts +142 -0
- package/dist/types/core/src/appStore.d.ts +133 -0
- package/dist/types/core/src/appUpdates.d.ts +88 -0
- package/dist/types/core/src/atprotoIdentity.d.ts +49 -0
- package/dist/types/core/src/atprotoRepo.d.ts +101 -0
- package/dist/types/core/src/avatar.d.ts +19 -0
- package/dist/types/core/src/bip322.d.ts +57 -0
- package/dist/types/core/src/bitcoinAddress.d.ts +5 -0
- package/dist/types/core/src/bitcoinMessage.d.ts +15 -0
- package/dist/types/core/src/bitcoinScript.d.ts +69 -0
- package/dist/types/core/src/bolt11.d.ts +30 -0
- package/dist/types/core/src/bytes.d.ts +11 -0
- package/dist/types/core/src/callSignal.d.ts +232 -0
- package/dist/types/core/src/canonicalJson.d.ts +22 -0
- package/dist/types/core/src/capsRecord.d.ts +234 -0
- package/dist/types/core/src/catchUp.d.ts +45 -0
- package/dist/types/core/src/chatFiles.d.ts +271 -0
- package/dist/types/core/src/clockWatch.d.ts +72 -0
- package/dist/types/core/src/communityRendezvous.d.ts +113 -0
- package/dist/types/core/src/crypto.d.ts +10 -0
- package/dist/types/core/src/datalink.d.ts +187 -0
- package/dist/types/core/src/deviceInvite.d.ts +51 -0
- package/dist/types/core/src/deviceLink.d.ts +70 -0
- package/dist/types/core/src/dhtDelivery.d.ts +407 -0
- package/dist/types/core/src/dhtFiles.d.ts +46 -0
- package/dist/types/core/src/did.d.ts +134 -0
- package/dist/types/core/src/didDht.d.ts +155 -0
- package/dist/types/core/src/directPath.d.ts +63 -0
- package/dist/types/core/src/dns.d.ts +41 -0
- package/dist/types/core/src/domainProofs.d.ts +88 -0
- package/dist/types/core/src/engineErrors.d.ts +91 -0
- package/dist/types/core/src/enroll.d.ts +98 -0
- package/dist/types/core/src/files.d.ts +73 -0
- package/dist/types/core/src/forwards.d.ts +12 -0
- package/dist/types/core/src/frames.d.ts +211 -0
- package/dist/types/core/src/ghostlink.d.ts +1532 -0
- package/dist/types/core/src/groupCommits.d.ts +103 -0
- package/dist/types/core/src/groupCommunity.d.ts +720 -0
- package/dist/types/core/src/groupCrypto.d.ts +60 -0
- package/dist/types/core/src/groupEdits.d.ts +67 -0
- package/dist/types/core/src/groupEntry.d.ts +61 -0
- package/dist/types/core/src/groupFiles.d.ts +106 -0
- package/dist/types/core/src/groupHubs.d.ts +61 -0
- package/dist/types/core/src/groupMentions.d.ts +27 -0
- package/dist/types/core/src/groupMeta.d.ts +162 -0
- package/dist/types/core/src/groupSession.d.ts +728 -0
- package/dist/types/core/src/groupSignals.d.ts +167 -0
- package/dist/types/core/src/groupTyping.d.ts +56 -0
- package/dist/types/core/src/groupWake.d.ts +68 -0
- package/dist/types/core/src/handoff.d.ts +255 -0
- package/dist/types/core/src/http.d.ts +125 -0
- package/dist/types/core/src/identity.d.ts +17 -0
- package/dist/types/core/src/identityProofs.d.ts +243 -0
- package/dist/types/core/src/identityTimeline.d.ts +72 -0
- package/dist/types/core/src/image.d.ts +33 -0
- package/dist/types/core/src/index.d.ts +100 -0
- package/dist/types/core/src/invite.d.ts +128 -0
- package/dist/types/core/src/link.d.ts +353 -0
- package/dist/types/core/src/linkPreview.d.ts +60 -0
- package/dist/types/core/src/linkTrace.d.ts +12 -0
- package/dist/types/core/src/lnurl.d.ts +99 -0
- package/dist/types/core/src/messageTime.d.ts +28 -0
- package/dist/types/core/src/miniApp.d.ts +146 -0
- package/dist/types/core/src/pairedApps.d.ts +151 -0
- package/dist/types/core/src/pairedCalls.d.ts +59 -0
- package/dist/types/core/src/pairedCapabilities.d.ts +101 -0
- package/dist/types/core/src/pairedEdits.d.ts +83 -0
- package/dist/types/core/src/pairedFiles.d.ts +30 -0
- package/dist/types/core/src/pairedHttp.d.ts +15 -0
- package/dist/types/core/src/pairedSession.d.ts +163 -0
- package/dist/types/core/src/pairedSignal.d.ts +17 -0
- package/dist/types/core/src/pairedTransports.d.ts +76 -0
- package/dist/types/core/src/pairedTyping.d.ts +130 -0
- package/dist/types/core/src/pairedWake.d.ts +109 -0
- package/dist/types/core/src/pairingProgress.d.ts +107 -0
- package/dist/types/core/src/paymentIntent.d.ts +111 -0
- package/dist/types/core/src/paymentUri.d.ts +34 -0
- package/dist/types/core/src/payments.d.ts +97 -0
- package/dist/types/core/src/peerProofs.d.ts +98 -0
- package/dist/types/core/src/pins.d.ts +73 -0
- package/dist/types/core/src/pkarr.d.ts +48 -0
- package/dist/types/core/src/pkdns.d.ts +35 -0
- package/dist/types/core/src/pubkyProofs.d.ts +25 -0
- package/dist/types/core/src/pubkyRing.d.ts +21 -0
- package/dist/types/core/src/reactions.d.ts +108 -0
- package/dist/types/core/src/records.d.ts +89 -0
- package/dist/types/core/src/relay.d.ts +459 -0
- package/dist/types/core/src/relayBreaker.d.ts +99 -0
- package/dist/types/core/src/replies.d.ts +41 -0
- package/dist/types/core/src/services.d.ts +35 -0
- package/dist/types/core/src/setUpdate.d.ts +53 -0
- package/dist/types/core/src/signal.d.ts +53 -0
- package/dist/types/core/src/signer.d.ts +13 -0
- package/dist/types/core/src/sparkAddress.d.ts +37 -0
- package/dist/types/core/src/sshsig.d.ts +74 -0
- package/dist/types/core/src/statusCards.d.ts +253 -0
- package/dist/types/core/src/storeForward.d.ts +144 -0
- package/dist/types/core/src/text.d.ts +20 -0
- package/dist/types/core/src/transport.d.ts +158 -0
- package/dist/types/core/src/transportSwitch.d.ts +160 -0
- package/dist/types/core/src/turnRead.d.ts +145 -0
- package/dist/types/core/src/turnRecord.d.ts +165 -0
- package/dist/types/core/src/version.d.ts +9 -0
- package/dist/types/core/src/video.d.ts +44 -0
- package/dist/types/core/src/voice.d.ts +88 -0
- package/dist/types/core/src/webPush.d.ts +58 -0
- package/dist/types/sdk/src/app.d.ts +23 -0
- package/dist/types/sdk/src/core.d.ts +7 -0
- package/dist/types/sdk/src/fakes.d.ts +6 -0
- package/dist/types/sdk/src/index.d.ts +26 -0
- package/dist/types/sdk/src/testing.d.ts +7 -0
- package/dist/types-DvtCy3WB.js +99 -0
- package/dist/types-DvtCy3WB.js.map +1 -0
- package/dist/verify-RjMx7eW6.js +106 -0
- package/dist/verify-RjMx7eW6.js.map +1 -0
- package/package.json +70 -4
package/dist/testing.js
ADDED
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
import { $l as emptyIdentityLedger, Fi as isBitcoinAddress, Hl as IDENTITY_MAX_EVIDENCE, Zl as IdentityExchange, au as newIdentityBinding, nu as identityStatement, oa as decodeBolt11, zb as createIdentity } from "./src-BqHTnRWH.js";
|
|
2
|
+
import { l as providerDescriptorProblems, o as isNothingSpentError } from "./types-DvtCy3WB.js";
|
|
3
|
+
import { i as verifyIdentity, r as descriptorProblems } from "./verify-RjMx7eW6.js";
|
|
4
|
+
import { _ as fakeAddress, a as fakeAccount, b as fakeOnchain, c as fakeKeySign, d as fakeRecord, f as fakeRecordText, g as TEST_PROVIDERS_FLAG, h as FakeOnchainProvider, i as TEST_IDENTITIES_FLAG, l as fakeKeySubject, m as FakeLightningProvider, n as FAKE_ISSUER, o as fakeAccountToken, p as testIdentitiesEnabled, r as FAKE_RECORD_HOST, s as fakeKey, t as FAKE_IDENTITY_PROVIDERS, u as fakeKeyring, v as fakeInvoice, x as testProvidersEnabled, y as fakeLightning } from "./fakes-JOzOFxn3.js";
|
|
5
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
6
|
+
//#region ../browser/src/engine/paymentAdapters/providers/contractSuite.ts
|
|
7
|
+
var WAIT = 1e4;
|
|
8
|
+
var limit = ({ timeout }) => timeout === void 0 ? {} : { timeout: 3 * timeout };
|
|
9
|
+
function describeLightningProvider(name, make, options = {}) {
|
|
10
|
+
const { timeout = WAIT } = options;
|
|
11
|
+
describe(`${name}: the LightningProvider contract`, limit(options), () => {
|
|
12
|
+
let harness;
|
|
13
|
+
const open = async () => harness = await make();
|
|
14
|
+
afterEach(async () => {
|
|
15
|
+
await harness?.provider.close();
|
|
16
|
+
harness = void 0;
|
|
17
|
+
});
|
|
18
|
+
it("has a well-formed descriptor, when it brings one", async () => {
|
|
19
|
+
const { descriptor, network } = await open();
|
|
20
|
+
if (!descriptor) return;
|
|
21
|
+
expect(providerDescriptorProblems(descriptor)).toEqual([]);
|
|
22
|
+
expect(descriptor.kind).toBe("lightning");
|
|
23
|
+
expect(descriptor.networks).toContain(network);
|
|
24
|
+
});
|
|
25
|
+
it("says which network it is on, and a whole-sat balance when it has one", async () => {
|
|
26
|
+
const { provider, network } = await open();
|
|
27
|
+
const info = await provider.info();
|
|
28
|
+
expect(info.network).toBe(network);
|
|
29
|
+
if (provider.capabilities.balance) expect(Number.isSafeInteger(info.balance) && info.balance >= 0).toBe(true);
|
|
30
|
+
});
|
|
31
|
+
it("creates an invoice that decodes to what was asked, and sees it paid", async () => {
|
|
32
|
+
const { provider, payIncoming } = await open();
|
|
33
|
+
if (!provider.capabilities.receive) return;
|
|
34
|
+
const invoice = await provider.createInvoice(21, "contract");
|
|
35
|
+
const decoded = decodeBolt11(invoice.invoice);
|
|
36
|
+
expect(decoded?.amountSat).toBe(21);
|
|
37
|
+
expect(invoice.amount).toBe(21);
|
|
38
|
+
expect(decoded?.paymentHash).toBe(invoice.paymentHash);
|
|
39
|
+
expect(invoice.expiresAt).toBeGreaterThan(Date.now());
|
|
40
|
+
if (!provider.capabilities.lookup) return;
|
|
41
|
+
expect((await provider.invoiceStatus(invoice)).state).toBe("open");
|
|
42
|
+
await payIncoming(invoice);
|
|
43
|
+
await vi.waitFor(async () => expect((await provider.invoiceStatus(invoice)).state).toBe("paid"), {
|
|
44
|
+
timeout,
|
|
45
|
+
interval: 250
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
it("pays an invoice within the fee limit, and says so again when asked", async () => {
|
|
49
|
+
const { provider, payable } = await open();
|
|
50
|
+
if (!provider.capabilities.send) return;
|
|
51
|
+
const invoice = await payable(25);
|
|
52
|
+
const hash = decodeBolt11(invoice).paymentHash;
|
|
53
|
+
const maxFee = provider.estimateFee ? await provider.estimateFee(invoice, 25) : 10;
|
|
54
|
+
const result = await provider.payInvoice(invoice, maxFee);
|
|
55
|
+
if (result.state === "paid") expect(result.fee ?? 0).toBeLessThanOrEqual(maxFee);
|
|
56
|
+
if (!provider.capabilities.lookup) return;
|
|
57
|
+
await vi.waitFor(async () => expect((await provider.paymentStatus({
|
|
58
|
+
invoice,
|
|
59
|
+
paymentHash: hash,
|
|
60
|
+
ref: result.ref
|
|
61
|
+
})).state).toBe("paid"), {
|
|
62
|
+
timeout,
|
|
63
|
+
interval: 250
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
it("refuses what it cannot pay with NothingSpentError, before anything leaves", async () => {
|
|
67
|
+
const { provider, refused } = await open();
|
|
68
|
+
if (!provider.capabilities.send || !refused) return;
|
|
69
|
+
await expect(provider.payInvoice(await refused(), 10)).rejects.toSatisfy((error) => isNothingSpentError(error));
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
function describeOnchainProvider(name, make, options = {}) {
|
|
74
|
+
const { timeout = WAIT } = options;
|
|
75
|
+
describe(`${name}: the OnchainProvider contract`, limit(options), () => {
|
|
76
|
+
let harness;
|
|
77
|
+
const open = async () => harness = await make();
|
|
78
|
+
afterEach(async () => {
|
|
79
|
+
await harness?.provider.close();
|
|
80
|
+
harness = void 0;
|
|
81
|
+
});
|
|
82
|
+
it("has a well-formed descriptor, when it brings one", async () => {
|
|
83
|
+
const { descriptor, network } = await open();
|
|
84
|
+
if (!descriptor) return;
|
|
85
|
+
expect(providerDescriptorProblems(descriptor)).toEqual([]);
|
|
86
|
+
expect(descriptor.kind).toBe("onchain");
|
|
87
|
+
expect(descriptor.networks).toContain(network);
|
|
88
|
+
});
|
|
89
|
+
it("gives fresh addresses of its own network, and a whole-sat balance", async () => {
|
|
90
|
+
const { provider, network } = await open();
|
|
91
|
+
expect((await provider.info()).network).toBe(network);
|
|
92
|
+
const [a, b] = [await provider.receiveAddress(), await provider.receiveAddress()];
|
|
93
|
+
expect(isBitcoinAddress(a, network) && isBitcoinAddress(b, network)).toBe(true);
|
|
94
|
+
expect(a).not.toBe(b);
|
|
95
|
+
const balance = await provider.balance();
|
|
96
|
+
expect(Number.isSafeInteger(balance.confirmed) && Number.isSafeInteger(balance.unconfirmed)).toBe(true);
|
|
97
|
+
});
|
|
98
|
+
it("refuses a fee above the cap when preparing, and spends nothing by preparing", async () => {
|
|
99
|
+
const { provider, recipient, fund } = await open();
|
|
100
|
+
await fund?.(2e4);
|
|
101
|
+
const before = await provider.balance();
|
|
102
|
+
await expect(provider.prepareSend({
|
|
103
|
+
address: await recipient(),
|
|
104
|
+
amount: 1e3,
|
|
105
|
+
feeCap: 0
|
|
106
|
+
})).rejects.toThrow();
|
|
107
|
+
expect((await provider.balance()).confirmed).toBe(before.confirmed);
|
|
108
|
+
});
|
|
109
|
+
it("broadcasts exactly the prepared transaction, and broadcasting it again pays nothing more", async () => {
|
|
110
|
+
const { provider, recipient, fund } = await open();
|
|
111
|
+
await fund?.(2e4);
|
|
112
|
+
const address = await recipient();
|
|
113
|
+
const prepared = await provider.prepareSend({
|
|
114
|
+
address,
|
|
115
|
+
amount: 1e3,
|
|
116
|
+
feeCap: 5e3
|
|
117
|
+
});
|
|
118
|
+
expect(prepared).toMatchObject({
|
|
119
|
+
address,
|
|
120
|
+
amount: 1e3
|
|
121
|
+
});
|
|
122
|
+
expect(prepared.fee).toBeLessThanOrEqual(5e3);
|
|
123
|
+
expect(prepared.txid).toMatch(/^[0-9a-f]{64}$/);
|
|
124
|
+
expect(await provider.broadcast(prepared)).toBe(prepared.txid);
|
|
125
|
+
const after = await provider.balance();
|
|
126
|
+
await provider.broadcast(prepared).catch(() => {});
|
|
127
|
+
expect((await provider.balance()).confirmed + (await provider.balance()).unconfirmed).toBe(after.confirmed + after.unconfirmed);
|
|
128
|
+
await vi.waitFor(async () => expect(["mempool", "confirmed"]).toContain((await provider.status(prepared)).state), {
|
|
129
|
+
timeout,
|
|
130
|
+
interval: 250
|
|
131
|
+
});
|
|
132
|
+
expect((await provider.history(10)).some((tx) => tx.txid === prepared.txid)).toBe(true);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
//#endregion
|
|
137
|
+
//#region ../browser/src/proofs/contractSuite.ts
|
|
138
|
+
var refuseNetwork = async (url) => {
|
|
139
|
+
throw new Error(`Unexpected network access: ${url}`);
|
|
140
|
+
};
|
|
141
|
+
function describeIdentityProof(name, make, { timeout = 1e4 } = {}) {
|
|
142
|
+
describe(`${name}: the IdentityProofProvider contract`, () => {
|
|
143
|
+
afterEach(() => {
|
|
144
|
+
vi.restoreAllMocks();
|
|
145
|
+
});
|
|
146
|
+
const ctxOf = (h) => ({
|
|
147
|
+
now: h.now ?? Math.floor(Date.now() / 1e3),
|
|
148
|
+
signal: new AbortController().signal,
|
|
149
|
+
fetch: h.fetch ?? refuseNetwork
|
|
150
|
+
});
|
|
151
|
+
const statementFor = (h, change = {}) => {
|
|
152
|
+
const { binding } = newIdentityBinding({
|
|
153
|
+
provider: h.provider.id,
|
|
154
|
+
subject: h.subject,
|
|
155
|
+
validitySeconds: h.provider.validity.defaultDays * 86400,
|
|
156
|
+
now: h.now
|
|
157
|
+
});
|
|
158
|
+
return identityStatement({
|
|
159
|
+
...binding,
|
|
160
|
+
...change
|
|
161
|
+
});
|
|
162
|
+
};
|
|
163
|
+
const verify = (h, statement, evidence) => verifyIdentity([h.provider], statement, JSON.parse(JSON.stringify(evidence)), ctxOf(h));
|
|
164
|
+
it("has a well-formed descriptor", async () => {
|
|
165
|
+
const h = await make();
|
|
166
|
+
expect(descriptorProblems(h.provider)).toEqual([]);
|
|
167
|
+
expect(h.provider.subject.normalize(h.subject)).toBe(h.subject);
|
|
168
|
+
if (h.provider.subject.options) expect(h.provider.subject.options.map((o) => o.value)).toContain(h.subject);
|
|
169
|
+
});
|
|
170
|
+
it("verifies its own evidence, after a trip through JSON, and says how", async () => {
|
|
171
|
+
const h = await make();
|
|
172
|
+
const statement = statementFor(h);
|
|
173
|
+
const evidence = await h.prove(statement);
|
|
174
|
+
expect(JSON.stringify(evidence).length).toBeLessThanOrEqual(IDENTITY_MAX_EVIDENCE);
|
|
175
|
+
const verified = await verify(h, statement, evidence);
|
|
176
|
+
expect(verified.source.length).toBeGreaterThan(0);
|
|
177
|
+
if (h.provider.category === "self-custodied") expect(verified.subject).toBe(h.subject);
|
|
178
|
+
else expect(verified.attester).toBeTruthy();
|
|
179
|
+
if (verified.expiresAt !== void 0) expect(verified.expiresAt).toBeGreaterThan(statement.binding.issuedAt);
|
|
180
|
+
}, timeout);
|
|
181
|
+
it("verifies through ctx.fetch only, never the global fetch", async () => {
|
|
182
|
+
const h = await make();
|
|
183
|
+
const statement = statementFor(h);
|
|
184
|
+
const evidence = await h.prove(statement);
|
|
185
|
+
const global = vi.spyOn(globalThis, "fetch").mockRejectedValue(/* @__PURE__ */ new Error("global fetch used"));
|
|
186
|
+
await verify(h, statement, evidence);
|
|
187
|
+
expect(global).not.toHaveBeenCalled();
|
|
188
|
+
}, timeout);
|
|
189
|
+
it("refuses evidence made for another statement: each field of the binding counts", async () => {
|
|
190
|
+
const h = await make();
|
|
191
|
+
const statement = statementFor(h);
|
|
192
|
+
const evidence = await h.prove(statement);
|
|
193
|
+
const b = statement.binding;
|
|
194
|
+
const changes = [
|
|
195
|
+
{ key: createIdentity().pubKeyZ32 },
|
|
196
|
+
{ nonce: "A".repeat(22) },
|
|
197
|
+
{ issuedAt: b.issuedAt - 1 },
|
|
198
|
+
{ expiresAt: b.expiresAt + 1 }
|
|
199
|
+
];
|
|
200
|
+
for (const change of changes) await expect(verify(h, identityStatement({
|
|
201
|
+
...b,
|
|
202
|
+
...change
|
|
203
|
+
}), evidence), JSON.stringify(change)).rejects.toThrow();
|
|
204
|
+
}, timeout);
|
|
205
|
+
it("refuses another identity's evidence", async () => {
|
|
206
|
+
const h = await make();
|
|
207
|
+
if (!h.proveAsOther) return;
|
|
208
|
+
const statement = statementFor(h);
|
|
209
|
+
await expect(verify(h, statement, await h.proveAsOther(statement))).rejects.toThrow();
|
|
210
|
+
}, timeout);
|
|
211
|
+
it("refuses malformed evidence before verifying", async () => {
|
|
212
|
+
const h = await make();
|
|
213
|
+
const statement = statementFor(h);
|
|
214
|
+
const evidence = await h.prove(statement);
|
|
215
|
+
const garbage = [
|
|
216
|
+
null,
|
|
217
|
+
42,
|
|
218
|
+
"evidence",
|
|
219
|
+
[],
|
|
220
|
+
[evidence],
|
|
221
|
+
{
|
|
222
|
+
...evidence,
|
|
223
|
+
extra: "x"
|
|
224
|
+
},
|
|
225
|
+
{ junk: "x".repeat(IDENTITY_MAX_EVIDENCE) }
|
|
226
|
+
];
|
|
227
|
+
for (const raw of garbage) await expect(verifyIdentity([h.provider], statement, raw, ctxOf(h))).rejects.toThrow();
|
|
228
|
+
}, timeout);
|
|
229
|
+
it("refuses a proof lasting longer than the provider allows", async () => {
|
|
230
|
+
const h = await make();
|
|
231
|
+
const b = statementFor(h).binding;
|
|
232
|
+
if (h.provider.validity.maxDays >= 400) return;
|
|
233
|
+
const long = identityStatement({
|
|
234
|
+
...b,
|
|
235
|
+
expiresAt: b.issuedAt + (h.provider.validity.maxDays + 1) * 86400
|
|
236
|
+
});
|
|
237
|
+
await expect(verify(h, long, await h.prove(long))).rejects.toThrow(/longer/);
|
|
238
|
+
}, timeout);
|
|
239
|
+
it("is verified by the contact it is shared with, and a copy replayed to another contact fails", async () => {
|
|
240
|
+
const h = await make();
|
|
241
|
+
const provider = h.provider;
|
|
242
|
+
const { binding, seed } = newIdentityBinding({
|
|
243
|
+
provider: provider.id,
|
|
244
|
+
subject: h.subject,
|
|
245
|
+
validitySeconds: provider.validity.defaultDays * 86400,
|
|
246
|
+
now: h.now
|
|
247
|
+
});
|
|
248
|
+
const statement = identityStatement(binding);
|
|
249
|
+
const local = {
|
|
250
|
+
statement,
|
|
251
|
+
evidence: await h.prove(statement),
|
|
252
|
+
seed
|
|
253
|
+
};
|
|
254
|
+
const [alice, bob, carol] = [
|
|
255
|
+
createIdentity().pubKeyZ32,
|
|
256
|
+
createIdentity().pubKeyZ32,
|
|
257
|
+
createIdentity().pubKeyZ32
|
|
258
|
+
];
|
|
259
|
+
const ledgers = /* @__PURE__ */ new Map();
|
|
260
|
+
const sent = [];
|
|
261
|
+
const peers = /* @__PURE__ */ new Map();
|
|
262
|
+
const make2 = (me, them, context) => {
|
|
263
|
+
const key = `${me}>${them}`;
|
|
264
|
+
ledgers.set(key, emptyIdentityLedger());
|
|
265
|
+
const x = new IdentityExchange({
|
|
266
|
+
scope: () => ({
|
|
267
|
+
subject: me,
|
|
268
|
+
audience: them,
|
|
269
|
+
context,
|
|
270
|
+
session: "c".repeat(64)
|
|
271
|
+
}),
|
|
272
|
+
send: (frame) => {
|
|
273
|
+
sent.push(frame);
|
|
274
|
+
queueMicrotask(() => void peers.get(`${them}>${me}`)?.receive(structuredClone(frame)));
|
|
275
|
+
},
|
|
276
|
+
storage: {
|
|
277
|
+
read: () => ledgers.get(key),
|
|
278
|
+
update: async (change) => {
|
|
279
|
+
ledgers.set(key, change(structuredClone(ledgers.get(key))));
|
|
280
|
+
}
|
|
281
|
+
},
|
|
282
|
+
providers: () => [provider.id],
|
|
283
|
+
localProof: (id) => id === statement.id ? local : void 0,
|
|
284
|
+
verify: (s, e) => verifyIdentity([provider], s, e, ctxOf(h)),
|
|
285
|
+
now: () => h.now ?? Math.floor(Date.now() / 1e3)
|
|
286
|
+
});
|
|
287
|
+
peers.set(key, x);
|
|
288
|
+
return x;
|
|
289
|
+
};
|
|
290
|
+
const aliceToBob = make2(alice, bob, "a".repeat(64));
|
|
291
|
+
make2(bob, alice, "a".repeat(64));
|
|
292
|
+
make2(carol, alice, "b".repeat(64));
|
|
293
|
+
await aliceToBob.share(statement.id, true);
|
|
294
|
+
await vi.waitFor(() => expect(ledgers.get(`${alice}>${bob}`).shared[0]?.status).toBe("accepted"), { timeout });
|
|
295
|
+
expect(ledgers.get(`${bob}>${alice}`).received).toHaveLength(1);
|
|
296
|
+
expect(ledgers.get(`${carol}>${alice}`).received).toHaveLength(0);
|
|
297
|
+
const present = sent.find((f) => f.t === "idp-present");
|
|
298
|
+
await peers.get(`${carol}>${alice}`).receive({
|
|
299
|
+
t: "idp-request",
|
|
300
|
+
id: statement.id,
|
|
301
|
+
provider: provider.id
|
|
302
|
+
});
|
|
303
|
+
const challenge = sent.filter((f) => f.t === "idp-challenge").at(-1);
|
|
304
|
+
await peers.get(`${carol}>${alice}`).receive({
|
|
305
|
+
...present,
|
|
306
|
+
nonce: challenge.nonce,
|
|
307
|
+
issuedAt: challenge.issuedAt
|
|
308
|
+
});
|
|
309
|
+
expect(ledgers.get(`${carol}>${alice}`).received).toHaveLength(0);
|
|
310
|
+
expect(sent.at(-1)).toMatchObject({
|
|
311
|
+
t: "idp-result",
|
|
312
|
+
ok: false
|
|
313
|
+
});
|
|
314
|
+
}, timeout);
|
|
315
|
+
it("fails a re-check once revoked, when it can go stale", async () => {
|
|
316
|
+
const h = await make();
|
|
317
|
+
if (!h.provider.recheck) return;
|
|
318
|
+
expect(h.revoke, "a provider with recheck must let the harness revoke").toBeTypeOf("function");
|
|
319
|
+
const statement = statementFor(h);
|
|
320
|
+
const evidence = await h.prove(statement);
|
|
321
|
+
await verify(h, statement, evidence);
|
|
322
|
+
await h.revoke();
|
|
323
|
+
await expect(verify(h, statement, evidence)).rejects.toThrow();
|
|
324
|
+
}, timeout);
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
//#endregion
|
|
328
|
+
export { FAKE_IDENTITY_PROVIDERS, FAKE_ISSUER, FAKE_RECORD_HOST, FakeLightningProvider, FakeOnchainProvider, TEST_IDENTITIES_FLAG, TEST_PROVIDERS_FLAG, describeIdentityProof, describeLightningProvider, describeOnchainProvider, fakeAccount, fakeAccountToken, fakeAddress, fakeInvoice, fakeKey, fakeKeySign, fakeKeySubject, fakeKeyring, fakeLightning, fakeOnchain, fakeRecord, fakeRecordText, testIdentitiesEnabled, testProvidersEnabled };
|
|
329
|
+
|
|
330
|
+
//# sourceMappingURL=testing.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"testing.js","names":[],"sources":["../../browser/src/engine/paymentAdapters/providers/contractSuite.ts","../../browser/src/proofs/contractSuite.ts"],"sourcesContent":["import { afterEach, describe, expect, it, vi } from \"vitest\";\nimport { decodeBolt11, isBitcoinAddress } from \"@ghostly/core\";\nimport type { LightningInvoice, LightningProvider, LightningProviderDescriptor } from \"./lightning\";\nimport type { OnchainProvider, OnchainProviderDescriptor } from \"./onchain\";\nimport { isNothingSpentError, providerDescriptorProblems, type ProviderDescriptor, type ProviderNetwork } from \"./types\";\n\n/**\n * The provider contract as tests: every Lightning or on-chain provider runs these against itself, with a\n * harness that knows how to make money move on its network (a fake, a regtest counterpart node, a\n * faucet). See PROVIDERS.md. A provider's own unit tests (mocked transport) go next to these.\n *\n * describeLightningProvider(\"NWC\", async () => ({ provider, network: \"regtest\", payIncoming, payable }));\n *\n * `timeout` is how long money may take to arrive (default 10 s). A harness that gives it is on a real network,\n * where a step takes seconds: its tests get three times that (the harness's own step, the provider's calls,\n * the wait) instead of the runner's default limit, which a busy machine does not meet.\n */\nexport interface ContractOptions { timeout?: number }\nconst WAIT = 10_000;\nconst limit = ({ timeout }: ContractOptions) => (timeout === undefined ? {} : { timeout: 3 * timeout });\n\nexport interface LightningHarness {\n provider: LightningProvider;\n network: ProviderNetwork;\n /** The descriptor the app would register: its shape is checked too. */\n descriptor?: LightningProviderDescriptor;\n /** Someone else pays this invoice of the provider (a counterpart node; a fake marks it paid). */\n payIncoming(invoice: LightningInvoice): Promise<void>;\n /** An invoice of someone else for `amount` sats, that the provider can pay. */\n payable(amount: number): Promise<string>;\n /** An invoice the provider must refuse before anything leaves (no route, not enough funds). */\n refused?(): Promise<string>;\n}\n\nexport function describeLightningProvider(name: string, make: () => Promise<LightningHarness>, options: ContractOptions = {}) {\n const { timeout = WAIT } = options;\n describe(`${name}: the LightningProvider contract`, limit(options), () => {\n let harness: LightningHarness | undefined;\n const open = async () => (harness = await make());\n afterEach(async () => { await harness?.provider.close(); harness = undefined; });\n\n it(\"has a well-formed descriptor, when it brings one\", async () => {\n const { descriptor, network } = await open();\n if (!descriptor) return;\n expect(providerDescriptorProblems(descriptor as ProviderDescriptor<unknown>)).toEqual([]);\n expect(descriptor.kind).toBe(\"lightning\");\n expect(descriptor.networks).toContain(network);\n });\n\n it(\"says which network it is on, and a whole-sat balance when it has one\", async () => {\n const { provider, network } = await open();\n const info = await provider.info();\n expect(info.network).toBe(network);\n if (provider.capabilities.balance) expect(Number.isSafeInteger(info.balance) && info.balance! >= 0).toBe(true);\n });\n\n it(\"creates an invoice that decodes to what was asked, and sees it paid\", async () => {\n const { provider, payIncoming } = await open();\n if (!provider.capabilities.receive) return;\n const invoice = await provider.createInvoice(21, \"contract\");\n const decoded = decodeBolt11(invoice.invoice);\n expect(decoded?.amountSat).toBe(21);\n expect(invoice.amount).toBe(21);\n expect(decoded?.paymentHash).toBe(invoice.paymentHash);\n expect(invoice.expiresAt).toBeGreaterThan(Date.now());\n if (!provider.capabilities.lookup) return;\n expect((await provider.invoiceStatus(invoice)).state).toBe(\"open\");\n await payIncoming(invoice);\n await vi.waitFor(async () => expect((await provider.invoiceStatus(invoice)).state).toBe(\"paid\"), { timeout, interval: 250 });\n });\n\n it(\"pays an invoice within the fee limit, and says so again when asked\", async () => {\n const { provider, payable } = await open();\n if (!provider.capabilities.send) return;\n const invoice = await payable(25);\n const hash = decodeBolt11(invoice)!.paymentHash!;\n const maxFee = provider.estimateFee ? await provider.estimateFee(invoice, 25) : 10;\n const result = await provider.payInvoice(invoice, maxFee);\n if (result.state === \"paid\") expect(result.fee ?? 0).toBeLessThanOrEqual(maxFee);\n if (!provider.capabilities.lookup) return;\n await vi.waitFor(async () => expect((await provider.paymentStatus({ invoice, paymentHash: hash, ref: result.ref })).state).toBe(\"paid\"), { timeout, interval: 250 });\n });\n\n it(\"refuses what it cannot pay with NothingSpentError, before anything leaves\", async () => {\n const { provider, refused } = await open();\n if (!provider.capabilities.send || !refused) return;\n // By name, as the engine does: a provider built outside the app may carry its own copy of the class.\n await expect(provider.payInvoice(await refused(), 10)).rejects.toSatisfy((error: unknown) => isNothingSpentError(error));\n });\n });\n}\n\nexport interface OnchainHarness {\n provider: OnchainProvider;\n network: ProviderNetwork;\n /** The descriptor the app would register: its shape is checked too. */\n descriptor?: OnchainProviderDescriptor;\n /** An address of someone else on this network. */\n recipient(): Promise<string> | string;\n /** Makes the provider's wallet hold at least `amount` confirmed sats (a faucet, mining on regtest). */\n fund?(amount: number): Promise<void>;\n}\n\nexport function describeOnchainProvider(name: string, make: () => Promise<OnchainHarness>, options: ContractOptions = {}) {\n const { timeout = WAIT } = options;\n describe(`${name}: the OnchainProvider contract`, limit(options), () => {\n let harness: OnchainHarness | undefined;\n const open = async () => (harness = await make());\n afterEach(async () => { await harness?.provider.close(); harness = undefined; });\n\n it(\"has a well-formed descriptor, when it brings one\", async () => {\n const { descriptor, network } = await open();\n if (!descriptor) return;\n expect(providerDescriptorProblems(descriptor as ProviderDescriptor<unknown>)).toEqual([]);\n expect(descriptor.kind).toBe(\"onchain\");\n expect(descriptor.networks).toContain(network);\n });\n\n it(\"gives fresh addresses of its own network, and a whole-sat balance\", async () => {\n const { provider, network } = await open();\n expect((await provider.info()).network).toBe(network);\n const [a, b] = [await provider.receiveAddress(), await provider.receiveAddress()];\n expect(isBitcoinAddress(a, network) && isBitcoinAddress(b, network)).toBe(true);\n expect(a).not.toBe(b);\n const balance = await provider.balance();\n expect(Number.isSafeInteger(balance.confirmed) && Number.isSafeInteger(balance.unconfirmed)).toBe(true);\n });\n\n it(\"refuses a fee above the cap when preparing, and spends nothing by preparing\", async () => {\n const { provider, recipient, fund } = await open();\n await fund?.(20_000);\n const before = await provider.balance();\n await expect(provider.prepareSend({ address: await recipient(), amount: 1_000, feeCap: 0 })).rejects.toThrow();\n expect((await provider.balance()).confirmed).toBe(before.confirmed);\n });\n\n it(\"broadcasts exactly the prepared transaction, and broadcasting it again pays nothing more\", async () => {\n const { provider, recipient, fund } = await open();\n await fund?.(20_000);\n const address = await recipient();\n const prepared = await provider.prepareSend({ address, amount: 1_000, feeCap: 5_000 });\n expect(prepared).toMatchObject({ address, amount: 1_000 });\n expect(prepared.fee).toBeLessThanOrEqual(5_000);\n expect(prepared.txid).toMatch(/^[0-9a-f]{64}$/);\n expect(await provider.broadcast(prepared)).toBe(prepared.txid);\n const after = await provider.balance();\n await provider.broadcast(prepared).catch(() => {}); // A node may answer \"already known\": still the same transaction.\n expect((await provider.balance()).confirmed + (await provider.balance()).unconfirmed).toBe(after.confirmed + after.unconfirmed);\n await vi.waitFor(async () => expect([\"mempool\", \"confirmed\"]).toContain((await provider.status(prepared)).state), { timeout, interval: 250 });\n expect((await provider.history(10)).some((tx) => tx.txid === prepared.txid)).toBe(true);\n });\n });\n}\n","import { afterEach, describe, expect, it, vi } from \"vitest\";\nimport {\n createIdentity, emptyIdentityLedger, identityStatement, IdentityExchange, IDENTITY_MAX_EVIDENCE, newIdentityBinding,\n type IdentityBinding, type IdentityLedger, type IdentityStatement, type LocalIdentityProof,\n} from \"@ghostly/core\";\nimport type { IdentityFetch, IdentityProofProvider, VerifyContext } from \"./contract\";\nimport { descriptorProblems, verifyIdentity } from \"./verify\";\n\n/**\n * The identity-proof contract as tests: every provider runs these against itself, with a harness that\n * can produce evidence for it (a test key, a stubbed network). See PROOFS.md. Provider-specific tests\n * (real tool output, vectors, malformed inputs of its format) go next to these.\n *\n * describeIdentityProof(\"SSH\", async () => ({ provider: ssh, subject, prove: s => sshSign(s, key), proveAsOther: s => sshSign(s, otherKey) }));\n */\nexport interface IdentityProofHarness<E> {\n provider: IdentityProofProvider<E>;\n /** A canonical subject this harness controls (self-custodied), or the issuer (provider-attested). */\n subject: string;\n /** Valid evidence for `statement`, the way the provider's signer would produce it. */\n prove(statement: IdentityStatement): Promise<E>;\n /** Evidence for the same statement by another key/account: a self-custodied provider must refuse it. */\n proveAsOther?(statement: IdentityStatement): Promise<E>;\n /** The network the verifier sees (DNS, JWKS, published keys). Defaults to one that refuses everything. */\n fetch?: IdentityFetch;\n /** Make proofs made so far fail a re-check (unpublish the record, revoke the key). Providers with `recheck`. */\n revoke?(): void | Promise<void>;\n /** Seconds; defaults to the real clock. */\n now?: number;\n}\n\nconst refuseNetwork: IdentityFetch = async url => { throw new Error(`Unexpected network access: ${url}`); };\n\nexport function describeIdentityProof<E>(name: string, make: () => Promise<IdentityProofHarness<E>>, { timeout = 10_000 } = {}) {\n describe(`${name}: the IdentityProofProvider contract`, () => {\n afterEach(() => { vi.restoreAllMocks(); });\n const ctxOf = (h: IdentityProofHarness<E>): VerifyContext => ({ now: h.now ?? Math.floor(Date.now() / 1000), signal: new AbortController().signal, fetch: h.fetch ?? refuseNetwork });\n const statementFor = (h: IdentityProofHarness<E>, change: Partial<IdentityBinding> = {}): IdentityStatement => {\n const { binding } = newIdentityBinding({ provider: h.provider.id, subject: h.subject, validitySeconds: h.provider.validity.defaultDays * 86_400, now: h.now });\n return identityStatement({ ...binding, ...change });\n };\n const verify = (h: IdentityProofHarness<E>, statement: IdentityStatement, evidence: unknown) =>\n verifyIdentity([h.provider as IdentityProofProvider], statement, JSON.parse(JSON.stringify(evidence)), ctxOf(h));\n\n it(\"has a well-formed descriptor\", async () => {\n const h = await make();\n expect(descriptorProblems(h.provider as IdentityProofProvider)).toEqual([]);\n expect(h.provider.subject.normalize(h.subject)).toBe(h.subject);\n if (h.provider.subject.options) expect(h.provider.subject.options.map(o => o.value)).toContain(h.subject);\n });\n\n it(\"verifies its own evidence, after a trip through JSON, and says how\", async () => {\n const h = await make();\n const statement = statementFor(h);\n const evidence = await h.prove(statement);\n expect(JSON.stringify(evidence).length).toBeLessThanOrEqual(IDENTITY_MAX_EVIDENCE);\n const verified = await verify(h, statement, evidence);\n expect(verified.source.length).toBeGreaterThan(0);\n if (h.provider.category === \"self-custodied\") expect(verified.subject).toBe(h.subject);\n else expect(verified.attester).toBeTruthy();\n if (verified.expiresAt !== undefined) expect(verified.expiresAt).toBeGreaterThan(statement.binding.issuedAt);\n }, timeout);\n\n it(\"verifies through ctx.fetch only, never the global fetch\", async () => {\n const h = await make();\n const statement = statementFor(h);\n const evidence = await h.prove(statement);\n const global = vi.spyOn(globalThis, \"fetch\").mockRejectedValue(new Error(\"global fetch used\"));\n await verify(h, statement, evidence);\n expect(global).not.toHaveBeenCalled();\n }, timeout);\n\n it(\"refuses evidence made for another statement: each field of the binding counts\", async () => {\n const h = await make();\n const statement = statementFor(h);\n const evidence = await h.prove(statement);\n const b = statement.binding;\n const changes: Partial<IdentityBinding>[] = [\n { key: createIdentity().pubKeyZ32 },\n { nonce: \"A\".repeat(22) },\n { issuedAt: b.issuedAt - 1 },\n { expiresAt: b.expiresAt + 1 },\n ];\n for (const change of changes) await expect(verify(h, identityStatement({ ...b, ...change }), evidence), JSON.stringify(change)).rejects.toThrow();\n }, timeout);\n\n it(\"refuses another identity's evidence\", async () => {\n const h = await make();\n if (!h.proveAsOther) return;\n const statement = statementFor(h);\n await expect(verify(h, statement, await h.proveAsOther(statement))).rejects.toThrow();\n }, timeout);\n\n it(\"refuses malformed evidence before verifying\", async () => {\n const h = await make();\n const statement = statementFor(h);\n const evidence = await h.prove(statement);\n const garbage: unknown[] = [null, 42, \"evidence\", [], [evidence], { ...(evidence as object), extra: \"x\" }, { junk: \"x\".repeat(IDENTITY_MAX_EVIDENCE) }];\n for (const raw of garbage) await expect(verifyIdentity([h.provider as IdentityProofProvider], statement, raw, ctxOf(h))).rejects.toThrow();\n }, timeout);\n\n it(\"refuses a proof lasting longer than the provider allows\", async () => {\n const h = await make();\n const b = statementFor(h).binding;\n if (h.provider.validity.maxDays >= 400) return;\n const long = identityStatement({ ...b, expiresAt: b.issuedAt + (h.provider.validity.maxDays + 1) * 86_400 });\n await expect(verify(h, long, await h.prove(long))).rejects.toThrow(/longer/);\n }, timeout);\n\n it(\"is verified by the contact it is shared with, and a copy replayed to another contact fails\", async () => {\n const h = await make();\n const provider = h.provider as IdentityProofProvider;\n const { binding, seed } = newIdentityBinding({ provider: provider.id, subject: h.subject, validitySeconds: provider.validity.defaultDays * 86_400, now: h.now });\n const statement = identityStatement(binding);\n const local: LocalIdentityProof = { statement, evidence: await h.prove(statement), seed };\n const [alice, bob, carol] = [createIdentity().pubKeyZ32, createIdentity().pubKeyZ32, createIdentity().pubKeyZ32];\n const ledgers = new Map<string, IdentityLedger>();\n const sent: Record<string, unknown>[] = [];\n const peers = new Map<string, IdentityExchange>();\n const make2 = (me: string, them: string, context: string) => {\n const key = `${me}>${them}`;\n ledgers.set(key, emptyIdentityLedger());\n const x = new IdentityExchange({\n scope: () => ({ subject: me, audience: them, context, session: \"c\".repeat(64) }),\n send: frame => { sent.push(frame as Record<string, unknown>); queueMicrotask(() => void peers.get(`${them}>${me}`)?.receive(structuredClone(frame) as Record<string, unknown>)); },\n storage: { read: () => ledgers.get(key)!, update: async change => { ledgers.set(key, change(structuredClone(ledgers.get(key)!))); } },\n providers: () => [provider.id],\n localProof: id => (id === statement.id ? local : undefined),\n verify: (s, e) => verifyIdentity([provider], s, e, ctxOf(h)),\n now: () => h.now ?? Math.floor(Date.now() / 1000),\n });\n peers.set(key, x);\n return x;\n };\n const aliceToBob = make2(alice, bob, \"a\".repeat(64));\n make2(bob, alice, \"a\".repeat(64));\n make2(carol, alice, \"b\".repeat(64));\n await aliceToBob.share(statement.id, true);\n await vi.waitFor(() => expect(ledgers.get(`${alice}>${bob}`)!.shared[0]?.status).toBe(\"accepted\"), { timeout });\n expect(ledgers.get(`${bob}>${alice}`)!.received).toHaveLength(1);\n expect(ledgers.get(`${carol}>${alice}`)!.received).toHaveLength(0);\n // Bob forwards what he received to Carol: no challenge of hers, wrong audience, and no proof key.\n const present = sent.find(f => f.t === \"idp-present\")!;\n await peers.get(`${carol}>${alice}`)!.receive({ t: \"idp-request\", id: statement.id, provider: provider.id });\n const challenge = sent.filter(f => f.t === \"idp-challenge\").at(-1)!;\n await peers.get(`${carol}>${alice}`)!.receive({ ...present, nonce: challenge.nonce, issuedAt: challenge.issuedAt });\n expect(ledgers.get(`${carol}>${alice}`)!.received).toHaveLength(0);\n expect(sent.at(-1)).toMatchObject({ t: \"idp-result\", ok: false });\n }, timeout);\n\n it(\"fails a re-check once revoked, when it can go stale\", async () => {\n const h = await make();\n if (!h.provider.recheck) return;\n expect(h.revoke, \"a provider with recheck must let the harness revoke\").toBeTypeOf(\"function\");\n const statement = statementFor(h);\n const evidence = await h.prove(statement);\n await verify(h, statement, evidence);\n await h.revoke!();\n await expect(verify(h, statement, evidence)).rejects.toThrow();\n }, timeout);\n });\n}\n"],"mappings":";;;;;;AAkBA,IAAM,OAAO;AACb,IAAM,SAAS,EAAE,cAAgC,YAAY,KAAA,IAAY,CAAC,IAAI,EAAE,SAAS,IAAI,QAAQ;AAerG,SAAgB,0BAA0B,MAAc,MAAuC,UAA2B,CAAC,GAAG;CAC5H,MAAM,EAAE,UAAU,SAAS;CAC3B,SAAS,GAAG,KAAK,mCAAmC,MAAM,OAAO,SAAS;EACxE,IAAI;EACJ,MAAM,OAAO,YAAa,UAAU,MAAM,KAAK;EAC/C,UAAU,YAAY;GAAE,MAAM,SAAS,SAAS,MAAM;GAAG,UAAU,KAAA;EAAW,CAAC;EAE/E,GAAG,oDAAoD,YAAY;GACjE,MAAM,EAAE,YAAY,YAAY,MAAM,KAAK;GAC3C,IAAI,CAAC,YAAY;GACjB,OAAO,2BAA2B,UAAyC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC;GACxF,OAAO,WAAW,IAAI,CAAC,CAAC,KAAK,WAAW;GACxC,OAAO,WAAW,QAAQ,CAAC,CAAC,UAAU,OAAO;EAC/C,CAAC;EAED,GAAG,wEAAwE,YAAY;GACrF,MAAM,EAAE,UAAU,YAAY,MAAM,KAAK;GACzC,MAAM,OAAO,MAAM,SAAS,KAAK;GACjC,OAAO,KAAK,OAAO,CAAC,CAAC,KAAK,OAAO;GACjC,IAAI,SAAS,aAAa,SAAS,OAAO,OAAO,cAAc,KAAK,OAAO,KAAK,KAAK,WAAY,CAAC,CAAC,CAAC,KAAK,IAAI;EAC/G,CAAC;EAED,GAAG,uEAAuE,YAAY;GACpF,MAAM,EAAE,UAAU,gBAAgB,MAAM,KAAK;GAC7C,IAAI,CAAC,SAAS,aAAa,SAAS;GACpC,MAAM,UAAU,MAAM,SAAS,c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Ac;IAAE,GAAG;IAAc,IAAI;GAAM,CAAC;EAClE,GAAG,OAAO;EAEV,GAAG,uDAAuD,YAAY;GACpE,MAAM,IAAI,MAAM,KAAK;GACrB,IAAI,CAAC,EAAE,SAAS,SAAS;GACzB,OAAO,EAAE,QAAQ,qDAAqD,CAAC,CAAC,WAAW,UAAU;GAC7F,MAAM,YAAY,aAAa,CAAC;GAChC,MAAM,WAAW,MAAM,EAAE,MAAM,SAAS;GACxC,MAAM,OAAO,GAAG,WAAW,QAAQ;GACnC,MAAM,EAAE,OAAQ;GAChB,MAAM,OAAO,OAAO,GAAG,WAAW,QAAQ,CAAC,CAAC,CAAC,QAAQ,QAAQ;EAC/D,GAAG,OAAO;CACZ,CAAC;AACH"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { LightningInvoice, LightningProvider, LightningProviderDescriptor } from "./lightning.js";
|
|
2
|
+
import type { OnchainProvider, OnchainProviderDescriptor } from "./onchain.js";
|
|
3
|
+
import { type ProviderNetwork } from "./types.js";
|
|
4
|
+
/**
|
|
5
|
+
* The provider contract as tests: every Lightning or on-chain provider runs these against itself, with a
|
|
6
|
+
* harness that knows how to make money move on its network (a fake, a regtest counterpart node, a
|
|
7
|
+
* faucet). See PROVIDERS.md. A provider's own unit tests (mocked transport) go next to these.
|
|
8
|
+
*
|
|
9
|
+
* describeLightningProvider("NWC", async () => ({ provider, network: "regtest", payIncoming, payable }));
|
|
10
|
+
*
|
|
11
|
+
* `timeout` is how long money may take to arrive (default 10 s). A harness that gives it is on a real network,
|
|
12
|
+
* where a step takes seconds: its tests get three times that (the harness's own step, the provider's calls,
|
|
13
|
+
* the wait) instead of the runner's default limit, which a busy machine does not meet.
|
|
14
|
+
*/
|
|
15
|
+
export interface ContractOptions {
|
|
16
|
+
timeout?: number;
|
|
17
|
+
}
|
|
18
|
+
export interface LightningHarness {
|
|
19
|
+
provider: LightningProvider;
|
|
20
|
+
network: ProviderNetwork;
|
|
21
|
+
/** The descriptor the app would register: its shape is checked too. */
|
|
22
|
+
descriptor?: LightningProviderDescriptor;
|
|
23
|
+
/** Someone else pays this invoice of the provider (a counterpart node; a fake marks it paid). */
|
|
24
|
+
payIncoming(invoice: LightningInvoice): Promise<void>;
|
|
25
|
+
/** An invoice of someone else for `amount` sats, that the provider can pay. */
|
|
26
|
+
payable(amount: number): Promise<string>;
|
|
27
|
+
/** An invoice the provider must refuse before anything leaves (no route, not enough funds). */
|
|
28
|
+
refused?(): Promise<string>;
|
|
29
|
+
}
|
|
30
|
+
export declare function describeLightningProvider(name: string, make: () => Promise<LightningHarness>, options?: ContractOptions): void;
|
|
31
|
+
export interface OnchainHarness {
|
|
32
|
+
provider: OnchainProvider;
|
|
33
|
+
network: ProviderNetwork;
|
|
34
|
+
/** The descriptor the app would register: its shape is checked too. */
|
|
35
|
+
descriptor?: OnchainProviderDescriptor;
|
|
36
|
+
/** An address of someone else on this network. */
|
|
37
|
+
recipient(): Promise<string> | string;
|
|
38
|
+
/** Makes the provider's wallet hold at least `amount` confirmed sats (a faucet, mining on regtest). */
|
|
39
|
+
fund?(amount: number): Promise<void>;
|
|
40
|
+
}
|
|
41
|
+
export declare function describeOnchainProvider(name: string, make: () => Promise<OnchainHarness>, options?: ContractOptions): void;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type { ProviderDescriptor, ProviderNetwork } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* A source of Lightning: something that can create invoices and pay them (the Cashu mints, a node, a
|
|
4
|
+
* remote wallet). One is active per profile and per wallet mode; the Lightning card, the invoice a chat
|
|
5
|
+
* request carries and paying a contact's invoice all go through it. See PROVIDERS.md.
|
|
6
|
+
*
|
|
7
|
+
* Amounts are whole sats. Times are milliseconds since the epoch.
|
|
8
|
+
*/
|
|
9
|
+
export interface LightningInfo {
|
|
10
|
+
/** The chain its channels are on; must be a network of the wallet mode it is used in. */
|
|
11
|
+
network: ProviderNetwork;
|
|
12
|
+
/** A name for the node or wallet, when it has one. */
|
|
13
|
+
alias?: string;
|
|
14
|
+
/** Spendable sats, when the source has a balance of its own (a node, a remote wallet). */
|
|
15
|
+
balance?: number;
|
|
16
|
+
}
|
|
17
|
+
export interface LightningInvoice {
|
|
18
|
+
/** BOLT 11, as the provider returned it. The engine checks it decodes, and its amount and hash. */
|
|
19
|
+
invoice: string;
|
|
20
|
+
paymentHash: string;
|
|
21
|
+
amount: number;
|
|
22
|
+
expiresAt: number;
|
|
23
|
+
/** The provider's own handle for it (a quote id, an invoice index), handed back to `invoiceStatus`. */
|
|
24
|
+
ref?: string;
|
|
25
|
+
}
|
|
26
|
+
export interface InvoiceStatus {
|
|
27
|
+
state: "open" | "paid" | "expired";
|
|
28
|
+
/** Sats received, once paid. */
|
|
29
|
+
amount?: number;
|
|
30
|
+
}
|
|
31
|
+
/** What was paid, or asked to be paid: handed back to `paymentStatus`. */
|
|
32
|
+
export interface LightningPaymentRef {
|
|
33
|
+
invoice: string;
|
|
34
|
+
paymentHash: string;
|
|
35
|
+
ref?: string;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The outcome of `payInvoice`. `pending`: the payment is in flight (HTLCs out, a mint holding it); the
|
|
39
|
+
* engine asks `paymentStatus` later. Never report `failed` here: a payment known not to have gone out is
|
|
40
|
+
* a thrown `NothingSpentError`.
|
|
41
|
+
*/
|
|
42
|
+
export type LightningPayResult = {
|
|
43
|
+
state: "paid"; /** Routing fee in sats, when known. */
|
|
44
|
+
fee?: number;
|
|
45
|
+
preimage?: string;
|
|
46
|
+
ref?: string;
|
|
47
|
+
} | {
|
|
48
|
+
state: "pending";
|
|
49
|
+
ref?: string;
|
|
50
|
+
};
|
|
51
|
+
export interface LightningPaymentStatus {
|
|
52
|
+
state: "paid" | "pending" | "failed";
|
|
53
|
+
fee?: number;
|
|
54
|
+
preimage?: string;
|
|
55
|
+
}
|
|
56
|
+
export interface LightningCapabilities {
|
|
57
|
+
receive: boolean;
|
|
58
|
+
send: boolean;
|
|
59
|
+
/** `info().balance` means something. */
|
|
60
|
+
balance: boolean;
|
|
61
|
+
/** `invoiceStatus` and `paymentStatus` answer. Without it an incoming invoice is never seen paid. */
|
|
62
|
+
lookup: boolean;
|
|
63
|
+
}
|
|
64
|
+
export interface LightningProvider {
|
|
65
|
+
readonly capabilities: LightningCapabilities;
|
|
66
|
+
/**
|
|
67
|
+
* The source settles and reports its own invoices and payments (the Cashu wallet mints its ecash and
|
|
68
|
+
* resolves pending melts by itself, and tells the engine). The engine then never polls it.
|
|
69
|
+
*/
|
|
70
|
+
readonly settlesItself?: boolean;
|
|
71
|
+
info(): Promise<LightningInfo>;
|
|
72
|
+
createInvoice(amount: number, memo?: string): Promise<LightningInvoice>;
|
|
73
|
+
invoiceStatus(invoice: LightningInvoice): Promise<InvoiceStatus>;
|
|
74
|
+
/** The most paying this invoice may cost in fees, when the source can tell before paying. */
|
|
75
|
+
estimateFee?(invoice: string, amount: number): Promise<number>;
|
|
76
|
+
/**
|
|
77
|
+
* Pays, spending at most `maxFee` in fees. Resolves `paid` or `pending`. Throws `NothingSpentError`
|
|
78
|
+
* only when it is certain nothing left the wallet; any other throw is an unknown outcome.
|
|
79
|
+
*/
|
|
80
|
+
payInvoice(invoice: string, maxFee: number, note?: string): Promise<LightningPayResult>;
|
|
81
|
+
paymentStatus(payment: LightningPaymentRef): Promise<LightningPaymentStatus>;
|
|
82
|
+
/**
|
|
83
|
+
* The handle `payInvoice` will give this invoice's payment, when the source knows it before paying. It is journaled
|
|
84
|
+
* before the spend, so a payment cut off before its answer can still be asked about.
|
|
85
|
+
*/
|
|
86
|
+
paymentRef?(invoice: string): string | undefined;
|
|
87
|
+
/**
|
|
88
|
+
* For a source that settles its own payments (`settlesItself`): what became of one the app was cut off from before
|
|
89
|
+
* its answer was written down. `paid` or `failed` only when the source can prove it; `pending` while it will report
|
|
90
|
+
* the outcome itself; `unknown` when it cannot tell.
|
|
91
|
+
*/
|
|
92
|
+
interruptedPayment?(payment: LightningPaymentRef): Promise<"paid" | "failed" | "pending" | "unknown">;
|
|
93
|
+
/** Ends connections and timers. The provider is not used again. */
|
|
94
|
+
close(): Promise<void>;
|
|
95
|
+
/**
|
|
96
|
+
* The source is removed for good: closes it like `close`, and deletes what it keeps on this device outside the
|
|
97
|
+
* profile's database (Breez's own databases). Optional: `close` is called instead.
|
|
98
|
+
*/
|
|
99
|
+
forget?(): Promise<void>;
|
|
100
|
+
}
|
|
101
|
+
export type LightningProviderDescriptor = ProviderDescriptor<LightningProvider> & {
|
|
102
|
+
kind: "lightning";
|
|
103
|
+
};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { ProviderDescriptor, ProviderNetwork } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* A source of on-chain Bitcoin: a wallet that can give an address, tell its balance and sign and
|
|
4
|
+
* broadcast a transaction (BDK, a Bitcoin Core wallet, …). One is active per profile and per wallet mode;
|
|
5
|
+
* the Bitcoin card and the "bitcoin" payment method go through it. See PROVIDERS.md.
|
|
6
|
+
*
|
|
7
|
+
* Amounts are whole sats; fee rates sat/vB.
|
|
8
|
+
*/
|
|
9
|
+
export interface OnchainInfo {
|
|
10
|
+
network: ProviderNetwork;
|
|
11
|
+
/** A name for the wallet or node, when it has one. */
|
|
12
|
+
alias?: string;
|
|
13
|
+
}
|
|
14
|
+
export interface OnchainBalance {
|
|
15
|
+
confirmed: number;
|
|
16
|
+
/** In the mempool, in or out: not spendable yet. */
|
|
17
|
+
unconfirmed: number;
|
|
18
|
+
}
|
|
19
|
+
export interface OnchainSendRequest {
|
|
20
|
+
address: string;
|
|
21
|
+
amount: number;
|
|
22
|
+
/** The most the person accepted to pay in fees, in sats. A transaction that costs more is refused. */
|
|
23
|
+
feeCap: number;
|
|
24
|
+
/** Chosen by the person; absent, the provider estimates one. */
|
|
25
|
+
feeRate?: number;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* A transaction built, checked and SIGNED for exactly one reviewed payment. It is saved with the review
|
|
29
|
+
* before it is broadcast, so a lost answer is reconciled by asking about (or re-broadcasting) this very
|
|
30
|
+
* transaction, never by building another: the same inputs cannot be spent twice.
|
|
31
|
+
*/
|
|
32
|
+
export interface OnchainPrepared {
|
|
33
|
+
txid: string;
|
|
34
|
+
address: string;
|
|
35
|
+
amount: number;
|
|
36
|
+
fee: number;
|
|
37
|
+
feeRate: number;
|
|
38
|
+
/** The signed transaction as hex, or what the provider needs to broadcast it again. JSON only. */
|
|
39
|
+
signed: string;
|
|
40
|
+
}
|
|
41
|
+
export interface OnchainTxStatus {
|
|
42
|
+
/**
|
|
43
|
+
* `missing`: no node or indexer knows it (dropped, or never arrived); re-broadcasting it is safe.
|
|
44
|
+
* `conflicted`: another transaction spent its inputs. It can never confirm.
|
|
45
|
+
*/
|
|
46
|
+
state: "confirmed" | "mempool" | "missing" | "conflicted";
|
|
47
|
+
confirmations: number;
|
|
48
|
+
}
|
|
49
|
+
export interface OnchainTx {
|
|
50
|
+
txid: string;
|
|
51
|
+
/** Positive when received, negative when sent (without the fee). */
|
|
52
|
+
amount: number;
|
|
53
|
+
fee?: number;
|
|
54
|
+
confirmations: number;
|
|
55
|
+
timestamp?: number;
|
|
56
|
+
}
|
|
57
|
+
export interface OnchainProvider {
|
|
58
|
+
info(): Promise<OnchainInfo>;
|
|
59
|
+
/** A fresh address of this wallet to be paid on. */
|
|
60
|
+
receiveAddress(): Promise<string>;
|
|
61
|
+
balance(): Promise<OnchainBalance>;
|
|
62
|
+
/**
|
|
63
|
+
* Builds and signs the transaction for a review, without broadcasting it. Throws when it cannot (not
|
|
64
|
+
* enough funds, a fee above `feeCap`): nothing is spent by preparing.
|
|
65
|
+
*/
|
|
66
|
+
prepareSend(request: OnchainSendRequest): Promise<OnchainPrepared>;
|
|
67
|
+
/**
|
|
68
|
+
* Broadcasts the prepared transaction and returns its txid. Throws `NothingSpentError` only when the
|
|
69
|
+
* transaction is known not to have reached anyone; any other throw is an unknown outcome.
|
|
70
|
+
*/
|
|
71
|
+
broadcast(prepared: OnchainPrepared): Promise<string>;
|
|
72
|
+
/** Where this transaction stands. Asked after an unknown outcome and until it confirms. */
|
|
73
|
+
status(prepared: OnchainPrepared): Promise<OnchainTxStatus>;
|
|
74
|
+
/** A review cancelled before approval: unlock the coins `prepareSend` reserved. */
|
|
75
|
+
release?(prepared: OnchainPrepared): Promise<void>;
|
|
76
|
+
/** Newest first. */
|
|
77
|
+
history(limit: number): Promise<OnchainTx[]>;
|
|
78
|
+
/**
|
|
79
|
+
* Optional: the transactions paying this address of the wallet, newest first, `amount` being what each
|
|
80
|
+
* paid to it. How the payee of a chat request knows it was paid; without it, a request is never seen paid
|
|
81
|
+
* (the payer's txid receipt is not proof: it can name any receive of the wallet).
|
|
82
|
+
*/
|
|
83
|
+
received?(address: string): Promise<OnchainTx[]>;
|
|
84
|
+
close(): Promise<void>;
|
|
85
|
+
}
|
|
86
|
+
export type OnchainProviderDescriptor = ProviderDescriptor<OnchainProvider> & {
|
|
87
|
+
kind: "onchain";
|
|
88
|
+
};
|