caelum0x 94c26e9496 feat: QIE Forge — clean-room AMM + lending vault, SDK, and CLI for QIE
Initial build for QIE Mainnet Hackathon 3.0 (DeFi & Financial
Infrastructure track). Includes:

- packages/contracts: constant-product AMM (factory/pair/router),
  over-collateralized lending vault, and an oracle adapter wrapping
  QIE's real deployed price-feed contracts. 25/25 Foundry tests pass
  locally. Clean-room: no existing DEX/lending protocol source was
  read or copied.
- packages/core: TypeScript SDK (viem-based) for QIE's chain, the
  real (corrected) oracle ABI, a fail-closed QUSDC helper, a
  Blockscout explorer client, and QIE Wallet connection constants.
  Live-verified against real testnet (chain 1983) and mainnet
  (chain 1990) RPCs and the real oracle contracts.
- packages/cli: a small CLI built on @qie-forge/core.

Real findings from building this, documented in README.md and
docs/STATUS.md: the previously-documented testnet RPC URL is dead
(replacement found and verified live); QIE's own oracle docs describe
a getPrice(string) function that reverts on the real deployed
contracts, which actually implement a Chainlink AggregatorV3-style
interface (verified live via raw eth_call); the oracle is deployed on
Mainnet only, not Testnet.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-26 23:34:51 +03:00

337 lines
14 KiB
JavaScript

import { network } from 'hardhat';
import { expect } from 'chai';
import { MAX_UINT64 } from '../helpers/constants';
import { getDomain } from '../helpers/eip712';
import { ERC4337Helper } from '../helpers/erc4337';
import { PackedUserOperation } from '../helpers/eip712-types';
import { NonNativeSigner, P256SigningKey, RSASHA256SigningKey, MultiERC7913SigningKey } from '../helpers/signers';
import { shouldBehaveLikeAccountCore, shouldBehaveLikeAccountHolder } from './Account.behavior';
import { shouldBehaveLikeERC1271 } from '../utils/cryptography/ERC1271.behavior';
import { shouldBehaveLikeERC7821 } from './extensions/ERC7821.behavior';
const connection = await network.create();
const {
ethers,
networkHelpers: { loadFixture },
} = connection;
// Prepare signers in advance (RSA are long to initialize)
const signerECDSA1 = ethers.Wallet.createRandom();
const signerECDSA2 = ethers.Wallet.createRandom();
const signerECDSA3 = ethers.Wallet.createRandom();
const signerECDSA4 = ethers.Wallet.createRandom(); // Unauthorized signer
const signerP256 = new NonNativeSigner(P256SigningKey.random());
const signerRSA = new NonNativeSigner(RSASHA256SigningKey.random());
// Minimal fixture common to the different signer verifiers
async function fixture() {
// EOAs and environment
const [beneficiary, other] = await ethers.getSigners();
const target = await ethers.deployContract('CallReceiverMock');
// ERC-7913 verifiers
const verifierP256 = await ethers.deployContract('ERC7913P256Verifier');
const verifierRSA = await ethers.deployContract('ERC7913RSAVerifier');
// ERC-4337 env
const helper = new ERC4337Helper(connection);
await helper.wait();
const entrypointDomain = await getDomain(ethers.predeploy.entrypoint.v09);
const domain = { name: 'AccountMultiSigner', version: '1', chainId: entrypointDomain.chainId }; // Missing verifyingContract
const makeMock = (signers, threshold) =>
helper.newAccount('$AccountMultiSignerMock', [signers, threshold, 'AccountMultiSigner', '1']).then(mock => {
domain.verifyingContract = mock.address;
return mock;
});
// Sign user operations using MultiERC7913SigningKey
const signUserOp = function (userOp) {
return this.signer
.signTypedData(entrypointDomain, { PackedUserOperation }, userOp.packed)
.then(signature => Object.assign(userOp, { signature }));
};
const invalidSig = function () {
return this.signer.signMessage('invalid');
};
return {
helper,
verifierP256,
verifierRSA,
domain,
target,
beneficiary,
other,
makeMock,
signUserOp,
invalidSig,
};
}
describe('AccountMultiSigner', function () {
beforeEach(async function () {
Object.assign(this, connection, await loadFixture(fixture));
});
describe('Multi ECDSA signers with threshold=1', function () {
beforeEach(async function () {
this.signer = new NonNativeSigner(new MultiERC7913SigningKey([signerECDSA1]));
this.mock = await this.makeMock([signerECDSA1.address], 1);
});
shouldBehaveLikeAccountCore();
shouldBehaveLikeAccountHolder();
shouldBehaveLikeERC1271({ erc7739: true });
shouldBehaveLikeERC7821();
});
describe('Multi ECDSA signers with threshold=2', function () {
beforeEach(async function () {
this.signer = new NonNativeSigner(new MultiERC7913SigningKey([signerECDSA1, signerECDSA2]));
this.mock = await this.makeMock([signerECDSA1.address, signerECDSA2.address], 2);
});
shouldBehaveLikeAccountCore();
shouldBehaveLikeAccountHolder();
shouldBehaveLikeERC1271({ erc7739: true });
shouldBehaveLikeERC7821();
});
describe('Mixed signers with threshold=2', function () {
beforeEach(async function () {
// Create signers array with all three types
signerP256.bytes = ethers.concat([
this.verifierP256.target,
signerP256.signingKey.publicKey.qx,
signerP256.signingKey.publicKey.qy,
]);
signerRSA.bytes = ethers.concat([
this.verifierRSA.target,
ethers.AbiCoder.defaultAbiCoder().encode(
['bytes', 'bytes'],
[signerRSA.signingKey.publicKey.e, signerRSA.signingKey.publicKey.n],
),
]);
this.signer = new NonNativeSigner(new MultiERC7913SigningKey([signerECDSA1, signerP256, signerRSA]));
this.mock = await this.makeMock([signerECDSA1.address, signerP256.bytes, signerRSA.bytes], 2);
});
shouldBehaveLikeAccountCore();
shouldBehaveLikeAccountHolder();
shouldBehaveLikeERC1271({ erc7739: true });
shouldBehaveLikeERC7821();
});
describe('Signer management', function () {
beforeEach(async function () {
this.signer = new NonNativeSigner(new MultiERC7913SigningKey([signerECDSA1, signerECDSA2]));
this.mock = await this.makeMock([signerECDSA1.address, signerECDSA2.address], 1);
await this.mock.deploy();
});
it('can add signers', async function () {
const signers = [signerECDSA3.address];
// Successfully adds a signer
const signersArrayBefore = await this.mock.getSigners(0, MAX_UINT64).then(s => s.map(ethers.getAddress));
await expect(this.mock.$_addSigners(signers))
.to.emit(this.mock, 'ERC7913SignerAdded')
.withArgs(signerECDSA3.address);
const signersArrayAfter = await this.mock.getSigners(0, MAX_UINT64).then(s => s.map(ethers.getAddress));
expect(signersArrayAfter.length).to.equal(signersArrayBefore.length + 1);
expect(signersArrayAfter).to.include(ethers.getAddress(signerECDSA3.address));
// Reverts if the signer was already added
await expect(this.mock.$_addSigners(signers))
.to.be.revertedWithCustomError(this.mock, 'MultiSignerERC7913AlreadyExists')
.withArgs(...signers.map(s => s.toLowerCase()));
});
it('can remove signers', async function () {
const signers = [signerECDSA2.address];
// Successfully removes an already added signer
const signersArrayBefore = await this.mock.getSigners(0, MAX_UINT64).then(s => s.map(ethers.getAddress));
await expect(this.mock.$_removeSigners(signers))
.to.emit(this.mock, 'ERC7913SignerRemoved')
.withArgs(signerECDSA2.address);
const signersArrayAfter = await this.mock.getSigners(0, MAX_UINT64).then(s => s.map(ethers.getAddress));
expect(signersArrayAfter.length).to.equal(signersArrayBefore.length - 1);
expect(signersArrayAfter).to.not.include(ethers.getAddress(signerECDSA2.address));
// Reverts removing a signer if it doesn't exist
await expect(this.mock.$_removeSigners(signers))
.to.be.revertedWithCustomError(this.mock, 'MultiSignerERC7913NonexistentSigner')
.withArgs(...signers.map(s => s.toLowerCase()));
// Reverts if removing a signer makes the threshold unreachable
await expect(this.mock.$_removeSigners([signerECDSA1.address]))
.to.be.revertedWithCustomError(this.mock, 'MultiSignerERC7913UnreachableThreshold')
.withArgs(0, 1);
});
it('can change threshold', async function () {
// Reachable threshold is set
await expect(this.mock.$_setThreshold(2)).to.emit(this.mock, 'ERC7913ThresholdSet');
// Unreachable threshold reverts
await expect(this.mock.$_setThreshold(3))
.to.be.revertedWithCustomError(this.mock, 'MultiSignerERC7913UnreachableThreshold')
.withArgs(2, 3);
// Zero threshold reverts
await expect(this.mock.$_setThreshold(0)).to.be.revertedWithCustomError(
this.mock,
'MultiSignerERC7913ZeroThreshold',
);
});
it('rejects invalid signer format', async function () {
const invalidSigner = '0x123456'; // Too short
await expect(this.mock.$_addSigners([invalidSigner]))
.to.be.revertedWithCustomError(this.mock, 'MultiSignerERC7913InvalidSigner')
.withArgs(invalidSigner);
});
it('can read signers and threshold', async function () {
await expect(
this.mock.getSigners(0, MAX_UINT64).then(s => s.map(ethers.getAddress)),
).to.eventually.have.deep.members([signerECDSA1.address, signerECDSA2.address]);
await expect(this.mock.threshold()).to.eventually.equal(1);
});
it('checks if an address is a signer', async function () {
// Should return true for authorized signers
await expect(this.mock.isSigner(signerECDSA1.address)).to.eventually.be.true;
await expect(this.mock.isSigner(signerECDSA2.address)).to.eventually.be.true;
// Should return false for unauthorized signers
await expect(this.mock.isSigner(signerECDSA3.address)).to.eventually.be.false;
await expect(this.mock.isSigner(signerECDSA4.address)).to.eventually.be.false;
});
});
describe('Signature validation', function () {
const TEST_MESSAGE = 'Test message';
const MESSAGE_HASH = ethers.hashMessage(TEST_MESSAGE);
const sortSigners = signers =>
signers.sort((a, b) =>
Buffer.compare(
ethers.getBytes(ethers.keccak256(a.address ?? a)),
ethers.getBytes(ethers.keccak256(b.address ?? b)),
),
);
const prepareMultisig = (signers, signatures) =>
ethers.AbiCoder.defaultAbiCoder().encode(['bytes[]', 'bytes[]'], [signers.map(s => s.address ?? s), signatures]);
beforeEach(async function () {
// Set up mock with authorized signers
this.mock = await this.makeMock([signerECDSA1.address, signerECDSA2.address], 1);
await this.mock.deploy();
});
it('accepts signatures from authorized signers', async function () {
const signers = sortSigners([signerECDSA1, signerECDSA2]);
const signatures = await Promise.all(signers.map(s => s.signMessage(TEST_MESSAGE)));
// Should pass because all signers are authorized.
await expect(this.mock.$_rawSignatureValidation(MESSAGE_HASH, prepareMultisig(signers, signatures))).to.eventually
.be.true;
});
it('rejects signatures from unauthorized signers', async function () {
const signers = sortSigners([signerECDSA1, signerECDSA4]); // signerECDSA4 is unauthorized
const signatures = await Promise.all(signers.map(s => s.signMessage(TEST_MESSAGE)));
// Should fail because one signer is not authorized
await expect(this.mock.$_rawSignatureValidation(MESSAGE_HASH, prepareMultisig(signers, signatures))).to.eventually
.be.false;
});
it('rejects invalid signatures from authorized signers', async function () {
const signers = sortSigners([signerECDSA1, signerECDSA2]);
const signatures = await Promise.all(
signers.map((s, i) => s.signMessage(i === 0 ? 'Invalid message' : TEST_MESSAGE)), // first signature is invalid
);
// Should fail because one signature is invalid
await expect(this.mock.$_rawSignatureValidation(MESSAGE_HASH, prepareMultisig(signers, signatures))).to.eventually
.be.false;
});
it('accepts signatures from unsorted signers', async function () {
const signers = sortSigners([signerECDSA1, signerECDSA2]).reverse(); // Unsorted signers
const signatures = await Promise.all(signers.map(s => s.signMessage(TEST_MESSAGE)));
// Should pass because signatures are valid even if signers are unsorted
await expect(this.mock.$_rawSignatureValidation(MESSAGE_HASH, prepareMultisig(signers, signatures))).to.eventually
.be.true;
});
it('rejects signatures when signers.length != signatures.length', async function () {
const signers = sortSigners([signerECDSA1, signerECDSA2]);
const signatures = await Promise.all(signers.slice(0, -1).map(s => s.signMessage(TEST_MESSAGE))); // slice the last signer
// Should fail because signers and signatures arrays have different lengths
await expect(this.mock.$_rawSignatureValidation(MESSAGE_HASH, prepareMultisig(signers, signatures))).to.eventually
.be.false;
});
it('rejects duplicated signers', async function () {
const signers = sortSigners([signerECDSA1, signerECDSA1]); // duplicated signer
const signatures = await Promise.all(signers.map(s => s.signMessage(TEST_MESSAGE)));
// Should fail because of duplicated signers
await expect(this.mock.$_rawSignatureValidation(MESSAGE_HASH, prepareMultisig(signers, signatures))).to.eventually
.be.false;
});
describe('returns false (does not revert) on malformed outer encoding', function () {
const word = v => ethers.zeroPadValue(ethers.toBeHex(v), 0x20);
const encode = (...items) => ethers.concat(items.map(word));
it('supports minimal encoding', async function () {
await expect(this.mock.$_rawSignatureValidation(MESSAGE_HASH, encode(0, 0))).to.eventually.be.false;
await expect(this.mock.$_rawSignatureValidation(MESSAGE_HASH, encode(0x20, 0))).to.eventually.be.false;
});
it('shorter than the minimum head layout (64 bytes)', async function () {
await expect(this.mock.$_rawSignatureValidation(MESSAGE_HASH, '0xdeadbeef')).to.eventually.be.false;
});
it('offset points past the calldata', async function () {
await expect(this.mock.$_rawSignatureValidation(MESSAGE_HASH, encode(0xffff, 0xffff, 0, 0))).to.eventually.be
.false;
});
it('offset near type(uint256).max (would overflow in checked arithmetic)', async function () {
await expect(
this.mock.$_rawSignatureValidation(MESSAGE_HASH, encode(ethers.MaxUint256, ethers.MaxUint256, 0, 0)),
).to.eventually.be.false;
});
it('array length exceeds Solidity dynamic-array cap (2**64-1)', async function () {
await expect(this.mock.$_rawSignatureValidation(MESSAGE_HASH, encode(0x40, 0x60, MAX_UINT64 + 1n, 0))).to
.eventually.be.false;
await expect(this.mock.$_rawSignatureValidation(MESSAGE_HASH, encode(0x40, 0x60, 0, MAX_UINT64 + 1n))).to
.eventually.be.false;
});
it('array length exceeds the remaining buffer', async function () {
await expect(this.mock.$_rawSignatureValidation(MESSAGE_HASH, encode(0x40, 0x60, 10, 0))).to.eventually.be
.false;
await expect(this.mock.$_rawSignatureValidation(MESSAGE_HASH, encode(0x40, 0x60, 0, 10))).to.eventually.be
.false;
});
});
});
});