@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.
@@ -0,0 +1,517 @@
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
+
8
+ export const WebrpcHeader = 'Webrpc'
9
+
10
+ export const WebrpcHeaderValue = 'webrpc@v0.23.1;gen-typescript@v0.16.3;identity-instrument@v0.1.0'
11
+
12
+ // WebRPC description and code-gen version
13
+ export const WebRPCVersion = 'v1'
14
+
15
+ // Schema version of your RIDL schema
16
+ export const WebRPCSchemaVersion = 'v0.1.0'
17
+
18
+ // Schema hash generated from your RIDL schema
19
+ export const WebRPCSchemaHash = '2338b17497d46b1813768be23a7338716a4b6d9f'
20
+
21
+ type WebrpcGenVersions = {
22
+ webrpcGenVersion: string
23
+ codeGenName: string
24
+ codeGenVersion: string
25
+ schemaName: string
26
+ schemaVersion: string
27
+ }
28
+
29
+ export function VersionFromHeader(headers: Headers): WebrpcGenVersions {
30
+ const headerValue = headers.get(WebrpcHeader)
31
+ if (!headerValue) {
32
+ return {
33
+ webrpcGenVersion: '',
34
+ codeGenName: '',
35
+ codeGenVersion: '',
36
+ schemaName: '',
37
+ schemaVersion: '',
38
+ }
39
+ }
40
+
41
+ return parseWebrpcGenVersions(headerValue)
42
+ }
43
+
44
+ function parseWebrpcGenVersions(header: string): WebrpcGenVersions {
45
+ const versions = header.split(';')
46
+ if (versions.length < 3) {
47
+ return {
48
+ webrpcGenVersion: '',
49
+ codeGenName: '',
50
+ codeGenVersion: '',
51
+ schemaName: '',
52
+ schemaVersion: '',
53
+ }
54
+ }
55
+
56
+ const [_, webrpcGenVersion] = versions[0]!.split('@')
57
+ const [codeGenName, codeGenVersion] = versions[1]!.split('@')
58
+ const [schemaName, schemaVersion] = versions[2]!.split('@')
59
+
60
+ return {
61
+ webrpcGenVersion: webrpcGenVersion ?? '',
62
+ codeGenName: codeGenName ?? '',
63
+ codeGenVersion: codeGenVersion ?? '',
64
+ schemaName: schemaName ?? '',
65
+ schemaVersion: schemaVersion ?? '',
66
+ }
67
+ }
68
+
69
+ //
70
+ // Types
71
+ //
72
+
73
+ export enum KeyType {
74
+ P256K1 = 'P256K1',
75
+ P256R1 = 'P256R1',
76
+ }
77
+
78
+ export enum IdentityType {
79
+ Guest = 'Guest',
80
+ Email = 'Email',
81
+ OIDC = 'OIDC',
82
+ }
83
+
84
+ export enum AuthMode {
85
+ Guest = 'Guest',
86
+ OTP = 'OTP',
87
+ IDToken = 'IDToken',
88
+ AccessToken = 'AccessToken',
89
+ AuthCode = 'AuthCode',
90
+ AuthCodePKCE = 'AuthCodePKCE',
91
+ }
92
+
93
+ export interface CommitVerifierParams {
94
+ authKey: AuthKey
95
+ identityType: IdentityType
96
+ authMode: AuthMode
97
+ metadata: { [key: string]: string }
98
+ handle?: string
99
+ signer?: string
100
+ }
101
+
102
+ export interface CompleteAuthParams {
103
+ authKey: AuthKey
104
+ identityType: IdentityType
105
+ authMode: AuthMode
106
+ verifier: string
107
+ answer: string
108
+ }
109
+
110
+ export interface SignParams {
111
+ signer: string
112
+ digest: string
113
+ authKey: AuthKey
114
+ signature: string
115
+ }
116
+
117
+ export interface Identity {
118
+ type: IdentityType
119
+ issuer: string
120
+ subject: string
121
+ email: string
122
+ }
123
+
124
+ export interface AuthID {
125
+ ecosystem: string
126
+ authMode: AuthMode
127
+ identityType: IdentityType
128
+ verifier: string
129
+ }
130
+
131
+ export interface AuthKey {
132
+ publicKey: string
133
+ keyType: KeyType
134
+ }
135
+
136
+ export interface AuthKeyData {
137
+ ecosystem: string
138
+ signerAddress: string
139
+ publicKey: string
140
+ keyType: KeyType
141
+ expiry: string
142
+ }
143
+
144
+ export interface SignerData {
145
+ ecosystem: string
146
+ identity: Identity
147
+ keyType: KeyType
148
+ privateKey: string
149
+ }
150
+
151
+ export interface AuthCommitmentData {
152
+ ecosystem: string
153
+ authKey: AuthKey
154
+ authMode: AuthMode
155
+ identityType: IdentityType
156
+ handle: string
157
+ signer: string
158
+ challenge: string
159
+ answer: string
160
+ metadata: { [key: string]: string }
161
+ expiry: string
162
+ }
163
+
164
+ export interface IdentityInstrument {
165
+ commitVerifier(args: CommitVerifierArgs, headers?: object, signal?: AbortSignal): Promise<CommitVerifierReturn>
166
+ completeAuth(args: CompleteAuthArgs, headers?: object, signal?: AbortSignal): Promise<CompleteAuthReturn>
167
+ sign(args: SignArgs, headers?: object, signal?: AbortSignal): Promise<SignReturn>
168
+ }
169
+
170
+ export interface CommitVerifierArgs {
171
+ params: CommitVerifierParams
172
+ }
173
+
174
+ export interface CommitVerifierReturn {
175
+ verifier: string
176
+ loginHint: string
177
+ challenge: string
178
+ }
179
+ export interface CompleteAuthArgs {
180
+ params: CompleteAuthParams
181
+ }
182
+
183
+ export interface CompleteAuthReturn {
184
+ signer: string
185
+ }
186
+ export interface SignArgs {
187
+ params: SignParams
188
+ }
189
+
190
+ export interface SignReturn {
191
+ signature: string
192
+ }
193
+
194
+ //
195
+ // Client
196
+ //
197
+ export class IdentityInstrument implements IdentityInstrument {
198
+ protected hostname: string
199
+ protected fetch: Fetch
200
+ protected path = '/rpc/IdentityInstrument/'
201
+
202
+ constructor(hostname: string, fetch: Fetch) {
203
+ this.hostname = hostname.replace(/\/*$/, '')
204
+ this.fetch = (input: RequestInfo, init?: RequestInit) => fetch(input, init)
205
+ }
206
+
207
+ private url(name: string): string {
208
+ return this.hostname + this.path + name
209
+ }
210
+
211
+ commitVerifier = (
212
+ args: CommitVerifierArgs,
213
+ headers?: object,
214
+ signal?: AbortSignal,
215
+ ): Promise<CommitVerifierReturn> => {
216
+ return this.fetch(this.url('CommitVerifier'), createHTTPRequest(args, headers, signal)).then(
217
+ (res) => {
218
+ return buildResponse(res).then((_data) => {
219
+ return {
220
+ verifier: <string>_data.verifier,
221
+ loginHint: <string>_data.loginHint,
222
+ challenge: <string>_data.challenge,
223
+ }
224
+ })
225
+ },
226
+ (error) => {
227
+ throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
228
+ },
229
+ )
230
+ }
231
+
232
+ completeAuth = (args: CompleteAuthArgs, headers?: object, signal?: AbortSignal): Promise<CompleteAuthReturn> => {
233
+ return this.fetch(this.url('CompleteAuth'), createHTTPRequest(args, headers, signal)).then(
234
+ (res) => {
235
+ return buildResponse(res).then((_data) => {
236
+ return {
237
+ signer: <string>_data.signer,
238
+ }
239
+ })
240
+ },
241
+ (error) => {
242
+ throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
243
+ },
244
+ )
245
+ }
246
+
247
+ sign = (args: SignArgs, headers?: object, signal?: AbortSignal): Promise<SignReturn> => {
248
+ return this.fetch(this.url('Sign'), createHTTPRequest(args, headers, signal)).then(
249
+ (res) => {
250
+ return buildResponse(res).then((_data) => {
251
+ return {
252
+ signature: <string>_data.signature,
253
+ }
254
+ })
255
+ },
256
+ (error) => {
257
+ throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
258
+ },
259
+ )
260
+ }
261
+ }
262
+
263
+ const createHTTPRequest = (body: object = {}, headers: object = {}, signal: AbortSignal | null = null): object => {
264
+ const reqHeaders: { [key: string]: string } = { ...headers, 'Content-Type': 'application/json' }
265
+ reqHeaders[WebrpcHeader] = WebrpcHeaderValue
266
+
267
+ return {
268
+ method: 'POST',
269
+ headers: reqHeaders,
270
+ body: JSON.stringify(body || {}),
271
+ signal,
272
+ }
273
+ }
274
+
275
+ const buildResponse = (res: Response): Promise<any> => {
276
+ return res.text().then((text) => {
277
+ let data
278
+ try {
279
+ data = JSON.parse(text)
280
+ } catch (error) {
281
+ let message = ''
282
+ if (error instanceof Error) {
283
+ message = error.message
284
+ }
285
+ throw WebrpcBadResponseError.new({
286
+ status: res.status,
287
+ cause: `JSON.parse(): ${message}: response text: ${text}`,
288
+ })
289
+ }
290
+ if (!res.ok) {
291
+ const code: number = typeof data.code === 'number' ? data.code : 0
292
+ throw (webrpcErrorByCode[code] || WebrpcError).new(data)
293
+ }
294
+ return data
295
+ })
296
+ }
297
+
298
+ //
299
+ // Errors
300
+ //
301
+
302
+ export class WebrpcError extends Error {
303
+ name: string
304
+ code: number
305
+ message: string
306
+ status: number
307
+ cause?: string
308
+
309
+ /** @deprecated Use message instead of msg. Deprecated in webrpc v0.11.0. */
310
+ msg: string
311
+
312
+ constructor(name: string, code: number, message: string, status: number, cause?: string) {
313
+ super(message)
314
+ this.name = name || 'WebrpcError'
315
+ this.code = typeof code === 'number' ? code : 0
316
+ this.message = message || `endpoint error ${this.code}`
317
+ this.msg = this.message
318
+ this.status = typeof status === 'number' ? status : 0
319
+ this.cause = cause
320
+ Object.setPrototypeOf(this, WebrpcError.prototype)
321
+ }
322
+
323
+ static new(payload: any): WebrpcError {
324
+ return new this(payload.error, payload.code, payload.message || payload.msg, payload.status, payload.cause)
325
+ }
326
+ }
327
+
328
+ // Webrpc errors
329
+
330
+ export class WebrpcEndpointError extends WebrpcError {
331
+ constructor(
332
+ name: string = 'WebrpcEndpoint',
333
+ code: number = 0,
334
+ message: string = `endpoint error`,
335
+ status: number = 0,
336
+ cause?: string,
337
+ ) {
338
+ super(name, code, message, status, cause)
339
+ Object.setPrototypeOf(this, WebrpcEndpointError.prototype)
340
+ }
341
+ }
342
+
343
+ export class WebrpcRequestFailedError extends WebrpcError {
344
+ constructor(
345
+ name: string = 'WebrpcRequestFailed',
346
+ code: number = -1,
347
+ message: string = `request failed`,
348
+ status: number = 0,
349
+ cause?: string,
350
+ ) {
351
+ super(name, code, message, status, cause)
352
+ Object.setPrototypeOf(this, WebrpcRequestFailedError.prototype)
353
+ }
354
+ }
355
+
356
+ export class WebrpcBadRouteError extends WebrpcError {
357
+ constructor(
358
+ name: string = 'WebrpcBadRoute',
359
+ code: number = -2,
360
+ message: string = `bad route`,
361
+ status: number = 0,
362
+ cause?: string,
363
+ ) {
364
+ super(name, code, message, status, cause)
365
+ Object.setPrototypeOf(this, WebrpcBadRouteError.prototype)
366
+ }
367
+ }
368
+
369
+ export class WebrpcBadMethodError extends WebrpcError {
370
+ constructor(
371
+ name: string = 'WebrpcBadMethod',
372
+ code: number = -3,
373
+ message: string = `bad method`,
374
+ status: number = 0,
375
+ cause?: string,
376
+ ) {
377
+ super(name, code, message, status, cause)
378
+ Object.setPrototypeOf(this, WebrpcBadMethodError.prototype)
379
+ }
380
+ }
381
+
382
+ export class WebrpcBadRequestError extends WebrpcError {
383
+ constructor(
384
+ name: string = 'WebrpcBadRequest',
385
+ code: number = -4,
386
+ message: string = `bad request`,
387
+ status: number = 0,
388
+ cause?: string,
389
+ ) {
390
+ super(name, code, message, status, cause)
391
+ Object.setPrototypeOf(this, WebrpcBadRequestError.prototype)
392
+ }
393
+ }
394
+
395
+ export class WebrpcBadResponseError extends WebrpcError {
396
+ constructor(
397
+ name: string = 'WebrpcBadResponse',
398
+ code: number = -5,
399
+ message: string = `bad response`,
400
+ status: number = 0,
401
+ cause?: string,
402
+ ) {
403
+ super(name, code, message, status, cause)
404
+ Object.setPrototypeOf(this, WebrpcBadResponseError.prototype)
405
+ }
406
+ }
407
+
408
+ export class WebrpcServerPanicError extends WebrpcError {
409
+ constructor(
410
+ name: string = 'WebrpcServerPanic',
411
+ code: number = -6,
412
+ message: string = `server panic`,
413
+ status: number = 0,
414
+ cause?: string,
415
+ ) {
416
+ super(name, code, message, status, cause)
417
+ Object.setPrototypeOf(this, WebrpcServerPanicError.prototype)
418
+ }
419
+ }
420
+
421
+ export class WebrpcInternalErrorError extends WebrpcError {
422
+ constructor(
423
+ name: string = 'WebrpcInternalError',
424
+ code: number = -7,
425
+ message: string = `internal error`,
426
+ status: number = 0,
427
+ cause?: string,
428
+ ) {
429
+ super(name, code, message, status, cause)
430
+ Object.setPrototypeOf(this, WebrpcInternalErrorError.prototype)
431
+ }
432
+ }
433
+
434
+ export class WebrpcClientDisconnectedError extends WebrpcError {
435
+ constructor(
436
+ name: string = 'WebrpcClientDisconnected',
437
+ code: number = -8,
438
+ message: string = `client disconnected`,
439
+ status: number = 0,
440
+ cause?: string,
441
+ ) {
442
+ super(name, code, message, status, cause)
443
+ Object.setPrototypeOf(this, WebrpcClientDisconnectedError.prototype)
444
+ }
445
+ }
446
+
447
+ export class WebrpcStreamLostError extends WebrpcError {
448
+ constructor(
449
+ name: string = 'WebrpcStreamLost',
450
+ code: number = -9,
451
+ message: string = `stream lost`,
452
+ status: number = 0,
453
+ cause?: string,
454
+ ) {
455
+ super(name, code, message, status, cause)
456
+ Object.setPrototypeOf(this, WebrpcStreamLostError.prototype)
457
+ }
458
+ }
459
+
460
+ export class WebrpcStreamFinishedError extends WebrpcError {
461
+ constructor(
462
+ name: string = 'WebrpcStreamFinished',
463
+ code: number = -10,
464
+ message: string = `stream finished`,
465
+ status: number = 0,
466
+ cause?: string,
467
+ ) {
468
+ super(name, code, message, status, cause)
469
+ Object.setPrototypeOf(this, WebrpcStreamFinishedError.prototype)
470
+ }
471
+ }
472
+
473
+ // Schema errors
474
+
475
+ export enum errors {
476
+ WebrpcEndpoint = 'WebrpcEndpoint',
477
+ WebrpcRequestFailed = 'WebrpcRequestFailed',
478
+ WebrpcBadRoute = 'WebrpcBadRoute',
479
+ WebrpcBadMethod = 'WebrpcBadMethod',
480
+ WebrpcBadRequest = 'WebrpcBadRequest',
481
+ WebrpcBadResponse = 'WebrpcBadResponse',
482
+ WebrpcServerPanic = 'WebrpcServerPanic',
483
+ WebrpcInternalError = 'WebrpcInternalError',
484
+ WebrpcClientDisconnected = 'WebrpcClientDisconnected',
485
+ WebrpcStreamLost = 'WebrpcStreamLost',
486
+ WebrpcStreamFinished = 'WebrpcStreamFinished',
487
+ }
488
+
489
+ export enum WebrpcErrorCodes {
490
+ WebrpcEndpoint = 0,
491
+ WebrpcRequestFailed = -1,
492
+ WebrpcBadRoute = -2,
493
+ WebrpcBadMethod = -3,
494
+ WebrpcBadRequest = -4,
495
+ WebrpcBadResponse = -5,
496
+ WebrpcServerPanic = -6,
497
+ WebrpcInternalError = -7,
498
+ WebrpcClientDisconnected = -8,
499
+ WebrpcStreamLost = -9,
500
+ WebrpcStreamFinished = -10,
501
+ }
502
+
503
+ export const webrpcErrorByCode: { [code: number]: any } = {
504
+ [0]: WebrpcEndpointError,
505
+ [-1]: WebrpcRequestFailedError,
506
+ [-2]: WebrpcBadRouteError,
507
+ [-3]: WebrpcBadMethodError,
508
+ [-4]: WebrpcBadRequestError,
509
+ [-5]: WebrpcBadResponseError,
510
+ [-6]: WebrpcServerPanicError,
511
+ [-7]: WebrpcInternalErrorError,
512
+ [-8]: WebrpcClientDisconnectedError,
513
+ [-9]: WebrpcStreamLostError,
514
+ [-10]: WebrpcStreamFinishedError,
515
+ }
516
+
517
+ export type Fetch = (input: RequestInfo, init?: RequestInit) => Promise<Response>
package/src/index.ts ADDED
@@ -0,0 +1,69 @@
1
+ import { Hex, Bytes } from 'ox'
2
+ import {
3
+ CommitVerifierReturn,
4
+ CompleteAuthReturn,
5
+ IdentityInstrument as IdentityInstrumentRpc,
6
+ KeyType,
7
+ IdentityType,
8
+ AuthMode,
9
+ } from './identity-instrument.gen.js'
10
+ import { Challenge } from './challenge.js'
11
+
12
+ export type { CommitVerifierReturn, CompleteAuthReturn }
13
+ export { KeyType, IdentityType, AuthMode }
14
+ export * from './challenge.js'
15
+
16
+ export class IdentityInstrument {
17
+ private rpc: IdentityInstrumentRpc
18
+
19
+ constructor(hostname: string, fetch = window.fetch) {
20
+ this.rpc = new IdentityInstrumentRpc(hostname.endsWith('/') ? hostname.slice(0, -1) : hostname, fetch)
21
+ }
22
+
23
+ async commitVerifier(authKey: AuthKey, challenge: Challenge) {
24
+ return this.rpc.commitVerifier({
25
+ params: {
26
+ ...challenge.getCommitParams(),
27
+ authKey: {
28
+ publicKey: authKey.address,
29
+ keyType: authKey.keyType,
30
+ },
31
+ },
32
+ })
33
+ }
34
+
35
+ async completeAuth(authKey: AuthKey, challenge: Challenge) {
36
+ return this.rpc.completeAuth({
37
+ params: {
38
+ ...challenge.getCompleteParams(),
39
+ authKey: {
40
+ publicKey: authKey.address,
41
+ keyType: authKey.keyType,
42
+ },
43
+ },
44
+ })
45
+ }
46
+
47
+ async sign(authKey: AuthKey, digest: Bytes.Bytes) {
48
+ const res = await this.rpc.sign({
49
+ params: {
50
+ signer: authKey.signer,
51
+ digest: Hex.fromBytes(digest),
52
+ authKey: {
53
+ publicKey: authKey.address,
54
+ keyType: authKey.keyType,
55
+ },
56
+ signature: await authKey.sign(digest),
57
+ },
58
+ })
59
+ Hex.assert(res.signature)
60
+ return res.signature
61
+ }
62
+ }
63
+
64
+ export interface AuthKey {
65
+ signer: string
66
+ address: string
67
+ keyType: KeyType
68
+ sign(digest: Bytes.Bytes): Promise<Hex.Hex>
69
+ }