UNPKG

mdip-agent-kit

Version:

Give any AI agent its own MDIP (did:mdip) identity on the SELF (https://selfid.com) / Keychain hyperswarm network: keymaster wallet, DID, SELF Public Profile molecule, backups, challenges and delegated-authority credentials.

20 lines (15 loc) • 1.13 kB
// Delegated authority: a human issues a credential to the agent's DID from their SELF app // (e.g. "may purchase on my behalf, up to $X, until <date>"). The agent accepts it, then checks // it before acting, and can prove it to a merchant via a challenge/response. import { openAgent } from 'mdip-agent-kit'; const HUMAN_DID = process.env.HUMAN_DID; // the principal's DID (from their SELF app) const PURCHASE_SCHEMA = process.env.PURCHASE_SCHEMA; // schema DID of the authorization template const agent = await openAgent(); // 1. once, when the human sends you the credential DID: if (process.argv[2]) await agent.acceptCredential(process.argv[2]); // 2. before every purchase: const grants = await agent.findCredentials({ schema: PURCHASE_SCHEMA, issuer: HUMAN_DID }); if (!grants.length) throw new Error('no valid purchase authorization from principal'); console.log('authorized by', HUMAN_DID, 'terms:', grants[0].credential); // 3. a merchant can verify it independently: they create a challenge requiring // { schema: PURCHASE_SCHEMA, issuers: [HUMAN_DID] } and the agent answers with respondToChallenge().