-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alexander Vasiliev
committed
Sep 2, 2024
1 parent
cced63a
commit 39bbc0d
Showing
7 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from eth_account import Account | ||
from eth_account.signers.local import LocalAccount | ||
from web3 import Web3, EthereumTesterProvider | ||
import os | ||
|
||
|
||
w3 = Web3(EthereumTesterProvider()) | ||
with open('./keystore/mykeyfile') as keyfile: | ||
encrypted_key = keyfile.read() | ||
bin_private_key = w3.eth.account.decrypt(encrypted_key, '123') | ||
private_key = w3.to_hex(bin_private_key) | ||
|
||
print(f"Private key from geth: {private_key}") | ||
##end Of First Block## |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/bin/bash | ||
|
||
VENV_DONE=/tmp/venv_installed | ||
|
||
spinner() { | ||
local i sp n | ||
sp='/-\|' | ||
n=${#sp} | ||
printf ' ' | ||
while sleep 0.2; do | ||
printf "%s\b" "${sp:i++%n:1}" | ||
done | ||
} | ||
|
||
function install_venv() { | ||
echo -e "\n[INFO] Installing venv" | ||
if [ ! -f "$VENV_DONE" ]; then | ||
wget http://nexus:8081/repository/binaries/geth/1.14.8/geth -O /usr/local/bin/geth && \ | ||
chmod +x /usr/local/bin/geth && \ | ||
python3.11 -m venv /opt/venv && \ | ||
source /opt/venv/bin/activate && \ | ||
echo "export SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt" >> ~/.bashrc && \ | ||
echo "source /opt/venv/bin/activate" >> ~/.bashrc && \ | ||
pip3 install -r ~/requirements.txt | ||
touch $VENV_DONE | ||
else | ||
echo already installed | ||
fi | ||
} | ||
|
||
spinner & | ||
install_venv | ||
#stop spinner | ||
kill "$!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pandas | ||
web3>=6.20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"title": "пример упражнения для blockchain", | ||
"description": "blockchain example", | ||
"details": { | ||
"steps": [ | ||
{ | ||
"title": "готовим окружение", | ||
"text": "step0.md" | ||
}, | ||
{ | ||
"title": "создаем кошелек", | ||
"text": "step1.md" | ||
} | ||
], | ||
"intro": { | ||
"text": "intro.md" | ||
}, | ||
"assets": { | ||
"host01": [ | ||
{ | ||
"file": "getPrivateKey.py", | ||
"target": "/root" | ||
}, | ||
{ | ||
"file": "prepare.sh", | ||
"target": "/usr/local/bin", | ||
"chmod": "+x" | ||
} | ||
] | ||
} | ||
}, | ||
"files": [], | ||
"environment": { | ||
"uilayout": "terminal", | ||
"uisettings": "yaml", | ||
"showide": true, | ||
"uimessage1": "\u001b[32mYour Interactive Bash Terminal.\u001b[m\r\n", | ||
"uimessage2": "\u001b[32mYour Interactive Bash Terminal.\u001b[m\r\n" | ||
}, | ||
"backend": { | ||
"imageid": "python" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Данный обучающий курс направлен на получение основных навыков работы с EVM-совесместимым блокчейном на программном уровне и базовых знаний о библиотеке web3.py. | ||
|
||
Python, web3.py и Jupyter выбраны исключительно для простоты демонстрации и легкости в обучении. | ||
|
||
Практически любой из языков программирования (точно Go, JavaScript, Java) имеют аналогичные библиотеки и зачастую называются схожим образом. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Установим venv и необходимые библиотеки: | ||
|
||
`prepare.sh && . ~/.bashrc`{{execute}} | ||
|
||
проверим установку | ||
|
||
`geth account --help`{{execute}} | ||
|
||
идем дальше |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
В учебном примере мы создадим отдельную директорию хранилища ключей в текущей директории. По умолчнию geth сохраняет ключи в домашней директории в папке ~/.ethereum/keystore | ||
|
||
При создании нового аккаунта (кошелька) geth попросит ввести пароль для шифрования приватного ключа вашего аккаунта. В учебном примере мы сохраняем пароль в файл. | ||
|
||
ВАЖНО! Для реальных аккаунтов пароль (и приватный ключ) должны быть надежно защищены. | ||
|
||
`mkdir -p ~/keystore && rm -rf ~/keystore/* && echo 123 > ~/password`{{execute}} | ||
|
||
Создадим новый аккаунт, файл ключа для удобства переименуем в 'mykeyfile': | ||
|
||
`geth --password ~/password --keystore ~/keystore account new && mv ~/keystore/$(ls ./keystore) ~/keystore/mykeyfile`{{execute}} | ||
|
||
Адрес кошелька будет указан в поле "Public address of the key:" | ||
|
||
Посмотрим содержимое файла ключа (данные хранятся в формате JSON): | ||
`cat ~/keystore/mykeyfile | jq -C`{{execute}} | ||
|
||
В поле "address" указан Ethereum-адрес (часть публичного ключа), а в поле "crypto" — зашифрованный паролем приватный ключ и параметры шифрования. | ||
Теперь попробуем расшифровать приватный ключ, зная пароль. Установим библиотеку eth-tester: | ||
|
||
`pip install eth-tester`{{execute}} | ||
|
||
Считываем файл ключа и, указав пароль, расшифровываем приватный ключ: | ||
|
||
`python ~/getPrivateKey.py`{{execute}} | ||
|
||
Получим адрес из расшифрованного приватного ключа. Для этого добавим блок кода в исходный файл: | ||
<pre class="file" data-filename="~/getPrivateKey.py" data-target="insert" data-marker="##end Of First Block##"> | ||
|
||
account = w3.eth.account.from_key(bin_private_key) | ||
address = w3.to_checksum_address(account.address) | ||
print(f"Ethereum address: {address}") | ||
</pre> | ||
|
||
Заново запустим и убеждаемся, что адрес совпадает с адресом из предыдущего шага: | ||
|
||
`python ~/getPrivateKey.py`{{execute}} |