forked from LedgerHQ/app-bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_sign.py
61 lines (48 loc) · 2.08 KB
/
test_sign.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from hashlib import sha256
import json
from pathlib import Path
from typing import Tuple, List, Dict, Any
from ecdsa.curves import SECP256k1
from ecdsa.keys import VerifyingKey
from ecdsa.util import sigdecode_der
from bitcoin_client.hwi.serialization import CTransaction
def sign_from_json(cmd, filepath: Path):
tx_dct: Dict[str, Any] = json.load(open(filepath, "r"))
raw_utxos: List[Tuple[bytes, int]] = [
(bytes.fromhex(utxo_dct["raw"]), output_index)
for utxo_dct in tx_dct["utxos"]
for output_index in utxo_dct["output_indexes"]
]
to_address: str = tx_dct["to"]
to_amount: int = tx_dct["amount"]
fees: int = tx_dct["fees"]
sigs = cmd.sign_new_tx(address=to_address,
amount=to_amount,
fees=fees,
change_path=tx_dct["change_path"],
sign_paths=tx_dct["sign_paths"],
raw_utxos=raw_utxos,
lock_time=tx_dct["lock_time"])
expected_tx = CTransaction.from_bytes(bytes.fromhex(tx_dct["raw"]))
witnesses = expected_tx.wit.vtxinwit
for witness, (tx_hash_digest, sign_pub_key, (v, der_sig)) in zip(witnesses, sigs):
expected_der_sig, expected_pubkey = witness.scriptWitness.stack
assert expected_pubkey == sign_pub_key
assert expected_der_sig == der_sig
pk: VerifyingKey = VerifyingKey.from_string(
sign_pub_key,
curve=SECP256k1,
hashfunc=sha256
)
assert pk.verify_digest(signature=der_sig[:-1], # remove sighash
digest=tx_hash_digest,
sigdecode=sigdecode_der) is True
def test_sign_p2wpkh(cmd):
for filepath in Path("data").rglob("p2wpkh/tx.json"):
sign_from_json(cmd, filepath)
def test_sign_p2sh_p2wpkh(cmd):
for filepath in Path("data").rglob("p2sh-p2wpkh/tx.json"):
sign_from_json(cmd, filepath)
def test_sign_p2pkh(cmd):
for filepath in Path("data").rglob("p2pkh/tx.json"):
sign_from_json(cmd, filepath)