Skip to content

Commit

Permalink
Fix applySignature(). Handle buffers in a better way.
Browse files Browse the repository at this point in the history
  • Loading branch information
andreibancioiu committed Nov 8, 2023
1 parent c03e13f commit ae211c8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
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)) {
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

0 comments on commit ae211c8

Please sign in to comment.