@totemsdk/omnia-lsp-provider 0.0.0-stage → 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/LICENSE +202 -0
- package/NOTICE +60 -0
- package/README.md +85 -2
- package/dist/cjs/host.d.ts +22 -0
- package/dist/cjs/host.js +135 -0
- package/dist/cjs/index.d.ts +10 -0
- package/dist/cjs/index.js +14 -0
- package/dist/cjs/ledger.d.ts +57 -0
- package/dist/cjs/ledger.js +73 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/policy.d.ts +56 -0
- package/dist/cjs/policy.js +134 -0
- package/dist/cjs/provider.d.ts +108 -0
- package/dist/cjs/provider.js +170 -0
- package/dist/cjs/quote.d.ts +30 -0
- package/dist/cjs/quote.js +82 -0
- package/dist/host.d.ts +22 -0
- package/dist/host.js +132 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +5 -0
- package/dist/ledger.d.ts +57 -0
- package/dist/ledger.js +70 -0
- package/dist/policy.d.ts +56 -0
- package/dist/policy.js +131 -0
- package/dist/provider.d.ts +108 -0
- package/dist/provider.js +167 -0
- package/dist/quote.d.ts +30 -0
- package/dist/quote.js +78 -0
- package/package.json +64 -3
package/dist/provider.js
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { parseDecimalUnits, verifyOpenQuote, } from '@totemsdk/omnia-lsp';
|
|
2
|
+
import { evaluateOpenRequest, hasCapacity, } from './policy.js';
|
|
3
|
+
import { createUnsignedOpenQuote, signOpenQuote, } from './quote.js';
|
|
4
|
+
import { createMemoryQuoteLedger } from './ledger.js';
|
|
5
|
+
function assertAllowed(decision) {
|
|
6
|
+
if (!decision.allowed) {
|
|
7
|
+
throw new Error(`Open request rejected: ${decision.errors.join('; ')}`);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
function reject(error, errorCode) {
|
|
11
|
+
return { success: false, error, errorCode };
|
|
12
|
+
}
|
|
13
|
+
function fundingInput(funding) {
|
|
14
|
+
return typeof funding === 'string' ? { fundingCoinId: funding } : funding ?? {};
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Reference provider facade.
|
|
18
|
+
*
|
|
19
|
+
* It does not hold keys, open channels or broadcast. It applies policy, issues
|
|
20
|
+
* signed quotes, reserves provider capacity and delegates the channel operation
|
|
21
|
+
* to the injected host.
|
|
22
|
+
*/
|
|
23
|
+
export function createOmniaLspProvider(config) {
|
|
24
|
+
const now = config.now ?? Date.now;
|
|
25
|
+
const ledger = config.quoteLedger ?? createMemoryQuoteLedger();
|
|
26
|
+
let quoteCounter = 0;
|
|
27
|
+
/**
|
|
28
|
+
* Re-apply admission to a quote that is being redeemed (OMN-066). The quote
|
|
29
|
+
* does not carry `direction` or the lease flag, so this uses the conservative
|
|
30
|
+
* `both` direction and derives lease from the quoted lease fee.
|
|
31
|
+
*/
|
|
32
|
+
function quoteAsRequest(quote) {
|
|
33
|
+
return {
|
|
34
|
+
providerId: quote.providerId,
|
|
35
|
+
consumerAddress: quote.consumerAddress,
|
|
36
|
+
consumerPartyId: quote.consumerPartyId,
|
|
37
|
+
consumerPublicKeyDigest: quote.consumerPublicKeyDigest,
|
|
38
|
+
...(quote.consumerSettlementAddress !== undefined
|
|
39
|
+
? { consumerSettlementAddress: quote.consumerSettlementAddress }
|
|
40
|
+
: {}),
|
|
41
|
+
...(quote.consumerTransportPubkey !== undefined
|
|
42
|
+
? { consumerTransportPubkey: quote.consumerTransportPubkey }
|
|
43
|
+
: {}),
|
|
44
|
+
tokenId: quote.tokenId,
|
|
45
|
+
localAmount: quote.localAmount,
|
|
46
|
+
remoteAmount: quote.remoteAmount,
|
|
47
|
+
direction: 'both',
|
|
48
|
+
...(quote.leaseFee !== undefined ? { lease: true } : {}),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
async function quote(request) {
|
|
52
|
+
const issuedAt = now();
|
|
53
|
+
const decision = evaluateOpenRequest(config.manifest, request, config.policy, issuedAt);
|
|
54
|
+
assertAllowed(decision);
|
|
55
|
+
const unsigned = createUnsignedOpenQuote({
|
|
56
|
+
manifest: config.manifest,
|
|
57
|
+
request,
|
|
58
|
+
now: issuedAt,
|
|
59
|
+
ttlMs: config.quoteTtlMs,
|
|
60
|
+
quoteId: `quote-${issuedAt.toString(36)}-${(++quoteCounter).toString(36)}`,
|
|
61
|
+
});
|
|
62
|
+
const signed = await signOpenQuote(unsigned, config.signQuote);
|
|
63
|
+
const remote = parseDecimalUnits(signed.remoteAmount);
|
|
64
|
+
if (!remote.ok)
|
|
65
|
+
throw new Error(`cannot reserve provider capacity: ${remote.error}`);
|
|
66
|
+
// Outstanding reservations count against the same capacity the manifest
|
|
67
|
+
// advertises, so concurrent quotes cannot collectively exceed it (OMN-068).
|
|
68
|
+
if (decision.asset) {
|
|
69
|
+
const outstanding = await ledger.outstanding(signed.tokenId, config.manifest.providerId, issuedAt);
|
|
70
|
+
if (!hasCapacity(decision.asset, request.direction, outstanding + remote.units)) {
|
|
71
|
+
throw new Error('Open request rejected: provider capacity is exhausted by outstanding quotes');
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
await ledger.reserve({
|
|
75
|
+
quoteId: signed.quoteId,
|
|
76
|
+
providerId: config.manifest.providerId,
|
|
77
|
+
tokenId: signed.tokenId,
|
|
78
|
+
amount: signed.remoteAmount,
|
|
79
|
+
amountUnits: remote.units,
|
|
80
|
+
expiresAt: signed.expiresAt,
|
|
81
|
+
state: 'reserved',
|
|
82
|
+
updatedAt: issuedAt,
|
|
83
|
+
});
|
|
84
|
+
return signed;
|
|
85
|
+
}
|
|
86
|
+
async function redeem(quote, funding) {
|
|
87
|
+
const consumed = await ledger.consume(quote.quoteId, now());
|
|
88
|
+
if (consumed === 'unknown') {
|
|
89
|
+
return reject(`Quote ${quote.quoteId} was not issued by this provider`, 'QUOTE_NOT_ISSUED');
|
|
90
|
+
}
|
|
91
|
+
if (consumed === 'expired') {
|
|
92
|
+
return reject('Quote expired', 'QUOTE_EXPIRED');
|
|
93
|
+
}
|
|
94
|
+
if (consumed === 'already-consumed') {
|
|
95
|
+
return reject(`Quote ${quote.quoteId} has already been used`, 'QUOTE_ALREADY_CONSUMED');
|
|
96
|
+
}
|
|
97
|
+
const result = await config.host.openChannel({
|
|
98
|
+
quote,
|
|
99
|
+
...fundingInput(funding),
|
|
100
|
+
});
|
|
101
|
+
if (result?.success !== true) {
|
|
102
|
+
// Nothing was funded, so the capacity goes back rather than being burned.
|
|
103
|
+
await ledger.release(quote.quoteId, now());
|
|
104
|
+
}
|
|
105
|
+
return result;
|
|
106
|
+
}
|
|
107
|
+
return {
|
|
108
|
+
manifest() {
|
|
109
|
+
return config.manifest;
|
|
110
|
+
},
|
|
111
|
+
evaluate(request) {
|
|
112
|
+
return evaluateOpenRequest(config.manifest, request, config.policy, now());
|
|
113
|
+
},
|
|
114
|
+
quote,
|
|
115
|
+
async open(request, funding) {
|
|
116
|
+
const issued = await quote(request);
|
|
117
|
+
return redeem(issued, funding);
|
|
118
|
+
},
|
|
119
|
+
async openQuote(quote, funding, options) {
|
|
120
|
+
const currentTime = now();
|
|
121
|
+
if (quote.expiresAt <= currentTime) {
|
|
122
|
+
return reject('Quote expired', 'QUOTE_EXPIRED');
|
|
123
|
+
}
|
|
124
|
+
if (quote.providerId !== config.manifest.providerId) {
|
|
125
|
+
return reject('Quote provider mismatch', 'QUOTE_PROVIDER_MISMATCH');
|
|
126
|
+
}
|
|
127
|
+
// Bind the quote to the authenticated caller before anything else can act
|
|
128
|
+
// on it. Without this the LSP authorization boundary is missing (OMN-066).
|
|
129
|
+
const consumer = options?.consumer;
|
|
130
|
+
const consumerBound = Boolean(consumer && (consumer.address || consumer.partyId || consumer.publicKeyDigest));
|
|
131
|
+
if (!consumerBound && config.requireConsumerBinding !== false) {
|
|
132
|
+
return reject('openQuote requires the authenticated consumer identity', 'QUOTE_CONSUMER_UNBOUND');
|
|
133
|
+
}
|
|
134
|
+
const verified = await verifyOpenQuote(quote, config.manifest, {
|
|
135
|
+
now: currentTime,
|
|
136
|
+
...(config.verifyQuoteSignature
|
|
137
|
+
? { verifySignature: config.verifyQuoteSignature }
|
|
138
|
+
: {}),
|
|
139
|
+
...(consumer?.partyId !== undefined
|
|
140
|
+
? { expectedConsumerPartyId: consumer.partyId }
|
|
141
|
+
: {}),
|
|
142
|
+
...(consumer?.publicKeyDigest !== undefined
|
|
143
|
+
? { expectedConsumerPublicKeyDigest: consumer.publicKeyDigest }
|
|
144
|
+
: {}),
|
|
145
|
+
...(consumer?.address !== undefined
|
|
146
|
+
? { expectedConsumerAddress: consumer.address }
|
|
147
|
+
: {}),
|
|
148
|
+
});
|
|
149
|
+
if (!verified.ok) {
|
|
150
|
+
return reject(`Quote rejected: ${verified.errors.join('; ')}`, 'QUOTE_INVALID');
|
|
151
|
+
}
|
|
152
|
+
// Re-apply the current policy: the manifest, custody mode and capacity may
|
|
153
|
+
// have changed since the quote was issued.
|
|
154
|
+
const policyDecision = evaluateOpenRequest(config.manifest, quoteAsRequest(quote), config.policy, currentTime);
|
|
155
|
+
if (!policyDecision.allowed) {
|
|
156
|
+
return reject(`Quote no longer satisfies provider policy: ${policyDecision.errors.join('; ')}`, 'QUOTE_POLICY_REJECTED');
|
|
157
|
+
}
|
|
158
|
+
return redeem(quote, funding);
|
|
159
|
+
},
|
|
160
|
+
async close(channelId) {
|
|
161
|
+
if (!config.host.closeChannel) {
|
|
162
|
+
return reject('Host does not implement closeChannel', 'UNSUPPORTED');
|
|
163
|
+
}
|
|
164
|
+
return config.host.closeChannel(channelId);
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
}
|
package/dist/quote.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { OmniaLspFeeBasis, OmniaLspManifest, OmniaLspOpenQuote, OpenQuoteSigningPayload } 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
|
+
/** Fee basis for percentage fees. Defaults to the total channel value. */
|
|
11
|
+
feeBasis?: OmniaLspFeeBasis;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Build the provider's unsigned open quote. The provider host signs it before
|
|
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.
|
|
20
|
+
*/
|
|
21
|
+
export declare function createUnsignedOpenQuote(options: CreateQuoteOptions): UnsignedOmniaLspOpenQuote;
|
|
22
|
+
/**
|
|
23
|
+
* Sign an unsigned quote with a provider identity signer. The signer is injected
|
|
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.
|
|
29
|
+
*/
|
|
30
|
+
export declare function signOpenQuote(quote: UnsignedOmniaLspOpenQuote, sign: (quote: UnsignedOmniaLspOpenQuote, payload: OpenQuoteSigningPayload) => Promise<string> | string): Promise<OmniaLspOpenQuote>;
|
package/dist/quote.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { buildOpenTerms, computeOpenFees, DEFAULT_FEE_BASIS, openQuoteSigningPayload, openTermsHash, } from '@totemsdk/omnia-lsp';
|
|
2
|
+
/**
|
|
3
|
+
* Build the provider's unsigned open quote. The provider host signs it before
|
|
4
|
+
* returning it to the consumer; a quote without a signature is not authoritative.
|
|
5
|
+
*
|
|
6
|
+
* The quoted fees come from the shared schedule (OMN-072) and the terms object
|
|
7
|
+
* is built by `buildOpenTerms`, which is the same function the verifier uses to
|
|
8
|
+
* recompute the hash.
|
|
9
|
+
*/
|
|
10
|
+
export function createUnsignedOpenQuote(options) {
|
|
11
|
+
const { manifest, request } = options;
|
|
12
|
+
const now = options.now ?? Date.now();
|
|
13
|
+
const ttlMs = options.ttlMs ?? 60000;
|
|
14
|
+
const expiresAt = now + ttlMs;
|
|
15
|
+
const basis = options.feeBasis ?? DEFAULT_FEE_BASIS;
|
|
16
|
+
const fees = computeOpenFees(manifest, request, basis);
|
|
17
|
+
if (!fees.ok) {
|
|
18
|
+
throw new Error(`cannot quote open request: ${fees.error}`);
|
|
19
|
+
}
|
|
20
|
+
const schedule = fees.schedule;
|
|
21
|
+
const terms = buildOpenTerms({
|
|
22
|
+
manifest,
|
|
23
|
+
consumerAddress: request.consumerAddress,
|
|
24
|
+
consumerPartyId: request.consumerPartyId,
|
|
25
|
+
consumerPublicKeyDigest: request.consumerPublicKeyDigest,
|
|
26
|
+
...(request.consumerSettlementAddress !== undefined
|
|
27
|
+
? { consumerSettlementAddress: request.consumerSettlementAddress }
|
|
28
|
+
: {}),
|
|
29
|
+
...(request.consumerTransportPubkey !== undefined
|
|
30
|
+
? { consumerTransportPubkey: request.consumerTransportPubkey }
|
|
31
|
+
: {}),
|
|
32
|
+
tokenId: request.tokenId,
|
|
33
|
+
localAmount: request.localAmount,
|
|
34
|
+
remoteAmount: request.remoteAmount,
|
|
35
|
+
openFee: schedule.openFee,
|
|
36
|
+
...(schedule.leaseFee !== undefined ? { leaseFee: schedule.leaseFee } : {}),
|
|
37
|
+
feeBasis: schedule.basis,
|
|
38
|
+
expiresAt,
|
|
39
|
+
});
|
|
40
|
+
const termsHash = openTermsHash(terms);
|
|
41
|
+
return {
|
|
42
|
+
version: 'omnia-lsp/open-quote/v1',
|
|
43
|
+
quoteId: options.quoteId ?? `quote-${termsHash.slice(2, 18)}-${now.toString(36)}`,
|
|
44
|
+
providerId: manifest.providerId,
|
|
45
|
+
consumerAddress: request.consumerAddress,
|
|
46
|
+
consumerPartyId: request.consumerPartyId,
|
|
47
|
+
consumerPublicKeyDigest: request.consumerPublicKeyDigest,
|
|
48
|
+
...(request.consumerSettlementAddress
|
|
49
|
+
? { consumerSettlementAddress: request.consumerSettlementAddress }
|
|
50
|
+
: {}),
|
|
51
|
+
...(request.consumerTransportPubkey
|
|
52
|
+
? { consumerTransportPubkey: request.consumerTransportPubkey }
|
|
53
|
+
: {}),
|
|
54
|
+
tokenId: request.tokenId,
|
|
55
|
+
localAmount: request.localAmount,
|
|
56
|
+
remoteAmount: request.remoteAmount,
|
|
57
|
+
openFee: schedule.openFee,
|
|
58
|
+
...(schedule.leaseFee !== undefined ? { leaseFee: schedule.leaseFee } : {}),
|
|
59
|
+
feeBasis: schedule.basis,
|
|
60
|
+
providerPartyId: manifest.channel.partyId,
|
|
61
|
+
providerPublicKeyDigest: manifest.channel.publicKeyDigest,
|
|
62
|
+
providerSettlementAddress: manifest.channel.settlementAddress,
|
|
63
|
+
termsHash,
|
|
64
|
+
expiresAt,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Sign an unsigned quote with a provider identity signer. The signer is injected
|
|
69
|
+
* so the package stays runtime-neutral and never holds a private key.
|
|
70
|
+
*
|
|
71
|
+
* The second argument is the exact payload to sign (`domain|termsHash`). A
|
|
72
|
+
* one-argument signer keeps working; a signer that ignores the payload must sign
|
|
73
|
+
* this same string to produce quotes that `verifyOpenQuote` accepts.
|
|
74
|
+
*/
|
|
75
|
+
export function signOpenQuote(quote, sign) {
|
|
76
|
+
const payload = openQuoteSigningPayload(quote);
|
|
77
|
+
return Promise.resolve(sign(quote, payload)).then((signature) => ({ ...quote, signature }));
|
|
78
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,67 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@totemsdk/omnia-lsp-provider",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Reference Omnia Liquidity Service Provider — policy, signed quotes and host adapter",
|
|
5
|
+
"main": "dist/cjs/index.js",
|
|
6
|
+
"types": "dist/cjs/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"require": {
|
|
14
|
+
"types": "./dist/cjs/index.d.ts",
|
|
15
|
+
"default": "./dist/cjs/index.js"
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"README.md",
|
|
22
|
+
"LICENSE",
|
|
23
|
+
"NOTICE"
|
|
24
|
+
],
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@totemsdk/core": "^1.6.1",
|
|
27
|
+
"@totemsdk/omnia-lsp": "^0.2.0"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/node": "^20.0.0",
|
|
31
|
+
"typescript": "^7.0.2"
|
|
32
|
+
},
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">=18.0.0"
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"license": "Apache-2.0",
|
|
40
|
+
"author": "Totem SDK",
|
|
41
|
+
"homepage": "https://docs.totem.ing",
|
|
42
|
+
"bugs": {
|
|
43
|
+
"url": "https://github.com/Totem-Edge/totem-sdk/issues"
|
|
44
|
+
},
|
|
45
|
+
"repository": {
|
|
46
|
+
"type": "git",
|
|
47
|
+
"url": "git+https://github.com/Totem-Edge/totem-sdk.git",
|
|
48
|
+
"directory": "packages/omnia-lsp-provider"
|
|
49
|
+
},
|
|
50
|
+
"keywords": [
|
|
51
|
+
"totem",
|
|
52
|
+
"totemsdk",
|
|
53
|
+
"minima",
|
|
54
|
+
"omnia",
|
|
55
|
+
"lsp",
|
|
56
|
+
"liquidity",
|
|
57
|
+
"provider",
|
|
58
|
+
"channel",
|
|
59
|
+
"onboarding"
|
|
60
|
+
],
|
|
61
|
+
"type": "module",
|
|
62
|
+
"scripts": {
|
|
63
|
+
"build": "tsc && tsc -p tsconfig.cjs.json && node -e \"require('fs').writeFileSync('dist/cjs/package.json', JSON.stringify({type:'commonjs'})+'\\n')\"",
|
|
64
|
+
"clean": "rm -rf dist",
|
|
65
|
+
"test": "jest --config ../../tests/jest/omnia-lsp-provider.cjs"
|
|
66
|
+
}
|
|
6
67
|
}
|