-
Notifications
You must be signed in to change notification settings - Fork 4
/
lib.js
214 lines (192 loc) · 6.05 KB
/
lib.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import * as UCAN from "./ucan.js"
import * as CBOR from "./codec/cbor.js"
import * as RAW from "./codec/raw.js"
import * as UTF8 from "./utf8.js"
import * as View from "./view.js"
import * as Parser from "./parser.js"
import * as Formatter from "./formatter.js"
import { sha256 } from "multiformats/hashes/sha2"
import { CID } from "multiformats/cid"
import { format as formatDID } from "./did.js"
export * from "./ucan.js"
/** @type {UCAN.Version} */
export const VERSION = "0.8.1"
export const name = "dag-ucan"
/** @type {typeof CBOR.code|typeof RAW.code} */
export const code = CBOR.code
/**
* Encodes given UCAN (in either IPLD or JWT representation) and encodes it into
* corresponding bytes representation. UCAN in IPLD representation is encoded as
* DAG-CBOR which JWT representation is encoded as raw bytes of JWT string.
*
* @template {UCAN.Capability} C
* @param {UCAN.UCAN<C>} ucan
* @returns {UCAN.ByteView<UCAN.UCAN<C>>}
*/
export const encode = ucan =>
ucan instanceof Uint8Array ? RAW.encode(ucan) : CBOR.encode(ucan)
/**
* Decodes binary encoded UCAN. It assumes UCAN is in primary IPLD
* representation and attempts to decode it with DAG-CBOR, if that
* fails it falls back to secondary representation and parses it as
* a JWT.
*
* @template {UCAN.Capability} C
* @param {UCAN.ByteView<UCAN.UCAN<C>>} bytes
* @returns {UCAN.View<C>}
*/
export const decode = bytes => {
try {
return CBOR.decode(/** @type {UCAN.ByteView<UCAN.Model<C>>} */ (bytes))
} catch (error) {
const jwt = UTF8.decode(/** @type {UCAN.RAW<C>} */ (bytes))
return parse(jwt)
}
}
/**
* Convenience function to create a CID for the given UCAN. If UCAN is
* in JWT represetation get CID with RAW multicodec, while UCANs in IPLD
* representation get UCAN multicodec code.
*
* @template {UCAN.Capability} C
* @param {UCAN.UCAN<C>} ucan
* @param {{hasher?: UCAN.MultihashHasher}} [options]
*/
export const link = async (ucan, options) => {
const { cid } = await write(ucan, options)
return cid
}
/**
* @template {UCAN.Capability} C
* @template {number} [A=number]
* @param {UCAN.UCAN<C>} data
* @param {{hasher?: UCAN.MultihashHasher<A>}} [options]
* @returns {Promise<{cid:UCAN.Proof<C> & CID, bytes: UCAN.ByteView<UCAN.UCAN<C>>, data: UCAN.UCAN<C> }>}
*/
export const write = async (
data,
{ hasher = /** @type {UCAN.MultihashHasher<any> } */ (sha256) } = {}
) => {
const [code, bytes] =
data instanceof Uint8Array
? [RAW.code, RAW.encode(data)]
: [CBOR.code, CBOR.encode(data)]
const cid = /** @type {CID & UCAN.Proof<C, A>} */ (
CID.createV1(code, await hasher.digest(bytes))
)
return { cid, bytes, data }
}
/**
* Parses UCAN formatted as JWT string. Returns UCAN view in IPLD representation
* when serailazing it back would produce original string, oherwise returns UCAN
* view in secondary JWT representation which is not as compact, but it retains
* key order and whitespaces so it could be formatted back to same JWT string.
* View will have `type` field with either `"IPLD"` or `"JWT"` value telling
* in which representation UCAN is.
*
* Note: Parsing does not perform validation of capabilities or semantics of the
* UCAN, it only ensures structure is spec compliant and throws `ParseError`
* if it is not.
*
* @template {UCAN.Capability} C
* @param {UCAN.JWT<C>} jwt
* @returns {UCAN.View<C>}
*/
export const parse = jwt => {
const model = Parser.parse(jwt)
// If formatting UCAN produces same jwt string we can use IPLD representation
// otherwise we need to fallback to raw representation. This decision will
// affect how we `encode` the UCAN.
return Formatter.format(model) === jwt
? View.cbor(model)
: View.jwt(model, UTF8.encode(jwt))
}
/**
* Takes UCAN object and formats it into JWT string.
*
* @template {UCAN.Capability} C
* @param {UCAN.UCAN<C>} ucan
* @returns {UCAN.JWT<C>}
*/
export const format = ucan =>
ucan instanceof Uint8Array ? UTF8.decode(ucan) : Formatter.format(ucan)
/**
* Creates a new signed token with a given `options.issuer`. If expiration is
* not set it defaults to 30 seconds from now. Returns UCAN in primary - IPLD
* representation.
*
* @template {number} A
* @template {UCAN.Capability} C
* @param {UCAN.UCANOptions<C, A>} options
* @returns {Promise<UCAN.View<C>>}
*/
export const issue = async ({
issuer,
audience,
capabilities,
lifetimeInSeconds = 30,
expiration = Math.floor(Date.now() / 1000) + lifetimeInSeconds,
notBefore,
facts = [],
proofs = [],
nonce,
}) => {
const data = CBOR.match({
version: VERSION,
issuer: parseDID(issuer, "issuer"),
audience: parseDID(audience, "audience"),
capabilities,
facts,
expiration,
notBefore,
proofs,
nonce,
// Provide fake signature to pass validation
// we'll replace this with actual signature
signature: EMPTY,
})
const payload = UTF8.encode(Formatter.formatSignPayload(data))
const signature = await issuer.sign(payload)
return View.cbor({ ...data, signature })
}
/**
* Verifies UCAN signature.
*
* @template {UCAN.Capability} C
* @param {UCAN.Model<C>} ucan
* @param {UCAN.Authority} authority
*/
export const verifySignature = (ucan, authority) =>
formatDID(ucan.issuer) === authority.did() &&
authority.verify(
UTF8.encode(Formatter.formatSignPayload(ucan)),
ucan.signature
)
/**
* Check if a UCAN is expired.
*
* @param {UCAN.Model} ucan
*/
export const isExpired = ucan => ucan.expiration <= now()
/**
* Check if a UCAN is not active yet.
* @param {UCAN.Model} ucan
*/
export const isTooEarly = ucan =>
ucan.notBefore != null && now() <= ucan.notBefore
/**
* Returns UTC Unix timestamp for comparing it against time window of the UCAN.
*/
export const now = () => Math.floor(Date.now() / 1000)
/**
*
* @param {unknown & {did?:unknown}} value
* @param {string} context
*/
const parseDID = (value, context) =>
value && typeof value.did === "function"
? Parser.parseDID(value.did(), `${context}.did()`)
: Parser.ParseError.throw(
`The ${context}.did() must be a function that returns DID`
)
const EMPTY = new Uint8Array()