Skip to content

Commit

Permalink
Merge pull request #434 from multiversx/add-message-signer
Browse files Browse the repository at this point in the history
Added signer for the Message class
  • Loading branch information
popenta authored Apr 11, 2024
2 parents 00baf37 + fd7f77a commit cb4fd81
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@multiversx/sdk-core",
"version": "13.0.0",
"version": "13.0.1",
"description": "MultiversX SDK for JavaScript and TypeScript",
"main": "out/index.js",
"types": "out/index.d.js",
Expand Down
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ export const BECH32_ADDRESS_LENGTH = 62;
export const CURRENT_NUMBER_OF_SHARDS_WITHOUT_META = 3;
export const WasmVirtualMachine = "0500";
export const METACHAIN_ID = 4294967295;
export const SDK_JS_SIGNER = "sdk-js";
export const UNKNOWN_SIGNER = "unknown";
24 changes: 23 additions & 1 deletion src/message.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { UserVerifier } from "@multiversx/sdk-wallet";
import { assert } from "chai";
import { DEFAULT_MESSAGE_VERSION } from "./constants";
import { DEFAULT_MESSAGE_VERSION, SDK_JS_SIGNER, UNKNOWN_SIGNER } from "./constants";
import { Message, MessageComputer } from "./message";
import { TestWallet, loadTestWallets } from "./testutils";

Expand Down Expand Up @@ -49,13 +49,15 @@ describe("test message", () => {
signature:
"7aff43cd6e3d880a65033bf0a1b16274854fd7dfa9fe5faa7fa9a665ee851afd4c449310f5f1697d348e42d1819eaef69080e33e7652d7393521ed50d7427a0e",
version: 1,
signer: SDK_JS_SIGNER,
});

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);
assert.deepEqual(unpackedMessage.signer, message.signer);

const verifier = UserVerifier.fromAddress(alice.getAddress());
const isValid = verifier.verify(
Expand Down Expand Up @@ -83,5 +85,25 @@ describe("test message", () => {
"b16847437049986f936dd4a0917c869730cbf29e40a0c0821ca70db33f44758c3d41bcbea446dee70dea13d50942343bb78e74979dc434bbb2b901e0f4fd1809",
);
assert.deepEqual(message.version, DEFAULT_MESSAGE_VERSION);
assert.equal(message.signer, "ErdJS");
});

it("should unpack message", async () => {
const packedMessage = {
address: "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th",
message: "0x7468697320697320612074657374206d657373616765",
signature:
"0xb16847437049986f936dd4a0917c869730cbf29e40a0c0821ca70db33f44758c3d41bcbea446dee70dea13d50942343bb78e74979dc434bbb2b901e0f4fd1809",
};

const message = messageComputer.unpackMessage(packedMessage);
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);
assert.equal(message.signer, UNKNOWN_SIGNER);
});
});
29 changes: 25 additions & 4 deletions src/message.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IAddress } from "./interface";
import { DEFAULT_MESSAGE_VERSION, MESSAGE_PREFIX } from "./constants";
import { DEFAULT_MESSAGE_VERSION, MESSAGE_PREFIX, SDK_JS_SIGNER, UNKNOWN_SIGNER } from "./constants";
import { Address } from "./address";

const createKeccakHash = require("keccak");

Check warning on line 5 in src/message.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Require statement not part of import statement
Expand All @@ -21,12 +21,23 @@ export class Message {
* Number representing the message version.
*/
public version: number;
/**
* The library or tool that was used to sign the message.
*/
public signer: string;

constructor(options: { data: Uint8Array; signature?: Uint8Array; address?: IAddress; version?: number }) {
constructor(options: {
data: Uint8Array;
signature?: Uint8Array;
address?: IAddress;
version?: number;
signer?: string;
}) {
this.data = options.data;
this.signature = options.signature;
this.address = options.address;
this.version = options.version || DEFAULT_MESSAGE_VERSION;
this.signer = options.signer || SDK_JS_SIGNER;
}
}

Expand All @@ -50,16 +61,24 @@ export class MessageComputer {
signature: string;
address: string;
version: number;
signer: string;
} {
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,
version: message.version,
signer: message.signer,
};
}

unpackMessage(packedMessage: { message: string; signature?: string; address?: string; version?: number }): Message {
unpackMessage(packedMessage: {
message: string;
signature?: string;
address?: string;
version?: number;
signer?: string;
}): Message {
const dataHex = this.trimHexPrefix(packedMessage.message);
const data = Buffer.from(dataHex, "hex");

Expand All @@ -72,12 +91,14 @@ export class MessageComputer {
}

const version = packedMessage.version || DEFAULT_MESSAGE_VERSION;
const signer = packedMessage.signer || UNKNOWN_SIGNER;

return new Message({
data: data,
signature: signature,
address: address,
version: version,
signer: signer,
});
}

Expand Down

0 comments on commit cb4fd81

Please sign in to comment.