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

com.klaytn.caver.account.AccountKeyLegacy Maven / Gradle / Ivy

There is a newer version: 1.12.2-android
Show newest version
/*
 * Copyright 2020 The caver-java Authors
 *
 * 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.
 */

package com.klaytn.caver.account;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.klaytn.caver.tx.account.AccountKey;
import org.web3j.utils.Numeric;

import java.io.IOException;
import java.util.Arrays;

/**
 * AccountKeyLegacy represents a key of legacy account types. If an account has AccountKeyLegacy,
 * the tx validation process is done like below (as Ethereum did):
 *
 * Get the public key from ecrecover(txhash, txsig)
 * Get the address of the public key
 * The address is the sender
 *
 */
@JsonSerialize(using = AccountKeyLegacy.AccountKeyLegacySerializer.class)
public class AccountKeyLegacy implements IAccountKey{

    /**
     * AccountKeyLegacy's RLP-encoded data.
     */
    private static final byte[] RLP = new byte[]{(byte) 0x01, (byte) 0xc0};

    /**
     * AccountKeyLegacy's Type attribute.
     */
    private static final String TYPE = "0x01";


    /**
     * Creates an AccountKeyLegacy instance
     */
    public AccountKeyLegacy() {

    }

    /**
     * Decodes a RLP-encoded AccountKeyLegacy string
     * @param rlpEncodedKey A RLP-encoded AccountKeyLegacy string
     * @return AccountKeyLegacy
     */
    public static AccountKeyLegacy decode(String rlpEncodedKey) {
        return decode(Numeric.hexStringToByteArray(rlpEncodedKey));
    }

    /**
     * Decodes a RLP-encoded AccountKeyLegacy byte array
     * @param rlpEncodedKey RLP-encoded AccountKeyLegacy byte array
     * @return AccountKeyLegacy
     */
    public static AccountKeyLegacy decode(byte[] rlpEncodedKey) {
        if(!Arrays.equals(RLP, rlpEncodedKey)) {
            throw new RuntimeException("Invalid RLP-encoded account key String");
        }

        return new AccountKeyLegacy();
    }

    /**
     * Encodes a AccountKeyLegacy Object by RLP-encoding method.
     * @return RLP-encoded AccountKeyLegacy string
     */
    @Override
    public String getRLPEncoding() {
        return Numeric.toHexString(RLP);
    }

    /**
     * Returns an AccountKeyLegacy's type attribute
     * @return AccountKeyLegacy's type attribute
     */
    public static String getType() {
        return TYPE;
    }

    /**
     * Serialize class to AccountKeyLegacy object into JSON.
     */
    public static class AccountKeyLegacySerializer extends JsonSerializer {
        @Override
        public void serialize(AccountKeyLegacy value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
            gen.writeStartObject();

            gen.writeFieldName("keyType");
            gen.writeNumber(Numeric.toBigInt(getType()));

            gen.writeFieldName("key");
            gen.writeStartObject();
            gen.writeEndObject();

            gen.writeEndObject();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy