-
Notifications
You must be signed in to change notification settings - Fork 11
/
SchemaRegistry.ts
73 lines (66 loc) · 1.86 KB
/
SchemaRegistry.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
/**
* Copyright (c) 2024 DSR Corporation, Denver, Colorado.
* https://www.dsr-corporation.com
* SPDX-License-Identifier: Apache-2.0
*/
import { concat, getBytes, keccak256, Signature, toUtf8Bytes, toUtf8String } from 'ethers'
import { SchemaMetadataStruct } from '../typechain-types/contracts/anoncreds/SchemaRegistry'
import { Contract } from '../utils/contract'
export type SchemaRecord = {
schema: string
metadata: SchemaMetadataStruct
}
export class SchemaRegistry extends Contract {
constructor(sender?: any) {
super(SchemaRegistry.name, sender)
}
public async createSchema(identity: string, id: string, issuerId: string, schema: string) {
const tx = await this.instance.createSchema(identity, keccak256(toUtf8Bytes(id)), issuerId, toUtf8Bytes(schema))
return tx.wait()
}
public async createSchemaSigned(
identity: string,
id: string,
issuerId: string,
schema: string,
signature: Signature,
) {
const tx = await this.instance.createSchemaSigned(
identity,
signature.v,
signature.r,
signature.s,
keccak256(toUtf8Bytes(id)),
issuerId,
toUtf8Bytes(schema),
)
return tx.wait()
}
public async resolveSchema(id: string): Promise<SchemaRecord> {
const record = await this.instance.resolveSchema(keccak256(toUtf8Bytes(id)))
return {
schema: toUtf8String(getBytes(record.schema)),
metadata: {
created: record.metadata.created,
},
}
}
public async signCreateSchemaEndorsementData(
identity: string,
privateKey: Uint8Array,
id: string,
issuerId: string,
schema: string,
) {
return this.signEndorsementData(
privateKey,
concat([
identity,
toUtf8Bytes('createSchema'),
getBytes(keccak256(toUtf8Bytes(id)), 'hex'),
toUtf8Bytes(issuerId),
toUtf8Bytes(schema),
]),
)
}
}