duly / worker/src/keys.js · raw
// Signing for the Worker. Keys arrive as secrets in NEAR's "ed25519:<base58>" form// (64 bytes: seed then public key). They are never logged or returned.import { b58decode, concat, encodeSignedTransaction, encodeTransaction, sha256 } from '../../web/lib/near.js';// Fixed PKCS#8 header for a raw Ed25519 seed (RFC 8410).const PKCS8_PREFIX = Uint8Array.from([  0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, 0x04, 0x22, 0x04, 0x20,]);export async function loadKey(secret) {  if (!secret) throw new Error('Signing key is not configured');  const bytes = b58decode(secret.replace(/^ed25519:/, ''));  if (bytes.length !== 64) throw new Error('Signing key has the wrong length');  const key = await crypto.subtle.importKey('pkcs8', concat(PKCS8_PREFIX, bytes.slice(0, 32)), { name: 'Ed25519' }, false, ['sign']);  return { key, publicKey: bytes.slice(32) };}export async function signTransaction(signer, tx) {  const body = encodeTransaction(tx);  const sig = new Uint8Array(await crypto.subtle.sign({ name: 'Ed25519' }, signer.key, await sha256(body)));  return encodeSignedTransaction(body, sig);}