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 | 15x 15x 15x 15x 6x 6x 6x 6x 6x 6x 6x 6x 6x 2x 1x 1x 1x 1x 1x 1x 6x 9x 9x 9x 15x 15x 15x 15x 15x 8x 2x 6x 6x 6x 6x 1x 1x 15x | import { M } from '@endo/patterns';
import { makeDiscoverableExo } from '@metamask/kernel-utils/discoverable';
import { encodeTransfer } from './erc20.ts';
import { METHOD_CATALOG } from './method-catalog.ts';
import type { Address, DelegationGrant, Execution, Hex } from '../types.ts';
export type DelegationSection =
| { exo: object; method: 'transferNative' }
| { exo: object; method: 'transferFungible'; token: Address };
type DelegationTwinOptions = {
grant: DelegationGrant;
redeemFn: (execution: Execution) => Promise<Hex>;
};
/**
* Build a DelegationSection for a delegation grant.
* The resulting exo exposes the method covered by the grant, enforcing
* local guards and (for transferFungible) a local budget tracker.
*
* @param options - Twin construction options.
* @param options.grant - The semantic delegation grant to wrap.
* @param options.redeemFn - Submits an Execution to the delegation framework.
* @returns A DelegationSection wrapping the delegation exo.
*/
export function makeDelegationTwin(
options: DelegationTwinOptions,
): DelegationSection {
const { grant, redeemFn } = options;
const { delegation } = grant;
const idPrefix = delegation.id.slice(0, 12);
if (grant.method === 'transferNative') {
const { to } = grant;
// maxAmount may arrive as a string when the grant crosses a JSON boundary
// (e.g. CLI args or test helpers). Normalize to bigint so M.lte() and the
// method body comparison work correctly regardless of the source.
const maxAmount =
grant.maxAmount === undefined ? undefined : BigInt(grant.maxAmount);
const totalLimit =
grant.totalLimit === undefined ? undefined : BigInt(grant.totalLimit);
const toGuard = to === undefined ? M.string() : M.eq(to);
const amountGuard = maxAmount === undefined ? M.bigint() : M.lte(maxAmount);
const interfaceGuard = M.interface(
`DelegationTwin:transferNative:${idPrefix}`,
{
transferNative: M.callWhen(toGuard, amountGuard).returns(M.string()),
},
{ defaultGuards: 'passable' },
);
let spent = 0n;
const cumulativeMax = totalLimit ?? 2n ** 256n - 1n;
const exo = makeDiscoverableExo(
`DelegationTwin:transferNative:${idPrefix}`,
{
async transferNative(recipient: Address, amount: bigint): Promise<Hex> {
if (maxAmount !== undefined && amount > maxAmount) {
throw new Error(`Amount ${amount} exceeds limit ${maxAmount}`);
}
Iif (amount > cumulativeMax - spent) {
throw new Error(
`Insufficient budget: requested ${amount}, remaining ${cumulativeMax - spent}`,
);
}
// Reserve before the await so concurrent calls see the updated budget.
spent += amount;
const execution: Execution = {
target: recipient,
value: `0x${amount.toString(16)}`,
callData: '0x' as Hex,
};
try {
return await redeemFn(execution);
} catch (error) {
// Roll back on redeemFn failure.
spent -= amount;
throw error;
}
},
},
{ transferNative: METHOD_CATALOG.transferNative },
interfaceGuard,
);
return { exo, method: 'transferNative' };
}
// transferFungible — normalize token address to lowercase for consistent matching.
const { to } = grant;
const token = grant.token.toLowerCase() as Address;
// totalLimit may arrive as a string when the grant crosses a JSON boundary.
// Normalize to bigint so arithmetic comparisons work correctly.
const totalLimit =
grant.totalLimit === undefined ? undefined : BigInt(grant.totalLimit);
let spent = 0n;
const max = totalLimit ?? 2n ** 256n - 1n;
const toGuard = to === undefined ? M.string() : M.eq(to);
const interfaceGuard = M.interface(
`DelegationTwin:transferFungible:${idPrefix}`,
{
transferFungible: M.callWhen(M.eq(token), toGuard, M.bigint()).returns(
M.string(),
),
},
{ defaultGuards: 'passable' },
);
const exo = makeDiscoverableExo(
`DelegationTwin:transferFungible:${idPrefix}`,
{
async transferFungible(
tokenAddress: Address,
recipient: Address,
amount: bigint,
): Promise<Hex> {
if (amount > max - spent) {
throw new Error(
`Insufficient budget: requested ${amount}, remaining ${max - spent}`,
);
}
// Reserve before the await so concurrent calls see the updated budget.
spent += amount;
const execution: Execution = {
target: tokenAddress,
value: '0x0' as Hex,
callData: encodeTransfer(recipient, amount),
};
try {
return await redeemFn(execution);
} catch (error) {
// Roll back on redeemFn failure.
spent -= amount;
throw error;
}
},
},
{ transferFungible: METHOD_CATALOG.transferFungible },
interfaceGuard,
);
return { exo, method: 'transferFungible', token };
}
|