Search in sources :

Example 41 with ECKey

use of io.nuls.core.tools.crypto.ECKey in project nuls by nuls-io.

the class AccountServiceImpl method signMessage.

/**
 * @auther EdwardChan
 *
 * @since Mar. 20th 2019
 *
 * 对消息进行签名
 *
 * @param address 对消息进行签名的地址
 *
 * @param password 账户的密码(如果已经设置密码则密码输入)
 *
 * @param message 需要签名的字符串
 *
 * @return 签名结果字符串
 */
@Override
public Result<String> signMessage(String address, String password, String message) throws NulsException {
    String signatureBase64;
    // 计算签名
    // 查询出账户私钥
    Account account = getAccountByAddress(address);
    if (null == account) {
        return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST);
    }
    if (account.isEncrypted() && account.isLocked()) {
        if (!account.unlock(password)) {
            return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG);
        }
    }
    // 将消息加盐
    message = AccountConstant.SIGN_MESSAGE_SALT + message;
    ECKey key = ECKey.fromPrivate(new BigInteger(account.getPriKey()));
    signatureBase64 = key.signMessage(message, null);
    return Result.getSuccess().setData(signatureBase64);
}
Also used : BigInteger(java.math.BigInteger) ECKey(io.nuls.core.tools.crypto.ECKey)

Example 42 with ECKey

use of io.nuls.core.tools.crypto.ECKey in project nuls by nuls-io.

the class AddressTest method test.

@Test
public void test() {
    short chainId = 8964;
    NulsContext.getInstance().defaultChainId = chainId;
    ECKey ecKey = ECKey.fromPrivate(new BigInteger(1, Hex.decode("60a958883bee99abc16b59a191ae25f8854c5edb2daea7ee1e19425b728edf6a")));
    System.out.println(ecKey.getPublicKeyAsHex(true));
    // while (true) {
    // ECKey ecKey = new ECKey();
    // String address = getAddress(chainId, ecKey.getPubKey());
    // System.out.println(address );//+ ":::::::" + ecKey.getPrivateKeyAsHex());
    // }
    System.out.println(getAddress(chainId, Hex.decode("0369926e4c82ab5499a14ef115e1d2d0d4dfb1c44b4de46434e79fc3da928d29ab")));
}
Also used : BigInteger(java.math.BigInteger) ECKey(io.nuls.core.tools.crypto.ECKey) Test(org.junit.Test)

Example 43 with ECKey

use of io.nuls.core.tools.crypto.ECKey in project nuls by nuls-io.

the class Account method encrypt.

/**
 * 根据密码加密账户(给账户设置密码)
 * Password-encrypted account (set password for account)
 */
public void encrypt(String password, boolean isForce) throws NulsException {
    if (this.isEncrypted()) {
        if (isForce) {
            if (isLocked()) {
                throw new NulsException(AccountErrorCode.ACCOUNT_IS_ALREADY_ENCRYPTED_AND_LOCKED);
            }
        } else {
            throw new NulsException(AccountErrorCode.ACCOUNT_IS_ALREADY_ENCRYPTED);
        }
    }
    ECKey eckey = this.getEcKey();
    byte[] privKeyBytes = eckey.getPrivKeyBytes();
    EncryptedData encryptedPrivateKey = AESEncrypt.encrypt(privKeyBytes, EncryptedData.DEFAULT_IV, new KeyParameter(Sha256Hash.hash(password.getBytes())));
    eckey.setEncryptedPrivateKey(encryptedPrivateKey);
    ECKey result = ECKey.fromEncrypted(encryptedPrivateKey, getPubKey());
    this.setPriKey(new byte[0]);
    this.setEcKey(result);
    this.setEncryptedPriKey(encryptedPrivateKey.getEncryptedBytes());
}
Also used : NulsException(io.nuls.kernel.exception.NulsException) KeyParameter(org.spongycastle.crypto.params.KeyParameter) ECKey(io.nuls.core.tools.crypto.ECKey) EncryptedData(io.nuls.core.tools.crypto.EncryptedData)

Aggregations

ECKey (io.nuls.core.tools.crypto.ECKey)43 NulsException (io.nuls.kernel.exception.NulsException)26 IOException (java.io.IOException)20 CoinDataResult (io.nuls.account.ledger.model.CoinDataResult)15 BigInteger (java.math.BigInteger)14 Account (io.nuls.account.model.Account)12 ArrayList (java.util.ArrayList)11 TransferTransaction (io.nuls.protocol.model.tx.TransferTransaction)10 MultiSigAccount (io.nuls.account.model.MultiSigAccount)8 ValidateResult (io.nuls.kernel.validate.ValidateResult)8 UnsupportedEncodingException (java.io.UnsupportedEncodingException)8 TransactionDataResult (io.nuls.account.ledger.model.TransactionDataResult)6 CryptoException (io.nuls.core.tools.crypto.Exception.CryptoException)6 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)4 MultipleAddressTransferModel (io.nuls.account.ledger.model.MultipleAddressTransferModel)3 Alias (io.nuls.account.model.Alias)3 AliasTransaction (io.nuls.account.tx.AliasTransaction)3 Agent (io.nuls.consensus.poc.protocol.entity.Agent)3 ContractResult (io.nuls.contract.dto.ContractResult)3 Script (io.nuls.kernel.script.Script)3