-
Notifications
You must be signed in to change notification settings - Fork 37
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0a34a8c
implement message class according to specs
popenta 65230d0
add empty lines
popenta e7e10fe
add short description for the transaction factories
popenta 9b7f2f2
fixes after review
popenta 80a4fce
fixes after review
popenta 2a7ca04
Merge branch 'feat/next' into message-and-msg-computer
popenta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { assert } from "chai"; | ||
import { Message, MessageComputer } from "./message"; | ||
import { loadTestWallets, TestWallet } from "./testutils"; | ||
import { UserVerifier } from "@multiversx/sdk-wallet"; | ||
import { DEFAULT_MESSAGE_VERSION } from "./constants"; | ||
|
||
describe("test message", () => { | ||
let alice: TestWallet; | ||
const messageComputer = new MessageComputer(); | ||
|
||
before(async function () { | ||
({ alice } = await loadTestWallets()); | ||
}); | ||
|
||
it("should test message compute bytes for signing", async () => { | ||
const data = Buffer.from("test message"); | ||
|
||
const message = new Message({ | ||
data: data, | ||
}); | ||
|
||
const serialized = messageComputer.computeBytesForSigning(message); | ||
|
||
assert.equal( | ||
Buffer.from(serialized).toString("hex"), | ||
"2162d6271208429e6d3e664139e98ba7c5f1870906fb113e8903b1d3f531004d", | ||
); | ||
}); | ||
|
||
it("should create, sign, pack, unpack and verify message", async () => { | ||
const data = Buffer.from("test"); | ||
|
||
const message = new Message({ | ||
data: data, | ||
address: alice.getAddress(), | ||
}); | ||
|
||
message.signature = await alice.signer.sign(Buffer.from(messageComputer.computeBytesForSigning(message))); | ||
|
||
assert.equal( | ||
Buffer.from(message.signature).toString("hex"), | ||
"7aff43cd6e3d880a65033bf0a1b16274854fd7dfa9fe5faa7fa9a665ee851afd4c449310f5f1697d348e42d1819eaef69080e33e7652d7393521ed50d7427a0e", | ||
); | ||
|
||
const packedMessage = messageComputer.packMessage(message); | ||
assert.deepEqual(packedMessage, { | ||
address: "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th", | ||
message: "74657374", | ||
signature: | ||
"7aff43cd6e3d880a65033bf0a1b16274854fd7dfa9fe5faa7fa9a665ee851afd4c449310f5f1697d348e42d1819eaef69080e33e7652d7393521ed50d7427a0e", | ||
version: 1, | ||
}); | ||
|
||
const unpackedMessage = messageComputer.unpackMessage(packedMessage); | ||
assert.deepEqual(unpackedMessage.address, alice.getAddress()); | ||
assert.deepEqual(unpackedMessage.data, message.data); | ||
assert.deepEqual(unpackedMessage.signature, message.signature); | ||
assert.deepEqual(unpackedMessage.version, message.version); | ||
|
||
const verifier = UserVerifier.fromAddress(alice.getAddress()); | ||
const isValid = verifier.verify( | ||
Buffer.from(messageComputer.computeBytesForVerifying(unpackedMessage)), | ||
Buffer.from(unpackedMessage.signature!), | ||
); | ||
assert.equal(isValid, true); | ||
}); | ||
|
||
it("should unpack legacy message", async () => { | ||
const legacyMessage = { | ||
address: "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th", | ||
message: "0x7468697320697320612074657374206d657373616765", | ||
signature: | ||
"0xb16847437049986f936dd4a0917c869730cbf29e40a0c0821ca70db33f44758c3d41bcbea446dee70dea13d50942343bb78e74979dc434bbb2b901e0f4fd1809", | ||
version: 1, | ||
signer: "ErdJS", | ||
}; | ||
|
||
const message = messageComputer.unpackMessage(legacyMessage); | ||
assert.deepEqual(message.address, alice.getAddress()); | ||
assert.deepEqual(Buffer.from(message.data).toString(), "this is a test message"); | ||
assert.deepEqual( | ||
Buffer.from(message.signature!).toString("hex"), | ||
"b16847437049986f936dd4a0917c869730cbf29e40a0c0821ca70db33f44758c3d41bcbea446dee70dea13d50942343bb78e74979dc434bbb2b901e0f4fd1809", | ||
); | ||
assert.deepEqual(message.version, DEFAULT_MESSAGE_VERSION); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { IAddress } from "./interface"; | ||
import { DEFAULT_MESSAGE_VERSION, MESSAGE_PREFIX } from "./constants"; | ||
import { Address } from "./address"; | ||
|
||
const createKeccakHash = require("keccak"); | ||
|
||
export class Message { | ||
/** | ||
* Actual message being signed. | ||
*/ | ||
public data: Uint8Array; | ||
/** | ||
* The message signature. | ||
*/ | ||
public signature?: Uint8Array; | ||
/** | ||
* Address of the wallet that performed the signing operation. | ||
*/ | ||
public address?: IAddress; | ||
/** | ||
* Number representing the message version. | ||
*/ | ||
public version: number; | ||
|
||
constructor(options: { data: Uint8Array; signature?: Uint8Array; address?: IAddress; version?: number }) { | ||
this.data = options.data; | ||
this.signature = options.signature; | ||
this.address = options.address; | ||
this.version = options.version || DEFAULT_MESSAGE_VERSION; | ||
} | ||
} | ||
|
||
export class MessageComputer { | ||
constructor() {} | ||
|
||
computeBytesForSigning(message: Message): Uint8Array { | ||
const messageSize = Buffer.from(message.data.length.toString()); | ||
const signableMessage = Buffer.concat([messageSize, message.data]); | ||
let bytesToHash = Buffer.concat([Buffer.from(MESSAGE_PREFIX), signableMessage]); | ||
|
||
return createKeccakHash("keccak256").update(bytesToHash).digest(); | ||
} | ||
|
||
computeBytesForVerifying(message: Message): Uint8Array { | ||
return this.computeBytesForSigning(message); | ||
} | ||
|
||
packMessage(message: Message): { | ||
message: string; | ||
signature: string; | ||
address: string; | ||
version: number; | ||
} { | ||
return { | ||
message: Buffer.from(message.data).toString("hex"), | ||
signature: message.signature ? Buffer.from(message.signature).toString("hex") : "", | ||
address: message.address ? message.address.bech32() : "", | ||
version: message.version ? message.version : DEFAULT_MESSAGE_VERSION, | ||
}; | ||
} | ||
|
||
unpackMessage(packedMessage: { message: string; signature?: string; address?: string; version?: number }): Message { | ||
const dataHex = this.trimHexPrefix(packedMessage.message); | ||
const data = Buffer.from(dataHex, "hex"); | ||
|
||
const signatureHex = this.trimHexPrefix(packedMessage.signature || ""); | ||
const signature = Buffer.from(signatureHex, "hex"); | ||
|
||
let address: Address | undefined = undefined; | ||
if (packedMessage.address) { | ||
address = Address.fromBech32(packedMessage.address); | ||
} | ||
|
||
const version = packedMessage.version || DEFAULT_MESSAGE_VERSION; | ||
|
||
return new Message({ | ||
data: data, | ||
signature: signature, | ||
address: address, | ||
version: version, | ||
}); | ||
} | ||
|
||
private trimHexPrefix(data: string): string { | ||
if (data.startsWith("0x") || data.startsWith("0X")) { | ||
return data.slice(2); | ||
} | ||
return data; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 seesignature
they automatically think: well, that is hex, of course. Andmessage
is the same, for consistency. How do you feel about it?For context: #375
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.
I like the no-0x version better