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: methods, classes and tests for creating nano contract transactions using a wallet #604

Merged
merged 40 commits into from
Mar 14, 2024

Conversation

pedroferreira1
Copy link
Member

@pedroferreira1 pedroferreira1 commented Jan 19, 2024

Acceptance Criteria

  • Add base class for nano contracts.
  • Add serializer for nano fields.
  • Add builder to create nano contract transactions.
  • Integrate nano contract creation/execution in the wallet class.
  • Add methods to handle oracles.
  • Improve nano API error handling and typing.

Tests

The tests need the new nano contract code that is only available in the private repository for now, that's why I changed the default docker image for the integration tests.

The latest version of hathor-core added a protection, so we can only enable nano contract if the network is called nano-testnet-alpha, then I had to change the network used in the integration tests to use testnet, instead of privatenet. (We interpret nano-testnet-alpha as a testnet network, just like we do with testnet-foxtrot).

  • Update docker image in the integration tests to use a custom one with nano support.
  • Unit tests for serializer class.
  • Integration tests for nano operations using Bet blueprint.
  • Add helper methods to wait for tx to be received and get first block confirming a tx.

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.

Copy link

codecov bot commented Feb 27, 2024

Codecov Report

Attention: Patch coverage is 83.83234% with 54 lines in your changes are missing coverage. Please review.

Project coverage is 79.13%. Comparing base (6ec738c) to head (2f42d54).

Files Patch % Lines
src/api/nano.ts 62.16% 14 Missing ⚠️
src/nano_contracts/builder.ts 87.73% 13 Missing ⚠️
src/nano_contracts/utils.ts 77.58% 13 Missing ⚠️
src/errors.ts 60.00% 4 Missing ⚠️
src/nano_contracts/serializer.ts 93.87% 3 Missing ⚠️
src/new/wallet.js 88.88% 3 Missing ⚠️
src/utils/address.ts 70.00% 3 Missing ⚠️
src/nano_contracts/nano_contract.ts 96.87% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev     #604      +/-   ##
==========================================
+ Coverage   78.33%   79.13%   +0.79%     
==========================================
  Files          69       73       +4     
  Lines        5419     5727     +308     
  Branches     1151     1214      +63     
==========================================
+ Hits         4245     4532     +287     
- Misses       1157     1178      +21     
  Partials       17       17              

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

@pedroferreira1 pedroferreira1 force-pushed the feat/nano-wallet-headless-integration branch from ba9c684 to 4f2c18f Compare March 1, 2024 02:37
* @memberof Errors
* @inner
*/
export class NanoContractTransactionParseError extends Error {
Copy link
Member Author

Choose a reason for hiding this comment

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

This is only used in the explorer integration PR but I ended up adding it to this PR by mistake. Since it's only an error class definition, I decided to leave it here.

@pedroferreira1 pedroferreira1 requested review from r4mmer and andreabadesso and removed request for r4mmer March 1, 2024 14:14
src/api/nano.ts Outdated Show resolved Hide resolved
src/api/nano.ts Show resolved Hide resolved
src/new/wallet.js Outdated Show resolved Hide resolved
src/new/wallet.js Outdated Show resolved Hide resolved
src/new/wallet.js Outdated Show resolved Hide resolved
src/new/wallet.js Outdated Show resolved Hide resolved
src/new/wallet.js Outdated Show resolved Hide resolved
src/nano_contracts/builder.ts Outdated Show resolved Hide resolved
src/nano_contracts/builder.ts Outdated Show resolved Hide resolved
src/nano_contracts/builder.ts Outdated Show resolved Hide resolved
src/new/wallet.js Show resolved Hide resolved
src/new/wallet.js Outdated Show resolved Hide resolved
src/new/wallet.js Outdated Show resolved Hide resolved
__tests__/integration/helpers/wallet.helper.js Outdated Show resolved Hide resolved
__tests__/integration/nanocontracts/bet.test.js Outdated Show resolved Hide resolved
__tests__/integration/nanocontracts/bet.test.js Outdated Show resolved Hide resolved
Comment on lines 196 to 198
const outputData = {
address: changeAddressStr
} as IDataOutput
Copy link
Member

Choose a reason for hiding this comment

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

This cast works since the createOutputScript only requires the address for p2sh and p2pkh addresses to work.
But I dont particularly like a wrongful cast since the util can change later and this may stop working.

Since createOutputScript is a complete util (it works with timelock, data outputs, etc.) we can create a simpler util that receives an address and creates the output script from it, what do you think?

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes sense. Done c52a91c

throw new Error('Signed data requires 3 parameters.');
}
// First value must be a Buffer but comes as hex
splittedValue[0] = hexToBuffer(splittedValue[0]);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
splittedValue[0] = hexToBuffer(splittedValue[0]);
const inputData = hexToBuffer(splittedValue[0]);
const type = splittedValue[2];

