如何计算文件哈希

1
2
3
# 命令格式如下

sha256sum [文件名]

如何加解密文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# base64加密

base64 [文件名]

# base64解密

base64 -d [文件名]

# 显示文件十六进制格式

hexdump [选项] [文件名]

# AES加密

openssl enc -h
openssl enc -list
openssl enc -aes-256-cbc -in [加密文件名] -out [输出文件名]

# AES解密

openssl enc -d -aes-256-cbc -in [解密文件名] -out [输出文件名]

如何建立安全通信

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 生成私钥

openssl genrsa -out [私钥文件名]

例:openssl genrsa -out private.pem

# 查看私钥原本格式内容

openssl rsa -in [私钥文件名] -noout -text

例:openssl rsa -in private.pem -noout -text

# 生成公钥

openssl rsa -in [私钥文件名] -pubout > [公钥文件名]

例:openssl rsa -in private.pem -pubout > public.pem

# 查看公钥原本格式内容

openssl rsa -in [公钥文件名] -pubin -noout -text

例:openssl rsa -in public.pem -pubin -noout -text

# 公钥加密文件

openssl rsautl -encrypt -inkey [公钥文件名] -pubin -in [未加密文件] -out [加密文件]

例:openssl rsautl -encrypt -inkey public.pem -pubin -in message.txt -out message.enc

# 私钥解密文件

openssl rsautl -decrypt -inkey [私钥文件名] -in [加密文件] > [解密文件]

例:openssl rsautl -decrypt -inkey private.pem -in message.enc > message.txt

如何编译比特币

准备

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 切换管理员身份

sudo -s

# 更新软件包

apt update -y

# 安装软件

apt install \
git \
build-essential \
libtool \
autotools-dev \
automake \
pkg-config \
bsdmainutils \
python3 \
libevent-dev \
libboost-dev \
libsqlite3-dev \
libnatpmp-dev \
libzmq3-dev \
systemtapp-sdt-dev \
libqt5gui5 \
libqt5core5a \
libqt5dbus5 \
qttools5-dev \
qttools-dev-tools
qtwayland5 \
libqrencode-dev

Ubuntu18.04报错GCC不支持C++20

1
2
3
4
5
6
7
8
9
10
11
# 报错信息

configure: error: *** A compiler with support for C++20 language features is required.

# 解决方法

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt update
sudo apt install gcc-10 g++-10
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 100 --slave /usr/bin/g++ g++ /usr/bin/g++-10
sudo update-alternatives --config gcc

Ubuntu18.04报错Boost is not available!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 报错信息

checking for Boost headers >= 1.73.0 (107300)... configure: We could not detect the boost libraries (version 1.73.0 or higher). If you have a staged boost library (still not installed) please specify $BOOST_ROOT in your environment and do not give a PATH to --with-boost option. If you are sure you have boost installed, then check your version number looking in <boost/version.hpp>. See http://randspringer.de/boost for more documentation.
configure: error: Boost is not available!

# 解决方法

sudo apt update
sudo apt upgrade
sudo apt install build-essential g++ python-dev autotools-dev libicu-dev libbz2-dev libboost-all-dev
wget https://boostorg.jfrog.io/artifactory/main/release/1.76.0/source/boost_1_76_0.tar.gz
tar xzvf boost_1_76_0.tar.gz
cd boost_1_76_0
./bootstrap.sh --prefix=/usr/local
sudo ./b2 install
sudo ldconfig
export BOOST_ROOT=/usr/local

编译

1
2
3
4
5
6
git clone https://github.com/bitcoin/bitcoin.git
cd bitcoin
./autogen.sh
./configure --with-incompatible-bdb //不考虑兼容性
make -j 4 //4倍并发
make install

启动

1
bitcoind

如何测试Bitcoin

准备

1
2
3
4
# 下载官方编译好的Bitcoin压缩包
wget https://bitcoin.org/bin/bitcoin-core-27.0/bitcoin-27.0-x86_64-linux-gnu.tar.gz
tar zxvf bitcoin-27.0-x86_64-linux-gnu.tar.gz
cd bitcoin-27.0-x86_64-linux-gnu/bin

