@totemsdk/omnia-lsp-provider 0.1.0 → 0.2.0
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/dist/cjs/host.js +47 -10
- package/dist/cjs/index.d.ts +3 -1
- package/dist/cjs/index.js +3 -1
- package/dist/cjs/ledger.d.ts +57 -0
- package/dist/cjs/ledger.js +73 -0
- package/dist/cjs/policy.d.ts +20 -3
- package/dist/cjs/policy.js +95 -26
- package/dist/cjs/provider.d.ts +37 -5
- package/dist/cjs/provider.js +123 -17
- package/dist/cjs/quote.d.ts +12 -2
- package/dist/cjs/quote.js +33 -43
- package/dist/host.js +47 -10
- package/dist/index.d.ts +3 -1
- package/dist/index.js +1 -0
- package/dist/ledger.d.ts +57 -0
- package/dist/ledger.js +70 -0
- package/dist/policy.d.ts +20 -3
- package/dist/policy.js +95 -26
- package/dist/provider.d.ts +37 -5
- package/dist/provider.js +124 -18
- package/dist/quote.d.ts +12 -2
- package/dist/quote.js +33 -43
- package/package.json +2 -2
package/dist/cjs/provider.js
CHANGED
|
@@ -1,62 +1,168 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createOmniaLspProvider = createOmniaLspProvider;
|
|
4
|
+
const omnia_lsp_1 = require("@totemsdk/omnia-lsp");
|
|
4
5
|
const policy_js_1 = require("./policy.js");
|
|
5
6
|
const quote_js_1 = require("./quote.js");
|
|
7
|
+
const ledger_js_1 = require("./ledger.js");
|
|
6
8
|
function assertAllowed(decision) {
|
|
7
9
|
if (!decision.allowed) {
|
|
8
10
|
throw new Error(`Open request rejected: ${decision.errors.join('; ')}`);
|
|
9
11
|
}
|
|
10
12
|
}
|
|
13
|
+
function reject(error, errorCode) {
|
|
14
|
+
return { success: false, error, errorCode };
|
|
15
|
+
}
|
|
16
|
+
function fundingInput(funding) {
|
|
17
|
+
return typeof funding === 'string' ? { fundingCoinId: funding } : funding ?? {};
|
|
18
|
+
}
|
|
11
19
|
/**
|
|
12
20
|
* Reference provider facade.
|
|
13
21
|
*
|
|
14
22
|
* It does not hold keys, open channels or broadcast. It applies policy, issues
|
|
15
|
-
* signed quotes and delegates the channel operation
|
|
23
|
+
* signed quotes, reserves provider capacity and delegates the channel operation
|
|
24
|
+
* to the injected host.
|
|
16
25
|
*/
|
|
17
26
|
function createOmniaLspProvider(config) {
|
|
18
27
|
const now = config.now ?? Date.now;
|
|
28
|
+
const ledger = config.quoteLedger ?? (0, ledger_js_1.createMemoryQuoteLedger)();
|
|
29
|
+
let quoteCounter = 0;
|
|
30
|
+
/**
|
|
31
|
+
* Re-apply admission to a quote that is being redeemed (OMN-066). The quote
|
|
32
|
+
* does not carry `direction` or the lease flag, so this uses the conservative
|
|
33
|
+
* `both` direction and derives lease from the quoted lease fee.
|
|
34
|
+
*/
|
|
35
|
+
function quoteAsRequest(quote) {
|
|
36
|
+
return {
|
|
37
|
+
providerId: quote.providerId,
|
|
38
|
+
consumerAddress: quote.consumerAddress,
|
|
39
|
+
consumerPartyId: quote.consumerPartyId,
|
|
40
|
+
consumerPublicKeyDigest: quote.consumerPublicKeyDigest,
|
|
41
|
+
...(quote.consumerSettlementAddress !== undefined
|
|
42
|
+
? { consumerSettlementAddress: quote.consumerSettlementAddress }
|
|
43
|
+
: {}),
|
|
44
|
+
...(quote.consumerTransportPubkey !== undefined
|
|
45
|
+
? { consumerTransportPubkey: quote.consumerTransportPubkey }
|
|
46
|
+
: {}),
|
|
47
|
+
tokenId: quote.tokenId,
|
|
48
|
+
localAmount: quote.localAmount,
|
|
49
|
+
remoteAmount: quote.remoteAmount,
|
|
50
|
+
direction: 'both',
|
|
51
|
+
...(quote.leaseFee !== undefined ? { lease: true } : {}),
|
|
52
|
+
};
|
|
53
|
+
}
|
|
19
54
|
async function quote(request) {
|
|
20
|
-
const
|
|
55
|
+
const issuedAt = now();
|
|
56
|
+
const decision = (0, policy_js_1.evaluateOpenRequest)(config.manifest, request, config.policy, issuedAt);
|
|
21
57
|
assertAllowed(decision);
|
|
22
58
|
const unsigned = (0, quote_js_1.createUnsignedOpenQuote)({
|
|
23
59
|
manifest: config.manifest,
|
|
24
60
|
request,
|
|
25
|
-
now:
|
|
61
|
+
now: issuedAt,
|
|
26
62
|
ttlMs: config.quoteTtlMs,
|
|
63
|
+
quoteId: `quote-${issuedAt.toString(36)}-${(++quoteCounter).toString(36)}`,
|
|
27
64
|
});
|
|
28
|
-
|
|
65
|
+
const signed = await (0, quote_js_1.signOpenQuote)(unsigned, config.signQuote);
|
|
66
|
+
const remote = (0, omnia_lsp_1.parseDecimalUnits)(signed.remoteAmount);
|
|
67
|
+
if (!remote.ok)
|
|
68
|
+
throw new Error(`cannot reserve provider capacity: ${remote.error}`);
|
|
69
|
+
// Outstanding reservations count against the same capacity the manifest
|
|
70
|
+
// advertises, so concurrent quotes cannot collectively exceed it (OMN-068).
|
|
71
|
+
if (decision.asset) {
|
|
72
|
+
const outstanding = await ledger.outstanding(signed.tokenId, config.manifest.providerId, issuedAt);
|
|
73
|
+
if (!(0, policy_js_1.hasCapacity)(decision.asset, request.direction, outstanding + remote.units)) {
|
|
74
|
+
throw new Error('Open request rejected: provider capacity is exhausted by outstanding quotes');
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
await ledger.reserve({
|
|
78
|
+
quoteId: signed.quoteId,
|
|
79
|
+
providerId: config.manifest.providerId,
|
|
80
|
+
tokenId: signed.tokenId,
|
|
81
|
+
amount: signed.remoteAmount,
|
|
82
|
+
amountUnits: remote.units,
|
|
83
|
+
expiresAt: signed.expiresAt,
|
|
84
|
+
state: 'reserved',
|
|
85
|
+
updatedAt: issuedAt,
|
|
86
|
+
});
|
|
87
|
+
return signed;
|
|
88
|
+
}
|
|
89
|
+
async function redeem(quote, funding) {
|
|
90
|
+
const consumed = await ledger.consume(quote.quoteId, now());
|
|
91
|
+
if (consumed === 'unknown') {
|
|
92
|
+
return reject(`Quote ${quote.quoteId} was not issued by this provider`, 'QUOTE_NOT_ISSUED');
|
|
93
|
+
}
|
|
94
|
+
if (consumed === 'expired') {
|
|
95
|
+
return reject('Quote expired', 'QUOTE_EXPIRED');
|
|
96
|
+
}
|
|
97
|
+
if (consumed === 'already-consumed') {
|
|
98
|
+
return reject(`Quote ${quote.quoteId} has already been used`, 'QUOTE_ALREADY_CONSUMED');
|
|
99
|
+
}
|
|
100
|
+
const result = await config.host.openChannel({
|
|
101
|
+
quote,
|
|
102
|
+
...fundingInput(funding),
|
|
103
|
+
});
|
|
104
|
+
if (result?.success !== true) {
|
|
105
|
+
// Nothing was funded, so the capacity goes back rather than being burned.
|
|
106
|
+
await ledger.release(quote.quoteId, now());
|
|
107
|
+
}
|
|
108
|
+
return result;
|
|
29
109
|
}
|
|
30
110
|
return {
|
|
31
111
|
manifest() {
|
|
32
112
|
return config.manifest;
|
|
33
113
|
},
|
|
34
114
|
evaluate(request) {
|
|
35
|
-
return (0, policy_js_1.evaluateOpenRequest)(config.manifest, request, config.policy);
|
|
115
|
+
return (0, policy_js_1.evaluateOpenRequest)(config.manifest, request, config.policy, now());
|
|
36
116
|
},
|
|
37
117
|
quote,
|
|
38
118
|
async open(request, funding) {
|
|
39
119
|
const issued = await quote(request);
|
|
40
|
-
return
|
|
41
|
-
quote: issued,
|
|
42
|
-
...(typeof funding === 'string' ? { fundingCoinId: funding } : funding ?? {}),
|
|
43
|
-
});
|
|
120
|
+
return redeem(issued, funding);
|
|
44
121
|
},
|
|
45
|
-
async openQuote(quote, funding) {
|
|
46
|
-
|
|
47
|
-
|
|
122
|
+
async openQuote(quote, funding, options) {
|
|
123
|
+
const currentTime = now();
|
|
124
|
+
if (quote.expiresAt <= currentTime) {
|
|
125
|
+
return reject('Quote expired', 'QUOTE_EXPIRED');
|
|
48
126
|
}
|
|
49
127
|
if (quote.providerId !== config.manifest.providerId) {
|
|
50
|
-
return
|
|
128
|
+
return reject('Quote provider mismatch', 'QUOTE_PROVIDER_MISMATCH');
|
|
129
|
+
}
|
|
130
|
+
// Bind the quote to the authenticated caller before anything else can act
|
|
131
|
+
// on it. Without this the LSP authorization boundary is missing (OMN-066).
|
|
132
|
+
const consumer = options?.consumer;
|
|
133
|
+
const consumerBound = Boolean(consumer && (consumer.address || consumer.partyId || consumer.publicKeyDigest));
|
|
134
|
+
if (!consumerBound && config.requireConsumerBinding !== false) {
|
|
135
|
+
return reject('openQuote requires the authenticated consumer identity', 'QUOTE_CONSUMER_UNBOUND');
|
|
51
136
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
...(
|
|
137
|
+
const verified = await (0, omnia_lsp_1.verifyOpenQuote)(quote, config.manifest, {
|
|
138
|
+
now: currentTime,
|
|
139
|
+
...(config.verifyQuoteSignature
|
|
140
|
+
? { verifySignature: config.verifyQuoteSignature }
|
|
141
|
+
: {}),
|
|
142
|
+
...(consumer?.partyId !== undefined
|
|
143
|
+
? { expectedConsumerPartyId: consumer.partyId }
|
|
144
|
+
: {}),
|
|
145
|
+
...(consumer?.publicKeyDigest !== undefined
|
|
146
|
+
? { expectedConsumerPublicKeyDigest: consumer.publicKeyDigest }
|
|
147
|
+
: {}),
|
|
148
|
+
...(consumer?.address !== undefined
|
|
149
|
+
? { expectedConsumerAddress: consumer.address }
|
|
150
|
+
: {}),
|
|
55
151
|
});
|
|
152
|
+
if (!verified.ok) {
|
|
153
|
+
return reject(`Quote rejected: ${verified.errors.join('; ')}`, 'QUOTE_INVALID');
|
|
154
|
+
}
|
|
155
|
+
// Re-apply the current policy: the manifest, custody mode and capacity may
|
|
156
|
+
// have changed since the quote was issued.
|
|
157
|
+
const policyDecision = (0, policy_js_1.evaluateOpenRequest)(config.manifest, quoteAsRequest(quote), config.policy, currentTime);
|
|
158
|
+
if (!policyDecision.allowed) {
|
|
159
|
+
return reject(`Quote no longer satisfies provider policy: ${policyDecision.errors.join('; ')}`, 'QUOTE_POLICY_REJECTED');
|
|
160
|
+
}
|
|
161
|
+
return redeem(quote, funding);
|
|
56
162
|
},
|
|
57
163
|
async close(channelId) {
|
|
58
164
|
if (!config.host.closeChannel) {
|
|
59
|
-
return
|
|
165
|
+
return reject('Host does not implement closeChannel', 'UNSUPPORTED');
|
|
60
166
|
}
|
|
61
167
|
return config.host.closeChannel(channelId);
|
|
62
168
|
},
|
package/dist/cjs/quote.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { OmniaLspManifest, OmniaLspOpenQuote } from '@totemsdk/omnia-lsp';
|
|
1
|
+
import type { OmniaLspFeeBasis, OmniaLspManifest, OmniaLspOpenQuote, OpenQuoteSigningPayload } from '@totemsdk/omnia-lsp';
|
|
2
2
|
import type { OmniaLspOpenRequest } from './policy.js';
|
|
3
3
|
export type UnsignedOmniaLspOpenQuote = Omit<OmniaLspOpenQuote, 'signature'>;
|
|
4
4
|
export interface CreateQuoteOptions {
|
|
@@ -7,14 +7,24 @@ export interface CreateQuoteOptions {
|
|
|
7
7
|
now?: number;
|
|
8
8
|
ttlMs?: number;
|
|
9
9
|
quoteId?: string;
|
|
10
|
+
/** Fee basis for percentage fees. Defaults to the total channel value. */
|
|
11
|
+
feeBasis?: OmniaLspFeeBasis;
|
|
10
12
|
}
|
|
11
13
|
/**
|
|
12
14
|
* Build the provider's unsigned open quote. The provider host signs it before
|
|
13
15
|
* returning it to the consumer; a quote without a signature is not authoritative.
|
|
16
|
+
*
|
|
17
|
+
* The quoted fees come from the shared schedule (OMN-072) and the terms object
|
|
18
|
+
* is built by `buildOpenTerms`, which is the same function the verifier uses to
|
|
19
|
+
* recompute the hash.
|
|
14
20
|
*/
|
|
15
21
|
export declare function createUnsignedOpenQuote(options: CreateQuoteOptions): UnsignedOmniaLspOpenQuote;
|
|
16
22
|
/**
|
|
17
23
|
* Sign an unsigned quote with a provider identity signer. The signer is injected
|
|
18
24
|
* so the package stays runtime-neutral and never holds a private key.
|
|
25
|
+
*
|
|
26
|
+
* The second argument is the exact payload to sign (`domain|termsHash`). A
|
|
27
|
+
* one-argument signer keeps working; a signer that ignores the payload must sign
|
|
28
|
+
* this same string to produce quotes that `verifyOpenQuote` accepts.
|
|
19
29
|
*/
|
|
20
|
-
export declare function signOpenQuote(quote: UnsignedOmniaLspOpenQuote, sign: (quote: UnsignedOmniaLspOpenQuote) => Promise<string> | string): Promise<OmniaLspOpenQuote>;
|
|
30
|
+
export declare function signOpenQuote(quote: UnsignedOmniaLspOpenQuote, sign: (quote: UnsignedOmniaLspOpenQuote, payload: OpenQuoteSigningPayload) => Promise<string> | string): Promise<OmniaLspOpenQuote>;
|
package/dist/cjs/quote.js
CHANGED
|
@@ -2,62 +2,46 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createUnsignedOpenQuote = createUnsignedOpenQuote;
|
|
4
4
|
exports.signOpenQuote = signOpenQuote;
|
|
5
|
-
const
|
|
6
|
-
const DECIMAL_SCALE = 18n;
|
|
7
|
-
const DECIMAL_FACTOR = 10n ** DECIMAL_SCALE;
|
|
8
|
-
function decimalKey(value) {
|
|
9
|
-
const [whole = '0', fraction = ''] = value.split('.');
|
|
10
|
-
return (BigInt(whole) * DECIMAL_FACTOR +
|
|
11
|
-
BigInt(fraction.padEnd(Number(DECIMAL_SCALE), '0').slice(0, Number(DECIMAL_SCALE))));
|
|
12
|
-
}
|
|
13
|
-
function formatScaled(value) {
|
|
14
|
-
const negative = value < 0n;
|
|
15
|
-
const absolute = negative ? -value : value;
|
|
16
|
-
const whole = absolute / DECIMAL_FACTOR;
|
|
17
|
-
const fraction = absolute % DECIMAL_FACTOR;
|
|
18
|
-
const fractionText = fraction
|
|
19
|
-
.toString()
|
|
20
|
-
.padStart(Number(DECIMAL_SCALE), '0')
|
|
21
|
-
.replace(/0+$/, '');
|
|
22
|
-
return `${negative ? '-' : ''}${whole.toString()}${fractionText ? `.${fractionText}` : ''}`;
|
|
23
|
-
}
|
|
24
|
-
function applyBps(amount, bps) {
|
|
25
|
-
return formatScaled((decimalKey(amount) * BigInt(bps)) / 10000n);
|
|
26
|
-
}
|
|
5
|
+
const omnia_lsp_1 = require("@totemsdk/omnia-lsp");
|
|
27
6
|
/**
|
|
28
7
|
* Build the provider's unsigned open quote. The provider host signs it before
|
|
29
8
|
* returning it to the consumer; a quote without a signature is not authoritative.
|
|
9
|
+
*
|
|
10
|
+
* The quoted fees come from the shared schedule (OMN-072) and the terms object
|
|
11
|
+
* is built by `buildOpenTerms`, which is the same function the verifier uses to
|
|
12
|
+
* recompute the hash.
|
|
30
13
|
*/
|
|
31
14
|
function createUnsignedOpenQuote(options) {
|
|
32
15
|
const { manifest, request } = options;
|
|
33
16
|
const now = options.now ?? Date.now();
|
|
34
17
|
const ttlMs = options.ttlMs ?? 60000;
|
|
35
18
|
const expiresAt = now + ttlMs;
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
19
|
+
const basis = options.feeBasis ?? omnia_lsp_1.DEFAULT_FEE_BASIS;
|
|
20
|
+
const fees = (0, omnia_lsp_1.computeOpenFees)(manifest, request, basis);
|
|
21
|
+
if (!fees.ok) {
|
|
22
|
+
throw new Error(`cannot quote open request: ${fees.error}`);
|
|
23
|
+
}
|
|
24
|
+
const schedule = fees.schedule;
|
|
25
|
+
const terms = (0, omnia_lsp_1.buildOpenTerms)({
|
|
26
|
+
manifest,
|
|
44
27
|
consumerAddress: request.consumerAddress,
|
|
45
28
|
consumerPartyId: request.consumerPartyId,
|
|
46
29
|
consumerPublicKeyDigest: request.consumerPublicKeyDigest,
|
|
47
|
-
|
|
48
|
-
|
|
30
|
+
...(request.consumerSettlementAddress !== undefined
|
|
31
|
+
? { consumerSettlementAddress: request.consumerSettlementAddress }
|
|
32
|
+
: {}),
|
|
33
|
+
...(request.consumerTransportPubkey !== undefined
|
|
34
|
+
? { consumerTransportPubkey: request.consumerTransportPubkey }
|
|
35
|
+
: {}),
|
|
49
36
|
tokenId: request.tokenId,
|
|
50
37
|
localAmount: request.localAmount,
|
|
51
38
|
remoteAmount: request.remoteAmount,
|
|
52
|
-
openFee,
|
|
53
|
-
leaseFee: leaseFee
|
|
54
|
-
|
|
55
|
-
providerPublicKeyDigest: manifest.channel.publicKeyDigest,
|
|
56
|
-
providerSettlementAddress: manifest.channel.settlementAddress,
|
|
39
|
+
openFee: schedule.openFee,
|
|
40
|
+
...(schedule.leaseFee !== undefined ? { leaseFee: schedule.leaseFee } : {}),
|
|
41
|
+
feeBasis: schedule.basis,
|
|
57
42
|
expiresAt,
|
|
58
|
-
};
|
|
59
|
-
const
|
|
60
|
-
const termsHash = rawTermsHash.startsWith('0x') ? rawTermsHash : `0x${rawTermsHash}`;
|
|
43
|
+
});
|
|
44
|
+
const termsHash = (0, omnia_lsp_1.openTermsHash)(terms);
|
|
61
45
|
return {
|
|
62
46
|
version: 'omnia-lsp/open-quote/v1',
|
|
63
47
|
quoteId: options.quoteId ?? `quote-${termsHash.slice(2, 18)}-${now.toString(36)}`,
|
|
@@ -74,8 +58,9 @@ function createUnsignedOpenQuote(options) {
|
|
|
74
58
|
tokenId: request.tokenId,
|
|
75
59
|
localAmount: request.localAmount,
|
|
76
60
|
remoteAmount: request.remoteAmount,
|
|
77
|
-
openFee,
|
|
78
|
-
...(leaseFee !== undefined ? { leaseFee } : {}),
|
|
61
|
+
openFee: schedule.openFee,
|
|
62
|
+
...(schedule.leaseFee !== undefined ? { leaseFee: schedule.leaseFee } : {}),
|
|
63
|
+
feeBasis: schedule.basis,
|
|
79
64
|
providerPartyId: manifest.channel.partyId,
|
|
80
65
|
providerPublicKeyDigest: manifest.channel.publicKeyDigest,
|
|
81
66
|
providerSettlementAddress: manifest.channel.settlementAddress,
|
|
@@ -86,7 +71,12 @@ function createUnsignedOpenQuote(options) {
|
|
|
86
71
|
/**
|
|
87
72
|
* Sign an unsigned quote with a provider identity signer. The signer is injected
|
|
88
73
|
* so the package stays runtime-neutral and never holds a private key.
|
|
74
|
+
*
|
|
75
|
+
* The second argument is the exact payload to sign (`domain|termsHash`). A
|
|
76
|
+
* one-argument signer keeps working; a signer that ignores the payload must sign
|
|
77
|
+
* this same string to produce quotes that `verifyOpenQuote` accepts.
|
|
89
78
|
*/
|
|
90
79
|
function signOpenQuote(quote, sign) {
|
|
91
|
-
|
|
80
|
+
const payload = (0, omnia_lsp_1.openQuoteSigningPayload)(quote);
|
|
81
|
+
return Promise.resolve(sign(quote, payload)).then((signature) => ({ ...quote, signature }));
|
|
92
82
|
}
|
package/dist/host.js
CHANGED
|
@@ -3,6 +3,12 @@ function handlerFor(methods, name) {
|
|
|
3
3
|
? methods.get(name)
|
|
4
4
|
: undefined;
|
|
5
5
|
}
|
|
6
|
+
function isRecord(value) {
|
|
7
|
+
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
8
|
+
}
|
|
9
|
+
function text(value) {
|
|
10
|
+
return typeof value === 'string' && value.trim().length > 0 ? value : undefined;
|
|
11
|
+
}
|
|
6
12
|
/**
|
|
7
13
|
* Adapt an `omnia-host` method registry (`createHostMethods(context)`) to the
|
|
8
14
|
* LSP provider host port.
|
|
@@ -47,12 +53,35 @@ export function createOmniaHostProviderHost(options) {
|
|
|
47
53
|
...(contestDelayBlocks !== undefined ? { contestDelayBlocks } : {}),
|
|
48
54
|
operationId: `${prefix}-open-${quote.quoteId}`,
|
|
49
55
|
}));
|
|
56
|
+
// OMN-070: require explicit, operation-specific evidence. An undefined,
|
|
57
|
+
// non-object, `{}` or error-only response is a failure, and success
|
|
58
|
+
// without a channelId is not a successful open.
|
|
59
|
+
if (!isRecord(result)) {
|
|
60
|
+
return {
|
|
61
|
+
success: false,
|
|
62
|
+
error: `${openMethod} returned no structured result`,
|
|
63
|
+
errorCode: 'HOST_PROTOCOL_ERROR',
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
const channelId = text(result.channelId);
|
|
67
|
+
if (result.success !== true) {
|
|
68
|
+
return {
|
|
69
|
+
success: false,
|
|
70
|
+
error: text(result.error) ?? `${openMethod} reported failure`,
|
|
71
|
+
errorCode: text(result.errorCode) ?? 'HOST_OPEN_FAILED',
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
if (!channelId) {
|
|
75
|
+
return {
|
|
76
|
+
success: false,
|
|
77
|
+
error: `${openMethod} reported success without a channelId`,
|
|
78
|
+
errorCode: 'HOST_PROTOCOL_ERROR',
|
|
79
|
+
};
|
|
80
|
+
}
|
|
50
81
|
return {
|
|
51
|
-
success:
|
|
52
|
-
|
|
53
|
-
...(result
|
|
54
|
-
...(result?.error ? { error: result.error } : {}),
|
|
55
|
-
...(result?.errorCode ? { errorCode: result.errorCode } : {}),
|
|
82
|
+
success: true,
|
|
83
|
+
channelId,
|
|
84
|
+
...(text(result.fundingTxId) ? { fundingTxId: text(result.fundingTxId) } : {}),
|
|
56
85
|
};
|
|
57
86
|
}
|
|
58
87
|
catch (error) {
|
|
@@ -77,11 +106,19 @@ export function createOmniaHostProviderHost(options) {
|
|
|
77
106
|
channelId,
|
|
78
107
|
operationId: `${prefix}-close-${channelId}`,
|
|
79
108
|
}));
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
109
|
+
// OMN-070: an undefined or malformed close response is not a success.
|
|
110
|
+
if (!isRecord(result) || result.success !== true) {
|
|
111
|
+
return {
|
|
112
|
+
success: false,
|
|
113
|
+
error: isRecord(result) && text(result.error)
|
|
114
|
+
? text(result.error)
|
|
115
|
+
: `${closeMethod} did not confirm the close`,
|
|
116
|
+
errorCode: isRecord(result) && text(result.errorCode)
|
|
117
|
+
? text(result.errorCode)
|
|
118
|
+
: 'HOST_CLOSE_FAILED',
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
return { success: true };
|
|
85
122
|
}
|
|
86
123
|
catch (error) {
|
|
87
124
|
return {
|
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,8 @@ export type { OmniaLspOpenDecision, OmniaLspOpenRequest, OmniaLspProviderPolicy,
|
|
|
3
3
|
export { createUnsignedOpenQuote, signOpenQuote, } from './quote.js';
|
|
4
4
|
export type { CreateQuoteOptions, UnsignedOmniaLspOpenQuote, } from './quote.js';
|
|
5
5
|
export { createOmniaLspProvider, } from './provider.js';
|
|
6
|
-
export type { OmniaLspHostCloseResult, OmniaLspHostOpenInput, OmniaLspHostOpenResult, OmniaLspProvider, OmniaLspProviderConfig, OmniaLspProviderHost, } from './provider.js';
|
|
6
|
+
export type { OmniaLspConsumerIdentity, OmniaLspHostCloseResult, OmniaLspHostOpenInput, OmniaLspHostOpenResult, OmniaLspOpenQuoteOptions, OmniaLspProvider, OmniaLspProviderConfig, OmniaLspProviderHost, } from './provider.js';
|
|
7
|
+
export { createMemoryQuoteLedger } from './ledger.js';
|
|
8
|
+
export type { ConsumeReservationResult, OmniaLspQuoteLedger, OmniaLspQuoteReservation, QuoteReservationState, } from './ledger.js';
|
|
7
9
|
export { createOmniaHostProviderHost } from './host.js';
|
|
8
10
|
export type { CreateOmniaHostProviderHostOptions, OmniaHostHandler, OmniaHostMethodMap, } from './host.js';
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { evaluateOpenRequest, } from './policy.js';
|
|
2
2
|
export { createUnsignedOpenQuote, signOpenQuote, } from './quote.js';
|
|
3
3
|
export { createOmniaLspProvider, } from './provider.js';
|
|
4
|
+
export { createMemoryQuoteLedger } from './ledger.js';
|
|
4
5
|
export { createOmniaHostProviderHost } from './host.js';
|
package/dist/ledger.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Quote reservations (OMN-068).
|
|
3
|
+
*
|
|
4
|
+
* Admission validates a request against the manifest's advertised capacity, but
|
|
5
|
+
* the manifest is static: without a ledger, N concurrent quotes each fit inside
|
|
6
|
+
* capacity while their sum does not. The facade now reserves the provider-side
|
|
7
|
+
* amount when it issues a quote, releases it on expiry or a failed open, and
|
|
8
|
+
* consumes it atomically when a quote is redeemed.
|
|
9
|
+
*
|
|
10
|
+
* The in-memory ledger is the default. A production provider injects an
|
|
11
|
+
* implementation backed by durable storage; the interface is intentionally
|
|
12
|
+
* small so that the durable version can make `consume` a single transaction.
|
|
13
|
+
*/
|
|
14
|
+
export type QuoteReservationState = 'reserved' | 'consumed' | 'released';
|
|
15
|
+
export interface OmniaLspQuoteReservation {
|
|
16
|
+
quoteId: string;
|
|
17
|
+
providerId: string;
|
|
18
|
+
tokenId: string;
|
|
19
|
+
/** Provider-side amount as quoted, decimal units. */
|
|
20
|
+
amount: string;
|
|
21
|
+
/** Provider-side amount in integer units (18 decimal places). */
|
|
22
|
+
amountUnits: bigint;
|
|
23
|
+
expiresAt: number;
|
|
24
|
+
state: QuoteReservationState;
|
|
25
|
+
updatedAt: number;
|
|
26
|
+
}
|
|
27
|
+
export type ConsumeReservationResult =
|
|
28
|
+
/** The reservation existed, was live, and is now consumed. */
|
|
29
|
+
'consumed'
|
|
30
|
+
/** No reservation with this id was ever recorded. */
|
|
31
|
+
| 'unknown'
|
|
32
|
+
/** The reservation exists but its TTL has passed. */
|
|
33
|
+
| 'expired'
|
|
34
|
+
/** The reservation was already consumed or released. */
|
|
35
|
+
| 'already-consumed';
|
|
36
|
+
export interface OmniaLspQuoteLedger {
|
|
37
|
+
/** Record a reservation. Throws if the quoteId is already known. */
|
|
38
|
+
reserve(entry: OmniaLspQuoteReservation): Promise<void> | void;
|
|
39
|
+
/**
|
|
40
|
+
* Atomically move a live reservation to consumed. `unknown` must be returned
|
|
41
|
+
* when this ledger has no record, never an implicit success.
|
|
42
|
+
*/
|
|
43
|
+
consume(quoteId: string, now?: number): Promise<ConsumeReservationResult> | ConsumeReservationResult;
|
|
44
|
+
/** Free a reservation. No-op when unknown or already final. */
|
|
45
|
+
release(quoteId: string, now?: number): Promise<void> | void;
|
|
46
|
+
/** Sum of live reservations for a token and provider, in integer units. */
|
|
47
|
+
outstanding(tokenId: string, providerId: string, now?: number): Promise<bigint> | bigint;
|
|
48
|
+
/** Optional: drop final or expired entries. */
|
|
49
|
+
prune?(now?: number): Promise<void> | void;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Default in-process ledger.
|
|
53
|
+
*
|
|
54
|
+
* Single-threaded JS makes the check-and-set inside `consume` atomic for one
|
|
55
|
+
* process. It does not survive a restart — inject a durable ledger for that.
|
|
56
|
+
*/
|
|
57
|
+
export declare function createMemoryQuoteLedger(): OmniaLspQuoteLedger;
|
package/dist/ledger.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Quote reservations (OMN-068).
|
|
3
|
+
*
|
|
4
|
+
* Admission validates a request against the manifest's advertised capacity, but
|
|
5
|
+
* the manifest is static: without a ledger, N concurrent quotes each fit inside
|
|
6
|
+
* capacity while their sum does not. The facade now reserves the provider-side
|
|
7
|
+
* amount when it issues a quote, releases it on expiry or a failed open, and
|
|
8
|
+
* consumes it atomically when a quote is redeemed.
|
|
9
|
+
*
|
|
10
|
+
* The in-memory ledger is the default. A production provider injects an
|
|
11
|
+
* implementation backed by durable storage; the interface is intentionally
|
|
12
|
+
* small so that the durable version can make `consume` a single transaction.
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Default in-process ledger.
|
|
16
|
+
*
|
|
17
|
+
* Single-threaded JS makes the check-and-set inside `consume` atomic for one
|
|
18
|
+
* process. It does not survive a restart — inject a durable ledger for that.
|
|
19
|
+
*/
|
|
20
|
+
export function createMemoryQuoteLedger() {
|
|
21
|
+
const entries = new Map();
|
|
22
|
+
return {
|
|
23
|
+
reserve(entry) {
|
|
24
|
+
if (entries.has(entry.quoteId)) {
|
|
25
|
+
throw new Error(`quote ${entry.quoteId} is already reserved`);
|
|
26
|
+
}
|
|
27
|
+
entries.set(entry.quoteId, { ...entry });
|
|
28
|
+
},
|
|
29
|
+
consume(quoteId, now = Date.now()) {
|
|
30
|
+
const entry = entries.get(quoteId);
|
|
31
|
+
if (!entry)
|
|
32
|
+
return 'unknown';
|
|
33
|
+
if (entry.state !== 'reserved')
|
|
34
|
+
return 'already-consumed';
|
|
35
|
+
if (entry.expiresAt <= now)
|
|
36
|
+
return 'expired';
|
|
37
|
+
entry.state = 'consumed';
|
|
38
|
+
entry.updatedAt = now;
|
|
39
|
+
return 'consumed';
|
|
40
|
+
},
|
|
41
|
+
release(quoteId, now = Date.now()) {
|
|
42
|
+
const entry = entries.get(quoteId);
|
|
43
|
+
if (!entry || entry.state !== 'reserved')
|
|
44
|
+
return;
|
|
45
|
+
entry.state = 'released';
|
|
46
|
+
entry.updatedAt = now;
|
|
47
|
+
},
|
|
48
|
+
outstanding(tokenId, providerId, now = Date.now()) {
|
|
49
|
+
let total = 0n;
|
|
50
|
+
for (const entry of entries.values()) {
|
|
51
|
+
if (entry.state !== 'reserved')
|
|
52
|
+
continue;
|
|
53
|
+
if (entry.expiresAt <= now)
|
|
54
|
+
continue;
|
|
55
|
+
if (entry.tokenId !== tokenId)
|
|
56
|
+
continue;
|
|
57
|
+
if (entry.providerId !== providerId)
|
|
58
|
+
continue;
|
|
59
|
+
total += entry.amountUnits;
|
|
60
|
+
}
|
|
61
|
+
return total;
|
|
62
|
+
},
|
|
63
|
+
prune(now = Date.now()) {
|
|
64
|
+
for (const [id, entry] of entries) {
|
|
65
|
+
if (entry.state !== 'reserved' || entry.expiresAt <= now)
|
|
66
|
+
entries.delete(id);
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
}
|
package/dist/policy.d.ts
CHANGED
|
@@ -16,8 +16,13 @@ export interface OmniaLspOpenRequest {
|
|
|
16
16
|
direction: OmniaLspDirection;
|
|
17
17
|
lease?: boolean;
|
|
18
18
|
jit?: boolean;
|
|
19
|
+
/** Reject when the total open fee exceeds this decimal amount. */
|
|
20
|
+
maxOpenFee?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Legacy ceiling for the percentage component. Also bounds a flat fee by
|
|
23
|
+
* converting it to the equivalent basis points on the fee basis (OMN-072).
|
|
24
|
+
*/
|
|
19
25
|
maxOpenFeeBps?: number;
|
|
20
|
-
now?: number;
|
|
21
26
|
}
|
|
22
27
|
export interface OmniaLspOpenDecision {
|
|
23
28
|
allowed: boolean;
|
|
@@ -29,11 +34,23 @@ export interface OmniaLspProviderPolicy {
|
|
|
29
34
|
requireNonCustodial?: boolean;
|
|
30
35
|
/** Minimum provider-bond score accepted by this provider. Informational only. */
|
|
31
36
|
minProviderScore?: number;
|
|
37
|
+
/**
|
|
38
|
+
* Spendable provider funds for the requested asset, decimal units. When set,
|
|
39
|
+
* the provider-side contribution must fit inside it (OMN-067).
|
|
40
|
+
*/
|
|
41
|
+
availableProviderFunds?: string;
|
|
32
42
|
}
|
|
43
|
+
declare function hasCapacity(asset: OmniaLspAsset, direction: OmniaLspDirection, amount: bigint): boolean;
|
|
33
44
|
/**
|
|
34
45
|
* Evaluate an open request against a provider manifest.
|
|
35
46
|
*
|
|
36
47
|
* This is the provider's admission policy. It does not reserve capacity; the
|
|
37
|
-
* provider MUST
|
|
48
|
+
* provider MUST do that through the quote ledger (OMN-068).
|
|
49
|
+
*
|
|
50
|
+
* Both sides of the channel are validated independently (OMN-067): the
|
|
51
|
+
* consumer's `localAmount` and the provider's own `remoteAmount`, because the
|
|
52
|
+
* provider is the party that funds the latter. `now` is the provider's trusted
|
|
53
|
+
* clock — never the requester's (OMN-069).
|
|
38
54
|
*/
|
|
39
|
-
export declare function evaluateOpenRequest(manifest: OmniaLspManifest, request: OmniaLspOpenRequest, policy?: OmniaLspProviderPolicy): OmniaLspOpenDecision;
|
|
55
|
+
export declare function evaluateOpenRequest(manifest: OmniaLspManifest, request: OmniaLspOpenRequest, policy?: OmniaLspProviderPolicy, now?: number): OmniaLspOpenDecision;
|
|
56
|
+
export { hasCapacity };
|