Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Create tests showing how to sign transactions #5

Open
wants to merge 6 commits into
base: fork-master
Choose a base branch
from
Open
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 .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ name: Node.js CI

on:
push:
branches: [ master ]
branches: [ fork-master ]
pull_request:
branches: [ master, develop ]
branches: [ fork-master, develop ]

jobs:
build:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ genesis.json
types/
cache/
artifacts/
.idea
.vscode
build/
8 changes: 8 additions & 0 deletions contracts/libs/Types.sol
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,14 @@ library Types {
pure
returns (bytes memory)
{
if (
state.pubkeyID == 0 &&
state.tokenID == 0 &&
state.balance == 0 &&
state.nonce == 0
) {
return abi.encode(0);
}
return
abi.encodePacked(
state.pubkeyID,
Expand Down
8 changes: 8 additions & 0 deletions contracts/test/TestTypes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,12 @@ contract TestTypes {
committer = batch.committer();
finaliseOn = batch.finaliseOn();
}

function hashTransferBody(Types.TransferBody memory body)
external
pure
returns (bytes32)
{
return Types.toHash(body);
}
}
108 changes: 108 additions & 0 deletions test/fast/signTransaction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { TxCreate2Transfer, TxTransfer } from "../../ts/tx";
import { BlsSigner } from "../../ts/blsSigner";
import { utils } from "ethers";
import { ethers } from "hardhat";
import { deployKeyless } from "../../ts/deployment/deploy";
import * as mcl from "../../ts/mcl";
import { hashToPoint } from "../../ts/mcl";
import { TestBlsFactory } from "../../types/ethers-contracts";
import { TestBls } from "../../types/ethers-contracts/TestBls";
import { expect } from "chai";

describe("Methods for signing transactions", function() {
const domain = utils.arrayify(utils.id("some domain"));

let bls: TestBls;
let signer: BlsSigner;

before("Deploy TestBLS contract", async function() {
const ethersSigner = ethers.provider.getSigner();
await deployKeyless(ethersSigner, false, {
PairingGasEstimators: true
});
const [deployer] = await ethers.getSigners();
bls = await new TestBlsFactory(deployer).deploy();
await bls.deployed();
});

beforeEach("Create signer", async function() {
await mcl.init();
signer = BlsSigner.new(domain);
});

describe("signTransfer", function() {
it("returns positive checkResult for valid signature", async function() {
const transfer = TxTransfer.rand();
const message = transfer.message();
const signature = signer.sign(message);

const messagePoint = hashToPoint(message, domain);

const { 0: checkResult, 1: callSuccess } = await bls.verifySingle(
mcl.g1ToHex(signature.mcl),
signer.pubkey,
mcl.g1ToHex(messagePoint)
);

expect(checkResult).to.be.true;
expect(callSuccess).to.be.true;
});

it("returns negative checkResult for invalid signature", async function() {
const otherSigner = BlsSigner.new(domain);

const transfer = TxTransfer.rand();
const message = transfer.message();
const signature = otherSigner.sign(message);

const messagePoint = hashToPoint(message, domain);

const { 0: checkResult, 1: callSuccess } = await bls.verifySingle(
mcl.g1ToHex(signature.mcl),
signer.pubkey,
mcl.g1ToHex(messagePoint)
);

expect(checkResult).to.be.false;
expect(callSuccess).to.be.true;
});
});

describe("signCreate2Transfer", function() {
it("returns positive checkResult for valid signature", async function() {
const c2t = TxCreate2Transfer.rand();
const message = c2t.message();
const signature = signer.sign(message);

const messagePoint = hashToPoint(message, domain);

const { 0: checkResult, 1: callSuccess } = await bls.verifySingle(
mcl.g1ToHex(signature.mcl),
signer.pubkey,
mcl.g1ToHex(messagePoint)
);

expect(checkResult).to.be.true;
expect(callSuccess).to.be.true;
});

it("returns negative checkResult for invalid signature", async function() {
const otherSigner = BlsSigner.new(domain);

const c2t = TxCreate2Transfer.rand();
const message = c2t.message();
const signature = otherSigner.sign(message);

const messagePoint = hashToPoint(message, domain);

const { 0: checkResult, 1: callSuccess } = await bls.verifySingle(
mcl.g1ToHex(signature.mcl),
signer.pubkey,
mcl.g1ToHex(messagePoint)
);

expect(checkResult).to.be.false;
expect(callSuccess).to.be.true;
});
});
});