-
Notifications
You must be signed in to change notification settings - Fork 14
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
Conversation
d9771e6
to
391b98f
Compare
Codecov ReportAttention: Patch coverage is
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. |
…ons using a wallet
…racle data to sign hash
ba9c684
to
4f2c18f
Compare
* @memberof Errors | ||
* @inner | ||
*/ | ||
export class NanoContractTransactionParseError extends Error { |
There was a problem hiding this comment.
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.
…ilder and utils, so it can be reused
…it for multisig tests in the future
src/nano_contracts/builder.ts
Outdated
const outputData = { | ||
address: changeAddressStr | ||
} as IDataOutput |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. Done c52a91c
…thod on the Serializer class
src/nano_contracts/serializer.ts
Outdated
throw new Error('Signed data requires 3 parameters.'); | ||
} | ||
// First value must be a Buffer but comes as hex | ||
splittedValue[0] = hexToBuffer(splittedValue[0]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
splittedValue[0] = hexToBuffer(splittedValue[0]); | |
const inputData = hexToBuffer(splittedValue[0]); | |
const type = splittedValue[2]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done 2f42d54
src/nano_contracts/serializer.ts
Outdated
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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]; | |
} |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
src/nano_contracts/serializer.ts
Outdated
serializeFromType(value: any, type: string): Buffer { | ||
switch (type) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done 2f42d54
src/nano_contracts/builder.ts
Outdated
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done 2f42d54
…rove fromSigned method
Acceptance Criteria
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 callednano-testnet-alpha
, then I had to change the network used in the integration tests to usetestnet
, instead ofprivatenet
. (We interpretnano-testnet-alpha
as atestnet
network, just like we do withtestnet-foxtrot
).Security Checklist