Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | 2x 224x 224x 121x 121x 60x 61x 224x 59x 59x 59x 59x 47x 47x 1x 46x 52x 52x 52x 52x 52x 6x 1x 5x 1x 4x 4x 1x 3x 3x 38x 17x 1x 16x 14x 24x 29x 29x 4x 4x 29x 73x 73x 1x 72x 17x 7x 7x 7x 7x | import { makeDefaultExo } from '@metamask/kernel-utils/exo';
import type { Logger } from '@metamask/logger';
import type { Baggage } from '@metamask/ocap-kernel';
import { DEFAULT_DELEGATION_MANAGER } from '../constants.ts';
import {
computeDelegationId,
makeDelegation,
prepareDelegationTypedData,
delegationMatchesAction,
explainDelegationMatch,
finalizeDelegation,
} from '../lib/delegation.ts';
import type {
Action,
Address,
CreateDelegationOptions,
Delegation,
DelegationMatchResult,
Eip712TypedData,
Hex,
} from '../types.ts';
const harden = globalThis.harden ?? (<T>(value: T): T => value);
/**
* Vat powers for the delegation vat.
*/
type VatPowers = {
logger?: Logger;
};
/**
* Build the root object for the delegation vat.
*
* The delegation vat manages Gator delegations: creating, storing,
* signing, matching, and revoking them.
*
* @param _vatPowers - Special powers granted to this vat.
* @param parameters - Initialization parameters.
* @param parameters.delegationManagerAddress - The delegation manager contract address.
* @param baggage - Root of vat's persistent state.
* @returns The root object for the delegation vat.
*/
export function buildRootObject(
_vatPowers: VatPowers,
parameters: { delegationManagerAddress?: Address } | undefined,
baggage: Baggage,
): object {
const delegationManagerAddress =
parameters?.delegationManagerAddress ?? DEFAULT_DELEGATION_MANAGER;
// Restore delegations from baggage
const delegations: Map<string, Delegation> = baggage.has('delegations')
? new Map(
Object.entries(
baggage.get('delegations') as Record<string, Delegation>,
),
)
: new Map();
/**
* Persist the current delegations map to baggage.
*/
function persistDelegations(): void {
const serialized = harden(Object.fromEntries(delegations));
if (baggage.has('delegations')) {
baggage.set('delegations', serialized);
} else {
baggage.init('delegations', serialized);
}
}
return makeDefaultExo('walletDelegation', {
async bootstrap(): Promise<void> {
// No services needed for the delegation vat
},
async createDelegation(
options: CreateDelegationOptions & { delegator: Address },
): Promise<Delegation> {
const delegation = harden(
makeDelegation({
delegator: options.delegator,
delegate: options.delegate,
caveats: options.caveats,
chainId: options.chainId,
...(options.salt ? { salt: options.salt } : {}),
}),
);
delegations.set(delegation.id, delegation);
persistDelegations();
return delegation;
},
async prepareDelegationForSigning(id: string): Promise<Eip712TypedData> {
const delegation = delegations.get(id);
if (!delegation) {
throw new Error(`Delegation not found: ${id}`);
}
return harden(
prepareDelegationTypedData({
delegation,
verifyingContract: delegationManagerAddress,
}),
);
},
async storeSigned(id: string, signature: Hex): Promise<void> {
const delegation = delegations.get(id);
Iif (!delegation) {
throw new Error(`Delegation not found: ${id}`);
}
const signed = harden(finalizeDelegation(delegation, signature));
delegations.set(id, signed);
persistDelegations();
},
async receiveDelegation(delegation: Delegation): Promise<void> {
if (delegation.status !== 'signed') {
throw new Error('Can only receive signed delegations');
}
if (!delegation.signature) {
throw new Error('Delegation has no signature');
}
// Verify the delegation ID is consistent with the fields
const expectedId = computeDelegationId(delegation);
if (delegation.id !== expectedId) {
throw new Error('Delegation ID mismatch');
}
// Signature verification is skipped here. When the delegator is a
// smart account, the EIP-712 signature is made by the underlying
// EOA owner — ecrecover returns the EOA, not the smart account
// address. The on-chain DelegationManager performs the authoritative
// signature check during delegation redemption.
delegations.set(delegation.id, delegation);
persistDelegations();
},
async findDelegationForAction(
action: Action,
chainId?: number,
currentTime?: number,
): Promise<Delegation | undefined> {
for (const delegation of delegations.values()) {
if (chainId !== undefined && delegation.chainId !== chainId) {
continue;
}
if (delegationMatchesAction(delegation, action, currentTime)) {
return delegation;
}
}
return undefined;
},
async explainActionMatch(
action: Action,
chainId?: number,
currentTime?: number,
): Promise<{ delegationId: string; result: DelegationMatchResult }[]> {
const results: {
delegationId: string;
result: DelegationMatchResult;
}[] = [];
for (const delegation of delegations.values()) {
Iif (chainId !== undefined && delegation.chainId !== chainId) {
continue;
}
results.push({
delegationId: delegation.id,
result: explainDelegationMatch(delegation, action, currentTime),
});
}
return harden(results);
},
async getDelegation(id: string): Promise<Delegation> {
const delegation = delegations.get(id);
if (!delegation) {
throw new Error(`Delegation not found: ${id}`);
}
return harden(delegation);
},
async listDelegations(): Promise<Delegation[]> {
return harden([...delegations.values()]);
},
async revokeDelegation(id: string): Promise<void> {
const delegation = delegations.get(id);
Iif (!delegation) {
throw new Error(`Delegation not found: ${id}`);
}
delegations.set(
id,
harden({ ...delegation, status: 'revoked' as const }),
);
persistDelegations();
},
});
}
|