@textrp/briij-js-sdk 43.1.1 → 44.0.0
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/CHANGELOG.md +11 -1
- package/README.md +18 -0
- package/lib/@types/auth.d.ts +38 -0
- package/lib/@types/auth.d.ts.map +1 -1
- package/lib/@types/auth.js.map +1 -1
- package/lib/@types/snarkjs.d.js +0 -0
- package/lib/@types/snarkjs.d.js.map +1 -0
- package/lib/auth/credential.d.ts +25 -0
- package/lib/auth/credential.d.ts.map +1 -0
- package/lib/auth/credential.js +48 -0
- package/lib/auth/credential.js.map +1 -0
- package/lib/auth/did.d.ts +22 -0
- package/lib/auth/did.d.ts.map +1 -0
- package/lib/auth/did.js +66 -0
- package/lib/auth/did.js.map +1 -0
- package/lib/auth/wallet.d.ts +22 -0
- package/lib/auth/wallet.d.ts.map +1 -0
- package/lib/auth/wallet.js +27 -0
- package/lib/auth/wallet.js.map +1 -0
- package/lib/auth/zkpE2EE.d.ts +22 -0
- package/lib/auth/zkpE2EE.d.ts.map +1 -0
- package/lib/auth/zkpE2EE.js +69 -0
- package/lib/auth/zkpE2EE.js.map +1 -0
- package/lib/briij.d.ts +4 -0
- package/lib/briij.d.ts.map +1 -1
- package/lib/briij.js +4 -0
- package/lib/briij.js.map +1 -1
- package/lib/client.d.ts +40 -1
- package/lib/client.d.ts.map +1 -1
- package/lib/client.js +259 -113
- package/lib/client.js.map +1 -1
- package/lib/credits.d.ts +24 -0
- package/lib/credits.d.ts.map +1 -0
- package/lib/credits.js +53 -0
- package/lib/credits.js.map +1 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +2 -0
- package/lib/index.js.map +1 -1
- package/package.json +3 -1
- package/src/@types/auth.ts +44 -0
- package/src/@types/snarkjs.d.ts +17 -0
- package/src/auth/credential.ts +63 -0
- package/src/auth/did.ts +89 -0
- package/src/auth/wallet.ts +50 -0
- package/src/auth/zkpE2EE.ts +88 -0
- package/src/briij.ts +12 -0
- package/src/client.ts +187 -0
- package/src/components/LoginStepper.tsx +50 -0
- package/src/credits.ts +60 -0
- package/src/index.ts +2 -0
package/src/client.ts
CHANGED
|
@@ -204,6 +204,11 @@ import {
|
|
|
204
204
|
} from "./webrtc/groupCall.ts";
|
|
205
205
|
import { MediaHandler } from "./webrtc/mediaHandler.ts";
|
|
206
206
|
import {
|
|
207
|
+
type CredentialCreateResult,
|
|
208
|
+
type CredentialVerifyResult,
|
|
209
|
+
type DidCredentialMetadata,
|
|
210
|
+
type DidResolutionResult,
|
|
211
|
+
type ZkpVerifyResult,
|
|
207
212
|
type ILoginFlowsResponse,
|
|
208
213
|
type IRefreshTokenResponse,
|
|
209
214
|
type LoginRequest,
|
|
@@ -220,6 +225,15 @@ import {
|
|
|
220
225
|
type XrplAuthCompleteRequest,
|
|
221
226
|
type XrplWalletChallengePayload,
|
|
222
227
|
} from "./@types/auth.ts";
|
|
228
|
+
import { createMinimalDidDocument, deriveXrplDid, resolveDidViaHomeserver } from "./auth/did.ts";
|
|
229
|
+
import {
|
|
230
|
+
loadDidCredentialMetadata,
|
|
231
|
+
requestCredentialCreate,
|
|
232
|
+
storeDidCredentialMetadata,
|
|
233
|
+
verifyCredential,
|
|
234
|
+
} from "./auth/credential.ts";
|
|
235
|
+
import { generateE2eeZkProof } from "./auth/zkpE2EE.ts";
|
|
236
|
+
import { buildWalletLoginSubmission, type WalletProofProvider, type WalletProofResult } from "./auth/wallet.ts";
|
|
223
237
|
import { TypedEventEmitter } from "./models/typed-event-emitter.ts";
|
|
224
238
|
import { MAIN_ROOM_TIMELINE, ReceiptType } from "./@types/read_receipts.ts";
|
|
225
239
|
import { type MSC3575SlidingSyncRequest, type MSC3575SlidingSyncResponse, type SlidingSync } from "./sliding-sync.ts";
|
|
@@ -6997,6 +7011,179 @@ export class BriijClient extends TypedEventEmitter<EmittedEvents, ClientEventHan
|
|
|
6997
7011
|
});
|
|
6998
7012
|
}
|
|
6999
7013
|
|
|
7014
|
+
/**
|
|
7015
|
+
* XRPL DID + credential login flow:
|
|
7016
|
+
* 1) challenge request, 2) wallet proof submission, 3) DID resolve, 4) credential request.
|
|
7017
|
+
*/
|
|
7018
|
+
public async loginWithXrplDidCredential(params: {
|
|
7019
|
+
address: string;
|
|
7020
|
+
username?: string;
|
|
7021
|
+
network?: string;
|
|
7022
|
+
didNetwork?: "testnet" | "mainnet";
|
|
7023
|
+
e2eePubkeyCommitment?: string;
|
|
7024
|
+
longevityMode?: boolean;
|
|
7025
|
+
walletProofProvider: WalletProofProvider;
|
|
7026
|
+
zkpLongevityMode?: {
|
|
7027
|
+
enabled: boolean;
|
|
7028
|
+
e2eePrivateKey?: string;
|
|
7029
|
+
strict?: boolean;
|
|
7030
|
+
generateProof?: (context: {
|
|
7031
|
+
didUri: string;
|
|
7032
|
+
xrplAddress: string;
|
|
7033
|
+
credentialId: string;
|
|
7034
|
+
e2eePubkeyCommitment: string;
|
|
7035
|
+
}) => Promise<{ proof: Record<string, unknown>; publicSignals: string[] | Record<string, string> }>;
|
|
7036
|
+
};
|
|
7037
|
+
}): Promise<
|
|
7038
|
+
LoginResponse & {
|
|
7039
|
+
did: DidResolutionResult;
|
|
7040
|
+
didDocument: ReturnType<typeof createMinimalDidDocument>;
|
|
7041
|
+
credential: CredentialCreateResult;
|
|
7042
|
+
credentialVerification: CredentialVerifyResult;
|
|
7043
|
+
metadata: DidCredentialMetadata;
|
|
7044
|
+
zkp?: ZkpVerifyResult;
|
|
7045
|
+
}
|
|
7046
|
+
> {
|
|
7047
|
+
const network = params.network ?? "xrpl";
|
|
7048
|
+
const didNetwork = params.didNetwork ?? "testnet";
|
|
7049
|
+
|
|
7050
|
+
const challenge = await this.getXrplAuthChallenge({
|
|
7051
|
+
address: params.address,
|
|
7052
|
+
network,
|
|
7053
|
+
username: params.username,
|
|
7054
|
+
preferred_localpart: params.username,
|
|
7055
|
+
});
|
|
7056
|
+
|
|
7057
|
+
const walletProof: WalletProofResult = await params.walletProofProvider(challenge.challenge, network);
|
|
7058
|
+
if (walletProof.address !== params.address) {
|
|
7059
|
+
throw new Error("Wallet proof address mismatch");
|
|
7060
|
+
}
|
|
7061
|
+
|
|
7062
|
+
const loginResponse = await this.completeXrplAuth(
|
|
7063
|
+
buildWalletLoginSubmission(walletProof, challenge.session, network, params.username),
|
|
7064
|
+
);
|
|
7065
|
+
await this.applyLoginResponse(loginResponse, XRPL_WALLET_LOGIN_TYPE);
|
|
7066
|
+
|
|
7067
|
+
const didUri = deriveXrplDid(walletProof.address, didNetwork);
|
|
7068
|
+
const commitment = params.e2eePubkeyCommitment ?? (await this.createE2eeCommitment(loginResponse.user_id));
|
|
7069
|
+
const didDocument = createMinimalDidDocument(didUri, commitment);
|
|
7070
|
+
|
|
7071
|
+
const did = await resolveDidViaHomeserver(async (path: string) => {
|
|
7072
|
+
return this.http.authedRequest<DidResolutionResult>(Method.Get, path);
|
|
7073
|
+
}, walletProof.address);
|
|
7074
|
+
|
|
7075
|
+
const credential = await requestCredentialCreate(
|
|
7076
|
+
async (path, method, body) => this.http.authedRequest<CredentialCreateResult>(Method.Post, path, undefined, body),
|
|
7077
|
+
{
|
|
7078
|
+
subject: walletProof.address,
|
|
7079
|
+
did_uri: did.did_uri ?? didUri,
|
|
7080
|
+
e2ee_pubkey_commitment: commitment,
|
|
7081
|
+
},
|
|
7082
|
+
);
|
|
7083
|
+
|
|
7084
|
+
const credentialVerification = await verifyCredential(
|
|
7085
|
+
async (path, method, body) =>
|
|
7086
|
+
this.http.authedRequest<CredentialVerifyResult>(Method.Post, path, undefined, body),
|
|
7087
|
+
credential.credential_id,
|
|
7088
|
+
);
|
|
7089
|
+
|
|
7090
|
+
const zkpModeEnabled = params.longevityMode ?? params.zkpLongevityMode?.enabled ?? false;
|
|
7091
|
+
let zkp: ZkpVerifyResult | undefined;
|
|
7092
|
+
if (zkpModeEnabled) {
|
|
7093
|
+
try {
|
|
7094
|
+
const generatedProof =
|
|
7095
|
+
(await params.zkpLongevityMode?.generateProof?.({
|
|
7096
|
+
didUri: did.did_uri ?? didUri,
|
|
7097
|
+
xrplAddress: walletProof.address,
|
|
7098
|
+
credentialId: credential.credential_id,
|
|
7099
|
+
e2eePubkeyCommitment: commitment,
|
|
7100
|
+
})) ??
|
|
7101
|
+
(await generateE2eeZkProof(
|
|
7102
|
+
{
|
|
7103
|
+
didUri: did.did_uri ?? didUri,
|
|
7104
|
+
xrplAddress: walletProof.address,
|
|
7105
|
+
credentialId: credential.credential_id,
|
|
7106
|
+
e2eePrivateKey: params.zkpLongevityMode?.e2eePrivateKey ?? commitment,
|
|
7107
|
+
},
|
|
7108
|
+
{
|
|
7109
|
+
wasmPath: "/circuits/e2ee_credential.wasm",
|
|
7110
|
+
zkeyPath: "/circuits/e2ee_credential.zkey",
|
|
7111
|
+
},
|
|
7112
|
+
));
|
|
7113
|
+
|
|
7114
|
+
zkp = await this.http.authedRequest<ZkpVerifyResult>(Method.Post, "/zkp/verify", undefined, {
|
|
7115
|
+
proof: generatedProof.proof,
|
|
7116
|
+
public_signals: generatedProof.publicSignals,
|
|
7117
|
+
e2ee_pubkey_commitment: commitment,
|
|
7118
|
+
});
|
|
7119
|
+
} catch (error) {
|
|
7120
|
+
if (params.zkpLongevityMode?.strict) {
|
|
7121
|
+
throw error;
|
|
7122
|
+
}
|
|
7123
|
+
zkp = { valid: false, reason: "zkp_verification_skipped" };
|
|
7124
|
+
}
|
|
7125
|
+
}
|
|
7126
|
+
|
|
7127
|
+
const metadata: DidCredentialMetadata = {
|
|
7128
|
+
didUri: did.did_uri ?? didUri,
|
|
7129
|
+
credentialId: credential.credential_id,
|
|
7130
|
+
issuedAt: Date.now(),
|
|
7131
|
+
};
|
|
7132
|
+
storeDidCredentialMetadata(loginResponse.user_id, metadata);
|
|
7133
|
+
await this.persistDidDeviceBinding({
|
|
7134
|
+
didUri: metadata.didUri,
|
|
7135
|
+
credentialId: metadata.credentialId,
|
|
7136
|
+
e2eePubkeyCommitment: commitment,
|
|
7137
|
+
xrplAddress: walletProof.address,
|
|
7138
|
+
network,
|
|
7139
|
+
});
|
|
7140
|
+
|
|
7141
|
+
return {
|
|
7142
|
+
...loginResponse,
|
|
7143
|
+
did,
|
|
7144
|
+
didDocument,
|
|
7145
|
+
credential,
|
|
7146
|
+
credentialVerification,
|
|
7147
|
+
metadata,
|
|
7148
|
+
zkp,
|
|
7149
|
+
};
|
|
7150
|
+
}
|
|
7151
|
+
|
|
7152
|
+
public getStoredDidCredentialMetadata(userId: string): DidCredentialMetadata | null {
|
|
7153
|
+
return loadDidCredentialMetadata(userId);
|
|
7154
|
+
}
|
|
7155
|
+
|
|
7156
|
+
private async createE2eeCommitment(seed: string): Promise<string> {
|
|
7157
|
+
if (!globalThis.crypto?.subtle) {
|
|
7158
|
+
return encodeUnpaddedBase64Url(new TextEncoder().encode(seed));
|
|
7159
|
+
}
|
|
7160
|
+
const digest = await globalThis.crypto.subtle.digest("SHA-256", new TextEncoder().encode(seed));
|
|
7161
|
+
return Array.from(new Uint8Array(digest))
|
|
7162
|
+
.map((b) => b.toString(16).padStart(2, "0"))
|
|
7163
|
+
.join("");
|
|
7164
|
+
}
|
|
7165
|
+
|
|
7166
|
+
private async persistDidDeviceBinding(binding: {
|
|
7167
|
+
didUri: string;
|
|
7168
|
+
credentialId: string;
|
|
7169
|
+
e2eePubkeyCommitment: string;
|
|
7170
|
+
xrplAddress: string;
|
|
7171
|
+
network: string;
|
|
7172
|
+
}): Promise<void> {
|
|
7173
|
+
try {
|
|
7174
|
+
await this.setAccountData(WALLET_IDENTITY_ACCOUNT_DATA_TYPE, {
|
|
7175
|
+
chain_id: "xrpl",
|
|
7176
|
+
account_id: binding.xrplAddress,
|
|
7177
|
+
network: binding.network,
|
|
7178
|
+
did_uri: binding.didUri,
|
|
7179
|
+
credential_id: binding.credentialId,
|
|
7180
|
+
e2ee_pubkey_commitment: binding.e2eePubkeyCommitment,
|
|
7181
|
+
});
|
|
7182
|
+
} catch (error) {
|
|
7183
|
+
logger.warn("Failed to persist DID/E2EE device binding metadata", error);
|
|
7184
|
+
}
|
|
7185
|
+
}
|
|
7186
|
+
|
|
7000
7187
|
/**
|
|
7001
7188
|
* Store a chain-agnostic wallet recovery envelope in account data.
|
|
7002
7189
|
*
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2026 Xurge Digital Lab
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/* Mermaid flow reference:
|
|
12
|
+
flowchart LR
|
|
13
|
+
wallet[Connect Wallet] --> login[Submit wallet proof to /login]
|
|
14
|
+
login --> did[Resolve/Create DID]
|
|
15
|
+
did --> cred[Request CredentialCreate]
|
|
16
|
+
cred --> done[Persist DID + credential metadata]
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
export interface LoginStepperProps {
|
|
20
|
+
currentStep: 1 | 2 | 3 | 4 | 5;
|
|
21
|
+
longevityModeEnabled?: boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Lightweight JSX component for apps consuming the SDK.
|
|
26
|
+
*/
|
|
27
|
+
export function LoginStepper({ currentStep, longevityModeEnabled = false }: LoginStepperProps): string {
|
|
28
|
+
const longevityToggle = `${longevityModeEnabled ? "[x]" : "[ ]"} Enable Longevity Mode (ZKP)`;
|
|
29
|
+
const warning = longevityModeEnabled
|
|
30
|
+
? "ZKP path enabled: strongest DID-bound E2EE continuity."
|
|
31
|
+
: "Warning: continuing without ZKP uses wallet-signature fallback only.";
|
|
32
|
+
const steps = [
|
|
33
|
+
"1. Wallet Proof",
|
|
34
|
+
"2. DID Resolve/Create",
|
|
35
|
+
"3. Credential Create",
|
|
36
|
+
"4. Persist Metadata",
|
|
37
|
+
"5. ZKP Longevity Verify (optional)",
|
|
38
|
+
];
|
|
39
|
+
const renderedSteps = steps
|
|
40
|
+
.map((step, idx) => {
|
|
41
|
+
if (idx === 4 && !longevityModeEnabled) {
|
|
42
|
+
return `[ ] ${step}`;
|
|
43
|
+
}
|
|
44
|
+
const active = idx + 1 <= currentStep ? "[x]" : "[ ]";
|
|
45
|
+
return `${active} ${step}`;
|
|
46
|
+
})
|
|
47
|
+
.join("\n");
|
|
48
|
+
|
|
49
|
+
return `${longevityToggle}\n${warning}\n${renderedSteps}`;
|
|
50
|
+
}
|
package/src/credits.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { BriijClient } from "./client.ts";
|
|
2
|
+
|
|
3
|
+
export interface McreditPackage {
|
|
4
|
+
id: number;
|
|
5
|
+
name: string;
|
|
6
|
+
description?: string;
|
|
7
|
+
credits_amount: number;
|
|
8
|
+
price_usd_cents: number;
|
|
9
|
+
sort_order?: number;
|
|
10
|
+
is_active?: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class CreditsClient {
|
|
14
|
+
constructor(private matrixClient: BriijClient) {}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Public endpoint: Get packages available for purchase
|
|
18
|
+
*/
|
|
19
|
+
async getPackages(): Promise<McreditPackage[]> {
|
|
20
|
+
const baseUrl = this.matrixClient.getHomeserverUrl();
|
|
21
|
+
const response = await fetch(`${baseUrl}/_briij/credits`, {
|
|
22
|
+
method: "GET",
|
|
23
|
+
headers: {
|
|
24
|
+
"Content-Type": "application/json",
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
if (!response.ok) {
|
|
29
|
+
throw new Error(`Failed to fetch mCredits packages: ${response.status}`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return response.json();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Admin only: Get all packages (including inactive) for admin UI
|
|
37
|
+
* Uses the current Matrix access token for authentication
|
|
38
|
+
*/
|
|
39
|
+
async getAdminPackages(): Promise<McreditPackage[]> {
|
|
40
|
+
const baseUrl = this.matrixClient.getHomeserverUrl();
|
|
41
|
+
const accessToken = this.matrixClient.getAccessToken();
|
|
42
|
+
|
|
43
|
+
const response = await fetch(`${baseUrl}/_briij/admin/credits/packages`, {
|
|
44
|
+
method: "GET",
|
|
45
|
+
headers: {
|
|
46
|
+
"Content-Type": "application/json",
|
|
47
|
+
Authorization: `Bearer ${accessToken}`,
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
if (!response.ok) {
|
|
52
|
+
if (response.status === 403) {
|
|
53
|
+
throw new Error("Admin access required");
|
|
54
|
+
}
|
|
55
|
+
throw new Error(`Failed to fetch admin packages: ${response.status}`);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return response.json();
|
|
59
|
+
}
|
|
60
|
+
}
|