All Downloads are FREE. Search and download functionalities are using the official Maven repository.

chat.dim.compat.CompatibleMetaFactory Maven / Gradle / Ivy

There is a newer version: 0.3.0
Show newest version
/* license: https://mit-license.org
 *
 *  Ming-Ke-Ming : Decentralized User Identity Authentication
 *
 *                                Written in 2020 by Moky 
 *
 * ==============================================================================
 * The MIT License (MIT)
 *
 * Copyright (c) 2020 Albert Moky
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * ==============================================================================
 */
package chat.dim.compat;

import java.util.Map;

import chat.dim.crypto.PrivateKey;
import chat.dim.crypto.SignKey;
import chat.dim.crypto.VerifyKey;
import chat.dim.format.TransportableData;
import chat.dim.format.UTF8;
import chat.dim.mkm.AccountFactoryManager;
import chat.dim.mkm.ETHMeta;
import chat.dim.protocol.Meta;
import chat.dim.protocol.MetaType;

public final class CompatibleMetaFactory implements Meta.Factory {

    private final int version;

    public CompatibleMetaFactory(MetaType type) {
        super();
        version = type.value;
    }

    @Override
    public Meta createMeta(VerifyKey key, String seed, TransportableData fingerprint) {
        if (MetaType.MKM.equals(version)) {
            // MKM
            return new CompatibleDefaultMeta(version, key, seed, fingerprint);
        } else if (MetaType.BTC.equals(version)) {
            // BTC
            return new CompatibleBTCMeta(version, key);
        } else if (MetaType.ExBTC.equals(version)) {
            // ExBTC
            return new CompatibleBTCMeta(version, key, seed, fingerprint);
        } else if (MetaType.ETH.equals(version)) {
            // ETH
            return new ETHMeta(version, key);
        } else if (MetaType.ExETH.equals(version)) {
            // ExETH
            return new ETHMeta(version, key, seed, fingerprint);
        }
        return null;
    }

    @Override
    public Meta generateMeta(SignKey sKey, String seed) {
        TransportableData fingerprint;
        if (seed == null || seed.length() == 0) {
            fingerprint = null;
        } else {
            byte[] sig = sKey.sign(UTF8.encode(seed));
            fingerprint = TransportableData.create(sig);
        }
        VerifyKey key = ((PrivateKey) sKey).getPublicKey();
        return createMeta(key, seed, fingerprint);
    }

    @Override
    public Meta parseMeta(Map meta) {
        Meta out;
        AccountFactoryManager man = AccountFactoryManager.getInstance();
        int type = man.generalFactory.getMetaType(meta, 0);
        if (MetaType.MKM.equals(type)) {
            // MKM
            out = new CompatibleDefaultMeta(meta);
        } else if (MetaType.BTC.equals(type) || MetaType.ExBTC.equals(type)) {
            // BTC, ExBTC
            out = new CompatibleBTCMeta(meta);
        } else if (MetaType.ETH.equals(type) || MetaType.ExETH.equals(type)) {
            // ETH, ExETH
            out = new ETHMeta(meta);
        } else {
            throw new IllegalArgumentException("unknown meta type: " + type);
        }
        return out.isValid() ? out : null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy