UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

9 lines • 8.92 kB
import{o as getAsyncWorkSignal}from"./async-work-scope-DBcxYU4O.mjs";import{n as sanitizeTerminalText}from"./safe-text-B_QTgn0s.mjs";import{l as displayFingerprint,u as fingerprint}from"./validate-Btv7lFe1.mjs";import{R as signRebind}from"./src-BTTzpNjb.mjs";import{n as throwExpectedCliError}from"./expected-cli-error-oDVmXYil.mjs";import{n as callGatewayFromCli}from"./gateway-rpc-17pTfL8i.mjs";import{r as promptYesNo}from"./prompt-BGeJBE18.mjs";import{t as readByteStreamWithLimit}from"./read-byte-stream-with-limit-Ockoa332.mjs";import{t as applyParentDefaultHelpAction}from"./parent-default-help-BQhbR5_q.mjs";import{setTimeout}from"node:timers/promises";import{password}from"@clack/prompts"; //!src/cli/id-cli-rebind.ts async function displayKey(e){return displayFingerprint(await fingerprint(e))}async function describeRebind(e,t){return[`Move ${e.hostname} to new keys?`,` Owner root: ${await displayKey(e.root_pub)} (matches your recovery kit)`,` Binding: ${e.current.binding_seq} -> ${t.body.binding_seq}`,` Endpoint key: ${await displayKey(e.current.k_srv)} -> ${await displayKey(e.k_srv)}`,` Gateway key: ${await displayKey(e.current.k_gw)} -> ${await displayKey(e.k_gw)}`,`Your devices, people, nodes and invites stay. Every connected device disconnects for a`,`moment and reconnects by itself.`].join(` `)} //!src/cli/id-cli.ts const DEFAULT_TIMEOUT_MS=6e4;const PUBLICATION_WAIT_MS=3e4;const KIT_MAX_BYTES=1024;function addIdGatewayOptions(e){return e.option(`--url <url>`,`Gateway WebSocket URL (must be this machine's Gateway)`).option(`--token <token>`,`Gateway token (if required)`).option(`--timeout <ms>`,`Timeout in ms`,String(DEFAULT_TIMEOUT_MS)).option(`--json`,`Output JSON`,false)}const authority=(e,t)=>callGatewayFromCli(`openclawId.authority`,e,t,{scopes:[`operator.admin`]});async function readPeopleLink(e,t){return(await callGatewayFromCli(`openclawId.people`,e,{},{scopes:[`operator.admin`],signal:t})).link}function describeKeysState(e){if(e?.state!==`keys_refused`)return`Remote access: ${e?.state??`unknown`}`;return e.reason===`key_mismatch`?`Remote access is off: the connection key on this computer isn't the one your devices trust. Restore the original key file, or move the claw to new keys with your recovery kit: openclaw id rebind`:`Remote access is off: this claw's connection keys are missing from this computer, so your devices can't reach it. Move it to new keys with your recovery kit: openclaw id rebind`}function parseBindingSeq(e){if(e===void 0)return;const t=Number(e);if(!Number.isSafeInteger(t)||t<1)throwExpectedCliError(`--binding-seq must be a positive whole number.`);return t}async function readLinkCode(e){if(e.code?.trim())return e.code.trim();if(e.kitStdin)throwExpectedCliError(`--kit-stdin needs --code <link-code> from your OpenClaw ID account.`);const t=await password({message:`Link code from your OpenClaw ID account (Add a claw page, claws.<domain>/link)`,mask:``});if(typeof t===`symbol`||!t.trim())throw new Error(`Cancelled.`);return t.trim()}function describeSignRefusal(e,t){switch(e.code){case`wrong_kit`:return`This recovery kit is for another claw (${e.kitRoot}). Use the kit made for ${sanitizeTerminalText(t)}.`;case`published_unverifiable`:return`${e.detail}. Nothing was signed. If you trust this computer's copy, pass --binding-seq with a number above every binding you know of.`;case`published_unavailable`:return`Couldn't read the binding Discovery publishes (${sanitizeTerminalText(e.detail)}). Try again, or pass --binding-seq ${e.atLeast} or higher.`;case`binding_seq_too_low`:return`--binding-seq must be at least ${e.atLeast}.`;default:return e}}async function readKit(e){if(e){const e=await readByteStreamWithLimit(process.stdin,{maxBytes:KIT_MAX_BYTES});return Buffer.from(e).toString(`utf8`)}const t=await password({message:`Recovery kit (OCK1-…)`,mask:``});if(typeof t===`symbol`)throw new Error(`Cancelled.`);return t}function write(e,t,n){process.stdout.write(e.json?`${JSON.stringify(t,null,2)}\n`:`${n}\n`)}async function runStatus(e,t){const n=await callGatewayFromCli(`openclawId.status`,e,{},{scopes:[`operator.read`],signal:t});const r=n.link?.linked?await readPeopleLink(e,t):void 0;const i=[describeKeysState(n.status)];if(n.link?.hostname)i.push(`Claw: ${sanitizeTerminalText(n.link.hostname)}`);if(r?.binding)i.push(`Binding: ${r.binding.bindingSeq} (Discovery has ${r.binding.publishedBindingSeq})`,`Endpoint key: ${await displayKey(r.binding.kSrv)}`,`Gateway key: ${await displayKey(r.binding.kGw)}`);const a=r?.publication?.superseded;if(r?.publication?.code===`binding_superseded`&&a)i.push(`Publication: Discovery lists newer keys for this claw (binding ${a.publishedBindingSeq}; this computer has ${a.bindingSeq}) that this computer doesn't have.`,` Published endpoint key: ${await displayKey(a.kSrv)}`,` Published Gateway key: ${await displayKey(a.kGw)}`,` If you restored this computer from a backup, run openclaw id rebind.`,` If you didn't, someone may have your recovery kit: run openclaw id rebind now. If that fails because your account is also compromised, relink this claw as a new claw from this computer.`);else if(r?.publication?.code===`needs_link_code`)i.push(`Publication: the new keys wait for a link code from your OpenClaw ID account: openclaw id publish --code <link-code>`);else if(r?.publication?.state)i.push(`Publication: ${r.publication.state}${r.publication.code?` (${r.publication.code})`:``}`);if(!t?.aborted)write(e,{...n,people:r??null},i.join(` `))}async function waitForPublication(e,t){const n=Date.now()+PUBLICATION_WAIT_MS;while(Date.now()<n){if(((await readPeopleLink(e))?.binding?.publishedBindingSeq??0)>=t)return true;await setTimeout(1e3)}return false}function writeCommittedWarning(e,t,n,r){const i=sanitizeTerminalText(n instanceof Error?n.message:String(n));write(e,{moved:true,bindingSeq:t,published:null,[r===`follow-up`?`followUpError`:`publicationError`]:i,nextStep:`openclaw id status`},[`Moved to new keys (committed).`,`${r===`follow-up`?`Follow-up failed`:`Publication check failed`}: ${i}`,`Run openclaw id status to check remote access and publication. Do not repeat rebind: it rotates keys again.`].join(` `));process.exitCode=2}async function runRebind(e){if(e.cancel){await authority(e,{action:`rebind_cancel`});write(e,{cancelled:true},`Cancelled: the keys staged for the move were deleted.`);return}if(e.yes&&!e.kitStdin)throwExpectedCliError(`--yes needs --kit-stdin: type the recovery kit at the prompt otherwise.`);const t=parseBindingSeq(e.bindingSeq);const n=await readLinkCode(e);const r=await authority(e,{action:`rebind_prepare`,code:n});const i=await signRebind(r,await readKit(e.kitStdin===true),{bindingSeq:t});if(!i.ok)throwExpectedCliError(describeSignRefusal(i,r.hostname));if(!e.yes){process.stdout.write(`${await describeRebind(r,i.binding)}\n`);if(!await promptYesNo(`Move to new keys?`)){write(e,{moved:false},`Not moved. The staged keys stay for the next try (openclaw id rebind --cancel deletes them).`);return}}const a=await authority(e,{action:`rebind_commit`,binding:i.binding,code:n});if(a.followUpError!==void 0){writeCommittedWarning(e,a.bindingSeq,a.followUpError,`follow-up`);return}let o;try{o=await waitForPublication(e,a.bindingSeq)}catch(t){writeCommittedWarning(e,a.bindingSeq,t,`publication`);return}write(e,{moved:true,bindingSeq:a.bindingSeq,published:o},[`Moved to new keys. Devices reconnect by themselves.`,o?`Discovery has the new keys.`:`Discovery doesn't have the new keys yet; the Gateway keeps publishing them (openclaw id status).`,`If you did this because you think a key leaked, also check Your devices and People.`].join(` `))}function registerIdCli(e){const t=e.command(`id`).description(`OpenClaw ID remote access of this machine's Gateway`);addIdGatewayOptions(t.command(`status`).description(`Show remote access, the claw's keys and publication`)).action(async e=>{const t=getAsyncWorkSignal();try{await runStatus(e,t)}catch(e){if(!t?.aborted)throw e}});addIdGatewayOptions(t.command(`rebind`).description(`Move this claw to new keys with its recovery kit (on this machine only)`).option(`--kit-stdin`,`Read the recovery kit from stdin instead of a hidden prompt`,false).option(`--code <link-code>`,`One-time link code from your OpenClaw ID account`).option(`--binding-seq <n>`,`Binding number to sign when Discovery can't be read (above every known binding)`).option(`--yes`,`Skip the confirmation (needs --kit-stdin)`,false).option(`--cancel`,`Delete keys staged by an unfinished move`,false)).action(async e=>await runRebind(e));addIdGatewayOptions(t.command(`publish`).description(`Publish this claw to Discovery now`).option(`--code <code>`,`One-time link code from your OpenClaw ID account`)).action(async e=>{const t=await authority(e,{action:`publish`,...e.code?{enrollmentCode:e.code}:{}});write(e,t,JSON.stringify(t))});applyParentDefaultHelpAction(t)}export{registerIdCli};