theseus/bundled-addons/bchwallet/lib/eip712.js
Local Dev 8fcc0e2433 feat(theseus/aegis): EIP-712 signTypedData_v4 + Solana multi-signer send
Two follow-ups to the dapp bridges. Both change wire shape only — no new
UI, existing wallets keep signing byte-identically for the flows they
already covered.

- lib/eip712.js: full EIP-712 typed-data encoder — encodeType with
  alphabetically-sorted transitive sub-types, typeHash, encodeValue for
  string / address / bool / uint*/int* (any width) / bytes / bytesN /
  nested structs / dynamic and fixed arrays, hashStruct recursion,
  digest = keccak256(0x19 || 0x01 || domainSeparator || hashStruct).
  Verified against the spec §"Ether Mail" test vector — hashStruct on
  both the domain and the message plus the final digest all match the
  canonical values byte-for-byte (see scratchpad/verify-eip712.mjs).
- chain-eth.js: exposes signTypedDataDigest(digest32) that signs the
  precomputed digest with r||s||v (v = 27+recid), the same envelope
  personal_sign uses. Aegis computes the digest server-side (in the
  addon) so a bug in the encoder can't be tricked by a malicious dapp
  into signing over data the user never saw.
- index.js: eth.signTypedData handler shows domain (name · version ·
  chainId), primary type, and a truncated JSON preview of the message
  in the approval overlay — every classic phishing signal (mismatched
  domain, unexpected primary type) is in front of the user before they
  hit Sign. Accepts either an already-parsed typedData object or the
  JSON-string form older MetaMask specs used.
- wallet-inject.js router: eth_signTypedData_v4 (and _v3 for the same
  payload shape) route to eth.signTypedData. v1's flat "type[]" form
  is unwired — dapps that still use v1 should upgrade.
- Solana signAndSend: bridge now passes the FULL wire (from
  tx.serialize({requireAllSignatures:false, verifySignatures:false}))
  instead of just the message. The addon parses compact-u16 signature
  count, finds this wallet's pubkey in the message's account-key list,
  signs the message, and patches ONLY its own slot in the signature
  array — any partial signatures the dapp had already filled with
  tx.partialSign() (session keys, escrow co-signers, permissioned
  authorities) are preserved. Multi-signer flows work now; single-signer
  is the degenerate case of sigCount=1.
- Approval overlay for sol.signAndSend now shows required-signer count
  and the wallet's slot index so multi-signer requests are visibly
  distinct from a plain single-signer send.
2026-09-07 22:19:51 +02:00

143 lines
6.1 KiB
JavaScript

