@river-build/sdk
Version:
For more details, visit the following resources:
48 lines • 2.4 kB
JavaScript
import { makeAuthenticationRpcClient } from './makeAuthenticationRpcClient';
import { makeNotificationRpcClient } from './makeNotificationRpcClient';
import { bin_fromHexString, check } from '@river-build/dlog';
import { notificationServiceHash, riverSign } from './sign';
import { isDefined } from './check';
import { hashPersonalMessage } from '@ethereumjs/util';
export class NotificationService {
static async _authenticateCommon(userId, serviceUrl, opts, getSignature, extraFinishAuthParams) {
const authenticationRpcClient = makeAuthenticationRpcClient(serviceUrl, opts);
const startResponse = await authenticationRpcClient.startAuthentication({ userId });
check(startResponse.challenge.length >= 16, 'challenge must be 16 bytes');
check(isDefined(startResponse.expiration), 'expiration must be defined');
const hash = notificationServiceHash(userId, startResponse.expiration.seconds, startResponse.challenge);
const signature = await getSignature(hash);
const finishResponse = await authenticationRpcClient.finishAuthentication({
userId,
challenge: startResponse.challenge,
signature,
...extraFinishAuthParams,
});
const notificationRpcClient = makeNotificationRpcClient(serviceUrl, finishResponse.sessionToken, opts);
return {
startResponse,
finishResponse,
notificationRpcClient,
};
}
static async authenticate(signerContext, serviceUrl, opts) {
const userId = signerContext.creatorAddress;
return this._authenticateCommon(userId, serviceUrl, opts, async (hashSrc) => {
const hash = hashPersonalMessage(hashSrc);
return await riverSign(hash, signerContext.signerPrivateKey());
}, {
delegateSig: signerContext.delegateSig,
delegateExpiryEpochMs: signerContext.delegateExpiryEpochMs,
});
}
static async authenticateWithSigner(userId, signer, serviceUrl, opts) {
if (typeof userId === 'string') {
userId = bin_fromHexString(userId);
}
return this._authenticateCommon(userId, serviceUrl, opts, async (hash) => {
const sigHex = await signer.signMessage(hash);
return bin_fromHexString(sigHex);
}, {});
}
}
//# sourceMappingURL=notificationService.js.map