@0xsequence/identity-instrument 0.0.0-20250520201059
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +5 -0
- package/CHANGELOG.md +7 -0
- package/LICENSE +202 -0
- package/dist/challenge.d.ts +69 -0
- package/dist/challenge.d.ts.map +1 -0
- package/dist/challenge.js +187 -0
- package/dist/identity-instrument.gen.d.ts +207 -0
- package/dist/identity-instrument.gen.d.ts.map +1 -0
- package/dist/identity-instrument.gen.js +289 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +47 -0
- package/package.json +31 -0
- package/src/challenge.ts +221 -0
- package/src/identity-instrument.gen.ts +517 -0
- package/src/index.ts +69 -0
- package/test/challenge.test.ts +194 -0
- package/tsconfig.json +10 -0
- package/vitest.config.ts +8 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"identity-instrument.gen.d.ts","sourceRoot":"","sources":["../src/identity-instrument.gen.ts"],"names":[],"mappings":"AAOA,eAAO,MAAM,YAAY,WAAW,CAAA;AAEpC,eAAO,MAAM,iBAAiB,qEAAqE,CAAA;AAGnG,eAAO,MAAM,aAAa,OAAO,CAAA;AAGjC,eAAO,MAAM,mBAAmB,WAAW,CAAA;AAG3C,eAAO,MAAM,gBAAgB,6CAA6C,CAAA;AAE1E,KAAK,iBAAiB,GAAG;IACvB,gBAAgB,EAAE,MAAM,CAAA;IACxB,WAAW,EAAE,MAAM,CAAA;IACnB,cAAc,EAAE,MAAM,CAAA;IACtB,UAAU,EAAE,MAAM,CAAA;IAClB,aAAa,EAAE,MAAM,CAAA;CACtB,CAAA;AAED,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,OAAO,GAAG,iBAAiB,CAarE;AA+BD,oBAAY,OAAO;IACjB,MAAM,WAAW;IACjB,MAAM,WAAW;CAClB;AAED,oBAAY,YAAY;IACtB,KAAK,UAAU;IACf,KAAK,UAAU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,iBAAiB,EAAE;IAAE,CAAC,IAAI,EAAE,MAAM,GAAG,GAAG,CAAA;CAYpD,CAAA;AAED,MAAM,MAAM,KAAK,GAAG,CAAC,KAAK,EAAE,WAAW,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAA"}
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
// identity-instrument v0.1.0 2338b17497d46b1813768be23a7338716a4b6d9f
|
|
3
|
+
// --
|
|
4
|
+
// Code generated by webrpc-gen@v0.23.1 with typescript generator. DO NOT EDIT.
|
|
5
|
+
//
|
|
6
|
+
// webrpc-gen -schema=identity-instrument.ridl -target=typescript -client -out=./clients/identity-instrument.gen.ts
|
|
7
|
+
export const WebrpcHeader = 'Webrpc';
|
|
8
|
+
export const WebrpcHeaderValue = 'webrpc@v0.23.1;gen-typescript@v0.16.3;identity-instrument@v0.1.0';
|
|
9
|
+
// WebRPC description and code-gen version
|
|
10
|
+
export const WebRPCVersion = 'v1';
|
|
11
|
+
// Schema version of your RIDL schema
|
|
12
|
+
export const WebRPCSchemaVersion = 'v0.1.0';
|
|
13
|
+
// Schema hash generated from your RIDL schema
|
|
14
|
+
export const WebRPCSchemaHash = '2338b17497d46b1813768be23a7338716a4b6d9f';
|
|
15
|
+
export function VersionFromHeader(headers) {
|
|
16
|
+
const headerValue = headers.get(WebrpcHeader);
|
|
17
|
+
if (!headerValue) {
|
|
18
|
+
return {
|
|
19
|
+
webrpcGenVersion: '',
|
|
20
|
+
codeGenName: '',
|
|
21
|
+
codeGenVersion: '',
|
|
22
|
+
schemaName: '',
|
|
23
|
+
schemaVersion: '',
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
return parseWebrpcGenVersions(headerValue);
|
|
27
|
+
}
|
|
28
|
+
function parseWebrpcGenVersions(header) {
|
|
29
|
+
const versions = header.split(';');
|
|
30
|
+
if (versions.length < 3) {
|
|
31
|
+
return {
|
|
32
|
+
webrpcGenVersion: '',
|
|
33
|
+
codeGenName: '',
|
|
34
|
+
codeGenVersion: '',
|
|
35
|
+
schemaName: '',
|
|
36
|
+
schemaVersion: '',
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
const [_, webrpcGenVersion] = versions[0].split('@');
|
|
40
|
+
const [codeGenName, codeGenVersion] = versions[1].split('@');
|
|
41
|
+
const [schemaName, schemaVersion] = versions[2].split('@');
|
|
42
|
+
return {
|
|
43
|
+
webrpcGenVersion: webrpcGenVersion ?? '',
|
|
44
|
+
codeGenName: codeGenName ?? '',
|
|
45
|
+
codeGenVersion: codeGenVersion ?? '',
|
|
46
|
+
schemaName: schemaName ?? '',
|
|
47
|
+
schemaVersion: schemaVersion ?? '',
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
//
|
|
51
|
+
// Types
|
|
52
|
+
//
|
|
53
|
+
export var KeyType;
|
|
54
|
+
(function (KeyType) {
|
|
55
|
+
KeyType["P256K1"] = "P256K1";
|
|
56
|
+
KeyType["P256R1"] = "P256R1";
|
|
57
|
+
})(KeyType || (KeyType = {}));
|
|
58
|
+
export var IdentityType;
|
|
59
|
+
(function (IdentityType) {
|
|
60
|
+
IdentityType["Guest"] = "Guest";
|
|
61
|
+
IdentityType["Email"] = "Email";
|
|
62
|
+
IdentityType["OIDC"] = "OIDC";
|
|
63
|
+
})(IdentityType || (IdentityType = {}));
|
|
64
|
+
export var AuthMode;
|
|
65
|
+
(function (AuthMode) {
|
|
66
|
+
AuthMode["Guest"] = "Guest";
|
|
67
|
+
AuthMode["OTP"] = "OTP";
|
|
68
|
+
AuthMode["IDToken"] = "IDToken";
|
|
69
|
+
AuthMode["AccessToken"] = "AccessToken";
|
|
70
|
+
AuthMode["AuthCode"] = "AuthCode";
|
|
71
|
+
AuthMode["AuthCodePKCE"] = "AuthCodePKCE";
|
|
72
|
+
})(AuthMode || (AuthMode = {}));
|
|
73
|
+
//
|
|
74
|
+
// Client
|
|
75
|
+
//
|
|
76
|
+
export class IdentityInstrument {
|
|
77
|
+
hostname;
|
|
78
|
+
fetch;
|
|
79
|
+
path = '/rpc/IdentityInstrument/';
|
|
80
|
+
constructor(hostname, fetch) {
|
|
81
|
+
this.hostname = hostname.replace(/\/*$/, '');
|
|
82
|
+
this.fetch = (input, init) => fetch(input, init);
|
|
83
|
+
}
|
|
84
|
+
url(name) {
|
|
85
|
+
return this.hostname + this.path + name;
|
|
86
|
+
}
|
|
87
|
+
commitVerifier = (args, headers, signal) => {
|
|
88
|
+
return this.fetch(this.url('CommitVerifier'), createHTTPRequest(args, headers, signal)).then((res) => {
|
|
89
|
+
return buildResponse(res).then((_data) => {
|
|
90
|
+
return {
|
|
91
|
+
verifier: _data.verifier,
|
|
92
|
+
loginHint: _data.loginHint,
|
|
93
|
+
challenge: _data.challenge,
|
|
94
|
+
};
|
|
95
|
+
});
|
|
96
|
+
}, (error) => {
|
|
97
|
+
throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` });
|
|
98
|
+
});
|
|
99
|
+
};
|
|
100
|
+
completeAuth = (args, headers, signal) => {
|
|
101
|
+
return this.fetch(this.url('CompleteAuth'), createHTTPRequest(args, headers, signal)).then((res) => {
|
|
102
|
+
return buildResponse(res).then((_data) => {
|
|
103
|
+
return {
|
|
104
|
+
signer: _data.signer,
|
|
105
|
+
};
|
|
106
|
+
});
|
|
107
|
+
}, (error) => {
|
|
108
|
+
throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` });
|
|
109
|
+
});
|
|
110
|
+
};
|
|
111
|
+
sign = (args, headers, signal) => {
|
|
112
|
+
return this.fetch(this.url('Sign'), createHTTPRequest(args, headers, signal)).then((res) => {
|
|
113
|
+
return buildResponse(res).then((_data) => {
|
|
114
|
+
return {
|
|
115
|
+
signature: _data.signature,
|
|
116
|
+
};
|
|
117
|
+
});
|
|
118
|
+
}, (error) => {
|
|
119
|
+
throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` });
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
const createHTTPRequest = (body = {}, headers = {}, signal = null) => {
|
|
124
|
+
const reqHeaders = { ...headers, 'Content-Type': 'application/json' };
|
|
125
|
+
reqHeaders[WebrpcHeader] = WebrpcHeaderValue;
|
|
126
|
+
return {
|
|
127
|
+
method: 'POST',
|
|
128
|
+
headers: reqHeaders,
|
|
129
|
+
body: JSON.stringify(body || {}),
|
|
130
|
+
signal,
|
|
131
|
+
};
|
|
132
|
+
};
|
|
133
|
+
const buildResponse = (res) => {
|
|
134
|
+
return res.text().then((text) => {
|
|
135
|
+
let data;
|
|
136
|
+
try {
|
|
137
|
+
data = JSON.parse(text);
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
let message = '';
|
|
141
|
+
if (error instanceof Error) {
|
|
142
|
+
message = error.message;
|
|
143
|
+
}
|
|
144
|
+
throw WebrpcBadResponseError.new({
|
|
145
|
+
status: res.status,
|
|
146
|
+
cause: `JSON.parse(): ${message}: response text: ${text}`,
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
if (!res.ok) {
|
|
150
|
+
const code = typeof data.code === 'number' ? data.code : 0;
|
|
151
|
+
throw (webrpcErrorByCode[code] || WebrpcError).new(data);
|
|
152
|
+
}
|
|
153
|
+
return data;
|
|
154
|
+
});
|
|
155
|
+
};
|
|
156
|
+
//
|
|
157
|
+
// Errors
|
|
158
|
+
//
|
|
159
|
+
export class WebrpcError extends Error {
|
|
160
|
+
name;
|
|
161
|
+
code;
|
|
162
|
+
message;
|
|
163
|
+
status;
|
|
164
|
+
cause;
|
|
165
|
+
/** @deprecated Use message instead of msg. Deprecated in webrpc v0.11.0. */
|
|
166
|
+
msg;
|
|
167
|
+
constructor(name, code, message, status, cause) {
|
|
168
|
+
super(message);
|
|
169
|
+
this.name = name || 'WebrpcError';
|
|
170
|
+
this.code = typeof code === 'number' ? code : 0;
|
|
171
|
+
this.message = message || `endpoint error ${this.code}`;
|
|
172
|
+
this.msg = this.message;
|
|
173
|
+
this.status = typeof status === 'number' ? status : 0;
|
|
174
|
+
this.cause = cause;
|
|
175
|
+
Object.setPrototypeOf(this, WebrpcError.prototype);
|
|
176
|
+
}
|
|
177
|
+
static new(payload) {
|
|
178
|
+
return new this(payload.error, payload.code, payload.message || payload.msg, payload.status, payload.cause);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
// Webrpc errors
|
|
182
|
+
export class WebrpcEndpointError extends WebrpcError {
|
|
183
|
+
constructor(name = 'WebrpcEndpoint', code = 0, message = `endpoint error`, status = 0, cause) {
|
|
184
|
+
super(name, code, message, status, cause);
|
|
185
|
+
Object.setPrototypeOf(this, WebrpcEndpointError.prototype);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
export class WebrpcRequestFailedError extends WebrpcError {
|
|
189
|
+
constructor(name = 'WebrpcRequestFailed', code = -1, message = `request failed`, status = 0, cause) {
|
|
190
|
+
super(name, code, message, status, cause);
|
|
191
|
+
Object.setPrototypeOf(this, WebrpcRequestFailedError.prototype);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
export class WebrpcBadRouteError extends WebrpcError {
|
|
195
|
+
constructor(name = 'WebrpcBadRoute', code = -2, message = `bad route`, status = 0, cause) {
|
|
196
|
+
super(name, code, message, status, cause);
|
|
197
|
+
Object.setPrototypeOf(this, WebrpcBadRouteError.prototype);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
export class WebrpcBadMethodError extends WebrpcError {
|
|
201
|
+
constructor(name = 'WebrpcBadMethod', code = -3, message = `bad method`, status = 0, cause) {
|
|
202
|
+
super(name, code, message, status, cause);
|
|
203
|
+
Object.setPrototypeOf(this, WebrpcBadMethodError.prototype);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
export class WebrpcBadRequestError extends WebrpcError {
|
|
207
|
+
constructor(name = 'WebrpcBadRequest', code = -4, message = `bad request`, status = 0, cause) {
|
|
208
|
+
super(name, code, message, status, cause);
|
|
209
|
+
Object.setPrototypeOf(this, WebrpcBadRequestError.prototype);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
export class WebrpcBadResponseError extends WebrpcError {
|
|
213
|
+
constructor(name = 'WebrpcBadResponse', code = -5, message = `bad response`, status = 0, cause) {
|
|
214
|
+
super(name, code, message, status, cause);
|
|
215
|
+
Object.setPrototypeOf(this, WebrpcBadResponseError.prototype);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
export class WebrpcServerPanicError extends WebrpcError {
|
|
219
|
+
constructor(name = 'WebrpcServerPanic', code = -6, message = `server panic`, status = 0, cause) {
|
|
220
|
+
super(name, code, message, status, cause);
|
|
221
|
+
Object.setPrototypeOf(this, WebrpcServerPanicError.prototype);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
export class WebrpcInternalErrorError extends WebrpcError {
|
|
225
|
+
constructor(name = 'WebrpcInternalError', code = -7, message = `internal error`, status = 0, cause) {
|
|
226
|
+
super(name, code, message, status, cause);
|
|
227
|
+
Object.setPrototypeOf(this, WebrpcInternalErrorError.prototype);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
export class WebrpcClientDisconnectedError extends WebrpcError {
|
|
231
|
+
constructor(name = 'WebrpcClientDisconnected', code = -8, message = `client disconnected`, status = 0, cause) {
|
|
232
|
+
super(name, code, message, status, cause);
|
|
233
|
+
Object.setPrototypeOf(this, WebrpcClientDisconnectedError.prototype);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
export class WebrpcStreamLostError extends WebrpcError {
|
|
237
|
+
constructor(name = 'WebrpcStreamLost', code = -9, message = `stream lost`, status = 0, cause) {
|
|
238
|
+
super(name, code, message, status, cause);
|
|
239
|
+
Object.setPrototypeOf(this, WebrpcStreamLostError.prototype);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
export class WebrpcStreamFinishedError extends WebrpcError {
|
|
243
|
+
constructor(name = 'WebrpcStreamFinished', code = -10, message = `stream finished`, status = 0, cause) {
|
|
244
|
+
super(name, code, message, status, cause);
|
|
245
|
+
Object.setPrototypeOf(this, WebrpcStreamFinishedError.prototype);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
// Schema errors
|
|
249
|
+
export var errors;
|
|
250
|
+
(function (errors) {
|
|
251
|
+
errors["WebrpcEndpoint"] = "WebrpcEndpoint";
|
|
252
|
+
errors["WebrpcRequestFailed"] = "WebrpcRequestFailed";
|
|
253
|
+
errors["WebrpcBadRoute"] = "WebrpcBadRoute";
|
|
254
|
+
errors["WebrpcBadMethod"] = "WebrpcBadMethod";
|
|
255
|
+
errors["WebrpcBadRequest"] = "WebrpcBadRequest";
|
|
256
|
+
errors["WebrpcBadResponse"] = "WebrpcBadResponse";
|
|
257
|
+
errors["WebrpcServerPanic"] = "WebrpcServerPanic";
|
|
258
|
+
errors["WebrpcInternalError"] = "WebrpcInternalError";
|
|
259
|
+
errors["WebrpcClientDisconnected"] = "WebrpcClientDisconnected";
|
|
260
|
+
errors["WebrpcStreamLost"] = "WebrpcStreamLost";
|
|
261
|
+
errors["WebrpcStreamFinished"] = "WebrpcStreamFinished";
|
|
262
|
+
})(errors || (errors = {}));
|
|
263
|
+
export var WebrpcErrorCodes;
|
|
264
|
+
(function (WebrpcErrorCodes) {
|
|
265
|
+
WebrpcErrorCodes[WebrpcErrorCodes["WebrpcEndpoint"] = 0] = "WebrpcEndpoint";
|
|
266
|
+
WebrpcErrorCodes[WebrpcErrorCodes["WebrpcRequestFailed"] = -1] = "WebrpcRequestFailed";
|
|
267
|
+
WebrpcErrorCodes[WebrpcErrorCodes["WebrpcBadRoute"] = -2] = "WebrpcBadRoute";
|
|
268
|
+
WebrpcErrorCodes[WebrpcErrorCodes["WebrpcBadMethod"] = -3] = "WebrpcBadMethod";
|
|
269
|
+
WebrpcErrorCodes[WebrpcErrorCodes["WebrpcBadRequest"] = -4] = "WebrpcBadRequest";
|
|
270
|
+
WebrpcErrorCodes[WebrpcErrorCodes["WebrpcBadResponse"] = -5] = "WebrpcBadResponse";
|
|
271
|
+
WebrpcErrorCodes[WebrpcErrorCodes["WebrpcServerPanic"] = -6] = "WebrpcServerPanic";
|
|
272
|
+
WebrpcErrorCodes[WebrpcErrorCodes["WebrpcInternalError"] = -7] = "WebrpcInternalError";
|
|
273
|
+
WebrpcErrorCodes[WebrpcErrorCodes["WebrpcClientDisconnected"] = -8] = "WebrpcClientDisconnected";
|
|
274
|
+
WebrpcErrorCodes[WebrpcErrorCodes["WebrpcStreamLost"] = -9] = "WebrpcStreamLost";
|
|
275
|
+
WebrpcErrorCodes[WebrpcErrorCodes["WebrpcStreamFinished"] = -10] = "WebrpcStreamFinished";
|
|
276
|
+
})(WebrpcErrorCodes || (WebrpcErrorCodes = {}));
|
|
277
|
+
export const webrpcErrorByCode = {
|
|
278
|
+
[0]: WebrpcEndpointError,
|
|
279
|
+
[-1]: WebrpcRequestFailedError,
|
|
280
|
+
[-2]: WebrpcBadRouteError,
|
|
281
|
+
[-3]: WebrpcBadMethodError,
|
|
282
|
+
[-4]: WebrpcBadRequestError,
|
|
283
|
+
[-5]: WebrpcBadResponseError,
|
|
284
|
+
[-6]: WebrpcServerPanicError,
|
|
285
|
+
[-7]: WebrpcInternalErrorError,
|
|
286
|
+
[-8]: WebrpcClientDisconnectedError,
|
|
287
|
+
[-9]: WebrpcStreamLostError,
|
|
288
|
+
[-10]: WebrpcStreamFinishedError,
|
|
289
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Hex, Bytes } from 'ox';
|
|
2
|
+
import { CommitVerifierReturn, CompleteAuthReturn, KeyType, IdentityType, AuthMode } from './identity-instrument.gen.js';
|
|
3
|
+
import { Challenge } from './challenge.js';
|
|
4
|
+
export type { CommitVerifierReturn, CompleteAuthReturn };
|
|
5
|
+
export { KeyType, IdentityType, AuthMode };
|
|
6
|
+
export * from './challenge.js';
|
|
7
|
+
export declare class IdentityInstrument {
|
|
8
|
+
private rpc;
|
|
9
|
+
constructor(hostname: string, fetch?: ((input: RequestInfo | URL, init?: RequestInit) => Promise<Response>) & typeof globalThis.fetch);
|
|
10
|
+
commitVerifier(authKey: AuthKey, challenge: Challenge): Promise<CommitVerifierReturn>;
|
|
11
|
+
completeAuth(authKey: AuthKey, challenge: Challenge): Promise<CompleteAuthReturn>;
|
|
12
|
+
sign(authKey: AuthKey, digest: Bytes.Bytes): Promise<`0x${string}`>;
|
|
13
|
+
}
|
|
14
|
+
export interface AuthKey {
|
|
15
|
+
signer: string;
|
|
16
|
+
address: string;
|
|
17
|
+
keyType: KeyType;
|
|
18
|
+
sign(digest: Bytes.Bytes): Promise<Hex.Hex>;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,IAAI,CAAA;AAC/B,OAAO,EACL,oBAAoB,EACpB,kBAAkB,EAElB,OAAO,EACP,YAAY,EACZ,QAAQ,EACT,MAAM,8BAA8B,CAAA;AACrC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAE1C,YAAY,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,CAAA;AACxD,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,CAAA;AAC1C,cAAc,gBAAgB,CAAA;AAE9B,qBAAa,kBAAkB;IAC7B,OAAO,CAAC,GAAG,CAAuB;gBAEtB,QAAQ,EAAE,MAAM,EAAE,KAAK,kGAAe;IAI5C,cAAc,CAAC,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS;IAYrD,YAAY,CAAC,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS;IAYnD,IAAI,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,KAAK;CAejD;AAED,MAAM,WAAW,OAAO;IACtB,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,MAAM,CAAA;IACf,OAAO,EAAE,OAAO,CAAA;IAChB,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;CAC5C"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { Hex } from 'ox';
|
|
2
|
+
import { IdentityInstrument as IdentityInstrumentRpc, KeyType, IdentityType, AuthMode, } from './identity-instrument.gen.js';
|
|
3
|
+
export { KeyType, IdentityType, AuthMode };
|
|
4
|
+
export * from './challenge.js';
|
|
5
|
+
export class IdentityInstrument {
|
|
6
|
+
rpc;
|
|
7
|
+
constructor(hostname, fetch = window.fetch) {
|
|
8
|
+
this.rpc = new IdentityInstrumentRpc(hostname.endsWith('/') ? hostname.slice(0, -1) : hostname, fetch);
|
|
9
|
+
}
|
|
10
|
+
async commitVerifier(authKey, challenge) {
|
|
11
|
+
return this.rpc.commitVerifier({
|
|
12
|
+
params: {
|
|
13
|
+
...challenge.getCommitParams(),
|
|
14
|
+
authKey: {
|
|
15
|
+
publicKey: authKey.address,
|
|
16
|
+
keyType: authKey.keyType,
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
async completeAuth(authKey, challenge) {
|
|
22
|
+
return this.rpc.completeAuth({
|
|
23
|
+
params: {
|
|
24
|
+
...challenge.getCompleteParams(),
|
|
25
|
+
authKey: {
|
|
26
|
+
publicKey: authKey.address,
|
|
27
|
+
keyType: authKey.keyType,
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
async sign(authKey, digest) {
|
|
33
|
+
const res = await this.rpc.sign({
|
|
34
|
+
params: {
|
|
35
|
+
signer: authKey.signer,
|
|
36
|
+
digest: Hex.fromBytes(digest),
|
|
37
|
+
authKey: {
|
|
38
|
+
publicKey: authKey.address,
|
|
39
|
+
keyType: authKey.keyType,
|
|
40
|
+
},
|
|
41
|
+
signature: await authKey.sign(digest),
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
Hex.assert(res.signature);
|
|
45
|
+
return res.signature;
|
|
46
|
+
}
|
|
47
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@0xsequence/identity-instrument",
|
|
3
|
+
"version": "0.0.0-20250520201059",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"private": false,
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@types/node": "^22.13.9",
|
|
18
|
+
"typescript": "^5.7.3",
|
|
19
|
+
"vitest": "^3.1.2",
|
|
20
|
+
"@repo/typescript-config": "^0.0.0-20250520201059"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"jwt-decode": "^4.0.0",
|
|
24
|
+
"ox": "^0.7.0"
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsc",
|
|
28
|
+
"dev": "tsc --watch",
|
|
29
|
+
"test": "vitest run"
|
|
30
|
+
}
|
|
31
|
+
}
|
package/src/challenge.ts
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { Bytes, Hash, Hex } from 'ox'
|
|
2
|
+
import { jwtDecode } from 'jwt-decode'
|
|
3
|
+
import { IdentityType, AuthMode } from './identity-instrument.gen.js'
|
|
4
|
+
|
|
5
|
+
interface CommitChallengeParams {
|
|
6
|
+
authMode: AuthMode
|
|
7
|
+
identityType: IdentityType
|
|
8
|
+
handle?: string
|
|
9
|
+
signer?: string
|
|
10
|
+
metadata: { [key: string]: string }
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface CompleteChallengeParams {
|
|
14
|
+
authMode: AuthMode
|
|
15
|
+
identityType: IdentityType
|
|
16
|
+
verifier: string
|
|
17
|
+
answer: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export abstract class Challenge {
|
|
21
|
+
public abstract getCommitParams(): CommitChallengeParams
|
|
22
|
+
public abstract getCompleteParams(): CompleteChallengeParams
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class IdTokenChallenge extends Challenge {
|
|
26
|
+
private handle = ''
|
|
27
|
+
private exp = ''
|
|
28
|
+
|
|
29
|
+
constructor(
|
|
30
|
+
readonly issuer: string,
|
|
31
|
+
readonly audience: string,
|
|
32
|
+
readonly idToken: string,
|
|
33
|
+
) {
|
|
34
|
+
super()
|
|
35
|
+
const decoded = jwtDecode(this.idToken)
|
|
36
|
+
const idTokenHash = Hash.keccak256(new TextEncoder().encode(this.idToken))
|
|
37
|
+
this.handle = Hex.fromBytes(idTokenHash)
|
|
38
|
+
this.exp = decoded.exp?.toString() ?? ''
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public getCommitParams(): CommitChallengeParams {
|
|
42
|
+
return {
|
|
43
|
+
authMode: AuthMode.IDToken,
|
|
44
|
+
identityType: IdentityType.OIDC,
|
|
45
|
+
handle: this.handle,
|
|
46
|
+
metadata: {
|
|
47
|
+
iss: this.issuer,
|
|
48
|
+
aud: this.audience,
|
|
49
|
+
exp: this.exp,
|
|
50
|
+
},
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public getCompleteParams(): CompleteChallengeParams {
|
|
55
|
+
return {
|
|
56
|
+
authMode: AuthMode.IDToken,
|
|
57
|
+
identityType: IdentityType.OIDC,
|
|
58
|
+
verifier: this.handle,
|
|
59
|
+
answer: this.idToken,
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export class AuthCodeChallenge extends Challenge {
|
|
65
|
+
private handle = ''
|
|
66
|
+
private signer?: string
|
|
67
|
+
|
|
68
|
+
constructor(
|
|
69
|
+
readonly issuer: string,
|
|
70
|
+
readonly audience: string,
|
|
71
|
+
readonly redirectUri: string,
|
|
72
|
+
readonly authCode: string,
|
|
73
|
+
) {
|
|
74
|
+
super()
|
|
75
|
+
const authCodeHash = Hash.keccak256(new TextEncoder().encode(this.authCode))
|
|
76
|
+
this.handle = Hex.fromBytes(authCodeHash)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
public getCommitParams(): CommitChallengeParams {
|
|
80
|
+
return {
|
|
81
|
+
authMode: AuthMode.AuthCode,
|
|
82
|
+
identityType: IdentityType.OIDC,
|
|
83
|
+
signer: this.signer,
|
|
84
|
+
handle: this.handle,
|
|
85
|
+
metadata: {
|
|
86
|
+
iss: this.issuer,
|
|
87
|
+
aud: this.audience,
|
|
88
|
+
redirect_uri: this.redirectUri,
|
|
89
|
+
},
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
public getCompleteParams(): CompleteChallengeParams {
|
|
94
|
+
return {
|
|
95
|
+
authMode: AuthMode.AuthCode,
|
|
96
|
+
identityType: IdentityType.OIDC,
|
|
97
|
+
verifier: this.handle,
|
|
98
|
+
answer: this.authCode,
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
public withSigner(signer: string): AuthCodeChallenge {
|
|
103
|
+
const challenge = new AuthCodeChallenge(this.issuer, this.audience, this.redirectUri, this.authCode)
|
|
104
|
+
challenge.handle = this.handle
|
|
105
|
+
challenge.signer = signer
|
|
106
|
+
return challenge
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export class AuthCodePkceChallenge extends Challenge {
|
|
111
|
+
private verifier?: string
|
|
112
|
+
private authCode?: string
|
|
113
|
+
private signer?: string
|
|
114
|
+
|
|
115
|
+
constructor(
|
|
116
|
+
readonly issuer: string,
|
|
117
|
+
readonly audience: string,
|
|
118
|
+
readonly redirectUri: string,
|
|
119
|
+
) {
|
|
120
|
+
super()
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
public getCommitParams(): CommitChallengeParams {
|
|
124
|
+
return {
|
|
125
|
+
authMode: AuthMode.AuthCodePKCE,
|
|
126
|
+
identityType: IdentityType.OIDC,
|
|
127
|
+
signer: this.signer,
|
|
128
|
+
metadata: {
|
|
129
|
+
iss: this.issuer,
|
|
130
|
+
aud: this.audience,
|
|
131
|
+
redirect_uri: this.redirectUri,
|
|
132
|
+
},
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
public getCompleteParams(): CompleteChallengeParams {
|
|
137
|
+
if (!this.verifier || !this.authCode) {
|
|
138
|
+
throw new Error('AuthCodePkceChallenge is not complete')
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return {
|
|
142
|
+
authMode: AuthMode.AuthCodePKCE,
|
|
143
|
+
identityType: IdentityType.OIDC,
|
|
144
|
+
verifier: this.verifier,
|
|
145
|
+
answer: this.authCode,
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
public withSigner(signer: string): AuthCodePkceChallenge {
|
|
150
|
+
const challenge = new AuthCodePkceChallenge(this.issuer, this.audience, this.redirectUri)
|
|
151
|
+
challenge.verifier = this.verifier
|
|
152
|
+
challenge.signer = signer
|
|
153
|
+
return challenge
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
public withAnswer(verifier: string, authCode: string): AuthCodePkceChallenge {
|
|
157
|
+
const challenge = new AuthCodePkceChallenge(this.issuer, this.audience, this.redirectUri)
|
|
158
|
+
challenge.signer = this.signer
|
|
159
|
+
challenge.verifier = verifier
|
|
160
|
+
challenge.authCode = authCode
|
|
161
|
+
return challenge
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export class OtpChallenge extends Challenge {
|
|
166
|
+
private answer?: string
|
|
167
|
+
private recipient?: string
|
|
168
|
+
private signer?: string
|
|
169
|
+
|
|
170
|
+
private constructor(readonly identityType: IdentityType) {
|
|
171
|
+
super()
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
public static fromRecipient(identityType: IdentityType, recipient: string): OtpChallenge {
|
|
175
|
+
const challenge = new OtpChallenge(identityType)
|
|
176
|
+
challenge.recipient = recipient
|
|
177
|
+
return challenge
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
public static fromSigner(identityType: IdentityType, signer: string): OtpChallenge {
|
|
181
|
+
const challenge = new OtpChallenge(identityType)
|
|
182
|
+
challenge.signer = signer
|
|
183
|
+
return challenge
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
public getCommitParams(): CommitChallengeParams {
|
|
187
|
+
if (!this.recipient && !this.signer) {
|
|
188
|
+
throw new Error('OtpChallenge is not complete')
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return {
|
|
192
|
+
authMode: AuthMode.OTP,
|
|
193
|
+
identityType: this.identityType,
|
|
194
|
+
handle: this.recipient,
|
|
195
|
+
signer: this.signer,
|
|
196
|
+
metadata: {},
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
public getCompleteParams(): CompleteChallengeParams {
|
|
201
|
+
if (!this.answer || (!this.recipient && !this.signer)) {
|
|
202
|
+
throw new Error('OtpChallenge is not complete')
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return {
|
|
206
|
+
authMode: AuthMode.OTP,
|
|
207
|
+
identityType: this.identityType,
|
|
208
|
+
verifier: this.recipient ?? this.signer ?? '',
|
|
209
|
+
answer: this.answer,
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
public withAnswer(codeChallenge: string, otp: string): OtpChallenge {
|
|
214
|
+
const challenge = new OtpChallenge(this.identityType)
|
|
215
|
+
challenge.recipient = this.recipient
|
|
216
|
+
challenge.signer = this.signer
|
|
217
|
+
const answerHash = Hash.keccak256(Bytes.fromString(codeChallenge + otp))
|
|
218
|
+
challenge.answer = Hex.fromBytes(answerHash)
|
|
219
|
+
return challenge
|
|
220
|
+
}
|
|
221
|
+
}
|