Repository navigation
Expand file tree
/
Copy pathtest_desktop_wallet.py
More file actions
61 lines (52 loc) · 2.73 KB
/
Copy pathtest_desktop_wallet.py
File metadata and controls
61 lines (52 loc) · 2.73 KB
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
import json
from pathlib import Path
import tempfile
import unittest
from unittest.mock import patch
from embit.transaction import Transaction, TransactionInput, TransactionOutput
from embit import script
from desktop_wallet import Coin, Snapshot, Wallet, coins, destination_script, parse_amount, prepare, estimated_fee
from dingo_wallet import COIN
class DesktopWalletTests(unittest.TestCase):
words = "abandon " * 11 + "about"
def setUp(self):
tmp = tempfile.TemporaryDirectory()
self.addCleanup(tmp.cleanup)
self.path = Path(tmp.name) / "sample.dingowallet"
self.wallet = Wallet.create(self.path, self.words, "sample password")
def test_recovery_encryption_and_existing_file(self):
self.assertNotIn(b"abandon", self.path.read_bytes())
with self.assertRaises(ValueError):
Wallet.open(self.path, "wrong")
with self.assertRaises(ValueError):
Wallet.create(self.path, self.words, "other")
opened = Wallet.open(self.path, "sample password")
self.assertEqual(opened.address(0, 0), "DBus3bamQjgJULBJtYXpEzDWQRwF5iwxgC")
def test_multiple_inputs_and_change(self):
coins_in, parents = [], {}
for i in (0, 1):
addr = self.wallet.address(i, 0)
out = TransactionOutput(2 * COIN, script.Script(destination_script(addr)))
parent = Transaction(vin=[TransactionInput(bytes([i]) * 32, 1)], vout=[out])
txid = parent.txid().hex()
parents[txid] = parent.serialize().hex()
coins_in.append(Coin(txid, 0, 2 * COIN, i, 0))
snap = Snapshot(4 * COIN, 0, coins_in, [1, 1])
with patch("mainnet_electrum.raw_transaction", side_effect=lambda txid, run: parents[txid]):
plan = prepare(self.wallet, snap, self.wallet.address(0, 1), parse_amount("2,5"))
tx = Transaction.from_string(plan["hex"])
self.assertEqual(len(tx.vin), 2)
self.assertEqual(plan["txid"], tx.txid().hex())
self.assertEqual(tx.vout[0].value, 250000000)
self.assertEqual(sum(o.value for o in tx.vout) + plan["fee"], 4 * COIN)
self.assertEqual(tx.vout[1].script_pubkey.data, destination_script(self.wallet.address(1, 1)))
def test_fee_rounds_up_each_started_kilobyte(self):
for size, expected in ((227, 1), (1000, 1), (1001, 2)):
self.assertEqual(estimated_fee(1, COIN, size - 78), expected * COIN)
def test_amount_and_network(self):
self.assertEqual(coins(100000), "0.001")
for text in ("NaN", "0", "-1", "0.123456789", "Infinity"):
with self.assertRaises(ValueError):
parse_amount(text)
with self.assertRaises(ValueError):
destination_script("PsGyoVVSD9VTYoDZGp5FmRB2BSoE69f9ZV")