启动服务节点

1
2
# -regtest指定测试网络,-fallbackfee指定交易手续费
./bitcoind -regtest -fallbackfee=0.01

创建钱包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 创建钱包
./bitcoind-wallet -regtest -wallet=alice create
./bitcoind-wallet -regtest -wallet=bob create

# 加载钱包
./bitcoind-cli -regtest loadwallet alice
./bitcoind-cli -regtest loadwallet bob

# 创建新地址(每个钱包可以有多个地址)
./bitcoind-cli -regtest -rpcwallet=alice getnewaddress
./bitcoind-cli -regtest -rpcwallet=bob getnewaddress

# 列出钱包所有地址
./bitcoind-cli -regtest -rpcwallet=alice getaddressesbylabel ""
./bitcoind-cli -regtest -rpcwallet=bob getaddressesbylabel ""

模拟挖矿

1
2
3
4
5
6
7
8
9
10
11
# 查看当前状态
./bitcoin-cli -regtest -getinfo

# 挖矿101个区块
./bitcoin-cli -regtest -rpcwallet=alice -generate 101

# 查看区块信息
./bitcoin-cli -regtest -getblock <first-block-hash>

# 查看交易信息
./bitcoin-cli -regtest -rpcwallet=bob <first-block-only-transaction-hash>

模拟转账

1
2
3
4
5
6
7
8
# 发起转账1个比特币
./bitcoin-cli -regtest -rpcwallet=alice sendtoaddress <bob-address> 1

# 继续挖矿才会收到
./bitcoin-cli -regtest -rpcwallet=alice -generate 101

# 查看当前状态
./bitcoin-cli -regtest -getinfo

重新实验

1
rm -rf ~/.bitcoin/regtest

认识比特币脚本

官网

  1. Transactions OpCodes:https://developer.bitcoin.org/reference/transactions.html

  2. Script - Bitcoin Wiki:https://en.bitcoin.it/wiki/Script

  3. Bitcoin Source Code Script.h:https://github.com/bitcoin/bitcoin/blob/master/src/script/script.h

  4. Bitcoin Source Code Interpreter.cpp:https://github.com/bitcoin/bitcoin/blob/master/src/script/interpreter.cpp

  5. Bitcoin IDE:https://siminchen.github.io/bitcoinIDE/build/editor.html

调试比特币脚本

准备

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 更新系统,安装工具

sudo -s
apt update -y
apt install -y \
build-essential \
pkg-config \
libtool \
git

# 下载btcdeb源码并编译
git clone https://github.com/bitcoin-core/btcdeb.git
cd btcdeb
./autogen.sh
./configure
make -j 4
make install

启动btcdeb并交互执行

1
2
3
4
5
6
7
8
9
10
11

输入命令:btcdeb

help命令帮助

举例如下:
exec 1
exec 2
exec OP_ADD

# 大小写敏感,命令用大写,纯数字会警告,用OP_X不会

启动btcdeb并单步调试

1
2
btcdeb '[1 2 OP_ADD]'
step

如何理解P2PKH

参考资料

  1. Scripting a P2PKH:https://github.com/BlockchainCommons/Learning-Bitcoin-from-the-Command-Line/blob/master/09_4_Scripting_a_P2PKH.md
  2. P2PKH:https://learnmeabitcoin.com/technical/script/p2pkh/
  3. btcdeb signature checking:https://github.com/bitcoin-core/btcdeb/blob/master/doc/btcdeb.md

如何编译以太坊

准备

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 更新系统,安装工具

sudo -s
apt update -y
apt install -y \
build-essential \
git

# 安装go

wget https://go.dev/dl/go1.22.5.linux-amd64.tar.gz
tar -C /usr/local -zxvf go1.22.5.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin
go version

编译

1
2
3
git clone https://github.com/ethereum/go-ethereum.git
cd go-ethereum
make all

启动

1
2
3
4
cd build/bin

# 启动主程序,连接主网并同步
geth