UNPKG

@standard-crypto/farcaster-js-hub-rest

Version:

A tool for interacting with the REST API of any Farcaster hub.

57 lines 1.99 kB
import { ViemLocalEip712Signer, NobleEd25519Signer } from '@farcaster/core'; import { privateKeyToAccount, mnemonicToAccount } from 'viem/accounts'; import { createPublicClient, http } from 'viem'; import { mainnet } from 'viem/chains'; export function hexToSigner(signerHex) { let privateKeyBytes; if (signerHex.startsWith('0x')) { privateKeyBytes = hexToBytes(signerHex.slice(2)); } else { privateKeyBytes = hexToBytes(signerHex); } return new NobleEd25519Signer(privateKeyBytes); } export function eip712SignerFromMnemonicOrPrivateKey(mnemonicOrPrivateKey) { let account; if (mnemonicOrPrivateKey.startsWith('0x')) { account = privateKeyToAccount(mnemonicOrPrivateKey); } else if (mnemonicOrPrivateKey.split(' ').length === 1) { account = privateKeyToAccount(`0x${mnemonicOrPrivateKey}`); } else { account = mnemonicToAccount(mnemonicOrPrivateKey); } return new ViemLocalEip712Signer(account); } export function hexToBytes(hex) { if (typeof hex !== 'string') { throw new Error('hex string expected, got ' + typeof hex); } const len = hex.length; // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions if (len % 2) { throw new Error(`padded hex string expected, got un-padded hex of length ${len}`); } const array = new Uint8Array(len / 2); for (let i = 0; i < array.length; i++) { const j = i * 2; const hexByte = hex.slice(j, j + 2); const byte = Number.parseInt(hexByte, 16); if (Number.isNaN(byte) || byte < 0) { throw new Error('Invalid byte sequence'); } array[i] = byte; } return array; } export async function getLatestBlock() { const publicClient = createPublicClient({ chain: mainnet, transport: http(), }); const latestBlock = await publicClient.getBlock(); return latestBlock.hash; } //# sourceMappingURL=utils.js.map