Skip to content

refactor(accounts)!: unify frame account and payer constructors - #55

Open
nikhilkumar1612 wants to merge 4 commits into
JustaLab-co:mainfrom
nikhilkumar1612:ft/to-frame-account
Open

nikhilkumar1612 wants to merge 4 commits into
JustaLab-co:mainfrom
nikhilkumar1612:ft/to-frame-account

Conversation

@nikhilkumar1612

Copy link
Copy Markdown
Contributor

What this changes

Replaces the specialized toEoaFrameAccount, toP256FrameAccount, and toEoaFramePayerAccount constructors with the unified toFrameAccount and toFramePayerAccount APIs.

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:

  • Local secp256k1 accounts created with privateKeyToAccount
  • P-256-authorized delegated accounts
  • Codeless EOA payers
  • Injected or remote accounts with an explicit raw-digest signing adapter
  • Custom validation, payment, nonce, signature-entry, and signing policies

This 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:

  • A published figure, transcribed rather than derived
  • The golden byte vector or sig-hash from ethrex's frame_tx_wire_tests.rs
  • Re-encoding captured chain data and reproducing the transaction hash
  • A live receipt (recovered signer, maxCost, or the gasUsed decomposition)
  • N/A, this change has no wire-format or gas behaviour

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, or encodeFrameTx validating?

  • No
  • Yes, and the argument for it is below

Checks

Only tick what you ran.

  • bun run test
  • bun run typecheck
  • bun run build && bunx @arethetypeswrong/cli --pack . (the build passed; @arethetypeswrong/cli has not been run)
  • bun run test:live (optional, hits the public endpoint, not a gate)

Additional verification:

  • Package ESM, CJS, and declaration builds pass.
  • 375 tests pass.
  • Local secp256k1 account defaults are covered.
  • JSON-RPC/injected-account raw-signing adapters are covered.
  • P-256 delegated sender defaults are covered.
  • Codeless secp256k1 payer defaults are covered.
  • P-256 payer ownership is covered.
  • Payer signature selection no longer assumes a fixed signature index.
  • Mixed P-256 sender and secp256k1 payer signing is exercised by scripts/temp.ts.

Release impact

The commit history sets the version, so say which this is:

  • No release (docs, chore, test, ci, refactor, style, perf, build)
  • Patch (fix)
  • Minor (feat)
  • Major (feat! or a BREAKING CHANGE: footer)

This removes the following public exports:

  • toEoaFrameAccount
  • toP256FrameAccount
  • toEoaFramePayerAccount
  • Their specialized parameter, implementation, owner, and return types

Migration examples:

// Before
const account = await toEoaFrameAccount({ client, owner })

// After
const account = await toFrameAccount({ client, owner })

@mariano-aguero mariano-aguero left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for unifying these constructors, the single entry point makes the account API much easier to follow.

Comment thread src/accounts/toFramePayerAccount.ts Resolved
Comment thread src/accounts/toFramePayerAccount.ts Resolved
Comment thread src/accounts/toFrameAccount.ts Resolved
Comment thread src/accounts/toFrameAccount.ts Resolved
Comment thread src/accounts/toFrameAccount.ts Resolved
Comment thread src/accounts/toFramePayerAccount.ts Resolved
Comment thread src/accounts/defaults.ts Resolved
Comment thread src/accounts/toFramePayerAccount.ts Resolved
Comment thread src/accounts/toFrameAccount.ts Resolved
Comment thread README.md Resolved
@nikhilkumar1612

Copy link
Copy Markdown
Contributor Author

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 mariano-aguero left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick and thorough follow-up, nikhilkumar1612, the earlier points are all addressed and the new tests read really well.

Comment thread src/accounts/toFramePayerAccount.ts Resolved
Comment thread src/accounts/toFramePayerAccount.ts Resolved
await assertPayFrame(transaction)

let prepared = transaction
let signatureIndex = findOwnerSignatureIndex(prepared, owner, 'last', {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/accounts/toFrameAccount.ts Resolved
(async () => {
const useImplicitSigner =
parameters.delegateAddress === undefined &&
ownerSignatureScheme(owner) === 1 &&

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread docs/DESIGN.md Resolved
Comment thread src/accounts/toFrameAccount.ts Resolved
Comment thread src/accounts/toFrameAccount.ts Resolved
Comment thread scripts/send-eth-p256.ts Resolved
Reject ambiguous addresses and unsafe self-paying payer defaults, strengthen delegation validation, and add regression coverage for P-256 and shared-owner signing
@nikhilkumar1612

Copy link
Copy Markdown
Contributor Author

thanks for checking again @mariano-aguero fixes are in place, can you take a look again 🙏

Comment thread src/accounts/toFrameAccount.ts Resolved
Comment thread src/accounts/toFramePayerAccount.ts Resolved
Comment thread src/accounts/toFramePayerAccount.ts Resolved
await assertPayFrame(transaction)

let prepared = transaction
let signatureIndex = findOwnerSignatureIndex(prepared, owner, 'last', {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@nikhilkumar1612

Copy link
Copy Markdown
Contributor Author

thanks for the review @Ghadi8 it is ready to be reviewed again

@mariano-aguero mariano-aguero left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +188 to +205
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,
})
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +115 to +118
if (owner?.type === 'p256' && parameters.address === undefined)
throw new FrameEncodeError(
'P-256 frame accounts require an explicit address',
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +60 to +67
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 })

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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],

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants