Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions modules/abstract-eth/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,11 @@ export function decodeConfidentialTransferData(data: string): ConfidentialTransf

export interface SendMultiSigFlushERC7984Data {
forwarderAddress: string;
/** Native value carried by the sendMultiSig call; must be '0' for a legitimate consolidation. */
amount: string;
tokenContractAddress: string;
/** Native value attached to the inner callFromParent call; must be 0n for a legitimate consolidation. */
callFromParentValue: bigint;
parentAddress: string;
encryptedHandle: string;
}
Expand All @@ -765,25 +769,30 @@ export interface SendMultiSigFlushERC7984Data {
* Multisig consolidation shape:
* sendMultiSig(forwarder, 0, callFromParent(token, 0, confidentialTransferNoProof(parent, handle)), ...)
*
* The sendMultiSig amount and the callFromParent native value are both surfaced so
* verifiers can enforce they are zero (CECHO-2719).
*
* @param data The full calldata hex string starting with sendMultiSigMethodId
*/
export function decodeSendMultiSigFlushERC7984Data(data: string): SendMultiSigFlushERC7984Data {
if (!data.startsWith(sendMultisigMethodId)) {
throw new BuildTransactionError(`Invalid multisig flush bytecode: unexpected method ID ${data.slice(0, 10)}`);
}

const [forwarderAddress, , internalData] = getRawDecoded(
const [forwarderAddress, amount, internalData] = getRawDecoded(
sendMultiSigTypes,
getBufferedByteCode(sendMultisigMethodId, data)
);

const internalDataHex = bufferToHex(internalData as Buffer);
const { tokenContractAddress, parentAddress, encryptedHandle } =
const { tokenContractAddress, callFromParentValue, parentAddress, encryptedHandle } =
decodeFlushERC7984ForwarderTokenCalldata(internalDataHex);

return {
forwarderAddress: addHexPrefix(forwarderAddress as string),
amount: new BigNumber(bufferToHex(amount as Buffer)).toFixed(),
tokenContractAddress,
callFromParentValue,
parentAddress,
encryptedHandle,
};
Expand Down
11 changes: 9 additions & 2 deletions modules/abstract-eth/src/lib/zamaUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -672,13 +672,18 @@ export function buildFlushERC7984ForwarderTokenCalldata(
/**
* Decodes a FlushERC7984ForwarderToken calldata.
* Strips the outer callFromParent wrapper and the inner confidentialTransfer.
* Returns { tokenContractAddress, parentAddress, encryptedHandle }.
* Returns { tokenContractAddress, callFromParentValue, parentAddress, encryptedHandle }.
*
* The callFromParent native value is surfaced so verifiers can enforce that it is 0
* (a legitimate flush moves confidential balances via calldata only; a non-zero value
* lets the forwarder forward native funds to an arbitrary target — CECHO-2719).
*
* @param data ABI-encoded flush calldata (0x-prefixed)
* @returns Decoded fields
* @returns Decoded fields, including the native value attached to the callFromParent call
*/
export function decodeFlushERC7984ForwarderTokenCalldata(data: string): {
tokenContractAddress: string;
callFromParentValue: bigint;
parentAddress: string;
encryptedHandle: string;
} {
Expand All @@ -691,6 +696,7 @@ export function decodeFlushERC7984ForwarderTokenCalldata(data: string): {
const abiCoder = new ethers.utils.AbiCoder();
const outerDecoded = abiCoder.decode([...callFromParentTypes], '0x' + data.slice(10));
const tokenContractAddress: string = outerDecoded[0];
const callFromParentValue = BigInt(outerDecoded[1].toString());
const innerCalldata: string = ethers.utils.hexlify(outerDecoded[2]);

if (!innerCalldata.startsWith(confidentialTransferNoProofMethodId)) {
Expand All @@ -708,6 +714,7 @@ export function decodeFlushERC7984ForwarderTokenCalldata(data: string): {

return {
tokenContractAddress,
callFromParentValue,
parentAddress,
encryptedHandle,
};
Expand Down
62 changes: 62 additions & 0 deletions modules/abstract-eth/test/unit/zamaUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import {
callFromParentMethodId,
} from '../../src/lib/zamaUtils';
import { confidentialTransferNoProofMethodId } from '../../src/lib/walletUtil';
import { decodeSendMultiSigFlushERC7984Data, sendMultiSigData } from '../../src/lib/utils';
import { addHexPrefix } from 'ethereumjs-util';

describe('Zama Utils', () => {
const ACL_ADDRESS = '0xf0Ffdc93b7E186bC2f8CB3dAA75D86d1930A433D';
Expand Down Expand Up @@ -778,5 +780,65 @@ describe('Zama Utils', () => {
should.exist(decoded.tokenContractAddress);
decoded.tokenContractAddress.should.be.a.String();
});

it('should surface the callFromParent native value as 0n for builder output', () => {
const encoded = buildFlushERC7984ForwarderTokenCalldata(TOKEN_ADDRESS, PARENT_ADDRESS, HANDLE);
const decoded = decodeFlushERC7984ForwarderTokenCalldata(encoded);
decoded.callFromParentValue.should.equal(0n);
});

it('should surface a non-zero callFromParent native value', () => {
// Hand-encode callFromParent with a non-zero value, unlike wrapInCallFromParent
// which hardcodes 0 — the exact hostile shape from CECHO-2719.
const inner = buildConfidentialTransferByHandleCalldata(PARENT_ADDRESS, HANDLE);
const method = EthereumAbi.methodID('callFromParent', ['address', 'uint256', 'bytes']);
const args = EthereumAbi.rawEncode(
['address', 'uint256', 'bytes'],
[TOKEN_ADDRESS, 123, Buffer.from(inner.slice(2), 'hex')]
);
const encoded = addHexPrefix(Buffer.concat([method, args]).toString('hex'));
const decoded = decodeFlushERC7984ForwarderTokenCalldata(encoded);
decoded.callFromParentValue.should.equal(123n);
decoded.tokenContractAddress.toLowerCase().should.equal(TOKEN_ADDRESS.toLowerCase());
decoded.parentAddress.toLowerCase().should.equal(PARENT_ADDRESS.toLowerCase());
});
});
// -------------------------------------------------------------------------
describe('decodeSendMultiSigFlushERC7984Data (CECHO-2719 value surfacing)', () => {
const FORWARDER = '0xAbC1234567890123456789012345678901234567';
const PARENT = '0x2222222222222222222222222222222222222222';
const HANDLE = '0x' + 'ef'.repeat(32);
const SEQUENCE_ID = 14;
const EXPIRE_TIME = Math.floor(Date.now() / 1000) + 3600;
const SIGNATURE = '0x' + '00'.repeat(65);

it('should surface the sendMultiSig amount and callFromParent value as zero for a standard flush', () => {
const flushCalldata = buildFlushERC7984ForwarderTokenCalldata(TOKEN_ADDRESS, PARENT, HANDLE);
const sendData = sendMultiSigData(FORWARDER, '0', flushCalldata, EXPIRE_TIME, SEQUENCE_ID, SIGNATURE);
const decoded = decodeSendMultiSigFlushERC7984Data(sendData);
decoded.forwarderAddress.toLowerCase().should.equal(FORWARDER.toLowerCase());
decoded.amount.should.equal('0');
decoded.callFromParentValue.should.equal(0n);
decoded.tokenContractAddress.toLowerCase().should.equal(TOKEN_ADDRESS.toLowerCase());
decoded.parentAddress.toLowerCase().should.equal(PARENT.toLowerCase());
decoded.encryptedHandle.should.equal(HANDLE);
});

it('should surface a non-zero sendMultiSig amount', () => {
// sendMultiSig(attacker, balance, flush) — the multisig theft prebuild from
// CECHO-2719: the amount word was previously dropped and never checked.
const flushCalldata = buildFlushERC7984ForwarderTokenCalldata(TOKEN_ADDRESS, PARENT, HANDLE);
const sendData = sendMultiSigData(
FORWARDER,
'1000000000000000000',
flushCalldata,
EXPIRE_TIME,
SEQUENCE_ID,
SIGNATURE
);
const decoded = decodeSendMultiSigFlushERC7984Data(sendData);
decoded.amount.should.equal('1000000000000000000');
decoded.callFromParentValue.should.equal(0n);
});
});
});
Loading
Loading