// EIP-712 typed-data hashing (personal_sign's structured cousin). Produces
// the 32-byte digest that eth_signTypedData_v4 signs with the wallet's
// secp256k1 key.
//
// Reference: https://eips.ethereum.org/EIPS/eip-712
// Digest = keccak256(0x19 || 0x01 || domainSeparator || hashStruct(primaryType, message))
// - domainSeparator = hashStruct("EIP712Domain", typedData.domain)
// - hashStruct(type, data) = keccak256(typeHash(type) || encodeData(type, data))
// - typeHash(type) = keccak256(encodeType(type))
// - encodeType is the canonical string form; sub-types are appended in
// alphabetical order once, without recursion into themselves twice.
//
// This is enough for every mainstream EIP-712 payload — Permit / EIP-2612,
// OpenSea order signatures, WalletConnect handshakes, Snapshot votes. Not
// implemented: fixed-size arrays of atomic types wider than a byte (rare
// enough that no shipping dapp we care about uses them).
module.exports = function makeEip712({ keccak_256 }) {
const enc = new TextEncoder();
const concat = (...ps) => {
const n = ps.reduce((a, p) => a + p.length, 0);
const out = new Uint8Array(n); let k = 0;
for (const p of ps) { out.set(p, k); k += p.length; }
return out;
};
const hex2bytes = (h) => {
const s = String(h).replace(/^0x/i, "");
if (s.length % 2) throw new Error("hex: odd length");
const out = new Uint8Array(s.length / 2);
for (let i = 0; i < out.length; i++) out[i] = parseInt(s.slice(i * 2, i * 2 + 2), 16);
return out;
};
const bytesToBig = (b) => {
let v = 0n; for (const x of b) v = (v << 8n) | BigInt(x); return v;
};
const bigToBe32 = (v, signed) => {
let n = BigInt(v);
if (n < 0n) {
if (!signed) throw new Error("negative value for unsigned type");
// two's complement to 256 bits
n = (1n << 256n) + n;
}
const out = new Uint8Array(32);
for (let i = 31; i >= 0; i--) { out[i] = Number(n & 0xffn); n >>= 8n; }
return out;
};
// encodeType walker — resolves the primary type + every struct it
// transitively references, then emits "Primary(...)Sub1(...)Sub2(...)"
// with sub-types in alphabetical order per the spec.
function findDependencies(primaryType, types, found = new Set()) {
if (found.has(primaryType) || !types[primaryType]) return found;
found.add(primaryType);
for (const f of types[primaryType]) {
const base = f.type.replace(/\[.*\]$/, "");
if (types[base]) findDependencies(base, types, found);
}
return found;
}
function encodeType(primaryType, types) {
const deps = [...findDependencies(primaryType, types)].filter((t) => t !== primaryType).sort();
const all = [primaryType, ...deps];
return all.map((t) => `${t}(${types[t].map((f) => `${f.type} ${f.name}`).join(",")})`).join("");
}
function typeHash(primaryType, types) {
return keccak_256(enc.encode(encodeType(primaryType, types)));
}
// Encode one field value per its declared type. Struct + array types
// hash themselves to 32 bytes; atomics land in a 32-byte slot each.
function encodeValue(type, value, types) {
// Array types: `Type[]` (dynamic) or `Type[N]` (fixed) — both encode
// as keccak256(concat(encodeValue(baseType, element)...)) per EIP-712.
const arr = /^(.+)\[(\d*)\]$/.exec(type);
if (arr) {
const baseType = arr[1];
const items = Array.isArray(value) ? value : [];
const encoded = items.map((v) => encodeValue(baseType, v, types));
return keccak_256(concat(...encoded));
}
// Struct types: hashStruct recursion.
if (types[type]) return hashStruct(type, value, types);
// Atomic types.
if (type === "string") return keccak_256(enc.encode(String(value ?? "")));
if (type === "bytes") {
const b = typeof value === "string" ? hex2bytes(value) : Uint8Array.from(value || []);
return keccak_256(b);
}
if (type === "address") {
const h = hex2bytes(String(value || "0x0").replace(/^0x/, ""));
if (h.length !== 20) throw new Error("address must be 20 bytes");
const out = new Uint8Array(32);
out.set(h, 12);
return out;
}
if (type === "bool") {
const out = new Uint8Array(32);
out[31] = value ? 1 : 0;
return out;
}
// bytesN (fixed): left-aligned in a 32-byte word.
const bytesN = /^bytes(\d+)$/.exec(type);
if (bytesN) {
const n = Number(bytesN[1]);
if (n < 1 || n > 32) throw new Error("bytesN out of range");
const b = typeof value === "string" ? hex2bytes(value) : Uint8Array.from(value || []);
if (b.length !== n) throw new Error(`${type} expects ${n} bytes, got ${b.length}`);
const out = new Uint8Array(32);
out.set(b, 0);
return out;
}
// uint* / int*: encode as 32-byte big-endian.
const uintM = /^uint(\d*)$/.exec(type);
if (uintM) return bigToBe32(value, false);
const intM = /^int(\d*)$/.exec(type);
if (intM) return bigToBe32(value, true);
throw new Error("unsupported EIP-712 type: " + type);
}
function encodeData(primaryType, data, types) {
const fields = types[primaryType];
if (!fields) throw new Error("unknown type: " + primaryType);
const encoded = fields.map((f) => encodeValue(f.type, data ? data[f.name] : undefined, types));
return concat(...encoded);
}
function hashStruct(primaryType, data, types) {
return keccak_256(concat(typeHash(primaryType, types), encodeData(primaryType, data, types)));
}
// Full EIP-712 digest, ready for secp256k1.sign(digest, key).
function digest(typedData) {
const td = typedData && typeof typedData === "object" ? typedData : {};
const types = td.types || {};
if (!types.EIP712Domain) throw new Error("typedData.types.EIP712Domain missing");
const primary = String(td.primaryType || "");
if (!primary || !types[primary]) throw new Error(`typedData.primaryType "${primary}" not in types`);
const domainSeparator = hashStruct("EIP712Domain", td.domain || {}, types);
const messageHash = hashStruct(primary, td.message || {}, types);
return keccak_256(concat(Uint8Array.from([0x19, 0x01]), domainSeparator, messageHash));
}
return { digest, encodeType, typeHash, hashStruct };
};