accounts
Version:
Tempo Accounts SDK
60 lines • 2.51 kB
JavaScript
import { Provider } from 'ox';
import { Account as TempoAccount } from 'viem/tempo';
import * as core_AccessKey from './AccessKey.js';
export function find(options) {
const { accessKey = true, address, signable = false, store } = options;
const { accounts, activeAccount } = store.getState();
const activeAddr = accounts[activeAccount]?.address;
const root = address
? accounts.find((a) => a.address.toLowerCase() === address.toLowerCase())
: accounts.find((a) => activeAddr && a.address.toLowerCase() === activeAddr.toLowerCase());
if (!root)
throw address
? new Provider.UnauthorizedError({ message: `Account "${address}" not found.` })
: new Provider.DisconnectedError({ message: 'No active account.' });
// When accessKey is requested, prefer a locally-signable access key for this address.
if (accessKey) {
const account = core_AccessKey.selectAccount({
address: root.address,
calls: options.calls,
chainId: options.chainId ?? store.getState().chainId,
store,
});
if (account)
return account;
}
return hydrate(root, { signable });
}
export function hydrate(account, options = {}) {
const { signable = false } = options;
if (!signable)
return {
address: account.address,
type: 'json-rpc',
...(account.keyType ? { keyType: account.keyType } : {}),
};
if ('sign' in account && typeof account.sign === 'function')
return account;
if (!account.keyType)
throw new Provider.UnauthorizedError({ message: `Account "${account.address}" cannot sign.` });
switch (account.keyType) {
case 'secp256k1':
return TempoAccount.fromSecp256k1(account.privateKey);
case 'p256':
return TempoAccount.fromP256(account.privateKey);
case 'webCrypto':
return TempoAccount.fromWebCryptoP256(account.keyPair);
case 'webAuthn':
return TempoAccount.fromWebAuthnP256(account.credential, {
rpId: account.credential.rpId,
});
case 'webAuthn_headless':
return TempoAccount.fromHeadlessWebAuthn(account.privateKey, {
rpId: account.rpId,
origin: account.origin,
});
default:
throw new Provider.UnauthorizedError({ message: 'Unknown key type.' });
}
}
//# sourceMappingURL=Account.js.map