Coverage for ledger/parameters.py: 68%

19 statements  

« prev     ^ index     » next       coverage.py v7.5.3, created at 2025-07-10 13:43 +0000

1# The MIT License (MIT) 

2# 

3# Copyright (c) 2021 RSK Labs Ltd 

4# 

5# Permission is hereby granted, free of charge, to any person obtaining a copy of 

6# this software and associated documentation files (the "Software"), to deal in 

7# the Software without restriction, including without limitation the rights to 

8# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 

9# of the Software, and to permit persons to whom the Software is furnished to do 

10# so, subject to the following conditions: 

11# 

12# The above copyright notice and this permission notice shall be included in all 

13# copies or substantial portions of the Software. 

14# 

15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 

16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 

18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 

19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 

20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 

21# SOFTWARE. 

22 

23from enum import IntEnum 

24 

25 

26class _Network(IntEnum): 

27 MAINNET = 0x01 

28 TESTNET = 0x02 

29 REGTEST = 0x03 

30 

31 

32# Instances of this represent parameters 

33# of firmware installed on a powHSM 

34# Parameters consist of minimum required difficulty, checkpoint and 

35# target network. 

36class HSM2FirmwareParameters: 

37 # Shorthand 

38 Network = _Network 

39 

40 @staticmethod 

41 def from_dongle_format(param_bytes): 

42 if len(param_bytes) != 69: 

43 raise ValueError("Expected 69 bytes but got %d" % len(param_bytes)) 

44 # Format: 

45 # Bytes 0-31: initial block hash 

46 # Bytes 32-67: minimum required difficulty (unsigned big endian) 

47 # Byte 68: network identifier 

48 checkpoint = param_bytes[0:32].hex() 

49 mrd = int.from_bytes(param_bytes[32:68], byteorder="big", signed=False) 

50 network = _Network(param_bytes[68]) 

51 return HSM2FirmwareParameters(mrd, checkpoint, network) 

52 

53 def __init__(self, min_required_difficulty, checkpoint, network): 

54 self.min_required_difficulty = min_required_difficulty 

55 self.checkpoint = checkpoint 

56 self.network = network