progress, channel keys generate deterministically now

This commit is contained in:
Lex Berezhny 2021-10-15 10:56:20 -04:00
parent f741b00768
commit 6bd9b3744d
4 changed files with 23 additions and 11 deletions

View file

@ -41,12 +41,19 @@ class DeterministicChannelKeyManager:
self.public_key = account.public_key.child(2) self.public_key = account.public_key.child(2)
self.private_key = account.private_key.child(2) if account.private_key else None self.private_key = account.private_key.child(2) if account.private_key else None
def generate_next_key(self): async def generate_next_key(self):
db = self.account.ledger.db db = self.account.ledger.db
i = 0 i = 0
while True: while True:
next_key = self.private_key.child(i) next_key = self.private_key.child(i)
if not await db.is_channel_key_used(self.account, next_key.address): if not await db.is_channel_key_used(self.account.wallet, next_key.address()):
return next_key
i += 1
def get_private_key_from_pubkey_hash(self, pubkey_hash):
for i in range(100):
next_key = self.private_key.child(i)
if next_key.address() == pubkey_hash:
return next_key return next_key
@ -538,9 +545,7 @@ class Account:
return tx return tx
async def generate_channel_private_key(self): async def generate_channel_private_key(self):
key = self.deterministic_channel_keys.generate_next_key() return await self.deterministic_channel_keys.generate_next_key()
self.add_channel_private_key(key)
return key
def add_channel_private_key(self, private_key): def add_channel_private_key(self, private_key):
public_key_bytes = private_key.get_verifying_key().to_der() public_key_bytes = private_key.get_verifying_key().to_der()
@ -554,6 +559,7 @@ class Account:
return await asyncio.get_event_loop().run_in_executor( return await asyncio.get_event_loop().run_in_executor(
None, ecdsa.SigningKey.from_pem, private_key_pem, sha256 None, ecdsa.SigningKey.from_pem, private_key_pem, sha256
) )
return self.deterministic_channel_keys.get_private_key_from_pubkey_hash(channel_pubkey_hash)
async def maybe_migrate_certificates(self): async def maybe_migrate_certificates(self):
def to_der(private_key_pem): def to_der(private_key_pem):

View file

@ -1241,9 +1241,10 @@ class Database(SQLiteMixin):
async def set_address_history(self, address, history): async def set_address_history(self, address, history):
await self._set_address_history(address, history) await self._set_address_history(address, history)
async def is_channel_key_used(self, account, address): async def is_channel_key_used(self, wallet, address):
for channel in await self.get_channels(accounts=[account]): channels = await self.get_txos(wallet, txo_type=TXO_TYPES['channel'])
if channel.private_key.address == address: for channel in channels:
if channel.private_key.address() == address:
return True return True
return False return False

View file

@ -471,12 +471,12 @@ class Output(InputOutput):
def set_channel_private_key(self, private_key): def set_channel_private_key(self, private_key):
self.private_key = private_key self.private_key = private_key
self.claim.channel.public_key_bytes = private_key.get_verifying_key().to_der() self.claim.channel.public_key_bytes = private_key.public_key.pubkey_bytes
self.script.generate() self.script.generate()
return private_key return private_key
def is_channel_private_key(self, private_key): def is_channel_private_key(self, private_key):
return self.claim.channel.public_key_bytes == private_key.get_verifying_key().to_der() return self.claim.channel.public_key_bytes == private_key.signing_key.to_der()
@classmethod @classmethod
def pay_claim_name_pubkey_hash( def pay_claim_name_pubkey_hash(

View file

@ -177,5 +177,10 @@ class AccountManagement(CommandTestCase):
async def test_deterministic_channel_keys(self): async def test_deterministic_channel_keys(self):
seed = self.account.seed seed = self.account.seed
await self.channel_create('@foo1') channel1 = await self.channel_create('@foo1')
channel2 = await self.channel_create('@foo2')
self.assertNotEqual(
channel1['outputs'][0]['value']['public_key'],
channel2['outputs'][0]['value']['public_key'],
)
self.daemon2 = await self.add_daemon(seed=seed) self.daemon2 = await self.add_daemon(seed=seed)