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

com.rotilho.jnano.commons.NanoSignatures Maven / Gradle / Ivy

Go to download

JNano provides a set of low level Nano operations that includes signing, seed generation, block hashing and account creation.

There is a newer version: 1.5.0
Show newest version
package com.rotilho.jnano.commons;

import lombok.NonNull;

public final class NanoSignatures {
    private static final String SIGNATURE_REGEX = "([0-9A-Z]){128}";

    private NanoSignatures() {
    }

    @NonNull
    public static String sign(@NonNull byte[] privateKey, @NonNull String hash) {
        Preconditions.checkKey(privateKey);
        return NanoHelper.toHex(sign(privateKey, NanoHelper.toByteArray(hash)));
    }

    @NonNull
    public static byte[] sign(@NonNull byte[] privateKey, @NonNull byte[] hash) {
        Preconditions.checkKey(privateKey);
        return ED25519.sign(hash, privateKey);
    }

    public static boolean isValid(@NonNull String account, @NonNull String hash, @NonNull String signature) {
        return isValid(NanoBaseAccountType.NANO, account, hash, signature);
    }

    public static boolean isValid(@NonNull NanoAccountType type, @NonNull String account, @NonNull String hash, @NonNull String signature) {
        checkHash(hash);
        byte[] publicKey = NanoAccounts.toPublicKey(type, account);
        return signature.matches(SIGNATURE_REGEX) && isValid(publicKey, NanoHelper.toByteArray(hash), NanoHelper.toByteArray(signature));
    }

    // hash should moved to last parameter and receive a varargs. Maybe rename it to something else?
    public static boolean isValid(@NonNull byte[] publicKey, @NonNull byte[] hash, @NonNull byte[] signature) {
        return ED25519.verify(signature, hash.length == 32 ? hash : Hashes.digest256(hash), publicKey);
    }

    private static void checkHash(String hash) {
        Preconditions.checkArgument(NanoBlocks.isValid(hash), () -> "Invalid hash " + hash);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy