forked from storacha/w3cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coupon.js
55 lines (48 loc) · 1.35 KB
/
coupon.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
import fs from 'node:fs/promises'
import * as DID from '@ipld/dag-ucan/did'
import * as Account from './account.js'
import * as Space from './space.js'
import { getClient } from './lib.js'
import * as ucanto from '@ucanto/core'
export { Account, Space }
/**
* @typedef {object} CouponIssueOptions
* @property {string} customer
* @property {string[]|string} [can]
* @property {string} [password]
* @property {number} [expiration]
* @property {string} [output]
*
* @param {string} customer
* @param {CouponIssueOptions} options
*/
export const issue = async (
customer,
{ can = 'provider/add', expiration, password, output }
) => {
const client = await getClient()
const audience = DID.parse(customer)
const abilities = can ? [can].flat() : []
if (!abilities.length) {
console.error('Error: missing capabilities for delegation')
process.exit(1)
}
const capabilities = /** @type {ucanto.API.Capabilities} */ (
abilities.map((can) => ({ can, with: audience.did() }))
)
const coupon = await client.coupon.issue({
capabilities,
expiration: expiration === 0 ? Infinity : expiration,
password,
})
const { ok: bytes, error } = await coupon.archive()
if (!bytes) {
console.error(error)
return process.exit(1)
}
if (output) {
await fs.writeFile(output, bytes)
} else {
process.stdout.write(bytes)
}
}