From 0d8112f6b4a26c6ea523e6e8079c794c47f3eeea Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Mon, 11 Apr 2016 11:10:43 +0200 Subject: [PATCH] split trezor.crypto into more modules --- Makefile | 2 +- emu.sh | 1 + src/trezor/crypto.py | 4 ---- src/trezor/crypto/__init__.py | 0 src/trezor/crypto/base58.py | 21 +++++++++++++++++++++ src/trezor/crypto/sha256.py | 6 ++++++ 6 files changed, 29 insertions(+), 5 deletions(-) delete mode 100644 src/trezor/crypto.py create mode 100644 src/trezor/crypto/__init__.py create mode 100644 src/trezor/crypto/base58.py create mode 100644 src/trezor/crypto/sha256.py diff --git a/Makefile b/Makefile index a297ebf74..68d28edcc 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ build_unix: ## build unix port make -C vendor/micropython/unix run_unix: ## run unix port - ./vendor/micropython/unix/micropython + cd src ; ../vendor/micropython/unix/micropython flash: ## flash firmware using st-flash st-flash write $(STMHAL_BUILD_DIR)/firmware0.bin 0x8000000 diff --git a/emu.sh b/emu.sh index b111f3a8f..cf6046394 100755 --- a/emu.sh +++ b/emu.sh @@ -2,3 +2,4 @@ cd `dirname $0`/src rm -f ../pipe.* ../vendor/micropython/unix/micropython $* -O0 -X heapsize=100000 main.py +rm -f ../pipe.* diff --git a/src/trezor/crypto.py b/src/trezor/crypto.py deleted file mode 100644 index 50dcfe176..000000000 --- a/src/trezor/crypto.py +++ /dev/null @@ -1,4 +0,0 @@ -from TrezorCrypto import Base58, Sha256 - -base58 = Base58() -sha256 = Sha256() diff --git a/src/trezor/crypto/__init__.py b/src/trezor/crypto/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/src/trezor/crypto/base58.py b/src/trezor/crypto/base58.py new file mode 100644 index 000000000..1cff0f3cd --- /dev/null +++ b/src/trezor/crypto/base58.py @@ -0,0 +1,21 @@ +from TrezorCrypto import Base58 +from . import sha256 + +_base58 = Base58() + +def encode(data): + return _base58.encode(data) + +def decode(string): + return _base58.decode(string) + +def encode_check(data, hashlen=4): + h = sha256.hash(data) + return encode(data + h[:hashlen]) + +def decode_check(string, hashlen=4): + data = decode(string) + d, h = data[:-hashlen], data[-hashlen:] + if sha256.hash(d) != h: + raise RuntimeError('Checksum error') + return d diff --git a/src/trezor/crypto/sha256.py b/src/trezor/crypto/sha256.py new file mode 100644 index 000000000..a037e0932 --- /dev/null +++ b/src/trezor/crypto/sha256.py @@ -0,0 +1,6 @@ +from TrezorCrypto import Sha256 + +_sha256 = Sha256() + +def hash(data): + return _sha256.hash(data)