-
Notifications
You must be signed in to change notification settings - Fork 11
/
IndyDidRegistry.ts
89 lines (76 loc) · 2.84 KB
/
IndyDidRegistry.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
/**
* Copyright (c) 2024 DSR Corporation, Denver, Colorado.
* https://www.dsr-corporation.com
* SPDX-License-Identifier: Apache-2.0
*/
import { concat, getBytes, Signature, toUtf8Bytes, toUtf8String } from 'ethers'
import { DidMetadataStruct } from '../typechain-types/contracts/did/IndyDidRegistry'
import { Contract } from '../utils/contract'
export type DidRecord = {
document: string
metadata: DidMetadataStruct
}
export class IndyDidRegistry extends Contract {
constructor(sender?: any) {
super(IndyDidRegistry.name, sender)
}
public async createDid(identity: string, document: string) {
const tx = await this.instance.createDid(identity, toUtf8Bytes(document))
return tx.wait()
}
public async createDidSigned(identity: string, document: string, signature: Signature) {
const tx = await this.instance.createDidSigned(
identity,
signature.v,
signature.r,
signature.s,
toUtf8Bytes(document),
)
return tx.wait()
}
public async updateDid(identity: string, document: string) {
const tx = await this.instance.updateDid(identity, toUtf8Bytes(document))
return tx.wait()
}
public async updateDidSigned(identity: string, document: string, signature: Signature) {
const tx = await this.instance.updateDidSigned(
identity,
signature.v,
signature.r,
signature.s,
toUtf8Bytes(document),
)
return tx.wait()
}
public async deactivateDid(identity: string) {
const tx = await this.instance.deactivateDid(identity)
return tx.wait()
}
public async deactivateDidSigned(identity: string, signature: Signature) {
const tx = await this.instance.deactivateDidSigned(identity, signature.v, signature.r, signature.s)
return tx.wait()
}
public async resolveDid(identity: string): Promise<DidRecord> {
const record = await this.instance.resolveDid(identity)
return {
document: toUtf8String(getBytes(record.document)),
metadata: {
owner: record.metadata.owner,
sender: record.metadata.sender,
created: record.metadata.created,
updated: record.metadata.updated,
versionId: record.metadata.versionId,
deactivated: record.metadata.deactivated,
},
}
}
public signCreateDidEndorsementData(identity: string, privateKey: Uint8Array, document: string) {
return this.signEndorsementData(privateKey, concat([identity, toUtf8Bytes('createDid'), toUtf8Bytes(document)]))
}
public signUpdateDidEndorsementData(identity: string, privateKey: Uint8Array, document: string) {
return this.signEndorsementData(privateKey, concat([identity, toUtf8Bytes('updateDid'), toUtf8Bytes(document)]))
}
public signDeactivateDidEndorsementData(identity: string, privateKey: Uint8Array) {
return this.signEndorsementData(privateKey, concat([identity, toUtf8Bytes('deactivateDid')]))
}
}