Search in sources :

Example 6 with CryptoException

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

the class AccountLedgerResource method signTransaction.

@POST
@Path("/transaction/sign")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "交易签名", notes = "result.data: resultJson 返回交易对象")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success") })
public RpcClientResult signTransaction(@ApiParam(name = "form", value = "交易信息", required = true) TransactionHexForm form) {
    if (StringUtils.isBlank(form.getPriKey())) {
        return Result.getFailed(AccountErrorCode.PARAMETER_ERROR).toRpcClientResult();
    }
    if (StringUtils.isBlank(form.getTxHex())) {
        return Result.getFailed(AccountErrorCode.PARAMETER_ERROR).toRpcClientResult();
    }
    if (!AddressTool.validAddress(form.getAddress())) {
        return Result.getFailed(AccountErrorCode.PARAMETER_ERROR).toRpcClientResult();
    }
    String priKey = form.getPriKey();
    if (StringUtils.isNotBlank(form.getPassword())) {
        if (StringUtils.validPassword(form.getPassword())) {
            // decrypt
            byte[] privateKeyBytes = null;
            try {
                privateKeyBytes = AESEncrypt.decrypt(Hex.decode(priKey), form.getPassword());
            } catch (CryptoException e) {
                return Result.getFailed(AccountLedgerErrorCode.PARAMETER_ERROR).toRpcClientResult();
            }
            priKey = Hex.encode(privateKeyBytes);
        } else {
            return Result.getFailed(AccountLedgerErrorCode.PARAMETER_ERROR).toRpcClientResult();
        }
    }
    if (!ECKey.isValidPrivteHex(priKey)) {
        return Result.getFailed(AccountErrorCode.PARAMETER_ERROR).toRpcClientResult();
    }
    // is private key matches address
    ECKey key = ECKey.fromPrivate(new BigInteger(1, Hex.decode(priKey)));
    try {
        String newAddress = AccountTool.newAddress(key).getBase58();
        if (!newAddress.equals(form.getAddress())) {
            return Result.getFailed(AccountErrorCode.ADDRESS_ERROR).toRpcClientResult();
        }
    } catch (NulsException e) {
        return Result.getFailed(AccountErrorCode.ADDRESS_ERROR).toRpcClientResult();
    }
    try {
        byte[] data = Hex.decode(form.getTxHex());
        Transaction tx = TransactionManager.getInstance(new NulsByteBuffer(data));
        tx = accountLedgerService.signTransaction(tx, key);
        // Result validateResult = tx.verify();
        // if (validateResult.isFailed()) {
        // return Result.getFailed(validateResult.getErrorCode()).toRpcClientResult();
        // }
        // for (Coin coin : tx.getCoinData().getFrom()) {
        // Coin utxo = ledgerService.getUtxo(coin.());
        // if (utxo == null) {
        // return Result.getFailed(LedgerErrorCode.UTXO_NOT_FOUND).toRpcClientResult();
        // }
        // 
        // if (!form.getAddress().equals(AddressTool.getStringAddressByBytes(utxo.()))) {
        // return Result.getFailed(LedgerErrorCode.INVALID_INPUT).toRpcClientResult();
        // }
        // 
        // }
        Map<String, String> map = new HashMap<>();
        map.put("value", Hex.encode(tx.serialize()));
        return Result.getSuccess().setData(map).toRpcClientResult();
    } catch (Exception e) {
        Log.error(e);
        return Result.getFailed(LedgerErrorCode.DATA_PARSE_ERROR).toRpcClientResult();
    }
}
Also used : TransferTransaction(io.nuls.protocol.model.tx.TransferTransaction) NulsException(io.nuls.kernel.exception.NulsException) BigInteger(java.math.BigInteger) ECKey(io.nuls.core.tools.crypto.ECKey) CryptoException(io.nuls.core.tools.crypto.Exception.CryptoException) CryptoException(io.nuls.core.tools.crypto.Exception.CryptoException) NulsException(io.nuls.kernel.exception.NulsException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) IOException(java.io.IOException)

Aggregations

CryptoException (io.nuls.core.tools.crypto.Exception.CryptoException)6 ECKey (io.nuls.core.tools.crypto.ECKey)4 NulsException (io.nuls.kernel.exception.NulsException)4 BigInteger (java.math.BigInteger)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 AccountPo (io.nuls.account.storage.po.AccountPo)1 AliasPo (io.nuls.account.storage.po.AliasPo)1 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)1 Result (io.nuls.kernel.model.Result)1 TransferTransaction (io.nuls.protocol.model.tx.TransferTransaction)1 IOException (java.io.IOException)1 BufferedBlockCipher (org.spongycastle.crypto.BufferedBlockCipher)1 AESFastEngine (org.spongycastle.crypto.engines.AESFastEngine)1 CBCBlockCipher (org.spongycastle.crypto.modes.CBCBlockCipher)1 PaddedBufferedBlockCipher (org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher)1 KeyParameter (org.spongycastle.crypto.params.KeyParameter)1 ParametersWithIV (org.spongycastle.crypto.params.ParametersWithIV)1