Copy link
Member Author

Choose a reason for hiding this comment

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

Done 2f42d54

Comment on lines 195 to 203
if (splittedValue[2] === 'bytes') {
// If the result is expected as bytes, it will come here in the args as hex value
splittedValue[1] = hexToBuffer(splittedValue[1]);
} else if (splittedValue[2] === 'bool') {
// If the result is expected as boolean, it will come here as a string true/false
splittedValue[1] = splittedValue[1] === 'true';
}

const [inputData, value, type] = splittedValue;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (splittedValue[2] === 'bytes') {
// If the result is expected as bytes, it will come here in the args as hex value
splittedValue[1] = hexToBuffer(splittedValue[1]);
} else if (splittedValue[2] === 'bool') {
// If the result is expected as boolean, it will come here as a string true/false
splittedValue[1] = splittedValue[1] === 'true';
}
const [inputData, value, type] = splittedValue;
let value: Buffer | string | bool;
if (type === 'bytes') {
// If the result is expected as bytes, it will come here in the args as hex value
value = hexToBuffer(splittedValue[1]);
} else if (type === 'bool') {
// If the result is expected as boolean, it will come here as a string true/false
value = splittedValue[1] === 'true';
} else {
// Assuming this is a string
value = splittedValue[1];
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Done 2f42d54

this.pushLenValue(ret, serialized.length);
ret.push(serialized);

ret.push(this.fromBytes(inputData));
Copy link
Member

Choose a reason for hiding this comment

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

This is giving a warning because fromBytes is returning a string when ret is a buffer array.
Is this right?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is probably a wrong warning, I don't see it anywhere. But this method returns a Buffer

Comment on lines 47 to 48
serializeFromType(value: any, type: string): Buffer {
switch (type) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
serializeFromType(value: any, type: string): Buffer {
switch (type) {
serializeFromType(value: any, type: string): Buffer {
if (type.startsWith('SignedData[')) {
return this.fromSigned(value as string);
}
switch (type) {

Copy link
Member Author

Choose a reason for hiding this comment

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

Done 2f42d54

Comment on lines 331 to 337
let serialized: Buffer;
if (arg.type.startsWith('SignedData[')) {
serialized = serializer.fromSigned(this.args[index]);
} else {
serialized = serializer.serializeFromType(this.args[index], arg.type);
}
serializedArgs.push(serialized);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
let serialized: Buffer;
if (arg.type.startsWith('SignedData[')) {
serialized = serializer.fromSigned(this.args[index]);
} else {
serialized = serializer.serializeFromType(this.args[index], arg.type);
}
serializedArgs.push(serialized);
serializedArgs.push(serializer.serializeFromType(this.args[index], arg.type));

Copy link
Member Author

Choose a reason for hiding this comment

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

Done 2f42d54

@pedroferreira1 pedroferreira1 merged commit d651394 into dev Mar 14, 2024
6 checks passed
@pedroferreira1 pedroferreira1 deleted the feat/nano-wallet-headless-integration branch March 14, 2024 12:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants