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

feat: update SubAccount.formID to handle bigger numbers #526

Merged
merged 5 commits into from Feb 14, 2024

Conversation

ENJATZ
Copy link
Contributor

@ENJATZ ENJATZ commented Jan 18, 2024

Motivation

I am having a scenario where I need to handle more than 256 subaccounts. This is my workaround for now:

const principal = identity.getPrincipal()

const generatedAccountId = AccountIdentifier.fromPrincipal({
    principal,
    subAccount: SubAccount.fromBytes(numberToUint8Array32(newIndexNumber)),
})

where numberToUint8Array32 is the updated fromID method.

Changes

Updates the SubAccount.fromID method to handle up to nine quadrillion subaccounts (which is the max safe int).

It creates a buffer of 32 bytes and uses writeBigInt64BE to write the id using a 24 offest.

Tests

Added one more test for fromID method under account_identifier.spec.ts. All tests are passing.

Todos

  • Enjoy!

@sa-github-api
Copy link
Collaborator

Dear @ENJATZ,

In order to potentially merge your code in this open-source repository and therefore proceed with your contribution, we need to have your approval on DFINITY's CLA.

If you decide to agree with it, please visit this issue and read the instructions there. Once you have signed it, re-trigger the workflow on this PR to see if your code can be merged.

— The DFINITY Foundation

Copy link
Member

@peterpeterparker peterpeterparker left a comment

Choose a reason for hiding this comment

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

Hi,

Thanks for the PR.

Subaccounts seems indeed to be arbitrary 32-byte values source (I'm happy if one of my colleague confirm this) but I think NNS dapp limits the number of subaccounts to 256 source. That's why this limitation has probably been historically developed.

Therefore, the team would also need to replicate the logic in NNS dapp codebase before being able to merge this. I'm not mentioning this as a deal-breaker, just pointing out that it might require additional work on that side too.

packages/ledger-icp/src/account_identifier.ts Outdated Show resolved Hide resolved
@peterpeterparker
Copy link
Member

Therefore, the team would also need to replicate the logic in NNS dapp codebase before being able to merge this. I'm not mentioning this as a deal-breaker, just pointing out that it might require additional work on that side too.

My colleague @lmuntaner pointed out that NNS dapp frontend actually does not seem to use Subaccount.fromId on the client side, except fromId(0), so actually it would probably not need further work on that side, that's good.

Copy link
Member

@peterpeterparker peterpeterparker left a comment

Choose a reason for hiding this comment

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

LGTM, thanks a lot!

Copy link
Contributor

@lmuntaner lmuntaner left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks!

@peterpeterparker peterpeterparker merged commit a235169 into dfinity:main Feb 14, 2024
10 checks passed
@peterpeterparker
Copy link
Member

@ENJATZ: We just released your change in @dfinity/ledger-icp@2.2.0. Let us know how it goes and thanks for the PR.

github-merge-queue bot pushed a commit to dfinity/nns-dapp that referenced this pull request Feb 14, 2024
# Motivation

We just released a new
[version](https://github.com/dfinity/ic-js/releases/tag/2024.02.14-1600Z)
of ic-js libs, this PR bump those dependencies to their next version.

In comparison to the libs already included in main, extending ICP
`Subaccount.fromId` to accept more than 256 (see
[PR](dfinity/ic-js#526)) is the only notable
change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants