accounts
Version:
Tempo Accounts SDK
70 lines • 2.81 kB
JavaScript
import { Provider } from 'ox';
import { Account as TempoAccount } from 'viem/tempo';
/**
* Maps a stored account's key type to the signature key type used on the wire.
*
* The node sizes intrinsic gas in `eth_fillTransaction` from the request's
* `keyType`. Store-only key types (`webCrypto`, `webAuthn_headless`) sign with
* a `p256` / `webAuthn` signature envelope respectively, so they must be
* reported as their on-chain signature type — otherwise gas is underestimated.
*/
export function signatureKeyType(account) {
switch (account?.keyType) {
case 'secp256k1':
case 'p256':
case 'webAuthn':
return account.keyType;
case 'webCrypto':
return 'p256';
case 'webAuthn_headless':
return 'webAuthn';
default:
return undefined;
}
}
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