123 lines
3.8 KiB
TypeScript
123 lines
3.8 KiB
TypeScript
|
|
import type { PreKeyBundle, OneTimePreKey } from '@shade/core';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* HTTP transport client for the Shade Prekey Server.
|
||
|
|
*
|
||
|
|
* Usage:
|
||
|
|
* ```ts
|
||
|
|
* const transport = new ShadeFetchTransport('https://shade.example.com');
|
||
|
|
* await transport.register('alice', bundle, oneTimePreKeys);
|
||
|
|
* const bundle = await transport.fetchBundle('bob');
|
||
|
|
* ```
|
||
|
|
*/
|
||
|
|
export class ShadeFetchTransport {
|
||
|
|
constructor(
|
||
|
|
private readonly baseUrl: string,
|
||
|
|
private readonly authToken?: string,
|
||
|
|
) {}
|
||
|
|
|
||
|
|
private headers(): Record<string, string> {
|
||
|
|
const h: Record<string, string> = { 'Content-Type': 'application/json' };
|
||
|
|
if (this.authToken) h['Authorization'] = `Bearer ${this.authToken}`;
|
||
|
|
return h;
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Register identity and upload prekey bundle + one-time prekeys */
|
||
|
|
async register(
|
||
|
|
address: string,
|
||
|
|
identity: { signingKey: Uint8Array; dhKey: Uint8Array },
|
||
|
|
signedPreKey: { keyId: number; publicKey: Uint8Array; signature: Uint8Array },
|
||
|
|
oneTimePreKeys?: OneTimePreKey[],
|
||
|
|
): Promise<void> {
|
||
|
|
const body: any = {
|
||
|
|
address,
|
||
|
|
identitySigningKey: toB64(identity.signingKey),
|
||
|
|
identityDHKey: toB64(identity.dhKey),
|
||
|
|
signedPreKey: {
|
||
|
|
keyId: signedPreKey.keyId,
|
||
|
|
publicKey: toB64(signedPreKey.publicKey),
|
||
|
|
signature: toB64(signedPreKey.signature),
|
||
|
|
},
|
||
|
|
};
|
||
|
|
|
||
|
|
if (oneTimePreKeys?.length) {
|
||
|
|
body.oneTimePreKeys = oneTimePreKeys.map((k) => ({
|
||
|
|
keyId: k.keyId,
|
||
|
|
publicKey: toB64(k.keyPair.publicKey),
|
||
|
|
}));
|
||
|
|
}
|
||
|
|
|
||
|
|
const res = await fetch(`${this.baseUrl}/v1/keys/register`, {
|
||
|
|
method: 'POST',
|
||
|
|
headers: this.headers(),
|
||
|
|
body: JSON.stringify(body),
|
||
|
|
});
|
||
|
|
if (!res.ok) throw new Error(`Register failed: ${res.status}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Fetch a prekey bundle for a peer (consumes one one-time prekey) */
|
||
|
|
async fetchBundle(address: string): Promise<PreKeyBundle> {
|
||
|
|
const res = await fetch(`${this.baseUrl}/v1/keys/bundle/${encodeURIComponent(address)}`, {
|
||
|
|
headers: this.headers(),
|
||
|
|
});
|
||
|
|
if (!res.ok) throw new Error(`Fetch bundle failed: ${res.status}`);
|
||
|
|
|
||
|
|
const data = await res.json();
|
||
|
|
return {
|
||
|
|
registrationId: data.registrationId ?? 0,
|
||
|
|
identitySigningKey: fromB64(data.identitySigningKey),
|
||
|
|
identityDHKey: fromB64(data.identityDHKey),
|
||
|
|
signedPreKey: {
|
||
|
|
keyId: data.signedPreKey.keyId,
|
||
|
|
publicKey: fromB64(data.signedPreKey.publicKey),
|
||
|
|
signature: fromB64(data.signedPreKey.signature),
|
||
|
|
},
|
||
|
|
oneTimePreKey: data.oneTimePreKey
|
||
|
|
? {
|
||
|
|
keyId: data.oneTimePreKey.keyId,
|
||
|
|
publicKey: fromB64(data.oneTimePreKey.publicKey),
|
||
|
|
}
|
||
|
|
: undefined,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Upload additional one-time prekeys */
|
||
|
|
async replenish(
|
||
|
|
address: string,
|
||
|
|
keys: Array<{ keyId: number; publicKey: Uint8Array }>,
|
||
|
|
): Promise<number> {
|
||
|
|
const res = await fetch(`${this.baseUrl}/v1/keys/replenish`, {
|
||
|
|
method: 'POST',
|
||
|
|
headers: this.headers(),
|
||
|
|
body: JSON.stringify({
|
||
|
|
address,
|
||
|
|
oneTimePreKeys: keys.map((k) => ({
|
||
|
|
keyId: k.keyId,
|
||
|
|
publicKey: toB64(k.publicKey),
|
||
|
|
})),
|
||
|
|
}),
|
||
|
|
});
|
||
|
|
if (!res.ok) throw new Error(`Replenish failed: ${res.status}`);
|
||
|
|
const data = await res.json();
|
||
|
|
return data.remaining;
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Get remaining one-time prekey count */
|
||
|
|
async getKeyCount(address: string): Promise<number> {
|
||
|
|
const res = await fetch(`${this.baseUrl}/v1/keys/count/${encodeURIComponent(address)}`, {
|
||
|
|
headers: this.headers(),
|
||
|
|
});
|
||
|
|
if (!res.ok) throw new Error(`Count failed: ${res.status}`);
|
||
|
|
const data = await res.json();
|
||
|
|
return data.count;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function toB64(bytes: Uint8Array): string {
|
||
|
|
return Buffer.from(bytes).toString('base64');
|
||
|
|
}
|
||
|
|
|
||
|
|
function fromB64(str: string): Uint8Array {
|
||
|
|
return new Uint8Array(Buffer.from(str, 'base64'));
|
||
|
|
}
|