-
Notifications
You must be signed in to change notification settings - Fork 17
/
CredentialRegistrationId.ts
155 lines (143 loc) · 5.06 KB
/
CredentialRegistrationId.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
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
import { HexString } from '../types.js';
import { isHex } from '../util.js';
import { Buffer } from 'buffer/index.js';
import {
TypedJson,
TypedJsonDiscriminator,
makeFromTypedJson,
} from './util.js';
/**
* The {@linkcode TypedJsonDiscriminator} discriminator associated with {@linkcode Type} type.
* @deprecated
*/
export const JSON_DISCRIMINATOR =
TypedJsonDiscriminator.CredentialRegistrationId;
/**
* @deprecated
*/
export type Serializable = string;
/**
* Representation of a credential registration id, which enforces that it:
* - Is a valid Hex string
* - Has length exactly 96, because a credId is 48 bytes.
* - Checks the first bit is 1, which indicates that the value represents a compressed BLS12-381 curve point.
*/
class CredentialRegistrationId {
/** Having a private field prevents similar structured objects to be considered the same type (similar to nominal typing). */
private __type = JSON_DISCRIMINATOR;
constructor(
/** Representation of a credential registration id */
public readonly credId: string
) {}
/**
* Get a string representation of the credential registration id.
* @returns {string} The string representation.
*/
public toString(): string {
return this.credId;
}
/**
* Get a JSON-serializable representation of the credential registration id.
* @returns {string} The JSON-serializable representation.
*/
public toJSON(): string {
return this.credId;
}
}
/**
* Converts a `string` to a credential registration id.
* @param {string} json The JSON representation of the credential registration id.
* @throws If the provided input is: not a valid hex string, not of exactly 96 characters, the first bit is not 1.
* @returns {CredentialRegistrationId} The credential registration id.
*/
export function fromJSON(json: string): CredentialRegistrationId {
return fromHexString(json);
}
/**
* Representation of a credential registration id, which enforces that it:
* - Is a valid Hex string
* - Has length exactly 96, because a credId is 48 bytes.
* - Checks the first bit is 1, which indicates that the value represents a compressed BLS12-381 curve point.
*/
export type Type = CredentialRegistrationId;
/**
* Type predicate for {@linkcode Type}
*
* @param value value to check.
* @returns whether `value` is of type {@linkcode Type}
*/
export function instanceOf(value: unknown): value is CredentialRegistrationId {
return value instanceof CredentialRegistrationId;
}
/**
* Construct a CredentialRegistrationId from a hex string.
* @param {HexString} credId The hex encoding of the credential registration id.
* @throws If the provided input is: not a valid hex string, not of exactly 96 characters, the first bit is not 1.
* @returns {CredentialRegistrationId}
*/
export function fromHexString(credId: HexString): CredentialRegistrationId {
if (credId.length !== 96) {
throw new Error(
'The provided credId ' +
credId +
' is invalid as its length was not 96'
);
}
if (!isHex(credId)) {
throw new Error(
'The provided credId ' +
credId +
' does not represent a hexidecimal value'
);
}
// Check that the first bit is 1
if ((parseInt(credId.substring(0, 2), 16) & 0b10000000) === 0) {
throw new Error(
'The provided credId ' +
credId +
'does not represent a compressed BLS12-381 point'
);
}
return new CredentialRegistrationId(credId);
}
/**
* Get the hex string representation of the credential registatration ID.
* @param {CredentialRegistrationId} cred The credential registration ID.
* @returns {HexString} The hex encoding.
*/
export function toHexString(cred: CredentialRegistrationId): HexString {
return cred.credId;
}
/**
* Get the byte representation of the credential registatration ID.
* @param {CredentialRegistrationId} cred The credential registration ID.
* @returns {Uint8Array} Buffer with byte representation.
*/
export function toBuffer(cred: CredentialRegistrationId): Uint8Array {
return Buffer.from(cred.credId, 'hex');
}
/**
* Takes an {@linkcode Type} and transforms it to a {@linkcode TypedJson} format.
* @deprecated Use the {@linkcode CredentialRegistrationId.toJSON} method instead.
* @param {Type} value - The account address instance to transform.
* @returns {TypedJson} The transformed object.
*/
export function toTypedJSON(
value: CredentialRegistrationId
): TypedJson<Serializable> {
return {
['@type']: JSON_DISCRIMINATOR,
value: toHexString(value),
};
}
/**
* Takes a {@linkcode TypedJson} object and converts it to instance of type {@linkcode Type}.
* @deprecated Use the {@linkcode fromJSON} function instead.
* @param {TypedJson} json - The typed JSON to convert.
* @throws {TypedJsonParseError} - If unexpected JSON string is passed.
* @returns {Type} The parsed instance.
*/
export const fromTypedJSON = /*#__PURE__*/ makeFromTypedJson(
JSON_DISCRIMINATOR,
fromHexString
);