# Python Substrate Interface Library
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re
from hashlib import blake2b
from math import ceil
from scalecodec.types import Bytes
RE_JUNCTION = r'(\/\/?)([^/]+)'
JUNCTION_ID_LEN = 32
[docs]
class DeriveJunction:
def __init__(self, chain_code, is_hard=False):
self.chain_code = chain_code
self.is_hard = is_hard
[docs]
@classmethod
def from_derive_path(cls, path: str, is_hard=False):
if path.isnumeric():
byte_length = ceil(int(path).bit_length() / 8)
chain_code = int(path).to_bytes(byte_length, 'little').ljust(32, b'\x00')
else:
path_scale = Bytes()
path_scale.encode(path)
if len(path_scale.data) > JUNCTION_ID_LEN:
chain_code = blake2b(path_scale.data.data, digest_size=32).digest()
else:
chain_code = bytes(path_scale.data.data.ljust(32, b'\x00'))
return cls(chain_code=chain_code, is_hard=is_hard)