accounts
Version:
Tempo Accounts SDK
56 lines • 2.44 kB
JavaScript
import { Discovery, Wata, mobileWebAuth as core_mobileWebAuth } from 'wata';
import * as Keystore from '../../Keystore.js';
import { fromRequest } from '../internal/fromRequest.js';
/**
* Creates a mobile web auth adapter that forwards wallet RPC through Wata.
*
* Authentication opens a browser session and completes via an encrypted app-link
* callback carrying the wallet RPC response.
*/
export function mobileWebAuth(options) {
const { baseUrl, fetch, host, name, openAuthSession, rdns, redirectUri } = options;
// The host discovery document is fetched once and reused for the
// adapter's lifetime; a wallet rotating its document requires a new
// adapter instance to pick up.
let hostDocument;
function resolveHost() {
if (typeof host !== 'string')
return host;
hostDocument ??= Discovery.fetchHost(host, fetch !== undefined ? { fetch } : {}).catch((error) => {
hostDocument = undefined;
throw error;
});
return hostDocument;
}
return fromRequest({
// React Native may lack WebCrypto and persists through string-based
// storage, so this opts into pure-JS P-256 (the access key lives app-side
// and signs without a wallet round-trip).
keystores: { p256: Keystore.p256() },
name,
rdns,
async request(request) {
// Mobile web auth is single-exchange: one authorization URL carries one
// RPC request envelope, and one callback URL carries one response.
// The wallet learns this app's identity from the consumer discovery
// document served at `baseUrl`, not from session metadata.
const host = await resolveHost();
if (host === undefined)
throw new Error('`mobileWebAuth` requires a `host`.');
const wata = Wata.create({
baseUrl,
transports: [
core_mobileWebAuth({
callback: redirectUri,
host,
openAuthSession,
...(fetch !== undefined ? { fetch } : {}),
}),
],
});
const session = wata.start();
return (await session.send({ method: request.method, params: request.params ?? [] })).result;
},
});
}
//# sourceMappingURL=mobileWebAuth.js.map