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();
}
}
Aggregations