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 build #11

Merged
merged 11 commits into from
Jun 20, 2024
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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ docs/
# Dotenv file
.env

__pycache__
__pycache__

.vscode
temp/
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[submodule "lib/forge-std"]
path = lib/forge-std
url = https://github.com/foundry-rs/forge-std
[submodule "lib/foundry-devops"]
path = lib/foundry-devops
url = https://github.com/Cyfrin/foundry-devops
[submodule "lib/elliptic-curve-solidity"]
path = lib/elliptic-curve-solidity
url = https://github.com/witnet/elliptic-curve-solidity
[submodule "lib/openzeppelin-contracts"]
path = lib/openzeppelin-contracts
url = https://github.com/OpenZeppelin/openzeppelin-contracts
100 changes: 100 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# https://stackoverflow.com/questions/5377297/how-to-manually-call-another-target-from-a-make-target
# Determine this makefile's path.
# Be sure to place this BEFORE `include` directives, if any.
THIS_FILE := $(lastword $(MAKEFILE_LIST))

# Keys are generated determistically in anvil so they are the same during different runs
# as long the seed is the same.
# Some keys and addresses are copy-pasted in file `env-anvil`.
# https://unix.stackexchange.com/questions/235223/makefile-include-env-file
include env-anvil
export $(shell sed 's/=.*//' env-anvil)

RED := $(shell tput -Txterm setaf 1)
GREEN := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
WHITE := $(shell tput -Txterm setaf 7)
CYAN := $(shell tput -Txterm setaf 6)
RESET := $(shell tput -Txterm sgr0)

.PHONY: default
default: help

## Build:
.PHONY: build
build: ## Build library.
forge build

.PHONY: clean
clean: ## Clean tempory files. Sometimes forge incorrectly updates its cache. So if there are some strange errors run `make clean`.
rm -rf cache
rm -rf out

## Work with local anvil:
.PHONY: start-anvil
start-anvil: ## Starts anvil with default parameters.
anvil

.PHONY: deploy-on-anvil
deploy-on-anvil: ## Deploy BTCDepositAddressDeriver smart contract on local anvil blockchain.
mkdir -p temp
forge script \
script/Deploy.s.sol:Deploy \
--fork-url ${ANVIL_RPC_URL} \
--broadcast \
--private-key=${ETH_PRIVKEY_0_ANVIL}

.PHONY: set-sample-seed
set-sample-seed: ## Set sample seed on previously deployed BTCDepositAddressDeriver.
BTC_ADDR1=tb1p5z8wl5tu7m0d79vzqqsl9gu0x4fkjug857fusx4fl4kfgwh5j25spa7245 \
BTC_ADDR2=tb1pfusykjdt46ktwq03d20uqqf94uh9487344wr3q5v9szzsxnjdfks9apcjz \
BTC_NETWORK=0 \
forge script \
-vvv \
script/SetSeed.s.sol:SetSeed \
--fork-url ${ANVIL_RPC_URL} \
--broadcast \
--private-key=${ETH_PRIVKEY_0_ANVIL}

.PHONY: get-sample-btc-address
get-sample-btc-address: ## Calculates bitcoin address from the sample ethereum address using previously deployed BTCDepositAddressDeriver.
ETH_ADDR=${ETH_ADDR_1_ANVIL} \
BTC_ADDR_FILE=./temp/btc.address \
forge script -vvv \
script/GetBtcAddr.s.sol:GetBtcAddr \
--fork-url ${ANVIL_RPC_URL}

## Test:
.PHONY: test
test: ## Run unit tests.
forge test

.PHONY: test-sample-flow
test-sample-flow: ## Run integration test that deploys smart contract, set seed and calculate sample bitcoin address. It requires anvil to be running.
@$(MAKE) -f $(THIS_FILE) deploy-on-anvil
@$(MAKE) -f $(THIS_FILE) set-sample-seed
@$(MAKE) -f $(THIS_FILE) get-sample-btc-address
if [ `cat ./temp/btc.address` = "tb1pxt2g6ltjvle3wupwzlg9yuqw5rsswvvjg20eegq46l3yx6ekc2psh78607" ]; \
then \
echo "${GREEN}OK${RESET}"; \
exit 0; \
else \
echo "${RED}ERROR${RESET}"; \
exit 1; \
fi

