-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtypes.ts
127 lines (87 loc) · 2.94 KB
/
types.ts
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
import { SupportedEncodings } from "uint8arrays/util/bases"
import { Capability, isCapability, isEncodedCapability } from "./capability"
import * as util from "./util"
// 💎
export type Ucan<Prf = string> = {
header: UcanHeader
payload: UcanPayload<Prf>
signature: string
}
// CHUNKS
export interface UcanParts<Prf = string> {
header: UcanHeader
payload: UcanPayload<Prf>
}
export type UcanHeader = {
alg: string
typ: string
ucv: string
}
export type UcanPayload<Prf = string> = {
iss: string
aud: string
exp: number
nbf?: number
nnc?: string
att: Array<Capability>
fct?: Array<Fact>
prf: Array<Prf>
}
// FRAGMENTS
export type Fact = Record<string, unknown>
// CRYPTOGRAPHY
/** Unlike tslib's CryptoKeyPair, this requires the `privateKey` and `publicKey` fields */
export interface AvailableCryptoKeyPair {
privateKey: CryptoKey
publicKey: CryptoKey
}
export interface Didable {
publicKeyStr: (format?: Encodings) => string
did: () => string
}
export interface ExportableKey {
export: (format?: Encodings) => Promise<string>
}
export interface Keypair {
publicKey: Uint8Array
keyType: KeyType
sign: (msg: Uint8Array) => Promise<Uint8Array>
}
export type KeyType =
| "rsa"
| "p256"
| "p384"
| "p521"
| "ed25519"
| "bls12-381"
// https://developer.mozilla.org/en-US/docs/Web/API/EcKeyGenParams
export type NamedCurve = "P-256" | "P-384" | "P-521"
// MISC
export type Encodings = SupportedEncodings
// TYPE CHECKS
export function isAvailableCryptoKeyPair(keypair: CryptoKeyPair): keypair is AvailableCryptoKeyPair {
return keypair.publicKey != null && keypair.privateKey != null
}
export function isKeypair(obj: unknown): obj is Keypair {
return util.isRecord(obj)
&& util.hasProp(obj, "publicKey") && obj.publicKey instanceof Uint8Array
&& util.hasProp(obj, "keyType") && typeof obj.keyType === "string"
&& util.hasProp(obj, "sign") && typeof obj.sign === "function"
}
export function isUcanHeader(obj: unknown): obj is UcanHeader {
return util.isRecord(obj)
&& util.hasProp(obj, "alg") && typeof obj.alg === "string"
&& util.hasProp(obj, "typ") && typeof obj.typ === "string"
&& util.hasProp(obj, "ucv") && typeof obj.ucv === "string"
}
export function isUcanPayload(obj: unknown): obj is UcanPayload {
return util.isRecord(obj)
&& util.hasProp(obj, "iss") && typeof obj.iss === "string"
&& util.hasProp(obj, "aud") && typeof obj.aud === "string"
&& util.hasProp(obj, "exp") && typeof obj.exp === "number"
&& (!util.hasProp(obj, "nbf") || typeof obj.nbf === "number")
&& (!util.hasProp(obj, "nnc") || typeof obj.nnc === "string")
&& util.hasProp(obj, "att") && Array.isArray(obj.att) && obj.att.every(a => isCapability(a) || isEncodedCapability(a))
&& (!util.hasProp(obj, "fct") || Array.isArray(obj.fct) && obj.fct.every(util.isRecord))
&& util.hasProp(obj, "prf") && Array.isArray(obj.prf) && obj.prf.every(str => typeof str === "string")
}