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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | 1x 2x 25x 1x 25x 9x 9x 4x 5x 25x 15x 15x 15x 3x 15x 15x 3x 15x 15x 10x 10x 10x 2x 10x 2x 10x 10x 7x 7x 2x 2x 6x | import { makeDefaultExo } from '@metamask/kernel-utils/exo';
import type { Baggage } from '@metamask/ocap-kernel';
import { create } from '@metamask/superstruct';
import {
ENFORCER_CONTRACT_KEY_MAP,
PLACEHOLDER_CONTRACTS,
registerChainContracts,
} from '../constants.ts';
import type { ChainContracts } from '../constants.ts';
import {
makeCaveat,
encodeValueLte,
encodeNativeTokenTransferAmount,
encodeAllowedTargets,
encodeAllowedMethods,
encodeErc20TransferAmount,
encodeAllowedCalldata,
} from '../lib/caveats.ts';
import { makeDelegation, makeSaltGenerator } from '../lib/delegation.ts';
import { ERC20_TRANSFER_SELECTOR, FIRST_ARG_OFFSET } from '../lib/erc20.ts';
import type {
Address,
DelegationGrant,
Hex,
TransferFungibleGrant,
TransferNativeGrant,
} from '../types.ts';
import { DelegationGrantStruct } from '../types.ts';
const harden = globalThis.harden ?? (<T>(value: T): T => value);
/**
* ABI-encode an Ethereum address as a 32-byte padded hex value.
*
* @param address - The Ethereum address to encode.
* @returns A 0x-prefixed 64-character hex string.
*/
function abiEncodeAddress(address: Address): Hex {
return `0x${address.slice(2).toLowerCase().padStart(64, '0')}`;
}
/**
* Build the root object for the delegator vat.
*
* @param _vatPowers - Special powers granted to this vat (unused).
* @param _parameters - Initialization parameters (unused).
* @param baggage - Root of vat's persistent state.
* @returns The root object for the delegator vat.
*/
export function buildRootObject(
_vatPowers: unknown,
_parameters: unknown,
baggage: Baggage,
): object {
const grants: Map<string, DelegationGrant> = baggage.has('grants')
? new Map(
Object.entries(baggage.get('grants') as Record<string, unknown>).map(
([id, raw]) => [id, create(raw, DelegationGrantStruct)],
),
)
: new Map();
const saltGenerator = makeSaltGenerator();
/**
* Persist grants map to baggage (handles both init and update).
*/
function persistGrants(): void {
const serialized = harden(Object.fromEntries(grants));
if (baggage.has('grants')) {
baggage.set('grants', serialized);
} else {
baggage.init('grants', serialized);
}
}
return makeDefaultExo('walletDelegator', {
async buildTransferNativeGrant(options: {
delegator: Address;
delegate: Address;
to?: Address;
maxAmount?: bigint;
totalLimit?: bigint;
chainId: number;
}): Promise<TransferNativeGrant> {
const { delegator, delegate, to, maxAmount, totalLimit, chainId } =
options;
const caveats = [];
if (to !== undefined) {
caveats.push(
makeCaveat({
type: 'allowedTargets',
terms: encodeAllowedTargets([to]),
chainId,
}),
);
}
Iif (totalLimit !== undefined) {
caveats.push(
makeCaveat({
type: 'nativeTokenTransferAmount',
terms: encodeNativeTokenTransferAmount(totalLimit),
chainId,
}),
);
}
if (maxAmount !== undefined) {
caveats.push(
makeCaveat({
type: 'valueLte',
terms: encodeValueLte(maxAmount),
chainId,
}),
);
}
const delegation = makeDelegation({
delegator,
delegate,
caveats,
chainId,
saltGenerator,
});
return harden({
method: 'transferNative',
...(to !== undefined && { to }),
...(maxAmount !== undefined && { maxAmount }),
...(totalLimit !== undefined && { totalLimit }),
delegation,
});
},
async buildTransferFungibleGrant(options: {
delegator: Address;
delegate: Address;
token: Address;
to?: Address;
totalLimit?: bigint;
chainId: number;
}): Promise<TransferFungibleGrant> {
const { delegator, delegate, token, to, totalLimit, chainId } = options;
const caveats = [
makeCaveat({
type: 'allowedTargets',
terms: encodeAllowedTargets([token]),
chainId,
}),
makeCaveat({
type: 'allowedMethods',
terms: encodeAllowedMethods([ERC20_TRANSFER_SELECTOR]),
chainId,
}),
];
if (totalLimit !== undefined) {
caveats.push(
makeCaveat({
type: 'erc20TransferAmount',
terms: encodeErc20TransferAmount({ token, amount: totalLimit }),
chainId,
}),
);
}
if (to !== undefined) {
caveats.push(
makeCaveat({
type: 'allowedCalldata',
terms: encodeAllowedCalldata({
dataStart: FIRST_ARG_OFFSET,
value: abiEncodeAddress(to),
}),
chainId,
}),
);
}
const delegation = makeDelegation({
delegator,
delegate,
caveats,
chainId,
saltGenerator,
});
return harden({
method: 'transferFungible',
token,
...(to !== undefined && { to }),
...(totalLimit !== undefined && { totalLimit }),
delegation,
});
},
/**
* Register contract addresses for a chain so caveat builders can look up
* enforcer addresses. Called by the home coordinator after configureBundler
* and on resuscitation so this vat's module-level Map stays in sync.
*
* @param chainId - The chain ID to register.
* @param environment - The deployed contract addresses for this chain.
* @param environment.DelegationManager - DelegationManager address.
* @param environment.caveatEnforcers - Enforcer contract addresses.
*/
async registerContracts(
chainId: number,
environment: {
DelegationManager: Hex;
caveatEnforcers?: Record<string, Hex>;
},
): Promise<void> {
const rawEnforcers = environment.caveatEnforcers ?? {};
const enforcers = { ...PLACEHOLDER_CONTRACTS.enforcers };
for (const [key, addr] of Object.entries(rawEnforcers)) {
const caveatType = ENFORCER_CONTRACT_KEY_MAP[key];
if (caveatType !== undefined) {
enforcers[caveatType] = addr;
}
}
registerChainContracts(chainId, {
delegationManager: environment.DelegationManager,
enforcers,
} as ChainContracts);
},
async storeGrant(grant: DelegationGrant): Promise<void> {
grants.set(grant.delegation.id, grant);
persistGrants();
},
async removeGrant(id: string): Promise<void> {
grants.delete(id);
persistGrants();
},
async listGrants(): Promise<DelegationGrant[]> {
return harden([...grants.values()]);
},
});
}
|