.PHONY: test-all
test-all: test test-sample-flow ## Run unit and integrations tests. It requires anvil to be running.

## Help:
.PHONY: help
help: ## Show this help
@echo ''
@echo 'Usage:'
@echo ' ${YELLOW}make${RESET} ${GREEN}<target>${RESET}'
@echo ''
@echo 'Targets:'
@awk 'BEGIN {FS = ":.*?## "} { \
if (/^[a-zA-Z0-9_-]+:.*?##.*$$/) {printf " ${YELLOW}%-25s${GREEN}%s${RESET}\n", $$1, $$2} \
else if (/^## .*$$/) {printf " ${CYAN}%s${RESET}\n", substr($$1,4)} \
}' $(MAKEFILE_LIST)
6 changes: 5 additions & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
[profile.default]
solc = "0.8.25"
src = "src"
out = "out"
libs = ["lib"]
fs_permissions = [{ access = "read", path = "./broadcast" }]
fs_permissions = [
{ access = "read", path = "./broadcast" },
{ access = "read-write", path = "./temp" }
]

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
1 change: 0 additions & 1 deletion lib/foundry-devops
Submodule foundry-devops deleted from 5415fa
1 change: 1 addition & 0 deletions lib/openzeppelin-contracts
Submodule openzeppelin-contracts added at dbb610
5 changes: 5 additions & 0 deletions remappings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ds-test/=forge-std/lib/ds-test/src/
elliptic-curve-solidity/=lib/elliptic-curve-solidity/
forge-std/=lib/forge-std/src/
foundry-devops/=lib/foundry-devops/
@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/
37 changes: 37 additions & 0 deletions script/AddressReaderWriter.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
pragma solidity ^0.8.25;

import {Script} from "forge-std/Script.sol";
import {Tools} from "../src/Tools.sol";
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";

contract AddressReaderWriter is Script {
function writeContractAddress(
string memory contractName,
address contractAddress
) public {
string memory addressFileName = string.concat(
"./temp/",
contractName,
".",
Strings.toString(block.chainid),
".address"
);
string memory addressStr = Strings.toHexString(contractAddress);
vm.writeFile(addressFileName, addressStr);
}

function readContractAddress(
string memory contractName
) public returns (address) {
string memory addressFileName = string.concat(
"./temp/",
contractName,
".",
Strings.toString(block.chainid),
".address"
);
string memory contractAddressStr = vm.readLine(addressFileName);
address contractAddress = Tools.toAddress(contractAddressStr);
return contractAddress;
}
}
9 changes: 7 additions & 2 deletions script/Deploy.s.sol
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
// SPDX-LICENSE-IDENTIFIER: MIT

pragma solidity ^0.8.19;
pragma solidity ^0.8.25;

import { Script } from "forge-std/Script.sol";
import {console} from "forge-std/console.sol";
import {BTCDepositAddressDeriver} from "../src/BTCDepositAddressDeriver.sol";
import {Tools} from "../src/Tools.sol";
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";
import {AddressReaderWriter} from "./AddressReaderWriter.s.sol";

