Categories: Blockchain

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
yohanes.gultom@gmail.com

Share
Published by
yohanes.gultom@gmail.com
Tags: javaripple

Recent Posts

Get Unverified SSL Certificate Expiry Date with Python

Getting verified SSL information with Python (3.x) is very easy. Code examples for it are…

3 years ago

Spring Data Couchbase 4 Multibuckets in Spring Boot 2

By default, Spring Data Couchbase implements single-bucket configuration. In this default implementation, all POJO (Plain…

3 years ago

Firebase Auth Emulator with Python

Last year, Google released Firebase Auth Emulator as a new component in Firebase Emulator. In…

4 years ago

Google OIDC token generation/validation

One of the authentication protocol that is supported by most of Google Cloud services is…

4 years ago

Fast geolocation query with PostGIS

If you need to to add a spatial information querying in your application, PostGIS is…

4 years ago

Auto speech-to-text (Indonesian) with AWS Transcribe and Python

Amazon Web Service Transcribe provides API to automatically convert an audio speech file (mp3/wav) into…

5 years ago