Sign Ripple transaction locally using ripple-lib-java

Since rippled 1.1.0 disables public signing by default, we either need to host our own API server or sign Ripple transaction locally. Fortunately, ripple-lib-java gives clue on how to do this in one of its example.

Long story short, we can use this simple method to do the signing:

import java.math.BigDecimal;
import com.ripple.core.types.known.tx.txns.Payment;
import com.ripple.core.types.known.tx.signed.SignedTransaction;
import com.ripple.core.coretypes.uint.UInt32;
import com.ripple.core.coretypes.AccountID;
import com.ripple.core.coretypes.Amount;

...

/**
 * Generate signature locally for a payment transaction
 * @param xrpAmount
 * @param accountId
 * @param secret
 * @param addressTo
 * @param sequence
 * @param fee
 * @return transaction blob
 */
public String sign(BigDecimal xrpAmount, String accountId, String secret, String addressTo, int sequence, int fee) {
  Payment payment = new Payment();
  payment.as(AccountID.Account, accountId);
  payment.as(AccountID.Destination, addressTo);
  payment.as(Amount.Amount, new Amount(xrpAmount).toDropsString());
  payment.as(UInt32.Sequence, sequence);
  payment.as(Amount.Fee, String.valueOf(fee));
  SignedTransaction signed = payment.sign(secret);
  return signed.tx_blob;
}

The only catch is we need to provide sequence and fee ourselves. Luckily, we can use account_info API to get the sequence and use default transaction fee 10,000 drops (for testnet) or 10 drops (for main net).

Once we get the transaction blob, we just need to submit it using submit API. This snippet shows how to do it using a websocket client:

BigDecimal xrpAmount = new BigDecimal(1); // 1 XRP
String addressFrom = "...";
String addressFromSecret = "...";
String addressTo = "...";
// call account_info API and parse sequence from its response body
Integer sequence = client.getSequenceFromAccountInfoResponse(body);
int fee = 10000; // 10,000 drops
String txBlob = client.sign(
    xrpAmount,
    addressFrom, 
    addressFromSecret, 
    addressTo, 
    sequence, 
    fee
);

// send blob to submit API
client.send(client.getSubmitTransactionPayload(txBlob));

 

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments