/
ceramic-signer.ts
119 lines (105 loc) · 2.98 KB
/
ceramic-signer.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
import {
CreateJWSOptions,
DagJWS,
DagJWSResult,
DID,
VerifyJWSOptions,
VerifyJWSResult,
} from 'dids'
export {
CreateJWSOptions,
DagJWS,
DagJWSResult,
DIDResolutionResult,
VerifyJWSOptions,
VerifyJWSResult,
} from 'dids'
/**
* Interface for an object that can sign ceramic commits
*/
export interface UnderlyingCeramicSigner {
ensureAuthenticated(): Promise<void>
createJWS<T extends string | Record<string, any>>(
payload: T,
options?: CreateJWSOptions
): Promise<DagJWS>
createDagJWS(payload: Record<string, any>, options?: CreateJWSOptions): Promise<DagJWSResult>
verifyJWS(jws: string | DagJWS, options?: VerifyJWSOptions): Promise<VerifyJWSResult>
asController(): Promise<string>
}
export interface IntoSigner {
signer: CeramicSigner
}
export class CeramicSigner implements IntoSigner {
private isAuthenticated: boolean
private reqs?: UnderlyingCeramicSigner
constructor(reqs?: UnderlyingCeramicSigner) {
this.isAuthenticated = false
this.reqs = reqs
}
get signer(): CeramicSigner {
return this
}
static invalid(): CeramicSigner {
return new CeramicSigner()
}
static fromDID(did: DID): CeramicSigner {
const signer = new CeramicSigner()
signer.withDid(did)
return signer
}
public withDid(did: DID) {
this.reqs = {
createDagJWS: (payload, options) => did.createDagJWS(payload, options),
createJWS: (payload, options) => did.createJWS(payload, options),
verifyJWS: (payload, options) => did.verifyJWS(payload, options),
async ensureAuthenticated(): Promise<void> {
if (!did.authenticated) {
await did.authenticate()
}
},
async asController(): Promise<string> {
return did.hasParent ? did.parent : did.id
},
}
}
private assertRequirements(): Promise<void> {
if (!this.reqs) {
return Promise.reject('Requirements not met for signing. Was a DID set?')
}
}
async createJWS<T extends string | Record<string, any>>(
payload: T,
options?: CreateJWSOptions
): Promise<DagJWS> {
await this.assertRequirements()
if (!this.isAuthenticated) {
await this.reqs.ensureAuthenticated()
this.isAuthenticated = true
}
return this.reqs.createJWS(payload, options)
}
async createDagJWS(
payload: Record<string, any>,
options?: CreateJWSOptions
): Promise<DagJWSResult> {
await this.assertRequirements()
if (!this.isAuthenticated) {
await this.reqs.ensureAuthenticated()
this.isAuthenticated = true
}
return this.reqs.createDagJWS(payload, options)
}
async asController(): Promise<string> {
await this.assertRequirements()
if (!this.isAuthenticated) {
await this.reqs.ensureAuthenticated()
this.isAuthenticated = true
}
return this.reqs.asController()
}
async verifyJWS(jws: string | DagJWS, options?: VerifyJWSOptions): Promise<VerifyJWSResult> {
await this.assertRequirements()
return this.reqs.verifyJWS(jws, options)
}
}