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

Implemented the Message and MessageComputer classes #378

Merged
merged 6 commits into from
Feb 20, 2024

Conversation

popenta
Copy link
Contributor

@popenta popenta commented Feb 13, 2024

Implemented the Message and MessageComputer classes based on the sdk-specs.

@popenta popenta self-assigned this Feb 13, 2024
Copy link
Contributor

@andreibancioiu andreibancioiu left a comment

Choose a reason for hiding this comment

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

🚀

src/message.ts Outdated
/**
* Number representing the message version.
*/
public version?: number;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can be non-optional.

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

src/message.ts Outdated
this.data = options.data;
this.signature = options.signature;
this.address = options.address;
this.version = options.version ? options.version : DEFAULT_MESSAGE_VERSION;
Copy link
Contributor

Choose a reason for hiding this comment

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

Somehow unexpected when version will be set to 0 (but all right in practice, since there is no version 0). Maybe check for undefined instead of options.version?

(optional)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As discussed, since version = 0 it's not valid it ok how it is.

Copy link
Contributor

Choose a reason for hiding this comment

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

this.version = options.version || DEFAULT_MESSAGE_VERSION would be shorter
(optional)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed

src/message.ts Outdated
}

unpackMessage(packedMessage: { message: string; signature?: string; address?: string; version?: number }): Message {
let dataHex = this.trimHexPrefix(packedMessage.message);
Copy link
Contributor

Choose a reason for hiding this comment

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

const if possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed to const

src/message.ts Outdated
Comment on lines 66 to 70
let signature: Uint8Array | undefined = undefined;
if (packedMessage.signature) {
let signatureHex = this.trimHexPrefix(packedMessage.signature);
signature = Buffer.from(signatureHex, "hex");
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can also be (proposal):

const signatureHex = this.trimHexPrefix(packedMessage.signature || "");
const signature = Buffer.from(signatureHex, "hex")

Will be an empty buffer if signature not provided, but that is maybe even better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Replaced with your proposal. Thanks!

@@ -13,6 +13,9 @@ interface IConfig {
gasLimitPerByte: BigNumber.Value;
}

/**
* Use this class to create both RelyedV1 and RelayedV2 transactions.
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

src/interface.ts Outdated
}
}

export interface IMessage {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe here we can live without the interface (related to the brainstorming on sdk-py).

Thus, maybe the DTO should suffice, for now? Then introduce an interface when needed? CC: @ccorcoveanu, as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed the interface.

const packedMessage = messageComputer.packMessage(message);
assert.deepEqual(packedMessage, {
address: "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th",
message: "74657374",
Copy link
Contributor

Choose a reason for hiding this comment

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

And now, re-re-thinking about it 🙈

@ccorcoveanu, all right if we do it this way? That is, without the 0x prefix. Seems less cumbersome. And when people see signature they automatically think: well, that is hex, of course. And message is the same, for consistency. How do you feel about it?

For context: #375

Copy link
Contributor

Choose a reason for hiding this comment

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

I like the no-0x version better

return createKeccakHash("keccak256").update(bytesToHash).digest();
}

serializeForSigningRaw(): Buffer {
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder where is this used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Apparently it's not used anywhere. Should I delete it?

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will not delete it.

@@ -653,7 +653,7 @@ export class TransactionComputer {

const serialized = JSON.stringify(plainTransaction);

return new Uint8Array(Buffer.from(serialized));
return Buffer.from(serialized);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why this change? Won't it break compatibility for apps that strictly require Uint8Array?

I known we talk some time ago @andreibancioiu how using Buffer was not the best choice and about transitioning to Uint8Array - just trying to get up to speed if something changed along the way since then

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, in theory it would break compatibility, but this has not been yet released so there can't be any apps that use it. And since Buffer implements Uint8Array I assume it's all right.

Copy link
Contributor

@andreibancioiu andreibancioiu Feb 19, 2024

Choose a reason for hiding this comment

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

Indeed, TransactionComputer is new in v13. As of now, we've avoided the migration to Buffer in older components (we'll postpone it a bit more).

src/message.ts Outdated
this.data = options.data;
this.signature = options.signature;
this.address = options.address;
this.version = options.version ? options.version : DEFAULT_MESSAGE_VERSION;
Copy link
Contributor

Choose a reason for hiding this comment

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

this.version = options.version || DEFAULT_MESSAGE_VERSION would be shorter
(optional)

const packedMessage = messageComputer.packMessage(message);
assert.deepEqual(packedMessage, {
address: "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th",
message: "74657374",
Copy link
Contributor

Choose a reason for hiding this comment

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

I like the no-0x version better

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants