@totemsdk/omnia-lsp-provider 0.0.0-stage → 0.1.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.
@@ -0,0 +1,76 @@
1
+ import type { OmniaLspManifest, OmniaLspOpenQuote } from '@totemsdk/omnia-lsp';
2
+ import { type OmniaLspOpenDecision, type OmniaLspOpenRequest, type OmniaLspProviderPolicy } from './policy.js';
3
+ import { type UnsignedOmniaLspOpenQuote } from './quote.js';
4
+ /**
5
+ * Optional funding inputs for a provider-hosted open.
6
+ *
7
+ * A provider that already selected its funding coin can pass the coin id plus
8
+ * the node proof material. A provider that wants the host to select a coin can
9
+ * omit all of these; `omnia-host` then selects a sendable coin and builds the
10
+ * funding witness from its own wallet/node.
11
+ */
12
+ export interface OmniaLspHostFunding {
13
+ /** Address to select the provider funding coin from (simple wallet address). */
14
+ fundingAddress?: string;
15
+ fundingCoinId?: string;
16
+ fundingPubkey?: string;
17
+ fundingScript?: string;
18
+ fundingCoinProofsHex?: string | string[];
19
+ /** Provider-wallet-built witness. The consumer/dApp witness is still forbidden. */
20
+ fundingWitnessHex?: string;
21
+ /**
22
+ * Channel contest delay. Omit for the host/program default (normally 256).
23
+ * Test/dev hosts may pass a shorter value when OMNIA_ALLOW_SHORT_CONTEST_DELAY=1.
24
+ */
25
+ contestDelayBlocks?: number;
26
+ }
27
+ export interface OmniaLspHostOpenInput extends OmniaLspHostFunding {
28
+ quote: OmniaLspOpenQuote;
29
+ }
30
+ export interface OmniaLspHostOpenResult {
31
+ success: boolean;
32
+ channelId?: string;
33
+ fundingTxId?: string;
34
+ error?: string;
35
+ errorCode?: string;
36
+ }
37
+ export interface OmniaLspHostCloseResult {
38
+ success: boolean;
39
+ error?: string;
40
+ errorCode?: string;
41
+ }
42
+ /**
43
+ * Host port implemented by the provider runtime (for example an `omnia-host`
44
+ * wrapper). The LSP package owns policy and quotes; the host owns channel
45
+ * construction, signing and broadcast.
46
+ */
47
+ export interface OmniaLspProviderHost {
48
+ openChannel(input: OmniaLspHostOpenInput): Promise<OmniaLspHostOpenResult>;
49
+ closeChannel?(channelId: string): Promise<OmniaLspHostCloseResult>;
50
+ }
51
+ export interface OmniaLspProviderConfig {
52
+ manifest: OmniaLspManifest;
53
+ host: OmniaLspProviderHost;
54
+ policy?: OmniaLspProviderPolicy;
55
+ /** Provider identity signer for quotes. Required for quote issuance. */
56
+ signQuote: (quote: UnsignedOmniaLspOpenQuote) => Promise<string> | string;
57
+ /** Default quote TTL. Default 60s. */
58
+ quoteTtlMs?: number;
59
+ /** Clock injection for deterministic tests. */
60
+ now?: () => number;
61
+ }
62
+ export interface OmniaLspProvider {
63
+ manifest(): OmniaLspManifest;
64
+ evaluate(request: OmniaLspOpenRequest): OmniaLspOpenDecision;
65
+ quote(request: OmniaLspOpenRequest): Promise<OmniaLspOpenQuote>;
66
+ open(request: OmniaLspOpenRequest, funding?: OmniaLspHostFunding | string): Promise<OmniaLspHostOpenResult>;
67
+ openQuote(quote: OmniaLspOpenQuote, funding?: OmniaLspHostFunding | string): Promise<OmniaLspHostOpenResult>;
68
+ close(channelId: string): Promise<OmniaLspHostCloseResult>;
69
+ }
70
+ /**
71
+ * Reference provider facade.
72
+ *
73
+ * It does not hold keys, open channels or broadcast. It applies policy, issues
74
+ * signed quotes and delegates the channel operation to the injected host.
75
+ */
76
+ export declare function createOmniaLspProvider(config: OmniaLspProviderConfig): OmniaLspProvider;
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createOmniaLspProvider = createOmniaLspProvider;
4
+ const policy_js_1 = require("./policy.js");
5
+ const quote_js_1 = require("./quote.js");
6
+ function assertAllowed(decision) {
7
+ if (!decision.allowed) {
8
+ throw new Error(`Open request rejected: ${decision.errors.join('; ')}`);
9
+ }
10
+ }
11
+ /**
12
+ * Reference provider facade.
13
+ *
14
+ * It does not hold keys, open channels or broadcast. It applies policy, issues
15
+ * signed quotes and delegates the channel operation to the injected host.
16
+ */
17
+ function createOmniaLspProvider(config) {
18
+ const now = config.now ?? Date.now;
19
+ async function quote(request) {
20
+ const decision = (0, policy_js_1.evaluateOpenRequest)(config.manifest, request, config.policy);
21
+ assertAllowed(decision);
22
+ const unsigned = (0, quote_js_1.createUnsignedOpenQuote)({
23
+ manifest: config.manifest,
24
+ request,
25
+ now: now(),
26
+ ttlMs: config.quoteTtlMs,
27
+ });
28
+ return (0, quote_js_1.signOpenQuote)(unsigned, config.signQuote);
29
+ }
30
+ return {
31
+ manifest() {
32
+ return config.manifest;
33
+ },
34
+ evaluate(request) {
35
+ return (0, policy_js_1.evaluateOpenRequest)(config.manifest, request, config.policy);
36
+ },
37
+ quote,
38
+ async open(request, funding) {
39
+ const issued = await quote(request);
40
+ return config.host.openChannel({
41
+ quote: issued,
42
+ ...(typeof funding === 'string' ? { fundingCoinId: funding } : funding ?? {}),
43
+ });
44
+ },
45
+ async openQuote(quote, funding) {
46
+ if (quote.expiresAt <= now()) {
47
+ return { success: false, error: 'Quote expired', errorCode: 'QUOTE_EXPIRED' };
48
+ }
49
+ if (quote.providerId !== config.manifest.providerId) {
50
+ return { success: false, error: 'Quote provider mismatch', errorCode: 'QUOTE_PROVIDER_MISMATCH' };
51
+ }
52
+ return config.host.openChannel({
53
+ quote,
54
+ ...(typeof funding === 'string' ? { fundingCoinId: funding } : funding ?? {}),
55
+ });
56
+ },
57
+ async close(channelId) {
58
+ if (!config.host.closeChannel) {
59
+ return { success: false, error: 'Host does not implement closeChannel', errorCode: 'UNSUPPORTED' };
60
+ }
61
+ return config.host.closeChannel(channelId);
62
+ },
63
+ };
64
+ }
@@ -0,0 +1,20 @@
1
+ import type { OmniaLspManifest, OmniaLspOpenQuote } from '@totemsdk/omnia-lsp';
2
+ import type { OmniaLspOpenRequest } from './policy.js';
3
+ export type UnsignedOmniaLspOpenQuote = Omit<OmniaLspOpenQuote, 'signature'>;
4
+ export interface CreateQuoteOptions {
5
+ manifest: OmniaLspManifest;
6
+ request: OmniaLspOpenRequest;
7
+ now?: number;
8
+ ttlMs?: number;
9
+ quoteId?: string;
10
+ }
11
+ /**
12
+ * Build the provider's unsigned open quote. The provider host signs it before
13
+ * returning it to the consumer; a quote without a signature is not authoritative.
14
+ */
15
+ export declare function createUnsignedOpenQuote(options: CreateQuoteOptions): UnsignedOmniaLspOpenQuote;
16
+ /**
17
+ * Sign an unsigned quote with a provider identity signer. The signer is injected
18
+ * so the package stays runtime-neutral and never holds a private key.
19
+ */
20
+ export declare function signOpenQuote(quote: UnsignedOmniaLspOpenQuote, sign: (quote: UnsignedOmniaLspOpenQuote) => Promise<string> | string): Promise<OmniaLspOpenQuote>;
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createUnsignedOpenQuote = createUnsignedOpenQuote;
4
+ exports.signOpenQuote = signOpenQuote;
5
+ const core_1 = require("@totemsdk/core");
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
+ }
27
+ /**
28
+ * Build the provider's unsigned open quote. The provider host signs it before
29
+ * returning it to the consumer; a quote without a signature is not authoritative.
30
+ */
31
+ function createUnsignedOpenQuote(options) {
32
+ const { manifest, request } = options;
33
+ const now = options.now ?? Date.now();
34
+ const ttlMs = options.ttlMs ?? 60000;
35
+ const expiresAt = now + ttlMs;
36
+ const openFee = manifest.fees.openFlat ??
37
+ (manifest.fees.openBps ? applyBps(request.localAmount, manifest.fees.openBps) : '0');
38
+ const leaseFee = request.lease === true && manifest.fees.leaseBps
39
+ ? applyBps(request.localAmount, manifest.fees.leaseBps)
40
+ : undefined;
41
+ const terms = {
42
+ version: 'omnia-lsp/open-terms/v1',
43
+ providerId: manifest.providerId,
44
+ consumerAddress: request.consumerAddress,
45
+ consumerPartyId: request.consumerPartyId,
46
+ consumerPublicKeyDigest: request.consumerPublicKeyDigest,
47
+ consumerSettlementAddress: request.consumerSettlementAddress ?? null,
48
+ consumerTransportPubkey: request.consumerTransportPubkey ?? null,
49
+ tokenId: request.tokenId,
50
+ localAmount: request.localAmount,
51
+ remoteAmount: request.remoteAmount,
52
+ openFee,
53
+ leaseFee: leaseFee ?? null,
54
+ providerPartyId: manifest.channel.partyId,
55
+ providerPublicKeyDigest: manifest.channel.publicKeyDigest,
56
+ providerSettlementAddress: manifest.channel.settlementAddress,
57
+ expiresAt,
58
+ };
59
+ const rawTermsHash = (0, core_1.hashCanonical)('omnia-lsp:open-terms:v1', terms);
60
+ const termsHash = rawTermsHash.startsWith('0x') ? rawTermsHash : `0x${rawTermsHash}`;
61
+ return {
62
+ version: 'omnia-lsp/open-quote/v1',
63
+ quoteId: options.quoteId ?? `quote-${termsHash.slice(2, 18)}-${now.toString(36)}`,
64
+ providerId: manifest.providerId,
65
+ consumerAddress: request.consumerAddress,
66
+ consumerPartyId: request.consumerPartyId,
67
+ consumerPublicKeyDigest: request.consumerPublicKeyDigest,
68
+ ...(request.consumerSettlementAddress
69
+ ? { consumerSettlementAddress: request.consumerSettlementAddress }
70
+ : {}),
71
+ ...(request.consumerTransportPubkey
72
+ ? { consumerTransportPubkey: request.consumerTransportPubkey }
73
+ : {}),
74
+ tokenId: request.tokenId,
75
+ localAmount: request.localAmount,
76
+ remoteAmount: request.remoteAmount,
77
+ openFee,
78
+ ...(leaseFee !== undefined ? { leaseFee } : {}),
79
+ providerPartyId: manifest.channel.partyId,
80
+ providerPublicKeyDigest: manifest.channel.publicKeyDigest,
81
+ providerSettlementAddress: manifest.channel.settlementAddress,
82
+ termsHash,
83
+ expiresAt,
84
+ };
85
+ }
86
+ /**
87
+ * Sign an unsigned quote with a provider identity signer. The signer is injected
88
+ * so the package stays runtime-neutral and never holds a private key.
89
+ */
90
+ function signOpenQuote(quote, sign) {
91
+ return Promise.resolve(sign(quote)).then((signature) => ({ ...quote, signature }));
92
+ }
package/dist/host.d.ts ADDED
@@ -0,0 +1,22 @@
1
+ import type { OmniaLspProviderHost } from './provider.js';
2
+ export type OmniaHostHandler = (params: unknown) => Promise<unknown> | unknown;
3
+ export interface OmniaHostMethodMap {
4
+ get(name: string): OmniaHostHandler | undefined;
5
+ }
6
+ export interface CreateOmniaHostProviderHostOptions {
7
+ /** `createHostMethods(context)` map (or any Map-like handler registry). */
8
+ methods: OmniaHostMethodMap | Map<string, OmniaHostHandler>;
9
+ openMethod?: string;
10
+ closeMethod?: string;
11
+ /** Optional operation id prefix. Default: omnia-lsp. */
12
+ operationIdPrefix?: string;
13
+ }
14
+ /**
15
+ * Adapt an `omnia-host` method registry (`createHostMethods(context)`) to the
16
+ * LSP provider host port.
17
+ *
18
+ * The quote is written from the consumer's perspective: `localAmount` is the
19
+ * consumer side and `remoteAmount` is the provider side. The host is the provider,
20
+ * so those sides are swapped when calling `totem_omniaOpenChannel`.
21
+ */
22
+ export declare function createOmniaHostProviderHost(options: CreateOmniaHostProviderHostOptions): OmniaLspProviderHost;
package/dist/host.js ADDED
@@ -0,0 +1,95 @@
1
+ function handlerFor(methods, name) {
2
+ return typeof methods.get === 'function'
3
+ ? methods.get(name)
4
+ : undefined;
5
+ }
6
+ /**
7
+ * Adapt an `omnia-host` method registry (`createHostMethods(context)`) to the
8
+ * LSP provider host port.
9
+ *
10
+ * The quote is written from the consumer's perspective: `localAmount` is the
11
+ * consumer side and `remoteAmount` is the provider side. The host is the provider,
12
+ * so those sides are swapped when calling `totem_omniaOpenChannel`.
13
+ */
14
+ export function createOmniaHostProviderHost(options) {
15
+ const openMethod = options.openMethod ?? 'totem_omniaOpenChannel';
16
+ const closeMethod = options.closeMethod ?? 'totem_omniaCloseChannel';
17
+ const prefix = options.operationIdPrefix ?? 'omnia-lsp';
18
+ return {
19
+ async openChannel({ quote, fundingAddress, fundingCoinId, fundingPubkey, fundingScript, fundingCoinProofsHex, fundingWitnessHex, contestDelayBlocks, }) {
20
+ const handler = handlerFor(options.methods, openMethod);
21
+ if (!handler) {
22
+ return {
23
+ success: false,
24
+ error: `${openMethod} is not registered on this omnia-host`,
25
+ errorCode: 'UNSUPPORTED',
26
+ };
27
+ }
28
+ try {
29
+ const result = (await handler({
30
+ remotePartyId: quote.consumerPartyId,
31
+ remotePublicKeyDigest: quote.consumerPublicKeyDigest,
32
+ ...(quote.consumerSettlementAddress
33
+ ? { remoteSettlementAddress: quote.consumerSettlementAddress }
34
+ : {}),
35
+ ...(quote.consumerTransportPubkey
36
+ ? { remoteTransportPubkey: quote.consumerTransportPubkey }
37
+ : {}),
38
+ localAmount: quote.remoteAmount,
39
+ remoteAmount: quote.localAmount,
40
+ tokenId: quote.tokenId,
41
+ ...(fundingAddress ? { fundingAddress } : {}),
42
+ ...(fundingCoinId ? { fundingCoinId } : {}),
43
+ ...(fundingPubkey ? { fundingPubkey } : {}),
44
+ ...(fundingScript ? { fundingScript } : {}),
45
+ ...(fundingCoinProofsHex !== undefined ? { fundingCoinProofsHex } : {}),
46
+ ...(fundingWitnessHex ? { fundingWitnessHex } : {}),
47
+ ...(contestDelayBlocks !== undefined ? { contestDelayBlocks } : {}),
48
+ operationId: `${prefix}-open-${quote.quoteId}`,
49
+ }));
50
+ return {
51
+ success: result?.success !== false,
52
+ ...(result?.channelId ? { channelId: result.channelId } : {}),
53
+ ...(result?.fundingTxId ? { fundingTxId: result.fundingTxId } : {}),
54
+ ...(result?.error ? { error: result.error } : {}),
55
+ ...(result?.errorCode ? { errorCode: result.errorCode } : {}),
56
+ };
57
+ }
58
+ catch (error) {
59
+ return {
60
+ success: false,
61
+ error: error instanceof Error ? error.message : String(error),
62
+ errorCode: 'HOST_OPEN_FAILED',
63
+ };
64
+ }
65
+ },
66
+ async closeChannel(channelId) {
67
+ const handler = handlerFor(options.methods, closeMethod);
68
+ if (!handler) {
69
+ return {
70
+ success: false,
71
+ error: `${closeMethod} is not registered on this omnia-host`,
72
+ errorCode: 'UNSUPPORTED',
73
+ };
74
+ }
75
+ try {
76
+ const result = (await handler({
77
+ channelId,
78
+ operationId: `${prefix}-close-${channelId}`,
79
+ }));
80
+ return {
81
+ success: result?.success !== false,
82
+ ...(result?.error ? { error: result.error } : {}),
83
+ ...(result?.errorCode ? { errorCode: result.errorCode } : {}),
84
+ };
85
+ }
86
+ catch (error) {
87
+ return {
88
+ success: false,
89
+ error: error instanceof Error ? error.message : String(error),
90
+ errorCode: 'HOST_CLOSE_FAILED',
91
+ };
92
+ }
93
+ },
94
+ };
95
+ }
@@ -0,0 +1,8 @@
1
+ export { evaluateOpenRequest, } from './policy.js';
2
+ export type { OmniaLspOpenDecision, OmniaLspOpenRequest, OmniaLspProviderPolicy, } from './policy.js';
3
+ export { createUnsignedOpenQuote, signOpenQuote, } from './quote.js';
4
+ export type { CreateQuoteOptions, UnsignedOmniaLspOpenQuote, } from './quote.js';
5
+ export { createOmniaLspProvider, } from './provider.js';
6
+ export type { OmniaLspHostCloseResult, OmniaLspHostOpenInput, OmniaLspHostOpenResult, OmniaLspProvider, OmniaLspProviderConfig, OmniaLspProviderHost, } from './provider.js';
7
+ export { createOmniaHostProviderHost } from './host.js';
8
+ export type { CreateOmniaHostProviderHostOptions, OmniaHostHandler, OmniaHostMethodMap, } from './host.js';
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export { evaluateOpenRequest, } from './policy.js';
2
+ export { createUnsignedOpenQuote, signOpenQuote, } from './quote.js';
3
+ export { createOmniaLspProvider, } from './provider.js';
4
+ export { createOmniaHostProviderHost } from './host.js';
@@ -0,0 +1,39 @@
1
+ import type { OmniaLspAsset, OmniaLspDirection, OmniaLspManifest } from '@totemsdk/omnia-lsp';
2
+ export interface OmniaLspOpenRequest {
3
+ providerId: string;
4
+ consumerAddress: string;
5
+ /** Consumer Omnia party id (identity id or channel party id). */
6
+ consumerPartyId: string;
7
+ /** Consumer channel key digest (32-byte hex). */
8
+ consumerPublicKeyDigest: string;
9
+ consumerSettlementAddress?: string;
10
+ consumerTransportPubkey?: string;
11
+ tokenId: string;
12
+ /** Consumer side of the initial channel balance, decimal units. */
13
+ localAmount: string;
14
+ /** Provider side of the initial channel balance, decimal units. */
15
+ remoteAmount: string;
16
+ direction: OmniaLspDirection;
17
+ lease?: boolean;
18
+ jit?: boolean;
19
+ maxOpenFeeBps?: number;
20
+ now?: number;
21
+ }
22
+ export interface OmniaLspOpenDecision {
23
+ allowed: boolean;
24
+ errors: string[];
25
+ asset?: OmniaLspAsset;
26
+ }
27
+ export interface OmniaLspProviderPolicy {
28
+ /** Reject hosted-custody manifests. Default true for a non-custodial LSP. */
29
+ requireNonCustodial?: boolean;
30
+ /** Minimum provider-bond score accepted by this provider. Informational only. */
31
+ minProviderScore?: number;
32
+ }
33
+ /**
34
+ * Evaluate an open request against a provider manifest.
35
+ *
36
+ * This is the provider's admission policy. It does not reserve capacity; the
37
+ * provider MUST re-check capacity and channel state before co-signing.
38
+ */
39
+ export declare function evaluateOpenRequest(manifest: OmniaLspManifest, request: OmniaLspOpenRequest, policy?: OmniaLspProviderPolicy): OmniaLspOpenDecision;
package/dist/policy.js ADDED
@@ -0,0 +1,62 @@
1
+ const DECIMAL_SCALE = 18n;
2
+ const DECIMAL_FACTOR = 10n ** DECIMAL_SCALE;
3
+ function decimalKey(value) {
4
+ const [whole = '0', fraction = ''] = value.split('.');
5
+ return (BigInt(whole) * DECIMAL_FACTOR +
6
+ BigInt(fraction.padEnd(Number(DECIMAL_SCALE), '0').slice(0, Number(DECIMAL_SCALE))));
7
+ }
8
+ function gte(a, b) {
9
+ return decimalKey(a) >= decimalKey(b);
10
+ }
11
+ function hasCapacity(asset, direction, amount) {
12
+ if (direction === 'send')
13
+ return gte(asset.outboundCapacity, amount);
14
+ if (direction === 'receive')
15
+ return gte(asset.inboundCapacity, amount);
16
+ return gte(asset.inboundCapacity, amount) && gte(asset.outboundCapacity, amount);
17
+ }
18
+ /**
19
+ * Evaluate an open request against a provider manifest.
20
+ *
21
+ * This is the provider's admission policy. It does not reserve capacity; the
22
+ * provider MUST re-check capacity and channel state before co-signing.
23
+ */
24
+ export function evaluateOpenRequest(manifest, request, policy = {}) {
25
+ const errors = [];
26
+ const now = request.now ?? Date.now();
27
+ if (request.providerId !== manifest.providerId) {
28
+ errors.push('request.providerId does not match the manifest');
29
+ }
30
+ if (manifest.expiresAt <= now) {
31
+ errors.push('manifest is expired');
32
+ }
33
+ if (policy.requireNonCustodial !== false && (manifest.custody.mode === 'hosted' || manifest.custody.canMoveFunds)) {
34
+ errors.push('hosted custody is not accepted by this provider policy');
35
+ }
36
+ const asset = manifest.assets.find((entry) => entry.tokenId === request.tokenId);
37
+ if (!asset) {
38
+ errors.push('asset is not offered by the provider');
39
+ }
40
+ else {
41
+ if (!gte(request.localAmount, asset.minChannel)) {
42
+ errors.push('localAmount is below the provider minimum');
43
+ }
44
+ if (!gte(asset.maxChannel, request.localAmount)) {
45
+ errors.push('localAmount is above the provider maximum');
46
+ }
47
+ if (!hasCapacity(asset, request.direction, request.localAmount)) {
48
+ errors.push('insufficient capacity for the requested direction');
49
+ }
50
+ }
51
+ if (request.lease === true && manifest.terms.lease !== true) {
52
+ errors.push('lease is not offered by the provider');
53
+ }
54
+ if (request.jit === true && manifest.terms.jit !== true) {
55
+ errors.push('JIT is not offered by the provider');
56
+ }
57
+ if (request.maxOpenFeeBps !== undefined &&
58
+ (manifest.fees.openBps ?? 0) > request.maxOpenFeeBps) {
59
+ errors.push('provider open fee exceeds the request maximum');
60
+ }
61
+ return { allowed: errors.length === 0, errors, ...(asset ? { asset } : {}) };
62
+ }
@@ -0,0 +1,76 @@
1
+ import type { OmniaLspManifest, OmniaLspOpenQuote } from '@totemsdk/omnia-lsp';
2
+ import { type OmniaLspOpenDecision, type OmniaLspOpenRequest, type OmniaLspProviderPolicy } from './policy.js';
3
+ import { type UnsignedOmniaLspOpenQuote } from './quote.js';
4
+ /**
5
+ * Optional funding inputs for a provider-hosted open.
6
+ *
7
+ * A provider that already selected its funding coin can pass the coin id plus
8
+ * the node proof material. A provider that wants the host to select a coin can
9
+ * omit all of these; `omnia-host` then selects a sendable coin and builds the
10
+ * funding witness from its own wallet/node.
11
+ */
12
+ export interface OmniaLspHostFunding {
13
+ /** Address to select the provider funding coin from (simple wallet address). */
14
+ fundingAddress?: string;
15
+ fundingCoinId?: string;
16
+ fundingPubkey?: string;
17
+ fundingScript?: string;
18
+ fundingCoinProofsHex?: string | string[];
19
+ /** Provider-wallet-built witness. The consumer/dApp witness is still forbidden. */
20
+ fundingWitnessHex?: string;
21
+ /**
22
+ * Channel contest delay. Omit for the host/program default (normally 256).
23
+ * Test/dev hosts may pass a shorter value when OMNIA_ALLOW_SHORT_CONTEST_DELAY=1.
24
+ */
25
+ contestDelayBlocks?: number;
26
+ }
27
+ export interface OmniaLspHostOpenInput extends OmniaLspHostFunding {
28
+ quote: OmniaLspOpenQuote;
29
+ }
30
+ export interface OmniaLspHostOpenResult {
31
+ success: boolean;
32
+ channelId?: string;
33
+ fundingTxId?: string;
34
+ error?: string;
35
+ errorCode?: string;
36
+ }
37
+ export interface OmniaLspHostCloseResult {
38
+ success: boolean;
39
+ error?: string;
40
+ errorCode?: string;
41
+ }
42
+ /**
43
+ * Host port implemented by the provider runtime (for example an `omnia-host`
44
+ * wrapper). The LSP package owns policy and quotes; the host owns channel
45
+ * construction, signing and broadcast.
46
+ */
47
+ export interface OmniaLspProviderHost {
48
+ openChannel(input: OmniaLspHostOpenInput): Promise<OmniaLspHostOpenResult>;
49
+ closeChannel?(channelId: string): Promise<OmniaLspHostCloseResult>;
50
+ }
51
+ export interface OmniaLspProviderConfig {
52
+ manifest: OmniaLspManifest;
53
+ host: OmniaLspProviderHost;
54
+ policy?: OmniaLspProviderPolicy;
55
+ /** Provider identity signer for quotes. Required for quote issuance. */
56
+ signQuote: (quote: UnsignedOmniaLspOpenQuote) => Promise<string> | string;
57
+ /** Default quote TTL. Default 60s. */
58
+ quoteTtlMs?: number;
59
+ /** Clock injection for deterministic tests. */
60
+ now?: () => number;
61
+ }
62
+ export interface OmniaLspProvider {
63
+ manifest(): OmniaLspManifest;
64
+ evaluate(request: OmniaLspOpenRequest): OmniaLspOpenDecision;
65
+ quote(request: OmniaLspOpenRequest): Promise<OmniaLspOpenQuote>;
66
+ open(request: OmniaLspOpenRequest, funding?: OmniaLspHostFunding | string): Promise<OmniaLspHostOpenResult>;
67
+ openQuote(quote: OmniaLspOpenQuote, funding?: OmniaLspHostFunding | string): Promise<OmniaLspHostOpenResult>;
68
+ close(channelId: string): Promise<OmniaLspHostCloseResult>;
69
+ }
70
+ /**
71
+ * Reference provider facade.
72
+ *
73
+ * It does not hold keys, open channels or broadcast. It applies policy, issues
74
+ * signed quotes and delegates the channel operation to the injected host.
75
+ */
76
+ export declare function createOmniaLspProvider(config: OmniaLspProviderConfig): OmniaLspProvider;
@@ -0,0 +1,61 @@
1
+ import { evaluateOpenRequest, } from './policy.js';
2
+ import { createUnsignedOpenQuote, signOpenQuote, } from './quote.js';
3
+ function assertAllowed(decision) {
4
+ if (!decision.allowed) {
5
+ throw new Error(`Open request rejected: ${decision.errors.join('; ')}`);
6
+ }
7
+ }
8
+ /**
9
+ * Reference provider facade.
10
+ *
11
+ * It does not hold keys, open channels or broadcast. It applies policy, issues
12
+ * signed quotes and delegates the channel operation to the injected host.
13
+ */
14
+ export function createOmniaLspProvider(config) {
15
+ const now = config.now ?? Date.now;
16
+ async function quote(request) {
17
+ const decision = evaluateOpenRequest(config.manifest, request, config.policy);
18
+ assertAllowed(decision);
19
+ const unsigned = createUnsignedOpenQuote({
20
+ manifest: config.manifest,
21
+ request,
22
+ now: now(),
23
+ ttlMs: config.quoteTtlMs,
24
+ });
25
+ return signOpenQuote(unsigned, config.signQuote);
26
+ }
27
+ return {
28
+ manifest() {
29
+ return config.manifest;
30
+ },
31
+ evaluate(request) {
32
+ return evaluateOpenRequest(config.manifest, request, config.policy);
33
+ },
34
+ quote,
35
+ async open(request, funding) {
36
+ const issued = await quote(request);
37
+ return config.host.openChannel({
38
+ quote: issued,
39
+ ...(typeof funding === 'string' ? { fundingCoinId: funding } : funding ?? {}),
40
+ });
41
+ },
42
+ async openQuote(quote, funding) {
43
+ if (quote.expiresAt <= now()) {
44
+ return { success: false, error: 'Quote expired', errorCode: 'QUOTE_EXPIRED' };
45
+ }
46
+ if (quote.providerId !== config.manifest.providerId) {
47
+ return { success: false, error: 'Quote provider mismatch', errorCode: 'QUOTE_PROVIDER_MISMATCH' };
48
+ }
49
+ return config.host.openChannel({
50
+ quote,
51
+ ...(typeof funding === 'string' ? { fundingCoinId: funding } : funding ?? {}),
52
+ });
53
+ },
54
+ async close(channelId) {
55
+ if (!config.host.closeChannel) {
56
+ return { success: false, error: 'Host does not implement closeChannel', errorCode: 'UNSUPPORTED' };
57
+ }
58
+ return config.host.closeChannel(channelId);
59
+ },
60
+ };
61
+ }
@@ -0,0 +1,20 @@
1
+ import type { OmniaLspManifest, OmniaLspOpenQuote } from '@totemsdk/omnia-lsp';
2
+ import type { OmniaLspOpenRequest } from './policy.js';
3
+ export type UnsignedOmniaLspOpenQuote = Omit<OmniaLspOpenQuote, 'signature'>;
4
+ export interface CreateQuoteOptions {
5
+ manifest: OmniaLspManifest;
6
+ request: OmniaLspOpenRequest;
7
+ now?: number;
8
+ ttlMs?: number;
9
+ quoteId?: string;
10
+ }
11
+ /**
12
+ * Build the provider's unsigned open quote. The provider host signs it before
13
+ * returning it to the consumer; a quote without a signature is not authoritative.
14
+ */
15
+ export declare function createUnsignedOpenQuote(options: CreateQuoteOptions): UnsignedOmniaLspOpenQuote;
16
+ /**
17
+ * Sign an unsigned quote with a provider identity signer. The signer is injected
18
+ * so the package stays runtime-neutral and never holds a private key.
19
+ */
20
+ export declare function signOpenQuote(quote: UnsignedOmniaLspOpenQuote, sign: (quote: UnsignedOmniaLspOpenQuote) => Promise<string> | string): Promise<OmniaLspOpenQuote>;