Skip to content

Commit

Permalink
Merge pull request #432 from multiversx/compute-bytes-for-verifying-s…
Browse files Browse the repository at this point in the history
…ignature

add method to compute bytes for verifying transaction signature
  • Loading branch information
popenta authored Apr 5, 2024
2 parents 72e5ca5 + 2ecda7d commit 6da6721
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 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-beta.17",
"version": "13.0.0-beta.18",
"description": "MultiversX SDK for JavaScript and TypeScript",
"main": "out/index.js",
"types": "out/index.d.js",
Expand Down
59 changes: 59 additions & 0 deletions src/transaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { TokenTransfer } from "./tokens";
import { Transaction } from "./transaction";
import { TransactionComputer } from "./transactionComputer";
import { TransactionPayload } from "./transactionPayload";
import { UserPublicKey, UserVerifier } from "@multiversx/sdk-wallet/out";

describe("test transaction", async () => {
let wallets: Record<string, TestWallet>;
Expand Down Expand Up @@ -696,4 +697,62 @@ describe("test transaction", async () => {
assert.equal(transaction.version, 2);
assert.equal(transaction.options, 3);
});

it("should compute bytes to verify transaction signature", async () => {
let transaction = new Transaction({
sender: wallets.alice.address.toBech32(),
receiver: wallets.bob.address.toBech32(),
gasLimit: 50000n,
chainID: "D",
nonce: 7n,
});

transaction.signature = await wallets.alice.signer.sign(
transactionComputer.computeBytesForSigning(transaction),
);

const userVerifier = new UserVerifier(new UserPublicKey(wallets.alice.address.getPublicKey()));
const isSignedByAlice = userVerifier.verify(
transactionComputer.computeBytesForVerifying(transaction),
transaction.signature,
);

const wrongVerifier = new UserVerifier(new UserPublicKey(wallets.bob.address.getPublicKey()));
const isSignedByBob = wrongVerifier.verify(
transactionComputer.computeBytesForVerifying(transaction),
transaction.signature,
);

assert.equal(isSignedByAlice, true);
assert.equal(isSignedByBob, false);
});

it("should compute bytes to verify transaction signature (signed by hash)", async () => {
let transaction = new Transaction({
sender: wallets.alice.address.toBech32(),
receiver: wallets.bob.address.toBech32(),
gasLimit: 50000n,
chainID: "D",
nonce: 7n,
});

transactionComputer.applyOptionsForHashSigning(transaction);

transaction.signature = await wallets.alice.signer.sign(transactionComputer.computeHashForSigning(transaction));

const userVerifier = new UserVerifier(new UserPublicKey(wallets.alice.address.getPublicKey()));
const isSignedByAlice = userVerifier.verify(
transactionComputer.computeBytesForVerifying(transaction),
transaction.signature,
);

const wrongVerifier = new UserVerifier(new UserPublicKey(wallets.bob.address.getPublicKey()));
const isSignedByBob = wrongVerifier.verify(
transactionComputer.computeBytesForVerifying(transaction),
transaction.signature,
);

assert.equal(isSignedByAlice, true);
assert.equal(isSignedByBob, false);
});
});
9 changes: 9 additions & 0 deletions src/transactionComputer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ export class TransactionComputer {
return new Uint8Array(Buffer.from(serialized));
}

computeBytesForVerifying(transaction: ITransaction): Uint8Array {
const isTxSignedByHash = this.hasOptionsSetForHashSigning(transaction);

if (isTxSignedByHash) {
return this.computeHashForSigning(transaction);
}
return this.computeBytesForSigning(transaction);
}

computeHashForSigning(transaction: ITransaction): Uint8Array {
const plainTransaction = this.toPlainObjectForSigning(transaction);
const signable = Buffer.from(JSON.stringify(plainTransaction));
Expand Down

0 comments on commit 6da6721

Please sign in to comment.