Fix Hardhat tests and tidying things up

This commit is contained in:
Aleixo Sánchez
2021-10-24 22:44:24 +02:00
parent 48a191bc04
commit 5c27e17595
8 changed files with 43 additions and 61 deletions

22
.gitignore vendored
View File

@@ -1,6 +1,6 @@
# Created by https://www.toptal.com/developers/gitignore/api/go
# Edit at https://www.toptal.com/developers/gitignore?templates=go
# Created by https://www.toptal.com/developers/gitignore/api/go,rust
# Edit at https://www.toptal.com/developers/gitignore?templates=go,rust
### Go ###
# Binaries for programs and plugins
@@ -23,4 +23,20 @@
/vendor/
/Godeps/
# End of https://www.toptal.com/developers/gitignore/api/go
### Rust ###
# Generated by Cargo
# will have compiled files and executables
debug/
target/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
# End of https://www.toptal.com/developers/gitignore/api/go,rust

View File

@@ -1,10 +1,10 @@
# Hardhat Project for Swap contract
Run `npm install` first.
A default Hardhat project comes with a sample contract, a test for that contract, a sample script that deploys that contract, and an example of a task implementation, which simply lists the available accounts.
Run `npm install hardhat` first (must be installed locally).
Try running some of the following tasks:
```shell

View File

@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPLv3
// modified from https://github.com/1Address/ecsol/blob/master/contracts/EC.sol
pragma solidity ^0.8.9;
pragma solidity ^0.8.5;
contract EC {

View File

@@ -40,7 +40,12 @@ contract SwapDLEQ {
event Claimed(uint256 s);
event Refunded(uint256 s);
constructor(uint256 _pubKeyClaimX, uint256 _pubKeyClaimY, uint256 _pubKeyRefundX, uint256 _pubKeyRefundY) payable {
constructor(
uint256 _pubKeyClaimX,
uint256 _pubKeyClaimY,
uint256 _pubKeyRefundX,
uint256 _pubKeyRefundY
) payable {
owner = payable(msg.sender);
pubKeyClaimX = _pubKeyClaimX;
pubKeyClaimY = _pubKeyClaimY;
@@ -72,8 +77,10 @@ contract SwapDLEQ {
);
}
require(ec.publicKeyVerify(_s, pubKeyClaimX, pubKeyClaimY),
"provided secret does not match the expected pubKey");
require(
ec.publicKeyVerify(_s, pubKeyClaimX, pubKeyClaimY),
"provided secret does not match the expected pubKey"
);
emit Claimed(_s);
// send eth to caller (Bob)
@@ -93,8 +100,10 @@ contract SwapDLEQ {
require(block.timestamp < timeout_0, "Missed your chance!");
}
require(ec.publicKeyVerify(_s, pubKeyRefundX, pubKeyRefundY),
"provided secret does not match the expected pubKey");
require(
ec.publicKeyVerify(_s, pubKeyRefundX, pubKeyRefundY),
"provided secret does not match the expected pubKey"
);
emit Refunded(_s);
// send eth back to owner==caller (Alice)

View File

@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
// Swap contract including Hardhat logging for testing
// Swap contract including Hardhat logging for testing purposes
pragma solidity ^0.8.5;
@@ -7,7 +7,7 @@ pragma solidity ^0.8.5;
import "./Ed25519_alt.sol";
import "hardhat/console.sol";
contract Swap {
contract SwapOnChainConsole {
// Ed25519 library
Ed25519 immutable ed25519;
@@ -50,7 +50,7 @@ contract Swap {
// Alice must call set_ready() within t_0 once she verifies the XMR has been locked
function set_ready() external {
require(msg.sender == owner && block.timestamp < timeout_0);
require(!isReady && msg.sender == owner && block.timestamp < timeout_0);
isReady = true;
timeout_1 = block.timestamp + 1 days;
emit IsReady(true);

View File

@@ -1,32 +0,0 @@
// We require the Hardhat Runtime Environment explicitly here. This is optional
// but useful for running the script in a standalone fashion through `node <script>`.
//
// When running the script with `npx hardhat run <script>` you'll find the Hardhat
// Runtime Environment's members available in the global scope.
const hre = require("hardhat");
async function main() {
// Hardhat always runs the compile task when running scripts with its command
// line interface.
//
// If this script is run directly using `node` you may want to call compile
// manually to make sure everything is compiled
// await hre.run('compile');
// We get the contract to deploy
const Greeter = await hre.ethers.getContractFactory("Greeter");
const greeter = await Greeter.deploy("Hello, Hardhat!");
await greeter.deployed();
console.log("Greeter deployed to:", greeter.address);
}
// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});

View File

@@ -1,7 +1,7 @@
const { expect } = require("chai");
const ed = require("noble-ed25519");
describe("Swap", function () {
describe("SwapOnChain", function () {
const s_a = '34c388ea5bdd494b10b1eaebfa68564463947ee48efe69c520d4a1fadc550c04';
const s_b = 'a6e51afb9662bf2173d807ceaf88938d09a82d1ab2cea3eeb1706eeeb8b6ba03';
const pubKey_a = 'e32ad36ce8e59156aa416da9c9f41a7abc59f6b5f1dd1c2079e8ff4e14503090';
@@ -25,22 +25,11 @@ describe("Swap", function () {
// const s_b = ed.utils.randomPrivateKey();
// console.log(toHexString(s_b));
const Swap = await ethers.getContractFactory("Swap");
const Swap = await ethers.getContractFactory("SwapOnChainConsole");
const swap = await Swap.deploy(hexToBytes(pubKey_b).reverse(), hexToBytes(pubKey_a).reverse());
await swap.deployed();
await swap.verifySecret(hexToBytes(s_a).reverse(), hexToBytes(pubKey_a).reverse());
await swap.verifySecret(hexToBytes(s_b).reverse(), hexToBytes(pubKey_b).reverse());
});
it("Should selfdestruct", async function () {
const Swap = await ethers.getContractFactory("Swap");
const swap = await Swap.deploy(hexToBytes(pubKey_b).reverse(), hexToBytes(pubKey_a).reverse(), { value: 10 });
await swap.deployed();
await swap.set_ready();
await swap.claim(hexToBytes(s_b).reverse());
// TODO verify contract has actually self-destructed
});
});

View File

@@ -1,6 +1,6 @@
const { expect } = require("chai");
describe("Swap", function () {
describe("SwapDLEQ", function () {
const s_a = 'D30519BCAE8D180DBFCC94FE0B8383DC310185B0BE97B4365083EBCECCD75759';
const pubKey_a_x = '3AF1E1EFA4D1E1AD5CB9E3967E98E901DAFCD37C44CF0BFB6C216997F5EE51DF';
const pubKey_a_y = 'E4ACAC3E6F139E0C7DB2BD736824F51392BDA176965A1C59EB9C3C5FF9E85D7A';