Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ledger signer 221 #1246

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

PhilippeR26
Copy link
Collaborator

@PhilippeR26 PhilippeR26 commented Oct 11, 2024

Motivation and Resolution

Ledger has released a new version 2.2.1 of the Starknet APP.
It includes improvements, including a comprehensive display of the details of:

  • V1 & V3 transactions.
  • V1& V3 deployments.
    20241011_100145

Starknet.js is now able to use APP v1.1.1 & v2.2.1.

Usage related changes

Important

No breaking changes for code using Ledger APP v1.1.1 (just no comprehensive display, only blind signatures)

  • For users still using v1.1.1 :
    No change of the code :
const myLedgerSigner = new LedgerSigner(myLedgerTransport, 0);

LedgerSigner and LedgerSigner111 classes are equivalent.

  • For new users, using v2.2.1 to create a new account :
const myLedgerSigner = new LedgerSigner221(myLedgerTransport, 0);

They can also use the v1.1.1 signer ; they will only not have access to the display of transaction details.

  • For new users, using v1.1.1 to create a new account that will be natively compatible with v2.2.1 :
const myLedgerSigner = new LedgerSigner111(myLedgerTransport, 0, undefined, getLedgerPathBuffer221);
  • For users that have created an account with V1.1.1, and are now using v2.2.1 :
const myLedgerSigner = new LedgerSigner221(myLedgerTransport, 0, getLedgerPathBuffer111);

Development related changes

LedgerSigner and getLedgerPathBuffer are still there, and are related to v1.1.1 APP.
They are now also named LedgerSigner111 and getLedgerPathBuffer111.

LedgerSigner221 and getLedgerPathBuffer221 have been created to handle v2.2.1.

As several functions are common for both signers, the LedgerSigner221 is an extension of the LedgerSigner111.

Some maths included in hashFeeField (utils/hash/transactionHash/v3.ts) are necessary, and have been isolated in exportable functions.

The new signer has been tested successfully in Node ( https://github.com/PhilippeR26/starknet.js-workshop-typescript/blob/main/src/scripts/ledgerNano/6.testLedgerAccount221.ts ) and in a demo DAPP ( https://github.com/PhilippeR26/Starknet-Ledger-Wallet )

Checklist:

  • Performed a self-review of the code
  • Rebased to the last commit of the target branch (or merged it into my branch)
  • Documented the changes in code (API docs will be generated automatically)
  • Updated the tests
  • All tests are passing

@PhilippeR26 PhilippeR26 marked this pull request as ready for review October 11, 2024 10:24
Copy link
Collaborator

@tabaktoni tabaktoni left a comment

Choose a reason for hiding this comment

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

There are a couple of nitpicks but in general LGTM!

We would need to refactor this soon when v1 tx gets deprecated with RPC 0.8

@@ -354,12 +355,25 @@ describe('Ethereum signer', () => {
describe('Ledger Signer', () => {
// signature of Ledger can't be tested automatically.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

@PhilippeR26 PhilippeR26 Nov 4, 2024

Choose a reason for hiding this comment

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

Seems not to be very reliable. The dev (a French guy) that coded the APP has no NanoX, tested it with success using Speculos simulator.
On my side, I found several problems with my own NanoX. So, this simulator is useful for inital tests, but is not very reliable...

src/constants.ts Outdated
@@ -82,3 +82,7 @@ export const SNIP9_V1_INTERFACE_ID =
'0x68cfd18b92d1907b8ba3cc324900277f5a3622099431ea85dd8089255e4181';
export const SNIP9_V2_INTERFACE_ID =
'0x1d1144bb2138366ff28d8e9ab57456b1d332ac42196230c3a602003c89872';

// Ledger signer
export const HARDENING_BYTE = Number('0x80');
Copy link
Collaborator

Choose a reason for hiding this comment

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

nitpick, Can we store this as a static constant

Suggested change
export const HARDENING_BYTE = Number('0x80');
// 0x80
export const HARDENING_BYTE = 128;

Copy link
Collaborator Author

@PhilippeR26 PhilippeR26 Nov 4, 2024

Choose a reason for hiding this comment

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

Was to highlight that it's just the higher bit.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Modified.

src/constants.ts Outdated

// Ledger signer
export const HARDENING_BYTE = Number('0x80');
export const HARDENING_4BYTES = BigInt('0x80000000');
Copy link
Collaborator

Choose a reason for hiding this comment

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

nitpick , Same

Suggested change
export const HARDENING_4BYTES = BigInt('0x80000000');
// 0x80000000
export const HARDENING_4BYTES = 2147483648n

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Modified

@@ -24,17 +24,33 @@ export function hashDAMode(nonceDAMode: BigNumberish, feeDAMode: BigNumberish) {
return (BigInt(nonceDAMode) << DATA_AVAILABILITY_MODE_BITS) + BigInt(feeDAMode);
}

export function hashFeeField(tip: BigNumberish, bounds: ResourceBounds) {
const L1Bound =
export function encodeResourceBoundsL1(bounds: ResourceBounds): bigint {
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe add desc, and note what this encoding is for

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added.

@PhilippeR26
Copy link
Collaborator Author

Ledger informed me that they are preparing a new version, including a human readable account deployment .
So, hold on for this PR. I will update it once the new Ledger APP available, including at the same time your comments.

@PhilippeR26 PhilippeR26 changed the title Ledger signer 211 Ledger signer 221 Nov 4, 2024
@PhilippeR26
Copy link
Collaborator Author

PhilippeR26 commented Nov 4, 2024

The final version of the Ledger APP is 2.2.1.
The code has been updated in accordance.
Reviewers requests are implemented.
Tested in Node and in a Next.js DAPP.
The initial message of this PR has been updated.
Ready for final review.

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