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>
220 lines
6.4 KiB
JavaScript
220 lines
6.4 KiB
JavaScript
import { ethers } from 'ethers';
|
|
import { p256 } from '@noble/curves/nist.js';
|
|
import { generateKeyPairSync, privateEncrypt } from 'crypto';
|
|
|
|
// Lightweight version of BaseWallet
|
|
export class NonNativeSigner extends ethers.AbstractSigner {
|
|
#signingKey;
|
|
|
|
constructor(privateKey, provider) {
|
|
super(provider);
|
|
ethers.assertArgument(
|
|
privateKey && typeof privateKey.sign === 'function',
|
|
'invalid private key',
|
|
'privateKey',
|
|
'[ REDACTED ]',
|
|
);
|
|
this.#signingKey = privateKey;
|
|
}
|
|
|
|
get signingKey() {
|
|
return this.#signingKey;
|
|
}
|
|
get privateKey() {
|
|
return this.signingKey.privateKey;
|
|
}
|
|
|
|
async getAddress() {
|
|
throw new Error("NonNativeSigner doesn't have an address");
|
|
}
|
|
|
|
connect(provider) {
|
|
return new NonNativeSigner(this.#signingKey, provider);
|
|
}
|
|
|
|
async signTransaction(/*tx: TransactionRequest*/) {
|
|
throw new Error('NonNativeSigner cannot send transactions');
|
|
}
|
|
|
|
async signMessage(message /*: string | Uint8Array*/) /*: Promise<string>*/ {
|
|
return this.signingKey.sign(ethers.hashMessage(message)).serialized;
|
|
}
|
|
|
|
async signTypedData(
|
|
domain /*: TypedDataDomain*/,
|
|
types /*: Record<string, Array<TypedDataField>>*/,
|
|
value /*: Record<string, any>*/,
|
|
) /*: Promise<string>*/ {
|
|
// Populate any ENS names
|
|
const populated = await ethers.TypedDataEncoder.resolveNames(domain, types, value, async name => {
|
|
ethers.assert(this.provider != null, 'cannot resolve ENS names without a provider', 'UNSUPPORTED_OPERATION', {
|
|
operation: 'resolveName',
|
|
info: { name },
|
|
});
|
|
const address = await this.provider.resolveName(name);
|
|
ethers.assert(address != null, 'unconfigured ENS name', 'UNCONFIGURED_NAME', { value: name });
|
|
return address;
|
|
});
|
|
|
|
return this.signingKey.sign(ethers.TypedDataEncoder.hash(populated.domain, types, populated.value)).serialized;
|
|
}
|
|
}
|
|
|
|
export class P256SigningKey {
|
|
#privateKey;
|
|
|
|
constructor(privateKey) {
|
|
this.#privateKey = ethers.getBytes(privateKey);
|
|
}
|
|
|
|
static random() {
|
|
return new this(p256.utils.randomSecretKey());
|
|
}
|
|
|
|
get privateKey() {
|
|
return ethers.hexlify(this.#privateKey);
|
|
}
|
|
|
|
get publicKey() {
|
|
const publicKeyBytes = p256.getPublicKey(this.#privateKey, false);
|
|
return {
|
|
qx: ethers.dataSlice(publicKeyBytes, 0x01, 0x21),
|
|
qy: ethers.dataSlice(publicKeyBytes, 0x21, 0x41),
|
|
};
|
|
}
|
|
|
|
sign(digest /*: BytesLike*/) /*: ethers.Signature*/ {
|
|
ethers.assertArgument(ethers.dataLength(digest) === 32, 'invalid digest length', 'digest', digest);
|
|
|
|
const rawSignature = p256.sign(ethers.getBytes(digest), ethers.getBytes(this.#privateKey), {
|
|
prehash: false,
|
|
format: 'recovered',
|
|
});
|
|
|
|
return ethers.Signature.from({
|
|
r: ethers.dataSlice(rawSignature, 0x01, 0x21),
|
|
s: ethers.dataSlice(rawSignature, 0x21, 0x41),
|
|
v: rawSignature[0] ? 0x1c : 0x1b,
|
|
});
|
|
}
|
|
}
|
|
|
|
export class RSASigningKey {
|
|
#privateKey;
|
|
#publicKey;
|
|
|
|
constructor(keyPair) {
|
|
const jwk = keyPair.publicKey.export({ format: 'jwk' });
|
|
this.#privateKey = keyPair.privateKey;
|
|
this.#publicKey = { e: ethers.decodeBase64(jwk.e), n: ethers.decodeBase64(jwk.n) };
|
|
}
|
|
|
|
static random(modulusLength = 2048) {
|
|
return new this(generateKeyPairSync('rsa', { modulusLength }));
|
|
}
|
|
|
|
get privateKey() {
|
|
return ethers.hexlify(this.#privateKey);
|
|
}
|
|
|
|
get publicKey() {
|
|
return { e: ethers.hexlify(this.#publicKey.e), n: ethers.hexlify(this.#publicKey.n) };
|
|
}
|
|
|
|
sign(digest /*: BytesLike*/) /*: ethers.Signature*/ {
|
|
ethers.assertArgument(ethers.dataLength(digest) === 32, 'invalid digest length', 'digest', digest);
|
|
// SHA256 OID = 608648016503040201 (9 bytes) | NULL = 0500 (2 bytes) (explicit) | OCTET_STRING length (0x20) = 0420 (2 bytes)
|
|
return {
|
|
serialized: ethers.hexlify(
|
|
privateEncrypt(
|
|
this.#privateKey,
|
|
ethers.getBytes(ethers.concat(['0x3031300d060960864801650304020105000420', digest])),
|
|
),
|
|
),
|
|
};
|
|
}
|
|
}
|
|
|
|
export class RSASHA256SigningKey extends RSASigningKey {
|
|
sign(digest /*: BytesLike*/) /*: ethers.Signature*/ {
|
|
ethers.assertArgument(ethers.dataLength(digest) === 32, 'invalid digest length', 'digest', digest);
|
|
return super.sign(ethers.sha256(ethers.getBytes(digest)));
|
|
}
|
|
}
|
|
|
|
export class WebAuthnSigningKey extends P256SigningKey {
|
|
sign(digest /*: BytesLike*/) /*: { serialized: string } */ {
|
|
ethers.assertArgument(ethers.dataLength(digest) === 32, 'invalid digest length', 'digest', digest);
|
|
|
|
const clientDataJSON = JSON.stringify({
|
|
type: 'webauthn.get',
|
|
challenge: ethers.encodeBase64(digest).replaceAll('+', '-').replaceAll('/', '_').replaceAll('=', ''),
|
|
});
|
|
|
|
// Flags 0x05 = AUTH_DATA_FLAGS_UP | AUTH_DATA_FLAGS_UV
|
|
const authenticatorData = ethers.solidityPacked(
|
|
['bytes32', 'bytes1', 'bytes4'],
|
|
[ethers.ZeroHash, '0x05', '0x00000000'],
|
|
);
|
|
|
|
// Regular P256 signature
|
|
const { r, s } = super.sign(
|
|
ethers.sha256(ethers.concat([authenticatorData, ethers.sha256(ethers.toUtf8Bytes(clientDataJSON))])),
|
|
);
|
|
|
|
const serialized = ethers.AbiCoder.defaultAbiCoder().encode(
|
|
['bytes32', 'bytes32', 'uint256', 'uint256', 'bytes', 'string'],
|
|
[
|
|
r,
|
|
s,
|
|
clientDataJSON.indexOf('"challenge"'),
|
|
clientDataJSON.indexOf('"type"'),
|
|
authenticatorData,
|
|
clientDataJSON,
|
|
],
|
|
);
|
|
|
|
return { serialized };
|
|
}
|
|
}
|
|
|
|
export class MultiERC7913SigningKey {
|
|
// this is a sorted array of objects that contain {signer, weight}
|
|
#signers;
|
|
|
|
constructor(signers) {
|
|
ethers.assertArgument(
|
|
Array.isArray(signers) && signers.length > 0,
|
|
'signers must be a non-empty array',
|
|
'signers',
|
|
signers.length,
|
|
);
|
|
|
|
// Sorting is done at construction so that it doesn't have to be done in sign()
|
|
this.#signers = signers.sort((s1, s2) =>
|
|
Buffer.compare(
|
|
ethers.getBytes(ethers.keccak256(s1.bytes ?? s1.address)),
|
|
ethers.getBytes(ethers.keccak256(s2.bytes ?? s2.address)),
|
|
),
|
|
);
|
|
}
|
|
|
|
get signers() {
|
|
return this.#signers;
|
|
}
|
|
|
|
sign(digest /*: BytesLike*/ /*: ethers.Signature*/) {
|
|
ethers.assertArgument(ethers.dataLength(digest) === 32, 'invalid digest length', 'digest', digest);
|
|
|
|
return {
|
|
serialized: ethers.AbiCoder.defaultAbiCoder().encode(
|
|
['bytes[]', 'bytes[]'],
|
|
[
|
|
this.#signers.map(signer => signer.bytes ?? signer.address),
|
|
this.#signers.map(signer => signer.signingKey.sign(digest).serialized),
|
|
],
|
|
),
|
|
};
|
|
}
|
|
}
|