-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathservice.js
199 lines (182 loc) · 5.42 KB
/
service.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import * as Server from '@ucanto/server'
import * as Client from '@ucanto/client'
import * as CAR from '@ucanto/transport/car'
import { CBOR } from '@ucanto/core'
import { sha256 } from 'multiformats/hashes/sha2'
import * as Block from 'multiformats/block'
import * as DealerCaps from '@web3-storage/capabilities/filecoin/dealer'
import { DealTracker } from '@web3-storage/filecoin-client'
// eslint-disable-next-line no-unused-vars
import * as API from '../types.js'
import { StoreOperationFailed, DecodeBlockOperationFailed } from '../errors.js'
/**
* @param {API.Input<DealerCaps.aggregateOffer>} input
* @param {import('./api.js').ServiceContext} context
* @returns {Promise<API.UcantoInterface.Result<API.AggregateOfferSuccess, API.AggregateOfferFailure> | API.UcantoInterface.JoinBuilder<API.AggregateOfferSuccess>>}
*/
export const aggregateOffer = async ({ capability, invocation }, context) => {
const issuer = invocation.issuer.did()
const { aggregate, pieces } = capability.nb
const hasRes = await context.aggregateStore.has({ aggregate })
if (hasRes.error) {
return {
error: new StoreOperationFailed(hasRes.error.message),
}
}
if (!hasRes.ok) {
const piecesBlockRes = await findCBORBlock(
pieces,
invocation.iterateIPLDBlocks()
)
if (piecesBlockRes.error) {
return piecesBlockRes
}
// Write Spade formatted doc to offerStore before putting aggregate for tracking
const putOfferRes = await context.offerStore.put({
key: piecesBlockRes.ok.cid.toString(),
value: {
issuer,
aggregate,
pieces: piecesBlockRes.ok.value,
},
})
if (putOfferRes.error) {
return {
error: new StoreOperationFailed(putOfferRes.error.message),
}
}
// Put aggregate offered into store
const putRes = await context.aggregateStore.put({
aggregate,
pieces,
status: 'offered',
insertedAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
})
if (putRes.error) {
return {
error: new StoreOperationFailed(putRes.error.message),
}
}
}
// Effect
const fx = await DealerCaps.aggregateAccept
.invoke({
issuer: context.id,
audience: context.id,
with: context.id.did(),
nb: {
aggregate,
pieces,
},
expiration: Infinity,
})
.delegate()
/** @type {API.UcantoInterface.OkBuilder<API.AggregateOfferSuccess, API.AggregateOfferFailure>} */
const result = Server.ok({ aggregate })
return result.join(fx.link())
}
/**
* @param {API.Input<DealerCaps.aggregateAccept>} input
* @param {import('./api.js').ServiceContext} context
* @returns {Promise<API.UcantoInterface.Result<API.AggregateAcceptSuccess, API.AggregateAcceptFailure>>}
*/
export const aggregateAccept = async ({ capability }, context) => {
const { aggregate } = capability.nb
// Query current state
const info = await DealTracker.dealInfo(
context.dealTrackerService.invocationConfig,
aggregate,
{ connection: context.dealTrackerService.connection }
)
if (info.out.error) {
return {
error: info.out.error,
}
}
// If there are no deals for it, we can skip
const deals = Object.keys(info.out.ok.deals || {})
if (!deals.length) {
return {
error: new Server.Failure(
'no deals were obtained for given aggregate CID'
),
}
}
// For receipts, we only care about first deal
// TODO: We need to revisit this with renewals
const deal = {
dataType: 0n,
dataSource: {
dealID: BigInt(deals[0]),
},
}
return {
ok: {
aggregate,
dataSource: deal.dataSource,
dataType: deal.dataType,
},
}
}
/**
* @param {import('multiformats').Link} cid
* @param {IterableIterator<Server.API.Transport.Block<unknown, number, number, 1>>} blocks
* @returns {Promise<API.UcantoInterface.Result<import('multiformats').BlockView, DecodeBlockOperationFailed>>}
*/
const findCBORBlock = async (cid, blocks) => {
let bytes
for (const b of blocks) {
if (b.cid.equals(cid)) {
bytes = b.bytes
}
}
if (!bytes) {
return {
error: new DecodeBlockOperationFailed(`missing block: ${cid}`),
}
}
return {
ok: await Block.create({ cid, bytes, codec: CBOR, hasher: sha256 }),
}
}
/**
* @param {import('./api.js').ServiceContext} context
*/
export function createService(context) {
return {
aggregate: {
offer: Server.provideAdvanced({
capability: DealerCaps.aggregateOffer,
handler: (input) => aggregateOffer(input, context),
}),
accept: Server.provideAdvanced({
capability: DealerCaps.aggregateAccept,
handler: (input) => aggregateAccept(input, context),
}),
},
}
}
/**
* @param {API.UcantoServerContext & import('./api.js').ServiceContext} context
*/
export const createServer = (context) =>
Server.create({
id: context.id,
codec: context.codec || CAR.inbound,
service: createService(context),
catch: (error) => context.errorReporter.catch(error),
validateAuthorization: (auth) => context.validateAuthorization(auth),
})
/**
* @param {object} options
* @param {API.UcantoInterface.Principal} options.id
* @param {API.UcantoInterface.Transport.Channel<API.DealerService>} options.channel
* @param {API.UcantoInterface.OutboundCodec} [options.codec]
*/
export const connect = ({ id, channel, codec = CAR.outbound }) =>
Client.connect({
id,
channel,
codec,
})