Skip to content

feat: parsing big int values that come from http requests #830

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

Merged
merged 37 commits into from
Apr 11, 2025

Conversation

andreabadesso
Copy link
Contributor

@andreabadesso andreabadesso commented Mar 20, 2025

Acceptance Criteria

  • We should be able to parse numbers from wallet-service requests as big ints
  • We should use zod to validate all responses from the wallet-service lambdas
  • We should fix all current types that are different from the actual responses

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 force-pushed the fix/big-int-http-requests branch from b96c062 to c1824f1 Compare March 20, 2025 16:01
@andreabadesso andreabadesso self-assigned this Mar 20, 2025
@andreabadesso andreabadesso added the enhancement New feature or request label Mar 20, 2025
@andreabadesso andreabadesso moved this from Todo to In Progress (Done) in Hathor Network Mar 20, 2025
@andreabadesso andreabadesso moved this from In Progress (Done) to In Progress (WIP) in Hathor Network Mar 20, 2025
Copy link

codecov bot commented Mar 20, 2025

Codecov Report

Attention: Patch coverage is 84.28571% with 11 lines in your changes missing coverage. Please review.

Project coverage is 82.51%. Comparing base (de961dd) to head (1752d52).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
src/wallet/api/walletApi.ts 56.52% 10 Missing ⚠️
src/wallet/wallet.ts 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #830      +/-   ##
==========================================
+ Coverage   81.36%   82.51%   +1.14%     
==========================================
  Files          83       84       +1     
  Lines        6720     6767      +47     
  Branches     1444     1445       +1     
==========================================
+ Hits         5468     5584     +116     
+ Misses       1239     1170      -69     
  Partials       13       13              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@andreabadesso andreabadesso force-pushed the fix/big-int-http-requests branch 4 times, most recently from eb54782 to 40df312 Compare March 27, 2025 17:22
Comment on lines -127 to -128
outputs: TxProposalOutputs[]; // Outputs data of the tx proposal
tokens: string[];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -449,14 +454,16 @@ export interface TxByIdTokenData {
voided: boolean;
height?: number | null;
weight: number;
balance: Balance;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

tokenId: string;
walletId: string;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same, no walletId coming from the wallet-service lambdas

@andreabadesso andreabadesso requested a review from r4mmer March 28, 2025 00:20
@andreabadesso andreabadesso moved this from In Progress (WIP) to In Progress (Done) in Hathor Network Mar 28, 2025
* Schema for token information.
*/
export const tokenInfoSchema = z.object({
id: z.string(),
Copy link
Member

Choose a reason for hiding this comment

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

Check for 64 length and hex characters?
I included the native token case as well.

Suggested change
id: z.string(),
id: z.string().regex(/^[a-fA-F0-9]{64}$|^00$/),

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great, thanks!

rewardSpendMinBlocks: z.number(),
maxNumberInputs: z.number(),
maxNumberOutputs: z.number(),
});
Copy link
Member

Choose a reason for hiding this comment

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

Currently any new keys will be ignored, but we can pass them into the final data without validation (so that new keys from the fullnode do not break the api)

Suggested change
});
}).passthrough();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

Also added a test to test for new attributes

@andreabadesso andreabadesso requested a review from r4mmer March 31, 2025 13:50
@andreabadesso andreabadesso force-pushed the fix/big-int-http-requests branch from cdfa371 to 8adf25b Compare March 31, 2025 14:08
Comment on lines 399 to 402
script: {
type: 'Buffer';
data: number[];
};
Copy link
Member

Choose a reason for hiding this comment

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

Why?
Should the script not be serialized as hex or base64?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same, can you please check?

Comment on lines 379 to 382
script: {
type: 'Buffer';
data: number[];
};
Copy link
Member

Choose a reason for hiding this comment

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

If the wallet-service is sending this "Buffer" type maybe we should change how the wallet-service is serializing the script

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey, we actually receive a simpler TxInput and TxOutput in websocket transactions, e.g.:

{
  "height": 0,
  "inputs": [
    {
      "address": "H79QdDV5i6KuW3dNJEahkUNe8a9axtHuCu",
      "timelock": null,
      "type": "P2PKH"
    }
  ],
  "nonce": 16746194,
  "outputs": [
    {
      "address": "HDY5QHiE33goHcLxFyJmgxTErwgGHDrLdy",
      "timelock": null,
      "type": "P2PKH"
    }
  ],
  "parents": [],
  "signal_bits": 0,
  "timestamp": 1743779693,
  "token_name": null,
  "token_symbol": null,
  "tx_id": "000009532ec33ba944c5d23b8dc642063539055ca31b4f6d4f534d33651ce8a3",
  "version": 1,
  "voided": false,
  "weight": 16.818364568681208
}

Fixed the type and the schema validation

this.websocket.on('new-tx', payload => this.emit('new-tx', payload.data as WsTransaction));
this.websocket.on('update-tx', payload => this.emit('update-tx', payload.data));
this.websocket.on('new-tx', payload => {
const validatedTx = parseSchema(payload.data, wsTransactionSchema);
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should put a try..catch around this or use safeParse directly.
Or do you think its better to have an unhandled error than a silent fail (only log an error) on new-tx?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right, we shouldn't throw an error here

Fixed

@andreabadesso andreabadesso force-pushed the fix/big-int-http-requests branch from 8357e31 to fe1b9fb Compare April 4, 2025 16:03
@@ -539,7 +539,7 @@ class HathorWalletServiceWallet extends EventEmitter implements IHathorWallet {
let shouldGetNewAddresses = false;

for (const output of outputs) {
if (this.newAddresses.find(newAddress => newAddress.address === output.decoded.address)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was a bug, we were not generating new addresses when a new tx came from the websocket.

@andreabadesso andreabadesso requested a review from r4mmer April 4, 2025 16:05
@andreabadesso andreabadesso removed the status in Hathor Network Apr 4, 2025
@andreabadesso andreabadesso moved this to In Progress (Done) in Hathor Network Apr 4, 2025
r4mmer
r4mmer previously approved these changes Apr 7, 2025
@andreabadesso andreabadesso force-pushed the fix/big-int-http-requests branch from 027cfbd to e6d5a2d Compare April 8, 2025 20:18
@andreabadesso andreabadesso requested a review from r4mmer April 8, 2025 20:51
r4mmer
r4mmer previously approved these changes Apr 8, 2025
@andreabadesso andreabadesso moved this from In Progress (Done) to In Progress (WIP) in Hathor Network Apr 10, 2025
@andreabadesso andreabadesso force-pushed the fix/big-int-http-requests branch from 2e0f831 to 91fa491 Compare April 10, 2025 18:34
@andreabadesso andreabadesso force-pushed the fix/big-int-http-requests branch from 91fa491 to d09df09 Compare April 10, 2025 18:43
pedroferreira1
pedroferreira1 previously approved these changes Apr 11, 2025
@pedroferreira1 pedroferreira1 moved this from In Progress (WIP) to In Review (WIP) in Hathor Network Apr 11, 2025
@andreabadesso andreabadesso merged commit 13cf37e into master Apr 11, 2025
5 checks passed
@github-project-automation github-project-automation bot moved this from In Review (WIP) to Waiting to be deployed in Hathor Network Apr 11, 2025
@andreabadesso andreabadesso deleted the fix/big-int-http-requests branch April 11, 2025 18:49
@andreabadesso andreabadesso moved this from Waiting to be deployed to Done in Hathor Network Apr 17, 2025
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