1
0

increase nonce size to 32 byte and make it hex in event

This commit is contained in:
f321x
2025-02-12 10:09:11 +01:00
parent 947094c1b0
commit a47421490d
2 changed files with 11 additions and 5 deletions

View File

@@ -1413,7 +1413,7 @@ class NostrTransport(Logger):
'min_amount': sm._min_amount,
'max_amount': sm._max_amount,
'relays': sm.config.NOSTR_RELAYS,
'pow_nonce': str(sm.config.SWAPSERVER_ANN_POW_NONCE),
'pow_nonce': hex(sm.config.SWAPSERVER_ANN_POW_NONCE),
}
# the first value of a single letter tag is indexed and can be filtered for
tags = [['d', f'electrum-swapserver-{self.NOSTR_EVENT_VERSION}'],
@@ -1470,7 +1470,13 @@ class NostrTransport(Logger):
if event.created_at <= ts:
#print('skipping old event', pubkey[0:10], event.id)
continue
pow_bits = get_nostr_ann_pow_amount(bytes.fromhex(pubkey), int(content.get('pow_nonce', 0)))
try:
pow_bits = get_nostr_ann_pow_amount(
bytes.fromhex(pubkey),
int(content.get('pow_nonce', "0"), 16)
)
except ValueError:
continue
if pow_bits < self.config.SWAPSERVER_POW_TARGET:
self.logger.debug(f"too low pow: {pubkey}: pow: {pow_bits} nonce: {content.get('pow_nonce', 0)}")
continue

View File

@@ -2127,7 +2127,7 @@ def nostr_pow_worker(nonce, nostr_pubk, target_bits, hash_function, hash_len_bit
# we cannot check is_set on each iteration as it has a lot of overhead, this way we can check
# it with low overhead (just the additional range counter)
for i in range(1000000):
digest = hash_function(hash_preimage + nonce.to_bytes(8, 'big')).digest()
digest = hash_function(hash_preimage + nonce.to_bytes(32, 'big')).digest()
if int.from_bytes(digest, 'big') < (1 << (hash_len_bits - target_bits)):
shutdown.set()
return hash, nonce
@@ -2141,7 +2141,7 @@ async def gen_nostr_ann_pow(nostr_pubk: bytes, target_bits: int) -> Tuple[int, i
import multiprocessing # not available on Android, so we import it here
hash_function = hashlib.sha256
hash_len_bits = 256
max_nonce = 0xFFFFFFFFFFFFFFFF # 8 byte
max_nonce: int = (1 << (32 * 8)) - 1 # 32-byte nonce
start_nonce = 0
max_workers = max(multiprocessing.cpu_count() - 1, 1) # use all but one CPU
@@ -2181,6 +2181,6 @@ def get_nostr_ann_pow_amount(nostr_pubk: bytes, nonce: Optional[int]) -> int:
hash_len_bits = 256
hash_preimage = b'electrum-' + nostr_pubk
digest = hash_function(hash_preimage + nonce.to_bytes(8, 'big')).digest()
digest = hash_function(hash_preimage + nonce.to_bytes(32, 'big')).digest()
digest = int.from_bytes(digest, 'big')
return hash_len_bits - digest.bit_length()