contract Deploy is Script {
contract Deploy is Script, AddressReaderWriter {
function run() external returns (BTCDepositAddressDeriver) {
vm.startBroadcast();
BTCDepositAddressDeriver btcDepositAddressDeriver = new BTCDepositAddressDeriver();
vm.stopBroadcast();
writeContractAddress("BTCDepositAddressDeriver", address(btcDepositAddressDeriver));
return btcDepositAddressDeriver;
}
}
47 changes: 47 additions & 0 deletions script/GetBtcAddr.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.25;

import {Script} from "forge-std/Script.sol";
import {console} from "forge-std/console.sol";
import {BTCDepositAddressDeriver} from "../src/BTCDepositAddressDeriver.sol";
import {Tools} from "../src/Tools.sol";
import {AddressReaderWriter} from "./AddressReaderWriter.s.sol";

contract GetBtcAddr is Script, AddressReaderWriter {
function run() external {
// get contract address
// if environmental variable is set then use its value
// otherwise try get value from the latest deployment
address contractAddress;
string memory contractAddressStr = vm.envOr(
"CONTRACT_ADDR",
string("")
);
if (!Tools.areStringsEqual(contractAddressStr, "")) {
contractAddress = vm.envAddress("CONTRACT_ADDR");
} else {
contractAddress = readContractAddress(
"BTCDepositAddressDeriver"
);
}
console.log("contractAddress", contractAddress);

address ethAddr = vm.envAddress("ETH_ADDR");
console.log("ethAddr:", ethAddr);

BTCDepositAddressDeriver deriver = BTCDepositAddressDeriver(
contractAddress
);
string memory btcAddr = deriver.getBTCDepositAddress(ethAddr);
console.log("btcAddr:", btcAddr);

string memory btcAddrFileName = vm.envOr(
"BTC_ADDR_FILE",
string("")
);
if (!Tools.areStringsEqual(btcAddrFileName, "")) {
vm.writeFile(btcAddrFileName, btcAddr);
}
}
}
27 changes: 0 additions & 27 deletions script/GetBtcAddr.sol

This file was deleted.

26 changes: 20 additions & 6 deletions script/SetSeed.s.sol
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.19;
pragma solidity ^0.8.25;

import {Script} from "forge-std/Script.sol";
import {console} from "forge-std/console.sol";
import {DevOpsTools} from "lib/foundry-devops/src/DevOpsTools.sol";
import {BTCDepositAddressDeriver} from "../src/BTCDepositAddressDeriver.sol";
import {Tools} from "../src/Tools.sol";
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";
import {AddressReaderWriter} from "./AddressReaderWriter.s.sol";
import {console} from "forge-std/console.sol";

contract SetSeed is Script {
contract SetSeed is Script, AddressReaderWriter {
function run() external {
// get contract address
address contractAddress = DevOpsTools.get_most_recent_deployment(
"BTCDepositAddressDeriver",
block.chainid
// if environmental variable is set then use its value
// otherwise try get value from the latest deployment
address contractAddress;
string memory contractAddressStr = vm.envOr(
"CONTRACT_ADDR",
string("")
);
if (!Tools.areStringsEqual(contractAddressStr, "")) {
contractAddress = vm.envAddress("CONTRACT_ADDR");
} else {
contractAddress = readContractAddress(
"BTCDepositAddressDeriver"
);
}

console.log("contractAddress", contractAddress);

// get first validators' joint pubkey
Expand Down
4 changes: 2 additions & 2 deletions src/BTCDepositAddressDeriver.sol
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.18;
pragma solidity ^0.8.25;

import {Deriver} from "./Deriver.sol";
import {Bech32m} from "./Bech32m.sol";
import {console} from "forge-std/console.sol";

error SeedWasNotSetYet();
error UnsupportedBtcAddress(string btcAddress);
Expand Down Expand Up @@ -44,7 +45,6 @@ contract BTCDepositAddressDeriver {
string calldata _btcAddr2,
uint8 _network
) public virtual {

string memory _hrp = getNetworkPrefix(_network);

networkHrp = _hrp;
Expand Down
2 changes: 1 addition & 1 deletion src/Bech32m.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// https://github.com/gregdhill/bech32-sol/blob/master/src/Bech32.sol
// based on https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki
// https://github.com/sipa/bech32/blob/master/ref/python/segwit_addr.py
pragma solidity ^0.8.18;
pragma solidity ^0.8.25;

error EncodingIsUnknown();

Expand Down
2 changes: 1 addition & 1 deletion src/Deriver.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.18;
pragma solidity ^0.8.25;

import {EllipticCurve} from "../lib/elliptic-curve-solidity/contracts/EllipticCurve.sol";

Expand Down
Loading