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: added getAddressAtIndex method and API #425

Merged
merged 12 commits into from
Jun 6, 2023

Conversation

andreabadesso
Copy link
Collaborator

@andreabadesso andreabadesso commented Jun 1, 2023

Motivation

Currently the getAddressAtIndex method in the wallet service facade is broken since it expects this.seed to have the seed loaded and we don't keep it in memory, so it derives an address from an empty Mnemonic:

https://github.com/HathorNetwork/hathor-wallet-lib/blob/652b94e72021dd11cd0372d3a5b750d9d83f0e29/src/wallet/wallet.ts#L991

We could ask for the user's PIN and use his seed to derive the address at an index, but that would also force us to also ask the PIN on the fullnode facade, which is something we don't want to do.

Acceptance Criteria

  • We should be able to fetch an address by its index
  • We should include flake.nix, flake.lock and direnv files to setup a working nodejs14 environment using nix flakes

Security Checklist

  • Make sure you do not include new dependencies in the project unless strictly necessary and do not include dev-dependencies as production ones. More dependencies increase the possibility of one of them being hijacked and affecting us.

@andreabadesso andreabadesso self-assigned this Jun 1, 2023
@codecov
Copy link

codecov bot commented Jun 1, 2023

Codecov Report

Merging #425 (a3a71a4) into master (196e53f) will increase coverage by 0.03%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #425      +/-   ##
==========================================
+ Coverage   95.56%   95.60%   +0.03%     
==========================================
  Files          40       40              
  Lines        2436     2458      +22     
  Branches      274      278       +4     
==========================================
+ Hits         2328     2350      +22     
  Misses        108      108              
Impacted Files Coverage Δ
src/api/utils.ts 96.66% <ø> (ø)
src/types.ts 98.65% <ø> (ø)
src/api/addresses.ts 98.24% <100.00%> (+0.62%) ⬆️
src/api/errors.ts 100.00% <100.00%> (ø)
src/db/index.ts 99.67% <100.00%> (+<0.01%) ⬆️

@@ -0,0 +1,24 @@
{
Copy link
Collaborator

Choose a reason for hiding this comment

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

If this file accepts comments, I think we should explain what this is.

Maybe also mention in the README (or some other doc that we would link in the README) that we have these configs and how to use them.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done in 5c9ab39

src/db/index.ts Outdated
@@ -7,7 +7,7 @@
import { strict as assert } from 'assert';
import { ServerlessMysql } from 'serverless-mysql';
import { get } from 'lodash';
import { OkPacket } from 'mysql';
import { OkPacket } from 'mysql2';
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why this change was needed?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm uncertain if this change should be made here. I feel that having it in its own PR would make it easy for future developers to understand that:

  1. No other changes on the application were necessary (in case of a one-line PR), or
  2. What changes had to be made with this lib update

Copy link
Collaborator Author

@andreabadesso andreabadesso Jun 5, 2023

Choose a reason for hiding this comment

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

We have both mysql and mysql2 on the project, mysql2 is used by sequelize during migrations

Both OkPacket are exactly the same on both libraries, I changed this because I needed a type from mysql2 but ended up not using it and forgot to rollback to mysql instead of mysql2

Changed mysql2 to mysql in 79b8d38

const addresses = await mysql.query<AddressInfo[]>(
`
SELECT \`address\`, \`index\`, \`transactions\`
FROM \`address\` pd
Copy link
Collaborator

Choose a reason for hiding this comment

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

For an address to be available in this table, we need to have had a transaction involving the address, right?

I'm asking this because, if this is true, then this new API you're building won't be able to actually derive a new address at an index, right?

I don't know how this API will be used to evaluate whether this is a limitation or not

Copy link
Collaborator Author

@andreabadesso andreabadesso Jun 5, 2023

Choose a reason for hiding this comment

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

You're partially correct

We also make sure that the wallet always have at least MAX_GAP addresses starting from the last used index

So you're correct that this API won't be able to actually derive a new address at an index, but that's the expected behavior since this is exactly what the old facade does, so this is definetly a limitation but is not a problem for our use case

I added a notice in the API docs in 724fde2

src/api/addresses.ts Outdated Show resolved Hide resolved
src/db/index.ts Outdated
@@ -7,7 +7,7 @@
import { strict as assert } from 'assert';
import { ServerlessMysql } from 'serverless-mysql';
import { get } from 'lodash';
import { OkPacket } from 'mysql';
import { OkPacket } from 'mysql2';
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm uncertain if this change should be made here. I feel that having it in its own PR would make it easy for future developers to understand that:

  1. No other changes on the application were necessary (in case of a one-line PR), or
  2. What changes had to be made with this lib update

src/types.ts Outdated Show resolved Hide resolved
tests/api.test.ts Outdated Show resolved Hide resolved
@andreabadesso andreabadesso merged commit 6222276 into master Jun 6, 2023
@andreabadesso andreabadesso deleted the feat/get-address-at-index branch June 6, 2023 14:59
@andreabadesso andreabadesso mentioned this pull request Jul 24, 2023
2 tasks
@andreabadesso andreabadesso mentioned this pull request Aug 25, 2023
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants