-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdealer.js
111 lines (104 loc) · 3.49 KB
/
dealer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { connect } from '@ucanto/client'
import { CAR, HTTP } from '@ucanto/transport'
import { CBOR } from '@ucanto/core'
import * as Dealer from '@web3-storage/capabilities/filecoin/dealer'
import { services } from './service.js'
/**
* @typedef {import('./types.js').DealerService} DealerService
* @typedef {import('@ucanto/interface').ConnectionView<DealerService>} ConnectionView
*/
/** @type {ConnectionView} */
export const connection = connect({
id: services.DEALER.principal,
codec: CAR.outbound,
channel: HTTP.open({
url: services.DEALER.url,
method: 'POST',
}),
})
/**
* The `aggregate/offer` task can be executed to request an aggregate be added
* to a deal with a Storage Provider. It issues a signed receipt of the
* execution result. It is _also_ an effect linked from successful execution of
* a `piece/accept` task.
*
* A receipt for successful execution will contain an effect, linking to an
* `aggregate/accept` task that will complete asynchronously.
*
* Otherwise the task is failed and the receipt will contain details of the
* reason behind the failure.
*
* @see https://github.com/storacha/specs/blob/main/w3-filecoin.md#aggregateoffer
*
* @param {import('./types.js').InvocationConfig} conf - Configuration
* @param {import('@web3-storage/data-segment').PieceLink} aggregate
* @param {import('@web3-storage/data-segment').PieceLink[]} pieces
* @param {import('./types.js').RequestOptions<DealerService>} [options]
*/
export async function aggregateOffer(
{ issuer, with: resource, proofs, audience },
aggregate,
pieces,
options = {}
) {
/* c8 ignore next */
const conn = options.connection ?? connection
const block = await CBOR.write(pieces)
const invocation = Dealer.aggregateOffer.invoke({
issuer,
/* c8 ignore next */
audience: audience ?? services.AGGREGATOR.principal,
with: resource,
nb: {
aggregate,
pieces: block.cid,
},
proofs,
expiration: Infinity,
})
invocation.attach(block)
return await invocation.execute(conn)
}
/**
* The `aggregate/accept` task is an _effect_ linked from successful execution
* of a `aggregate/offer` task, it is executed to issue a receipt for the
* success or failure of the task.
*
* A receipt for successful execution indicates that an aggregate has been
* accepted for inclusion in a Filecoin deal. In this case the receipt will
* contain proofs that the piece was included in an aggregate and deal.
*
* Otherwise the task is failed and the receipt will contain details of the
* reason behind the failure, as well as multiple effects, linking to
* `piece/offer` tasks that will retry _valid_ pieces and complete
* asynchronously.
*
* @see https://github.com/storacha/specs/blob/main/w3-filecoin.md#aggregateaccept
*
* @param {import('./types.js').InvocationConfig} conf - Configuration
* @param {import('@web3-storage/data-segment').PieceLink} aggregate
* @param {import('@ucanto/interface').Link} pieces
* @param {import('./types.js').RequestOptions<DealerService>} [options]
*/
export async function aggregateAccept(
{ issuer, with: resource, proofs, audience },
aggregate,
pieces,
options = {}
) {
/* c8 ignore next */
const conn = options.connection ?? connection
const invocation = Dealer.aggregateAccept.invoke({
issuer,
/* c8 ignore next */
audience: audience ?? services.AGGREGATOR.principal,
with: resource,
nb: {
aggregate,
pieces,
},
proofs,
expiration: Infinity,
})
return await invocation.execute(conn)
}