리플(ripple) 전송하기

리플(ripple) 전송하기 updated_at: 2024-03-19 12:00

리플(ripple) 전송하기

xrpl(ver 2.x) 를 이용한 방식

xrpl 인스톨시 ver 2.x를 사용하기 바랍니다. 현재 (2024년 03월 기준) ver3.0은 오류가 있어 보입니다.

install

npm i xrpl@2
require('dotenv').config();
const xrpl = require("xrpl");

module.exports = class Ripple {
  constructor() {
  }

  /**
   * curl로 요청된 transaction 처리
   */
  async sendToAddress(to, tag, amount, mySecret) {
    const client = new xrpl.Client(process.env.RIPPLE_API_SERVER)
    try {
      await client.connect()
      const wallet = await xrpl.Wallet.fromSeed(mySecret);

      const params = {
        "TransactionType": "Payment",
         "Account": wallet.address,
        "Amount": xrpl.xrpToDrops(amount),
        "Destination": to,
        'DestinationTag': tag ? parseInt(tag, 10) : 0
      }
      
      const prepared = await client.autofill(params);
      const signed = wallet.sign(prepared);
      const result = await client.submitAndWait(signed.tx_blob);

      console.log(result);
      if(result.result.validated) {
        callback(null, result);
      } else {
        callback('validated Fail', result);
      }
      
    } catch (error) {
      console.error('error:', error);
    } finally {
      await client.disconnect();
    }
  }
} // Ripple(io)
  • 위의 result
result: {
  id: 10,
  result: {
    Account: 'rDHkxx.....',
    Amount: '2000000',
    DeliverMax: '2000000',
    Destination: 'rUz8u.....',
    DestinationTag: 3,
    Fee: '12',
    Flags: 0,
    LastLedgerSequence: 46282498,
    Sequence: 8271861,
    SigningPubKey: '038F78.....',
    TransactionType: 'Payment',
    TxnSignature: '3045022100A28.....',
    ctid: 'C2C236.....',
    date: 7641.....,
    hash: 'D0CA6.....E',
    inLedger: 462.....,
    ledger_index: 462.....,
    meta: {
      AffectedNodes: [Array],
      TransactionIndex: 5,
      TransactionResult: 'tesSUCCESS',
      delivered_amount: '2000000'
    },
    validated: true
  },
  type: 'response'
}

ripple-lib(ver 1.x)을 이용한 방식

nodeJs에서 ripple-lib을 이용한 방식

install

npm i ripple-lib
require('dotenv').config();
const async = require('async');
// ripple api initialize Start
const RippleAPI = require('ripple-lib').RippleAPI;

module.exports = class Ripple {
  constructor() {
    this.api = new RippleAPI({
      server: process.env.RIPPLE_API_SERVER
    });
  }

  // Continuing after connecting to the API
  /**
   * 코인전송을 위한 prepare
   * Account : 인출할 주소, Amount: 전송할 금액, Destination : 전송할 주소
   */
  async doPrepare(sender, receiver, tag, xrp) {
    const preparedTx = await this.api.prepareTransaction(
      {
        'Account': sender,
        'TransactionType': 'Payment',
        'Amount': this.api.xrpToDrops(xrp),
        'Destination': receiver,
        'DestinationTag': tag ? parseInt(tag, 10) : 0
      }, {
        // Expire this transaction if it doesn't execute within ~5 minutes:
        'maxLedgerVersionOffset': 75
      });
    return preparedTx.txJSON;
  }

  async doSubmit(txBlob) {
    const latestLedgerVersion = await this.api.getLedgerVersion();

    const result = await this.api.submit(txBlob)
    return {
      result,
      latestLedgerVersion: latestLedgerVersion + 1
    }
  };

  /**
   * curl로 요청된 transaction 처리
   */
  async sendToAddress(to, tag, amount, from, mySecret) {

    // 아래처럼 from을 별도로 받지 않고 mySecret를 이용해서도 가져올 수 있다.
    // const keypair = this.api.deriveKeypair(mySecret)
    // const from = this.api.deriveAddress(keypair.publicKey)

    // Step 1 : 전송할 데이타 만들기
    const txJSON = await this.doPrepare(from, to, tag, amount);

    
    try {
      // Step 2 : Sign the Transaction Instructions
      const response = await this.api.sign(txJSON, mySecret);
      const tx_blob = response.signedTransaction;

      // Step 3 : Submit the Signed Blob
      const submitResult = await this.doSubmit(tx_blob);

      // 정상적으로 처리되었으면
      if (submitResult.result.engine_result_code === 0) {
        cosole.log('txid: ', response.id);
      } else {
        
      }

    } catch (e) {
      console.error('catch error: ', e);
    }
  }
} // Ripple(io)
평점을 남겨주세요
평점 : 5.0
총 투표수 : 1

질문 및 답글