accounts
Version:
Tempo Accounts SDK
48 lines • 2.05 kB
JavaScript
import { Provider } from 'ox';
import { Account as TempoAccount } from 'viem/tempo';
export function find(options) {
const { 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.' });
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