1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-05 13:01:12 +00:00

src/trezor/crypto: refactor {blake256,sha256}_ripemd160_digest to trezor.crypto.scripts

This commit is contained in:
Pavol Rusnak 2018-10-10 13:40:55 +02:00
parent 2277a9c754
commit 4a26870fcc
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
4 changed files with 23 additions and 29 deletions

View File

@ -1,6 +1,7 @@
# generated from coininfo.py.mako # generated from coininfo.py.mako
# do not edit manually! # do not edit manually!
from trezor.crypto.base58 import blake256_32, groestl512d_32, sha256d_32 from trezor.crypto.base58 import blake256_32, groestl512d_32, sha256d_32
from trezor.crypto.scripts import blake256_ripemd160_digest, sha256_ripemd160_digest
class CoinInfo: class CoinInfo:
@ -48,12 +49,15 @@ class CoinInfo:
if curve_name == "secp256k1-groestl": if curve_name == "secp256k1-groestl":
self.b58_hash = groestl512d_32 self.b58_hash = groestl512d_32
self.sign_hash_double = False self.sign_hash_double = False
self.script_hash = sha256_ripemd160_digest
elif curve_name == "secp256k1-decred": elif curve_name == "secp256k1-decred":
self.b58_hash = blake256_32 self.b58_hash = blake256_32
self.sign_hash_double = False self.sign_hash_double = False
self.script_hash = blake256_ripemd160_digest
else: else:
self.b58_hash = sha256d_32 self.b58_hash = sha256d_32
self.sign_hash_double = True self.sign_hash_double = True
self.script_hash = sha256_ripemd160_digest
# fmt: off # fmt: off

View File

@ -1,7 +1,7 @@
from micropython import const from micropython import const
from trezor.crypto import base58, bech32, cashaddr from trezor.crypto import base58, bech32, cashaddr
from trezor.crypto.hashlib import ripemd160, sha256 from trezor.crypto.hashlib import sha256
from trezor.messages import FailureType, InputScriptType from trezor.messages import FailureType, InputScriptType
from trezor.utils import ensure from trezor.utils import ensure
@ -9,10 +9,8 @@ from apps.common import address_type
from apps.common.coininfo import CoinInfo from apps.common.coininfo import CoinInfo
from apps.wallet.sign_tx.multisig import multisig_get_pubkeys, multisig_pubkey_index from apps.wallet.sign_tx.multisig import multisig_get_pubkeys, multisig_pubkey_index
from apps.wallet.sign_tx.scripts import ( from apps.wallet.sign_tx.scripts import (
blake256_ripemd160_digest,
output_script_multisig, output_script_multisig,
output_script_native_p2wpkh_or_p2wsh, output_script_native_p2wpkh_or_p2wsh,
sha256_ripemd160_digest,
) )
# supported witness version for bech32 addresses # supported witness version for bech32 addresses
@ -93,10 +91,7 @@ def address_multisig_p2sh(pubkeys: bytes, m: int, coin: CoinInfo):
FailureType.ProcessError, "Multisig not enabled on this coin" FailureType.ProcessError, "Multisig not enabled on this coin"
) )
redeem_script = output_script_multisig(pubkeys, m) redeem_script = output_script_multisig(pubkeys, m)
if coin.decred: redeem_script_hash = coin.script_hash(redeem_script)
redeem_script_hash = blake256_ripemd160_digest(redeem_script)
else:
redeem_script_hash = sha256_ripemd160_digest(redeem_script)
return address_p2sh(redeem_script_hash, coin) return address_p2sh(redeem_script_hash, coin)
@ -121,7 +116,7 @@ def address_multisig_p2wsh(pubkeys: bytes, m: int, hrp: str):
def address_pkh(pubkey: bytes, coin: CoinInfo) -> str: def address_pkh(pubkey: bytes, coin: CoinInfo) -> str:
s = address_type.tobytes(coin.address_type) + sha256_ripemd160_digest(pubkey) s = address_type.tobytes(coin.address_type) + coin.script_hash(pubkey)
return base58.encode_check(bytes(s), coin.b58_hash) return base58.encode_check(bytes(s), coin.b58_hash)
@ -133,13 +128,13 @@ def address_p2sh(redeem_script_hash: bytes, coin: CoinInfo) -> str:
def address_p2wpkh_in_p2sh(pubkey: bytes, coin: CoinInfo) -> str: def address_p2wpkh_in_p2sh(pubkey: bytes, coin: CoinInfo) -> str:
pubkey_hash = ecdsa_hash_pubkey(pubkey, coin) pubkey_hash = ecdsa_hash_pubkey(pubkey, coin)
redeem_script = output_script_native_p2wpkh_or_p2wsh(pubkey_hash) redeem_script = output_script_native_p2wpkh_or_p2wsh(pubkey_hash)
redeem_script_hash = sha256_ripemd160_digest(redeem_script) redeem_script_hash = coin.script_hash(redeem_script)
return address_p2sh(redeem_script_hash, coin) return address_p2sh(redeem_script_hash, coin)
def address_p2wsh_in_p2sh(witness_script_hash: bytes, coin: CoinInfo) -> str: def address_p2wsh_in_p2sh(witness_script_hash: bytes, coin: CoinInfo) -> str:
redeem_script = output_script_native_p2wpkh_or_p2wsh(witness_script_hash) redeem_script = output_script_native_p2wpkh_or_p2wsh(witness_script_hash)
redeem_script_hash = sha256_ripemd160_digest(redeem_script) redeem_script_hash = coin.script_hash(redeem_script)
return address_p2sh(redeem_script_hash, coin) return address_p2sh(redeem_script_hash, coin)
@ -185,12 +180,7 @@ def ecdsa_hash_pubkey(pubkey: bytes, coin: CoinInfo) -> bytes:
else: else:
ensure(len(pubkey) == 33) # compresssed format ensure(len(pubkey) == 33) # compresssed format
if coin.decred: return coin.script_hash(pubkey)
return blake256_ripemd160_digest(pubkey)
h = sha256(pubkey).digest()
h = ripemd160(h).digest()
return h
def address_short(coin: CoinInfo, address: str) -> str: def address_short(coin: CoinInfo, address: str) -> str:

View File

@ -1,4 +1,3 @@
from trezor.crypto.hashlib import blake256, ripemd160, sha256
from trezor.messages.MultisigRedeemScriptType import MultisigRedeemScriptType from trezor.messages.MultisigRedeemScriptType import MultisigRedeemScriptType
from apps.common.coininfo import CoinInfo from apps.common.coininfo import CoinInfo
@ -265,15 +264,3 @@ def append_pubkey(w: bytearray, pubkey: bytes) -> bytearray:
write_op_push(w, len(pubkey)) write_op_push(w, len(pubkey))
write_bytes(w, pubkey) write_bytes(w, pubkey)
return w return w
def sha256_ripemd160_digest(b: bytes) -> bytes:
h = sha256(b).digest()
h = ripemd160(h).digest()
return h
def blake256_ripemd160_digest(b: bytes) -> bytes:
h = blake256(b).digest()
h = ripemd160(h).digest()
return h

View File

@ -0,0 +1,13 @@
from trezor.crypto.hashlib import blake256, ripemd160, sha256
def sha256_ripemd160_digest(b: bytes) -> bytes:
h = sha256(b).digest()
h = ripemd160(h).digest()
return h
def blake256_ripemd160_digest(b: bytes) -> bytes:
h = blake256(b).digest()
h = ripemd160(h).digest()
return h