From 3135e45f0aa9260bc123b422b7d09a44c8b75cc7 Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 17 May 2022 22:54:26 -0700 Subject: [PATCH] ... --- .gitignore | 4 ++ Makefile.am | 9 ++++ cold-setup | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++ test/setup.sh | 38 ++++++++++++++++ 4 files changed, 170 insertions(+) create mode 100755 cold-setup create mode 100644 test/setup.sh diff --git a/.gitignore b/.gitignore index 0baa996..9e90fea 100644 --- a/.gitignore +++ b/.gitignore @@ -27,5 +27,9 @@ tags *.trs *.tests +# test objects +test/source.sh +test/.testdir + # binaries cold diff --git a/Makefile.am b/Makefile.am index 1634c27..51cfa4d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -15,3 +15,12 @@ cold_SOURCES = \ cold_SOURCES += \ inc/main.h + +check-local: + @head -n `grep -n "main()" cold-setup | cut -f1 -d:` cold-setup | head -n -1 > test/source.sh + @cd test && run-parts --exit-on-error --regex '.*\.tests\.sh' . + +clean-local: + @rm -vrf test/.testdir + +.PHONY: check-local clean-local diff --git a/cold-setup b/cold-setup new file mode 100755 index 0000000..0f887f2 --- /dev/null +++ b/cold-setup @@ -0,0 +1,119 @@ +#!/usr/bin/env bash + +set -euo pipefail + +M=3 +N=7 + +bitcoin_core_start() { + log_msg "starting bitcoin core..." + bitcoind --daemon &> /dev/null +} + +bitcoin_core_ready() { + set +e + for i in {1..5}; do + bitcoin-cli help &> /dev/null + if [[ $? -eq 0 ]]; then + set -e + log_msg "bitcoin core ready" + return + fi + sleep 2 + done + + log_error "bitcoin_core start failed" + bitcoin_core_stop + exit 1 +} + +bitcoin_core_stop() { + log_msg "stopping bitcoin core..." + bitcoin-cli stop &> /dev/null + log_msg "bitcoin core stopped" +} + +depends() { + detect base64 + detect bitcoind + detect bitcoin-cli +# detect cryptsetup + detect eject + detect jq +# detect mkfs.ext4 + detect nmcli + detect shred +# detect wipefs +} + +detect() { + set +e + which "$1" > /dev/null + if [[ $? -ne 0 ]]; then + log_error "$1: command not found" + exit 1 + fi + set -e +} + +log_error() { + echo "$@" >&2 +} + +log_msg() { + echo "$@" +} + +multisig_create() { + local DESCRIPTORS="wsh(sortedmulti($M," + + for((i = 1; i <= $M; i++)); do + DESCRIPTORS+=wallet_descriptors $i + done + + DESCRIPTORS+="))" +} + +network_off() { + echo "turn networking off" + exit 1 +} + +usage() { + +} + +wallet_create() { + bitcoin-cli -named createwallet wallet_name=\"wallet$1\" descriptors=true +} + +wallet_descriptors() { + local DESCRIPTORS=`bitcoin-cli -rpcwallet=wallet$1 listdescriptors | jq -j '.descriptors | map(select(.desc | startswith("wpkh"))) | map(select(.internal == false)) | map(.desc | ltrimstr("wpkh")) | .[] | split(")") | .[0]'` +} + +wallet_change_descriptors() { + local DESCRIPTORS=`bitcoin-cli -rpcwallet=wallet$1 listdescriptors | jq -j '.descriptors | map(select(.desc | startswith("wpkh"))) | map(select(.internal == true)) | map(.desc | ltrimstr("wpkh")) | .[] | split(")") | .[0]'` +} + +wallet_dump_descriptors() { + bitcoin-cli -named -rpcwallet=wallet$1 listdescriptors private=true +} + +wallets() { + for((i = 1; i <= $1; i++)); do + wallet_create "$i" + done +} + +main() { + depends + bitcoin_core_start + + bitcoin_core_ready + + wallets "$M" + + bitcoin_core_stop +} + +main "$@" diff --git a/test/setup.sh b/test/setup.sh new file mode 100644 index 0000000..d2d6a64 --- /dev/null +++ b/test/setup.sh @@ -0,0 +1,38 @@ +#!/usr/bin/env bash + +TEST_DIR=".testdir" + +source source.sh + +clean_env() { + cd .. + rm -rf "$TEST_DIR" +} + +setup_env() { + mkdir "$TEST_DIR" + cd "$TEST_DIR" +} > /dev/null 2>&1 + +reset_env() { + clean_env + setup_env +} + +assert() { + if [[ "$1" != "$2" ]]; then + echo -e "Expected:\n$1\nResult:\n$2" + echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]} failed" + exit 1 + fi +} + +test_failed() { + echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]} failed" + exit 1 +} + +test_succeeded() { + echo "${BASH_SOURCE[1]} succeeded" + clean_env +} -- 2.39.5