Compare commits

...

10 Commits

View File

@ -454,7 +454,7 @@ def SignOutputRaw(json):
req = rpc_pb2.SignOutputRawRequest() req = rpc_pb2.SignOutputRawRequest()
json_format.Parse(json, req) json_format.Parse(json, req)
assert len(req.signDesc.pubKey) in [33, 0] #assert len(req.signDesc.pubKey) in [33, 0]
assert len(req.signDesc.doubleTweak) in [32, 0] assert len(req.signDesc.doubleTweak) in [32, 0]
assert len(req.signDesc.sigHashes.hashPrevOuts) == 64 assert len(req.signDesc.sigHashes.hashPrevOuts) == 64
assert len(req.signDesc.sigHashes.hashSequence) == 64 assert len(req.signDesc.sigHashes.hashSequence) == 64
@ -469,11 +469,8 @@ def SignOutputRaw(json):
def signOutputRaw(tx, signDesc): def signOutputRaw(tx, signDesc):
adr = None pri = derivePrivKey(signDesc.keyDescriptor)
if len(signDesc.pubKey) != 0: assert pri is not None
adr = bitcoin.pubkey_to_address('p2wpkh', binascii.hexlify(
signDesc.keyDescriptor.pubKey).decode("utf-8")) # Because this is all NewAddress supports
pri = fetchPrivKey(adr, signDesc.keyDescriptor.keyLocator.family, signDesc.keyDescriptor.keyLocator.index)
pri2 = maybeTweakPrivKey(signDesc, pri) pri2 = maybeTweakPrivKey(signDesc, pri)
sig = rawTxInWitnessSignature(tx, signDesc.sigHashes, signDesc.inputIndex, sig = rawTxInWitnessSignature(tx, signDesc.sigHashes, signDesc.inputIndex,
signDesc.output.value, signDesc.witnessScript, sigHashAll, pri2) signDesc.output.value, signDesc.witnessScript, sigHashAll, pri2)
@ -523,35 +520,30 @@ def ComputeInputScript(json):
return msg return msg
def fetchPrivKey(str_address, keyLocatorFamily, keyLocatorIndex, privKey=None): def fetchPrivKey(str_address, keyLocatorFamily, keyLocatorIndex):
pri = None pri = None
if str_address is not None: if str_address is not None:
assert privKey is None
pri, redeem_script = WALLET.export_private_key(str_address, None) pri, redeem_script = WALLET.export_private_key(str_address, None)
if redeem_script: if redeem_script:
print("ignoring redeem script", redeem_script) print("ignoring redeem script", redeem_script)
typ, pri, compressed = bitcoin.deserialize_privkey(pri) typ, pri, compressed = bitcoin.deserialize_privkey(pri)
if keyLocatorFamily == 0 and keyLocatorIndex == 0: return EC_KEY(pri)
ks = keystore.BIP32_KeyStore({}) ks = keystore.BIP32_KeyStore({})
der = "m/0'/" der = "m/0'/"
xtype = 'p2wpkh' xtype = 'p2wpkh'
ks.add_xprv_from_seed(int.from_bytes(pri, "big"), xtype, der) ks.add_xprv_from_seed(pri, xtype, der)
elif privKey is not None:
ks = keystore.BIP32_KeyStore({})
der = "m/0'/"
xtype = 'p2wpkh'
ks.add_xprv_from_seed(privKey.secret.to_bytes(32, 'big'), xtype, der)
else: else:
ks = WALLET.keystore ks = WALLET.keystore
if keyLocatorFamily is not None or keyLocatorIndex is not None: if keyLocatorFamily != 0 or keyLocatorIndex != 0:
pri = ks.get_private_key([1017, keyLocatorFamily, keyLocatorIndex], password=None)[0] pri = ks.get_private_key([1017, keyLocatorFamily, keyLocatorIndex], password=None)[0]
pri = EC_KEY(pri) pri = EC_KEY(pri)
else:
pri = privKey assert pri is not None
return pri return pri
@ -801,6 +793,7 @@ async def readReqAndReply(obj, writer):
await writer.drain() await writer.drain()
def privKeyForPubKey(pubKey): def privKeyForPubKey(pubKey):
global globalIdx
priv_keys = WALLET.storage.get("lightning_extra_keys", []) priv_keys = WALLET.storage.get("lightning_extra_keys", [])
for i in priv_keys: for i in priv_keys:
candidate = EC_KEY(i.to_bytes(32, "big")) candidate = EC_KEY(i.to_bytes(32, "big"))
@ -822,21 +815,20 @@ def privKeyForPubKey(pubKey):
print("ignoring redeem script", redeem_script) print("ignoring redeem script", redeem_script)
typ, pri, compressed = bitcoin.deserialize_privkey(pri) typ, pri, compressed = bitcoin.deserialize_privkey(pri)
return EC_KEY(pri.to_bytes(32, "big")) return EC_KEY(pri)
#assert False, "could not find private key for pubkey {} hex={}".format(pubKey, binascii.hexlify(pubKey).decode("ascii")) #assert False, "could not find private key for pubkey {} hex={}".format(pubKey, binascii.hexlify(pubKey).decode("ascii"))
def derivePrivKey(keyDesc): def derivePrivKey(keyDesc):
global globalIdx
keyDescFam = keyDesc.keyLocator.family keyDescFam = keyDesc.keyLocator.family
keyDescIdx = keyDesc.keyLocator.index keyDescIdx = keyDesc.keyLocator.index
keyDescPubKey = keyDesc.pubKey keyDescPubKey = keyDesc.pubKey
privKey = None privKey = None
if len(keyDescPubKey) != 0: if len(keyDescPubKey) != 0:
privKey = privKeyForPubKey(keyDescPubKey) return privKeyForPubKey(keyDescPubKey)
return fetchPrivKey(None, keyDescFam, keyDescIdx, privKey) return fetchPrivKey(None, keyDescFam, keyDescIdx)
def DerivePrivKey(json): def DerivePrivKey(json):
req = rpc_pb2.DerivePrivKeyRequest() req = rpc_pb2.DerivePrivKeyRequest()
@ -911,7 +903,7 @@ def ScalarMult(json):
m = rpc_pb2.ScalarMultResponse() m = rpc_pb2.ScalarMultResponse()
m.pubKey = pubkFromECKEY(EC_KEY(c.digest())) m.hashResult = c.digest()
msg = json_format.MessageToJson(m) msg = json_format.MessageToJson(m)
return msg return msg