Skip to main content

DIM Plugins (Python)

License PRs Welcome Platform Issues Repo Size Tags Version

Watchers Forks Stars Followers

Plugins

  1. Data Coding
    • Base-58
    • Base-64
    • Hex
    • UTF-8
    • JsON
    • PNF (Portable Network File)
    • TED (Transportable Encoded Data)
  2. Digest Digest
    • MD-5
    • SHA-1
    • SHA-256
    • Keccak-256
    • RipeMD-160
  3. Cryptography
    • AES-256 (AES/CBC/PKCS7Padding)
    • RSA-1024 (RSA/ECB/PKCS1Padding), (SHA256withRSA)
    • ECC (Secp256k1)
  4. Address
    • BTC
    • ETH
  5. Meta
    • MKM (Default)
    • BTC
    • ETH
  6. Document
    • Visa (User)
    • Profile
    • Bulletin (Group)

Extends

Address

from typing import Optional

from dimp import *
from dimplugins import *


class CompatibleAddressFactory(BaseAddressFactory):

    # Override
    def _parse(self, address: str) -> Optional[Address]:
        size = len(address)
        if size == 0:
            assert False, 'address should not be empty'
        elif size == 8:
            # "anywhere"
            if address.lower() == 'anywhere':
                return ANYWHERE
        elif size == 10:
            # "everywhere"
            if address.lower() == 'everywhere':
                return EVERYWHERE
        #
        #  checking normal address
        #
        if 26 <= size <= 35:
            res = BTCAddress.from_str(address=address)
        elif size == 42:
            res = ETHAddress.from_str(address=address)
        else:
            # assert False, 'invalid address: %s' % address
            res = None
        #
        #  TODO: other types of address
        #
        if res is None and 4 <= size <= 64:
            res = UnknownAddress(address=address)
        assert res is not None, 'invalid address: %s' % address
        return res


class UnknownAddress(ConstantString, Address):
    """
        Unsupported Address
        ~~~~~~~~~~~~~~~~~~~
    """

    def __init__(self, address: str):
        super().__init__(string=address)

    @property  # Override
    def network(self) -> int:
        return 0  # EntityType.USER.value

Meta

from collections.abc import Mapping
from typing import Optional

from dimp import Meta
from dimplugins import *


class CompatibleMetaFactory(BaseMetaFactory):

    # Override
    def parse_meta(self, meta: Mapping) -> Optional[Meta]:
        helper = account_helper()
        version = helper.get_meta_type(meta=meta)
        if version in ['1', 'mkm', 'MKM']:
            # MKM
            out = DefaultMeta(meta=meta)
        elif version in ['2', 'btc', 'BTC']:
            # BTC
            out = BTCMeta(meta=meta)
        elif version in ['4', 'eth', 'ETH']:
            # ETH
            out = ETHMeta(meta=meta)
        else:
            raise TypeError('unknown meta type: %d' % version)
        if out.is_valid:
            return out


def account_extensions() -> GeneralAccountExtension:
    return shared_account_extensions


def account_helper() -> GeneralAccountHelper:
    ext = account_extensions()
    return ext.helper

ExtensionLoader

from dimp import MetaType, ContentType
from dimplugins import ExtensionLoader

from ..protocol import HandshakeCommand, BaseHandshakeCommand
from ..protocol import AppCustomizedContent


class CommonExtensionLoader(ExtensionLoader):

    # Override
    def register_address_factory(self):
        Address.set_factory(factory=CompatibleAddressFactory())

    # Override
    def register_meta_factories(self):
        mkm = CompatibleMetaFactory(version=MetaType.MKM)
        btc = CompatibleMetaFactory(version=MetaType.BTC)
        eth = CompatibleMetaFactory(version=MetaType.ETH)
        Meta.set_factory(version='1', factory=mkm)
        Meta.set_factory(version='2', factory=btc)
        Meta.set_factory(version='4', factory=eth)
        Meta.set_factory(version='mkm', factory=mkm)
        Meta.set_factory(version='btc', factory=btc)
        Meta.set_factory(version='eth', factory=eth)
        Meta.set_factory(version='MKM', factory=mkm)
        Meta.set_factory(version='BTC', factory=btc)
        Meta.set_factory(version='ETH', factory=eth)

    # Override
    def register_content_factories(self):
        super().register_content_factories()
        self.register_customized_factories()

    # protected
    def register_customized_factories(self):
        # Application Customized
        self._set_content_factory(msg_type=ContentType.APPLICATION, content_class=AppCustomizedContent)
        self._set_content_factory(msg_type=ContentType.CUSTOMIZED, content_class=AppCustomizedContent)

    # Override
    def register_command_factories(self):
        super().register_command_factories()
        # Handshake
        self._set_command_factory(cmd=HandshakeCommand.HANDSHAKE, command_class=BaseHandshakeCommand)

Plugin Loader

from dimplugins import PluginLoader

from .mdx import MD5, MD5Digester
from .mdx import SHA1, SHA1Digester


class CommonPluginLoader(PluginLoader):

    # Override
    def _load_message_digesters(self):
        super()._load_message_digesters()
        self.register_md5_digester()
        self.register_sha1_digester()

    # protected
    def register_md5_digester(self):
        MD5.digester = MD5Digester()

    # protected
    def register_sha1_digester(self):
        SHA1.digester = SHA1Digester()

Usage

You must load all plugins before your business run:

from dimplugins import ExtensionLoader
from dimplugins import PluginLoader

from .compat_loader import CommonExtensionLoader
from .compat_loader import CommonPluginLoader


class LibraryLoader:

    def __init__(self, extensions: ExtensionLoader = None, plugins: PluginLoader = None):
        super().__init__()
        self.__extensions = CommonExtensionLoader() if extensions is None else extensions
        self.__plugins = CommonPluginLoader() if plugins is None else plugins
        self.__loaded = False

    def run(self):
        if self.__loaded:
            # no need to load it again
            return
        else:
            # mark it to loaded
            self.__loaded = True
        # try to load all plugins
        self.load()
        
    # protected
    def load(self):
        self.__extensions.load()
        self.__plugins.load()


if __name__ == '__main__':
  loader = LibraryLoader()
  loader.run()
  # do your jobs after all extensions & plugins loaded

You must ensure that every Address you extend has a Meta type that can correspond to it one by one.


Copyright © 2018-2026 Albert Moky Followers

Metadata

Release files for dimplugins 2.4.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dimplugins 2.4.3
File Size Uploaded Provenance
dimplugins-2.4.3.tar.gz 27.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for dimplugins 2.4.3
File Interpreter ABI Platform
dimplugins-2.4.3-py3-none-any.whl Python 3 none any Details

Total release size: 89.4 kB

Release files / dimplugins-2.4.3.tar.gz

Download URL dimplugins-2.4.3.tar.gz
Size 27.4 kB
Tags Source
SHA-256 checksum
How to use checksums
1ac9aa4fc09fc201e83cfe6381bc076bd90abf7f7bd8caff746c6c7f7be3157b
BLAKE2b-256 checksum
How to use checksums
eaf3167a1200c4dafee1540aa5821b58de151d7cdf844d6dee8a8e57c65230e5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/68.0.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.0b3

Release files / dimplugins-2.4.3-py3-none-any.whl

Download URL dimplugins-2.4.3-py3-none-any.whl
Size 62.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
732b7c2d87500f788afc5e00e41b384a30f748b1964f24d6ca75cdc97174ccc6
BLAKE2b-256 checksum
How to use checksums
46383b9f73a76d0499d501277883939849460f7c3c2c29cd421442bbc43506aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/68.0.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.0b3

Security for dimplugins 2.4.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page