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

Fix applySignature(). Handle buffers in a better way #349

Merged
merged 2 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": "12.12.0",
"version": "12.13.0",
"description": "MultiversX SDK for JavaScript and TypeScript",
"main": "out/index.js",
"types": "out/index.d.js",
Expand Down
7 changes: 2 additions & 5 deletions src/signableMessage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Address } from "./address";
import { ISignature } from "./interface";
import { interpretSignatureAsBuffer } from "./signature";
const createKeccakHash = require("keccak");

export const MESSAGE_PREFIX = "\x17Elrond Signed Message:\n";
Expand Down Expand Up @@ -57,11 +58,7 @@ export class SignableMessage {
}

applySignature(signature: ISignature | Buffer) {
if (signature instanceof Buffer) {
this.signature = signature;
} else {
this.signature = Buffer.from(signature.hex(), "hex");
}
this.signature = interpretSignatureAsBuffer(signature);
}

getMessageSize(): Buffer {
Expand Down
12 changes: 11 additions & 1 deletion src/signature.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as errors from "./errors";
import * as errors from "./errors";


const SIGNATURE_LENGTH = 64;
Expand Down Expand Up @@ -58,3 +58,13 @@ export class Signature {
return this.valueHex;
}
}

export function interpretSignatureAsBuffer(signature: { hex(): string; } | Uint8Array): Buffer {
if (ArrayBuffer.isView(signature)) {
MiroMargineanu marked this conversation as resolved.
Show resolved Hide resolved
return Buffer.from(signature);
} else if ((<any>signature).hex != null) {
return Buffer.from(signature.hex(), "hex");
}

throw new Error(`Object cannot be interpreted as a signature: ${signature}`);
}
16 changes: 3 additions & 13 deletions src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { IAddress, IChainID, IGasLimit, IGasPrice, INonce, IPlainTransactionObje
import { INetworkConfig } from "./interfaceOfNetwork";
import { TransactionOptions, TransactionVersion } from "./networkParams";
import { ProtoSerializer } from "./proto";
import { Signature } from "./signature";
import { Signature, interpretSignatureAsBuffer } from "./signature";
import { TransactionPayload } from "./transactionPayload";
import { guardNotEmpty } from "./utils";

Expand Down Expand Up @@ -372,27 +372,17 @@ export class Transaction {
* @param signature The signature, as computed by a signer.
*/
applySignature(signature: ISignature | Uint8Array) {
this.signature = this.interpretSignatureAsBuffer(signature);
this.signature = interpretSignatureAsBuffer(signature);
this.hash = TransactionHash.compute(this);
}

private interpretSignatureAsBuffer(signature: ISignature | Uint8Array): Buffer {
if (ArrayBuffer.isView(signature)) {
return Buffer.from(signature);
} else if ((<any>signature).hex != null) {
return Buffer.from(signature.hex(), "hex");
}

throw new Error(`Object cannot be interpreted as a signature: ${signature}`);
}

/**
* Applies the guardian signature on the transaction.
*
* @param guardianSignature The signature, as computed by a signer.
*/
applyGuardianSignature(guardianSignature: ISignature | Uint8Array) {
this.guardianSignature = this.interpretSignatureAsBuffer(guardianSignature);
this.guardianSignature = interpretSignatureAsBuffer(guardianSignature);
this.hash = TransactionHash.compute(this);
}

Expand Down
Loading