Skip to content

Commit 3a939ad

Browse files
committed
Merge branch 'toggle-passphrase' of https://github.com/ben-kaufman/specter-desktop into toggle-passphrase
2 parents de0b623 + 9b24809 commit 3a939ad

File tree

5 files changed

+32
-57
lines changed

5 files changed

+32
-57
lines changed
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
from ..device import Device
22

3-
43
class HWIDevice(Device):
54
def __init__(self, name, alias, device_type, keys, fullpath, manager):
65
Device.__init__(self, name, alias, device_type, keys, fullpath, manager)
76
self.hwi_support = True
87
self.exportable_to_wallet = False
98

109
def create_psbts(self, base64_psbt, wallet):
11-
# fills non_witness_utxo for all inputs
1210
return { 'hwi': wallet.fill_psbt(base64_psbt) }
Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
from .hwi_device import HWIDevice
2-
from ..helpers import decode_base58, get_xpub_fingerprint, hash160
3-
from hwilib.serializations import PSBT
4-
52

63
class SDCardDevice(HWIDevice):
74
def __init__(self, name, alias, device_type, keys, fullpath, manager):
@@ -10,43 +7,6 @@ def __init__(self, name, alias, device_type, keys, fullpath, manager):
107
self.exportable_to_wallet = True
118

129
def create_psbts(self, base64_psbt, wallet):
13-
psbts = HWIDevice.create_psbts(self, base64_psbt, wallet)
14-
sdcard_psbt = PSBT()
15-
sdcard_psbt.deserialize(base64_psbt)
16-
if len(wallet.keys) > 1:
17-
for k in wallet.keys:
18-
key = b'\x01' + decode_base58(k.xpub)
19-
if k.fingerprint != '':
20-
fingerprint = bytes.fromhex(k.fingerprint)
21-
else:
22-
fingerprint = _get_xpub_fingerprint(k.xpub)
23-
if k.derivation != '':
24-
der = _der_to_bytes(k.derivation)
25-
else:
26-
der = b''
27-
value = fingerprint + der
28-
sdcard_psbt.unknown[key] = value
29-
psbts['sdcard'] = sdcard_psbt.serialize()
10+
psbts = super().create_psbts(base64_psbt, wallet)
11+
psbts['sdcard'] = wallet.fill_psbt(base64_psbt, non_witness=False, xpubs=True)
3012
return psbts
31-
32-
def _get_xpub_fingerprint(xpub):
33-
b = decode_base58(xpub)
34-
return hash160(b[-33:])[:4]
35-
36-
def _der_to_bytes(derivation):
37-
items = derivation.split("/")
38-
if len(items) == 0:
39-
return b''
40-
if items[0] == 'm':
41-
items = items[1:]
42-
if items[-1] == '':
43-
items = items[:-1]
44-
res = b''
45-
for item in items:
46-
index = 0
47-
if item[-1] == 'h' or item[-1] == "'":
48-
index += 0x80000000
49-
item = item[:-1]
50-
index += int(item)
51-
res += index.to_bytes(4,'big')
52-
return res

src/cryptoadvance/specter/devices/specter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from .hwi_device import HWIDevice
33
from hwilib.serializations import PSBT
44

5-
65
class Specter(HWIDevice):
76
def __init__(self, name, alias, device_type, keys, fullpath, manager):
87
super().__init__(name, alias, 'specter', keys, fullpath, manager)
@@ -16,6 +15,7 @@ def create_psbts(self, base64_psbt, wallet):
1615
psbts = super().create_psbts(base64_psbt, wallet)
1716
qr_psbt = PSBT()
1817
qr_psbt.deserialize(base64_psbt)
18+
# replace with compressed wallet information
1919
for inp in qr_psbt.inputs + qr_psbt.outputs:
2020
inp.witness_script = b""
2121
inp.redeem_script = b""

src/cryptoadvance/specter/helpers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ def get_xpub_fingerprint(xpub):
114114
b = decode_base58(xpub)
115115
return hash160(b[-33:])[:4]
116116

117+
117118
def which(program):
118119
''' mimics the "which" command in bash but even for stuff not on the path.
119120
Also has implicit pyinstaller support
@@ -273,7 +274,7 @@ def der_to_bytes(derivation):
273274
index += 0x80000000
274275
item = item[:-1]
275276
index += int(item)
276-
res += index.to_bytes(4,'big')
277+
res += index.to_bytes(4,'little')
277278
return res
278279

279280
def get_devices_with_keys_by_type(app, cosigners, wallet_type):

src/cryptoadvance/specter/wallet.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -539,19 +539,35 @@ def createpsbt(self, address:str, amount:float, subtract:bool=False, fee_rate:fl
539539

540540
return psbt
541541

542-
def fill_psbt(self, b64psbt):
542+
def fill_psbt(self, b64psbt, non_witness:bool=True, xpubs:bool=True):
543543
psbt = PSBT()
544544
psbt.deserialize(b64psbt)
545-
for i, inp in enumerate(psbt.tx.vin):
546-
txid = inp.prevout.hash.to_bytes(32,'big').hex()
547-
try:
548-
res = self.cli.gettransaction(txid)
549-
except:
550-
raise SpecterError("Can't find previous transaction in the wallet.")
551-
stream = BytesIO(bytes.fromhex(res["hex"]))
552-
prevtx = CTransaction()
553-
prevtx.deserialize(stream)
554-
psbt.inputs[i].non_witness_utxo = prevtx
545+
if non_witness:
546+
for i, inp in enumerate(psbt.tx.vin):
547+
txid = inp.prevout.hash.to_bytes(32,'big').hex()
548+
try:
549+
res = self.cli.gettransaction(txid)
550+
except:
551+
raise SpecterError("Can't find previous transaction in the wallet.")
552+
stream = BytesIO(bytes.fromhex(res["hex"]))
553+
prevtx = CTransaction()
554+
prevtx.deserialize(stream)
555+
psbt.inputs[i].non_witness_utxo = prevtx
556+
if xpubs:
557+
# for multisig add xpub fields
558+
if len(self.keys) > 1:
559+
for k in self.keys:
560+
key = b'\x01' + decode_base58(k.xpub)
561+
if k.fingerprint != '':
562+
fingerprint = bytes.fromhex(k.fingerprint)
563+
else:
564+
fingerprint = get_xpub_fingerprint(k.xpub)
565+
if k.derivation != '':
566+
der = der_to_bytes(k.derivation)
567+
else:
568+
der = b''
569+
value = fingerprint + der
570+
psbt.unknown[key] = value
555571
return psbt.serialize()
556572

557573
def get_signed_devices(self, decodedpsbt):

0 commit comments

Comments
 (0)