2020117-agent 0.2.2 → 0.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/dist/agent.js +16 -17
- package/dist/api.d.ts +10 -0
- package/dist/api.js +7 -0
- package/dist/clink.d.ts +5 -0
- package/dist/clink.js +20 -0
- package/package.json +1 -1
package/dist/agent.js
CHANGED
|
@@ -696,36 +696,35 @@ async function startSwarmListener(label) {
|
|
|
696
696
|
}
|
|
697
697
|
const job = {
|
|
698
698
|
socket,
|
|
699
|
-
credit:
|
|
699
|
+
credit: CHUNKS_PER_PAYMENT, // start with free credit; debit after delivery
|
|
700
700
|
ndebit: msg.ndebit,
|
|
701
701
|
totalEarned: 0,
|
|
702
702
|
stopped: false,
|
|
703
703
|
};
|
|
704
704
|
p2pJobs.set(msg.id, job);
|
|
705
|
-
// Send offer
|
|
705
|
+
// Send offer (price quote — no payment yet)
|
|
706
706
|
node.send(socket, {
|
|
707
707
|
type: 'offer',
|
|
708
708
|
id: msg.id,
|
|
709
709
|
sats_per_chunk: SATS_PER_CHUNK,
|
|
710
710
|
chunks_per_payment: CHUNKS_PER_PAYMENT,
|
|
711
711
|
});
|
|
712
|
-
//
|
|
713
|
-
const paid = await collectP2PPayment({
|
|
714
|
-
job, node, jobId: msg.id,
|
|
715
|
-
satsPerChunk: SATS_PER_CHUNK,
|
|
716
|
-
chunksPerPayment: CHUNKS_PER_PAYMENT,
|
|
717
|
-
lightningAddress: LIGHTNING_ADDRESS,
|
|
718
|
-
label,
|
|
719
|
-
});
|
|
720
|
-
if (!paid) {
|
|
721
|
-
console.log(`[${label}] P2P job ${msg.id}: first debit failed, aborting`);
|
|
722
|
-
p2pJobs.delete(msg.id);
|
|
723
|
-
releaseSlot();
|
|
724
|
-
return;
|
|
725
|
-
}
|
|
726
|
-
// Start generating
|
|
712
|
+
// Generate first, pay after delivery
|
|
727
713
|
node.send(socket, { type: 'accepted', id: msg.id });
|
|
728
714
|
await runP2PGeneration(node, job, msg, label);
|
|
715
|
+
// Debit after result delivered
|
|
716
|
+
if (!job.stopped) {
|
|
717
|
+
const paid = await collectP2PPayment({
|
|
718
|
+
job, node, jobId: msg.id,
|
|
719
|
+
satsPerChunk: SATS_PER_CHUNK,
|
|
720
|
+
chunksPerPayment: CHUNKS_PER_PAYMENT,
|
|
721
|
+
lightningAddress: LIGHTNING_ADDRESS,
|
|
722
|
+
label,
|
|
723
|
+
});
|
|
724
|
+
if (!paid) {
|
|
725
|
+
console.log(`[${label}] P2P job ${msg.id}: post-delivery debit failed`);
|
|
726
|
+
}
|
|
727
|
+
}
|
|
729
728
|
}
|
|
730
729
|
if (msg.type === 'stop') {
|
|
731
730
|
const job = p2pJobs.get(msg.id);
|
package/dist/api.d.ts
CHANGED
|
@@ -90,3 +90,13 @@ export declare function updateProfile(fields: {
|
|
|
90
90
|
about?: string;
|
|
91
91
|
lightning_address?: string;
|
|
92
92
|
}): Promise<boolean>;
|
|
93
|
+
export interface ProxyDebitResult {
|
|
94
|
+
ok: boolean;
|
|
95
|
+
preimage?: string;
|
|
96
|
+
error?: string;
|
|
97
|
+
}
|
|
98
|
+
export declare function proxyDebit(opts: {
|
|
99
|
+
ndebit: string;
|
|
100
|
+
lightningAddress: string;
|
|
101
|
+
amountSats: number;
|
|
102
|
+
}): Promise<ProxyDebitResult | null>;
|
package/dist/api.js
CHANGED
|
@@ -275,3 +275,10 @@ export async function updateProfile(fields) {
|
|
|
275
275
|
const result = await apiPut('/api/me', fields);
|
|
276
276
|
return result !== null;
|
|
277
277
|
}
|
|
278
|
+
export async function proxyDebit(opts) {
|
|
279
|
+
return apiPost('/api/dvm/proxy-debit', {
|
|
280
|
+
ndebit: opts.ndebit,
|
|
281
|
+
lightning_address: opts.lightningAddress,
|
|
282
|
+
amount_sats: opts.amountSats,
|
|
283
|
+
});
|
|
284
|
+
}
|
package/dist/clink.d.ts
CHANGED
|
@@ -33,6 +33,11 @@ export declare function generateInvoice(lightningAddress: string, amountSats: nu
|
|
|
33
33
|
/**
|
|
34
34
|
* Full payment cycle: generate invoice from provider's Lightning Address,
|
|
35
35
|
* then debit customer's wallet via CLINK.
|
|
36
|
+
*
|
|
37
|
+
* Priority: platform proxy (if API key available) → direct debit (if agent key initialized).
|
|
38
|
+
* Proxy uses the platform's pre-authorized CLINK identity, so providers don't need
|
|
39
|
+
* individual DebitAccess on customer wallets. Direct debit works for power users
|
|
40
|
+
* who pre-authorize each other's keys.
|
|
36
41
|
*/
|
|
37
42
|
export declare function collectPayment(opts: {
|
|
38
43
|
ndebit: string;
|
package/dist/clink.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* Invoice generation via LNURL-pay from provider's own Lightning Address.
|
|
6
6
|
*/
|
|
7
7
|
import { ClinkSDK, decodeBech32, generateSecretKey, getPublicKey, newNdebitPaymentRequest } from '@shocknet/clink-sdk';
|
|
8
|
+
import { hasApiKey, proxyDebit } from './api.js';
|
|
8
9
|
// --- Agent identity ---
|
|
9
10
|
let agentKey = null;
|
|
10
11
|
let agentPubkey = null;
|
|
@@ -76,8 +77,27 @@ export async function generateInvoice(lightningAddress, amountSats) {
|
|
|
76
77
|
/**
|
|
77
78
|
* Full payment cycle: generate invoice from provider's Lightning Address,
|
|
78
79
|
* then debit customer's wallet via CLINK.
|
|
80
|
+
*
|
|
81
|
+
* Priority: platform proxy (if API key available) → direct debit (if agent key initialized).
|
|
82
|
+
* Proxy uses the platform's pre-authorized CLINK identity, so providers don't need
|
|
83
|
+
* individual DebitAccess on customer wallets. Direct debit works for power users
|
|
84
|
+
* who pre-authorize each other's keys.
|
|
79
85
|
*/
|
|
80
86
|
export async function collectPayment(opts) {
|
|
87
|
+
// Try platform proxy first (provider doesn't need DebitAccess)
|
|
88
|
+
if (hasApiKey()) {
|
|
89
|
+
console.log(`[clink] Proxy debit: ${opts.amountSats} sats → ${opts.lightningAddress}`);
|
|
90
|
+
const result = await proxyDebit({
|
|
91
|
+
ndebit: opts.ndebit,
|
|
92
|
+
lightningAddress: opts.lightningAddress,
|
|
93
|
+
amountSats: opts.amountSats,
|
|
94
|
+
});
|
|
95
|
+
if (result) {
|
|
96
|
+
return { ok: result.ok, preimage: result.preimage, error: result.error };
|
|
97
|
+
}
|
|
98
|
+
console.warn('[clink] Proxy debit unavailable, trying direct...');
|
|
99
|
+
}
|
|
100
|
+
// Fallback: direct debit (requires provider's CLINK key to be authorized)
|
|
81
101
|
const bolt11 = await generateInvoice(opts.lightningAddress, opts.amountSats);
|
|
82
102
|
return debitCustomer({
|
|
83
103
|
ndebit: opts.ndebit,
|