parent
b7b46b6980
commit
dc6701af90
@ -0,0 +1,118 @@
|
||||
from apps.wallet.sign_tx.writers import *
|
||||
|
||||
|
||||
# TX Scripts
|
||||
# ===
|
||||
|
||||
# -------------------------- First gen --------------------------
|
||||
|
||||
# =============== P2PK ===============
|
||||
# obsolete
|
||||
|
||||
|
||||
# =============== P2PKH ===============
|
||||
|
||||
def input_script_p2pkh_or_p2sh(pubkey: bytes, signature: bytes) -> bytearray:
|
||||
w = bytearray_with_cap(5 + len(signature) + 1 + 5 + len(pubkey))
|
||||
append_signature_and_pubkey(w, pubkey, signature)
|
||||
return w
|
||||
|
||||
|
||||
def output_script_p2pkh(pubkeyhash: bytes) -> bytearray:
|
||||
s = bytearray(25)
|
||||
s[0] = 0x76 # OP_DUP
|
||||
s[1] = 0xA9 # OP_HASH_160
|
||||
s[2] = 0x14 # pushing 20 bytes
|
||||
s[3:23] = pubkeyhash
|
||||
s[23] = 0x88 # OP_EQUALVERIFY
|
||||
s[24] = 0xAC # OP_CHECKSIG
|
||||
return s
|
||||
|
||||
|
||||
# =============== P2SH ===============
|
||||
# see https://github.com/bitcoin/bips/blob/master/bip-0016.mediawiki
|
||||
|
||||
# input script (scriptSig) is the same as input_script_p2pkh_or_p2sh
|
||||
|
||||
# output script (scriptPubKey) is A9 14 <scripthash> 87
|
||||
def output_script_p2sh(scripthash: bytes) -> bytearray:
|
||||
s = bytearray(23)
|
||||
s[0] = 0xA9 # OP_HASH_160
|
||||
s[1] = 0x14 # pushing 20 bytes
|
||||
s[2:22] = scripthash
|
||||
s[22] = 0x87 # OP_EQUAL
|
||||
return s
|
||||
|
||||
|
||||
# -------------------------- SegWit --------------------------
|
||||
|
||||
# =============== Native P2WPKH ===============
|
||||
# see https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#p2wpkh
|
||||
# P2WPKH (Pay-to-Witness-Public-Key-Hash) is the segwit native P2PKH
|
||||
# not backwards compatible
|
||||
|
||||
# input script is completely replaced by the witness and therefore empty
|
||||
def input_script_native_p2wpkh_or_p2wsh() -> bytearray:
|
||||
return bytearray(0)
|
||||
|
||||
|
||||
# output script consists of 00 14 <20-byte-key-hash>
|
||||
def output_script_native_p2wpkh_or_p2wsh(pubkeyhash: bytes) -> bytearray:
|
||||
w = bytearray_with_cap(3 + len(pubkeyhash))
|
||||
w.append(0x00) # witness version byte
|
||||
w.append(len(pubkeyhash)) # pub key hash length is 20 (P2WPKH) or 32 (P2WSH) bytes
|
||||
write_bytes(w, pubkeyhash) # pub key hash
|
||||
return w
|
||||
|
||||
|
||||
# =============== Native P2WPKH nested in P2SH ===============
|
||||
# P2WPKH is nested in P2SH to be backwards compatible
|
||||
# see https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#witness-program
|
||||
|
||||
# input script (scriptSig) is 16 00 14 <pubkeyhash>
|
||||
# signature is moved to the witness
|
||||
def input_script_p2wpkh_in_p2sh(pubkeyhash: bytes) -> bytearray:
|
||||
w = bytearray_with_cap(3 + len(pubkeyhash))
|
||||
w.append(0x16) # 0x16 - length of the redeemScript
|
||||
w.append(0x00) # witness version byte
|
||||
w.append(0x14) # P2WPKH witness program (pub key hash length)
|
||||
write_bytes(w, pubkeyhash) # pub key hash
|
||||
return w
|
||||
|
||||
# output script (scriptPubKey) is A9 14 <scripthash> 87
|
||||
# which is same as the output_script_p2sh
|
||||
|
||||
|
||||
# =============== Native P2WSH ===============
|
||||
# see https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#p2wsh
|
||||
# P2WSH (Pay-to-Witness-Script-Hash) is segwit native P2SH
|
||||
# not backwards compatible
|
||||
|
||||
# input script is completely replaced by the witness and therefore empty
|
||||
# same as input_script_native_p2wpkh_or_p2wsh
|
||||
|
||||
# output script consists of 00 20 <32-byte-key-hash>
|
||||
# same as output_script_native_p2wpkh_or_p2wsh (only different length)
|
||||
|
||||
|
||||
# -------------------------- Others --------------------------
|
||||
|
||||
# === OP_RETURN script
|
||||
|
||||
def output_script_paytoopreturn(data: bytes) -> bytearray:
|
||||
w = bytearray_with_cap(1 + 5 + len(data))
|
||||
w.append(0x6A) # OP_RETURN
|
||||
write_op_push(w, len(data))
|
||||
w.extend(data)
|
||||
return w
|
||||
|
||||
|
||||
# === helpers
|
||||
|
||||
def append_signature_and_pubkey(w: bytearray, pubkey: bytes, signature: bytes) -> bytearray:
|
||||
write_op_push(w, len(signature) + 1)
|
||||
write_bytes(w, signature)
|
||||
w.append(0x01) # SIGHASH_ALL
|
||||
write_op_push(w, len(pubkey))
|
||||
write_bytes(w, pubkey)
|
||||
return w
|
@ -0,0 +1,215 @@
|
||||
from common import *
|
||||
|
||||
from trezor.utils import chunks
|
||||
from trezor.crypto import bip32, bip39
|
||||
from trezor.messages.SignTx import SignTx
|
||||
from trezor.messages.TxInputType import TxInputType
|
||||
from trezor.messages.TxOutputType import TxOutputType
|
||||
from trezor.messages.TxRequest import TxRequest
|
||||
from trezor.messages.TxAck import TxAck
|
||||
from trezor.messages.TransactionType import TransactionType
|
||||
from trezor.messages.RequestType import TXINPUT, TXOUTPUT, TXMETA, TXFINISHED
|
||||
from trezor.messages.TxRequestDetailsType import TxRequestDetailsType
|
||||
from trezor.messages.TxRequestSerializedType import TxRequestSerializedType
|
||||
from trezor.messages import InputScriptType
|
||||
from trezor.messages import OutputScriptType
|
||||
|
||||
from apps.common import coins
|
||||
from apps.wallet.sign_tx import signing
|
||||
|
||||
|
||||
class TestSignSegwitTxNativeP2WPKH(unittest.TestCase):
|
||||
# pylint: disable=C0301
|
||||
|
||||
def test_send_native_p2wpkh(self):
|
||||
|
||||
coin = coins.by_name('Testnet')
|
||||
|
||||
seed = bip39.seed(' '.join(['all'] * 12), '')
|
||||
root = bip32.from_seed(seed, 'secp256k1')
|
||||
|
||||
inp1 = TxInputType(
|
||||
# 49'/1'/0'/0/0" - tb1qqzv60m9ajw8drqulta4ld4gfx0rdh82un5s65s
|
||||
address_n=[49 | 0x80000000, 1 | 0x80000000, 0 | 0x80000000, 0, 0],
|
||||
amount=12300000,
|
||||
prev_hash=unhexlify('09144602765ce3dd8f4329445b20e3684e948709c5cdcaf12da3bb079c99448a'),
|
||||
prev_index=0,
|
||||
script_type=InputScriptType.SPENDWITNESS,
|
||||
sequence=0xffffffff,
|
||||
)
|
||||
out1 = TxOutputType(
|
||||
address='2N4Q5FhU2497BryFfUgbqkAJE87aKHUhXMp',
|
||||
amount=5000000,
|
||||
script_type=OutputScriptType.PAYTOADDRESS,
|
||||
address_n=None, # @todo ask honza about sanitizing
|
||||
)
|
||||
out2 = TxOutputType(
|
||||
address='tb1q694ccp5qcc0udmfwgp692u2s2hjpq5h407urtu',
|
||||
script_type=OutputScriptType.PAYTOADDRESS,
|
||||
amount=12300000 - 11000 - 5000000,
|
||||
address_n=None,
|
||||
)
|
||||
tx = SignTx(coin_name='Testnet', version=None, lock_time=None, inputs_count=1, outputs_count=2)
|
||||
|
||||
messages = [
|
||||
None,
|
||||
|
||||
# check fee
|
||||
TxRequest(request_type=TXINPUT, details=TxRequestDetailsType(request_index=0, tx_hash=None)),
|
||||
TxAck(tx=TransactionType(inputs=[inp1])),
|
||||
|
||||
TxRequest(request_type=TXOUTPUT, details=TxRequestDetailsType(request_index=0, tx_hash=None), serialized=None),
|
||||
TxAck(tx=TransactionType(outputs=[out1])),
|
||||
|
||||
signing.UiConfirmOutput(out1, coin),
|
||||
True,
|
||||
|
||||
TxRequest(request_type=TXOUTPUT, details=TxRequestDetailsType(request_index=1, tx_hash=None), serialized=None),
|
||||
TxAck(tx=TransactionType(outputs=[out2])),
|
||||
|
||||
signing.UiConfirmOutput(out2, coin),
|
||||
True,
|
||||
|
||||
signing.UiConfirmTotal(12300000 - 11000, 11000, coin),
|
||||
True,
|
||||
|
||||
# sign tx
|
||||
TxRequest(request_type=TXINPUT, details=TxRequestDetailsType(request_index=0, tx_hash=None), serialized=None),
|
||||
TxAck(tx=TransactionType(inputs=[inp1])),
|
||||
|
||||
TxRequest(request_type=TXOUTPUT, details=TxRequestDetailsType(request_index=0, tx_hash=None), serialized=TxRequestSerializedType(
|
||||
# returned serialized inp1
|
||||
serialized_tx=unhexlify('010000000001018a44999c07bba32df1cacdc50987944e68e3205b4429438fdde35c76024614090000000000ffffffff'),
|
||||
)),
|
||||
TxAck(tx=TransactionType(outputs=[out1])),
|
||||
|
||||
TxRequest(request_type=TXOUTPUT, details=TxRequestDetailsType(request_index=1, tx_hash=None), serialized=TxRequestSerializedType(
|
||||
# returned serialized out1
|
||||
serialized_tx=unhexlify('02404b4c000000000017a9147a55d61848e77ca266e79a39bfc85c580a6426c987'),
|
||||
signature_index=None,
|
||||
signature=None,
|
||||
)),
|
||||
TxAck(tx=TransactionType(outputs=[out2])),
|
||||
|
||||
# segwit
|
||||
TxRequest(request_type=TXINPUT, details=TxRequestDetailsType(request_index=0, tx_hash=None), serialized=TxRequestSerializedType(
|
||||
# returned serialized out2
|
||||
serialized_tx=unhexlify('a8386f0000000000160014d16b8c0680c61fc6ed2e407455715055e41052f5'),
|
||||
signature_index=None,
|
||||
signature=None,
|
||||
)),
|
||||
TxAck(tx=TransactionType(inputs=[inp1])),
|
||||
|
||||
TxRequest(request_type=TXFINISHED, details=None, serialized=TxRequestSerializedType(
|
||||
serialized_tx=unhexlify('02483045022100a7ca8f097525f9044e64376dc0a0f5d4aeb8d15d66808ba97979a0475b06b66502200597c8ebcef63e047f9aeef1a8001d3560470cf896c12f6990eec4faec599b950121033add1f0e8e3c3136f7428dd4a4de1057380bd311f5b0856e2269170b4ffa65bf00000000'),
|
||||
signature_index=None,
|
||||
signature=None,
|
||||
)),
|
||||
]
|
||||
|
||||
signer = signing.sign_tx(tx, root)
|
||||
for request, response in chunks(messages, 2):
|
||||
self.assertEqualEx(signer.send(request), response)
|
||||
with self.assertRaises(StopIteration):
|
||||
signer.send(None)
|
||||
|
||||
def test_send_native_p2wpkh_change(self):
|
||||
|
||||
coin = coins.by_name('Testnet')
|
||||
|
||||
seed = bip39.seed(' '.join(['all'] * 12), '')
|
||||
root = bip32.from_seed(seed, 'secp256k1')
|
||||
|
||||
inp1 = TxInputType(
|
||||
# 49'/1'/0'/0/0" - tb1qqzv60m9ajw8drqulta4ld4gfx0rdh82un5s65s
|
||||
address_n=[49 | 0x80000000, 1 | 0x80000000, 0 | 0x80000000, 0, 0],
|
||||
amount=12300000,
|
||||
prev_hash=unhexlify('09144602765ce3dd8f4329445b20e3684e948709c5cdcaf12da3bb079c99448a'),
|
||||
prev_index=0,
|
||||
script_type=InputScriptType.SPENDWITNESS,
|
||||
sequence=0xffffffff,
|
||||
)
|
||||
out1 = TxOutputType(
|
||||
address='2N4Q5FhU2497BryFfUgbqkAJE87aKHUhXMp',
|
||||
amount=5000000,
|
||||
script_type=OutputScriptType.PAYTOADDRESS,
|
||||
address_n=None, # @todo ask honza about sanitizing
|
||||
)
|
||||
out2 = TxOutputType(
|
||||
address=None,
|
||||
address_n=[49 | 0x80000000, 1 | 0x80000000, 0 | 0x80000000, 1, 0],
|
||||
script_type=OutputScriptType.PAYTOWITNESS,
|
||||
amount=12300000 - 11000 - 5000000,
|
||||
)
|
||||
tx = SignTx(coin_name='Testnet', version=None, lock_time=None, inputs_count=1, outputs_count=2)
|
||||
|
||||
messages = [
|
||||
None,
|
||||
|
||||
# check fee
|
||||
TxRequest(request_type=TXINPUT, details=TxRequestDetailsType(request_index=0, tx_hash=None)),
|
||||
TxAck(tx=TransactionType(inputs=[inp1])),
|
||||
|
||||
TxRequest(request_type=TXOUTPUT, details=TxRequestDetailsType(request_index=0, tx_hash=None), serialized=None),
|
||||
TxAck(tx=TransactionType(outputs=[out1])),
|
||||
|
||||
signing.UiConfirmOutput(out1, coin),
|
||||
True,
|
||||
|
||||
TxRequest(request_type=TXOUTPUT, details=TxRequestDetailsType(request_index=1, tx_hash=None), serialized=None),
|
||||
TxAck(tx=TransactionType(outputs=[out2])),
|
||||
|
||||
signing.UiConfirmTotal(5000000, 11000, coin),
|
||||
True,
|
||||
|
||||
# sign tx
|
||||
TxRequest(request_type=TXINPUT, details=TxRequestDetailsType(request_index=0, tx_hash=None), serialized=None),
|
||||
TxAck(tx=TransactionType(inputs=[inp1])),
|
||||
|
||||
TxRequest(request_type=TXOUTPUT, details=TxRequestDetailsType(request_index=0, tx_hash=None), serialized=TxRequestSerializedType(
|
||||
# returned serialized inp1
|
||||
serialized_tx=unhexlify('010000000001018a44999c07bba32df1cacdc50987944e68e3205b4429438fdde35c76024614090000000000ffffffff'),
|
||||
)),
|
||||
TxAck(tx=TransactionType(outputs=[out1])),
|
||||
|
||||
TxRequest(request_type=TXOUTPUT, details=TxRequestDetailsType(request_index=1, tx_hash=None), serialized=TxRequestSerializedType(
|
||||
# returned serialized out1
|
||||
serialized_tx=unhexlify('02404b4c000000000017a9147a55d61848e77ca266e79a39bfc85c580a6426c987'),
|
||||
signature_index=None,
|
||||
signature=None,
|
||||
)),
|
||||
TxAck(tx=TransactionType(outputs=[out2])),
|
||||
|
||||
# segwit
|
||||
TxRequest(request_type=TXINPUT, details=TxRequestDetailsType(request_index=0, tx_hash=None), serialized=TxRequestSerializedType(
|
||||
# returned serialized out2
|
||||
serialized_tx=unhexlify('a8386f0000000000160014d16b8c0680c61fc6ed2e407455715055e41052f5'),
|
||||
signature_index=None,
|
||||
signature=None,
|
||||
)),
|
||||
TxAck(tx=TransactionType(inputs=[inp1])),
|
||||
|
||||
TxRequest(request_type=TXFINISHED, details=None, serialized=TxRequestSerializedType(
|
||||
serialized_tx=unhexlify('02483045022100a7ca8f097525f9044e64376dc0a0f5d4aeb8d15d66808ba97979a0475b06b66502200597c8ebcef63e047f9aeef1a8001d3560470cf896c12f6990eec4faec599b950121033add1f0e8e3c3136f7428dd4a4de1057380bd311f5b0856e2269170b4ffa65bf00000000'),
|
||||
signature_index=None,
|
||||
signature=None,
|
||||
)),
|
||||
]
|
||||
|
||||
signer = signing.sign_tx(tx, root)
|
||||
for request, response in chunks(messages, 2):
|
||||
self.assertEqualEx(signer.send(request), response)
|
||||
with self.assertRaises(StopIteration):
|
||||
signer.send(None)
|
||||
|
||||
def assertEqualEx(self, a, b):
|
||||
# hack to avoid adding __eq__ to signing.Ui* classes
|
||||
if ((isinstance(a, signing.UiConfirmOutput) and isinstance(b, signing.UiConfirmOutput)) or
|
||||
(isinstance(a, signing.UiConfirmTotal) and isinstance(b, signing.UiConfirmTotal))):
|
||||
return self.assertEqual(a.__dict__, b.__dict__)
|
||||
else:
|
||||
return self.assertEqual(a, b)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Loading…
Reference in new issue