Repository navigation
refactor(accounts)!: unify frame account and payer constructors - #55
nikhilkumar1612 wants to merge 4 commits into
Conversation
mariano-aguero
left a comment
There was a problem hiding this comment.
Thanks for unifying these constructors, the single entry point makes the account API much easier to follow.
|
thanks for the review @mariano-aguero will take a look at them in a while |
BREAKING CHANGE: Specialized EOA, P-256, and EOA payer constructors are replaced by toFrameAccount and toFramePayerAccount.
mariano-aguero
left a comment
There was a problem hiding this comment.
Thanks for the quick and thorough follow-up, nikhilkumar1612, the earlier points are all addressed and the new tests read really well.
| await assertPayFrame(transaction) | ||
|
|
||
| let prepared = transaction | ||
| let signatureIndex = findOwnerSignatureIndex(prepared, owner, 'last', { |
There was a problem hiding this comment.
The 'last' direction here and 'first' in toFrameAccount.ts:190 are what keep sender and payer on separate entries when one key controls both, for example a delegated sender with explicit signer K and a payer owned by K, which gives [{1, K}, {1, K}]. Both entries hash the same, so if either direction were flipped both accounts would sign the same index and leave the other one as 0x, and no test covers that layout: the sender == payer test at test/accounts.test.ts:200 uses [null, K], where explicitSigner already rules out index 0, and test/signFrameTransaction.test.ts uses distinct owners. A case that runs signFrameTransaction(account, tx, { payer }) with a shared owner and asserts both indices recover to K would make the direction load-bearing in the suite.
| parameters.getSignatureEntries ?? | ||
| (async (transaction: FrameTransaction) => { | ||
| await assertPayFrame(transaction) | ||
| return [ownerSignatureEntry(owner, owner.address)] |
There was a problem hiding this comment.
For a P-256 owner this default produces a scheme-2 entry with an explicit signer, but the only P-256 payer test (test/accounts.test.ts:286) overrides getSignatureEntries and signFrameTransaction and calls signFrameSignature(tx, 1, owner) itself, so it checks its own callbacks rather than the defaults the PR description lists as covered. Breaking the scheme, the explicit signer, or the explicitSigner lookup for scheme 2 would all go unnoticed. Building toFramePayerAccount({ owner: p256Owner, address: OTHER }) with no overrides, a pay frame targeting OTHER and signatures: [], then asserting the entry is { scheme: 2, signer: owner.address } and that it verifies with P256.verify, would cover the real path.
| (async () => { | ||
| const useImplicitSigner = | ||
| parameters.delegateAddress === undefined && | ||
| ownerSignatureScheme(owner) === 1 && |
There was a problem hiding this comment.
This condition decides whether a P-256 owner whose identity equals the sender gets the explicit signer that DESIGN.md requires for P256 entries, but no test reaches it: the P-256 tests either pass a distinct address or bring their own entry (test/p256.test.ts:217). Removing ownerSignatureScheme(owner) === 1 would emit signer: null for that owner, the lookup would still match because a null signer resolves to the sender, and nothing would fail. Starting the test at test/p256.test.ts:217 from signatures: [] and asserting signatures[0].signer === signer.address would cover it.
Reject ambiguous addresses and unsafe self-paying payer defaults, strengthen delegation validation, and add regression coverage for P-256 and shared-owner signing
|
thanks for checking again @mariano-aguero fixes are in place, can you take a look again 🙏 |
| await assertPayFrame(transaction) | ||
|
|
||
| let prepared = transaction | ||
| let signatureIndex = findOwnerSignatureIndex(prepared, owner, 'last', { |
There was a problem hiding this comment.
When one secp256k1 key owns both a delegated sender (with an explicit signer) and the default payer, and the transaction reaches signing without prepared entries, the payer's search for its last explicit-signer entry finds the sender's own entry at index 0. It skips appending, re-signs that entry, and returns a transaction with one signature. The pay frame then fails on-chain with no local error, while a payer with a different key gets the clear 'other entries are already signed' error for the same input. Looking up the payer entry at its protocol index (index 1 for the codeless default), or skipping entries that are already signed, would close this.
|
thanks for the review @Ghadi8 it is ready to be reviewed again |
mariano-aguero
left a comment
There was a problem hiding this comment.
Task: done. The specialized constructors are replaced, and the unified defaults produce the same addresses, nonces, entries and signatures as the base constructors for EOA, P-256 and codeless payer inputs.
Standard: close. One residual signing case on the non-codeless payer path and one undeclared signature change on the kept constructors (inline).
Verified at a349b4b: frozen install, typecheck, build, attw (no problems), 399 tests, a base vs head differential over the constructor outputs, and mutation testing on the changed lines. Not verified: test:live, and no CI ran on this head.
Release: .releaserc.json uses the default angular preset, which does not read ! in the header. A squash with only the title produces no release, so the squash body needs to keep the BREAKING CHANGE: footer, which makes this 1.0.0. CONTRIBUTING also says feat!: triggers a major, which is not true under this preset.
Description: scripts/temp.ts is not in the PR, and no committed test combines a P-256 sender with a secp256k1 payer. The test count is now 399, not 375. The AI assistance section of the template is missing.
Happy to re-review after the changes. The tests for the default payer guards read clearly, and each one fails when its guard is removed.
| let prepared = transaction | ||
| let signatureIndex = findOwnerSignatureIndex(prepared, owner, 'last', { | ||
| explicitSigner: true, | ||
| }) | ||
| if (signatureIndex === -1) { | ||
| if (transaction.signatures.some((entry) => entry.signature !== '0x')) | ||
| throw new FrameEncodeError( | ||
| `frame payer ${address} has no entry and other entries are already signed; install payer entries during preparation`, | ||
| ) | ||
| const entries = await getSignatureEntries(transaction) | ||
| prepared = { | ||
| ...transaction, | ||
| signatures: [...transaction.signatures, ...entries], | ||
| } | ||
| signatureIndex = findOwnerSignatureIndex(prepared, owner, 'last', { | ||
| explicitSigner: true, | ||
| }) | ||
| } |
There was a problem hiding this comment.
Should: When the payer is not the codeless default (address differs from owner.address) and shares its secp256k1 key with a delegated sender that uses an explicit signer, signing from signatures: [] lets the sender append and sign index 0, and this 'last' lookup then finds that same signed entry. The payer re-signs index 0 instead of appending its own entry, and signFrameTransaction returns a transaction with a single signature that passes assertValidFrameTx and fails at the pay frame on-chain.
The codeless path got the index-1 fix in a349b4b, but this path still has the case from the earlier thread on this line.
Suggestion: match only unsigned entries here, by adding an unsigned option to findOwnerSignatureIndex ((!options.unsigned || signature.signature === '0x')) and passing it in both payer lookups. With that change the already-signed guard below fires as intended and the suite stays green.
A test that builds toFrameAccount({ client, owner, address: sender, delegateAddress }) (with eth_getCode returning 0xef0100<delegate>) and toFramePayerAccount({ owner, address: payerAddress }), then expects signFrameTransaction(account, { ...tx, signatures: [] }, { payer }) to reject, would pin it. Right now, removing the guard at L193-196 or the explicitSigner filter keeps every test passing.
| if (owner?.type === 'p256' && parameters.address === undefined) | ||
| throw new FrameEncodeError( | ||
| 'P-256 frame accounts require an explicit address', | ||
| ) |
There was a problem hiding this comment.
Should: A P-256 owner with getAddress and no address is now rejected, by the getAddress?: never type and at runtime with "require an explicit address", even though an address resolver was supplied. On the base commit the generic toFrameAccount and toFramePayerAccount accepted that shape, and the removed payer test used it. The same applies to toFramePayerAccount.ts L82-85 and the types at L42-44.
The generic parameters of ToFrameAccountParameters and ToFrameAccountReturnType also changed from <implementation> to <chain, owner, extend>.
Suggestion: accept getAddress here (parameters.address === undefined && parameters.getAddress === undefined, dropping getAddress?: never), as asked in the earlier thread, or else list both changes in the BREAKING CHANGE: footer and in Release impact so downstream users see them in the changelog.
| type DefaultFrameAccountParameters< | ||
| chain extends Chain | undefined, | ||
| owner extends FrameAccountOwner, | ||
| extend extends object, | ||
| > = FrameAccountBaseParameters<chain, owner, extend> & | ||
| (owner extends P256FrameSigner | ||
| ? { owner: owner; address: Address; getAddress?: never } | ||
| : { owner: owner }) |
There was a problem hiding this comment.
Nit: With an owner present, the default branch accepts any viem Account, so toFrameAccount({ client, owner: jsonRpcAccount }) with no sign override compiles and constructs, and only fails at signing with "cannot sign a raw 32-byte digest". The removed EoaFrameOwner type caught this at compile time.
Suggestion: require sign in the parameter type when the owner is a JsonRpcAccount (for example owner extends JsonRpcAccount ? { sign: RawFrameSigner } : {}), here and in the payer parameters.
| const entries = await getSignatureEntries(transaction) | ||
| prepared = { | ||
| ...transaction, | ||
| signatures: [...entries, ...transaction.signatures], |
There was a problem hiding this comment.
Nit: Changing this prepend to an append keeps the whole suite green, but the order decides whether a codeless payer entry that is already installed stays at index 1.
Suggestion: add a test that signs a transaction holding only an unsigned codeless payer entry and asserts the sender entry lands at index 0 and the payer at index 1.
What this changes
Replaces the specialized
toEoaFrameAccount,toP256FrameAccount, andtoEoaFramePayerAccountconstructors with the unifiedtoFrameAccountandtoFramePayerAccountAPIs.The unified constructors infer defaults from the supplied owner while allowing account-specific behavior to be overridden.
Why
The specialized constructors duplicated address resolution, nonce handling, signature-entry construction, and signing behavior.
A unified API gives applications one consistent integration point for:
privateKeyToAccountThis also makes it possible to add future authorization mechanisms without introducing another top-level constructor for every account type.
How the new behaviour is pinned
Which oracle covers this change, and why it could actually fail:
frame_tx_wire_tests.rsmaxCost, or thegasUseddecomposition)The account tests independently recover generated secp256k1 signatures and verify P-256 signatures. Existing wire-format, captured-chain, signature, gas, and receipt-oracle tests continue to pass unchanged.
Load-bearing rules
Did this touch anything under "Rules that are not style preferences" in
CONTRIBUTING.md, such as a derived gas constant, a golden vector, a captured fixture, the module import order, orencodeFrameTxvalidating?Checks
Only tick what you ran.
bun run testbun run typecheckbun run build && bunx @arethetypeswrong/cli --pack .(the build passed;@arethetypeswrong/clihas not been run)bun run test:live(optional, hits the public endpoint, not a gate)Additional verification:
scripts/temp.ts.Release impact
The commit history sets the version, so say which this is:
docs,chore,test,ci,refactor,style,perf,build)fix)feat)feat!or aBREAKING CHANGE:footer)This removes the following public exports:
toEoaFrameAccounttoP256FrameAccounttoEoaFramePayerAccountMigration examples: