XRPLF / xrpl.js

A JavaScript/TypeScript API for interacting with the XRP Ledger in Node.js and the browser
https://xrpl.org/
1.2k stars 510 forks source link

How to add destination_tag to transaction ? #1342

Closed prodevcn closed 3 years ago

prodevcn commented 3 years ago

I created transaction and signed it and submitted but method returned response like that :

Identifying hash: 5A924302E53F2FFC52CCF68C93B13E70EC9ED4151D632E6CCC79B917C4EF0DE0 Signed blob: 120000228000000024038EB22B201B0395CA076140000000009896806840000000000017EB7321031D258AE06AE643E8663CA15A1FA5DD2EE211D24D2A7522D5AAFE59A6A11E353B7446304402202592AB0F1CD163A258019BC15C9009E42F078B44A78B488CCFA2CEC3A1AC31570220486B32A63813DBF8C8174653F0311A45CC9E2F20D9B77AC18A76FE1E942B16388114AB68020445C84CC798E76E4D683AA3ED50AD80D48314A025D8B3210251C94FA3AAC92E159673A140FAD9 Tentative result code: tecDST_TAG_NEEDED Tentative result message: A destination tag is required.

How can I add destination_tag to transaction object ?

elmurci commented 3 years ago

Hey @block-hac, you will need to specify the destination tag in the Payment Instruction:

const payment = {
  source: {
    address: sourceAddress,
    maxAmount: {
      value: '0.01',
      currency: 'XRP'
    }
  },
  destination: {
    address: paymentDestinationAddress,
    amount: {
      value: '0.01',
      currency: 'XRP'
    },
    tag: 12121982
  }
}
prodevcn commented 3 years ago

Thank you @elmurci I use ripple-lib api and source code is as follow :

api.connect()
        .then( () => {
            return api.prepareTransaction({
                "TransactionType": 'Payment',
                "Account": req.body.fromaddress,
                "Amount": api.xrpToDrops(req.body.amount),
                "Destination": req.body.toaddress
            }, {maxLedgerVersionOffset: 75}).then( async (preparedTx) => {
                const maxLedgerVersion = preparedTx.instructions.maxLedgerVersion;
                console.log("Prepared transaction instructions:", preparedTx.txJSON);
                console.log("Transaction cost:", preparedTx.instructions.fee, "XRP");
                console.log("Transaction expires after ledger:", maxLedgerVersion);
                const txJSON = preparedTx.txJSON;
                const response = api.sign(txJSON, req.body.secret);
                const txID = response.id;
                console.log("Identifying hash:", txID);
                const txBlob = response.signedTransaction;
                console.log("Signed blob:", txBlob);
                const latestLedgerVersion = await api.getLedgerVersion();
                api.submit(txBlob).then(async (result) => {
                    console.log("Tentative result code:", result.resultCode);
                    console.log("Tentative result message:", result.resultMessage);
                    res.send(result);
                }).catch(err => {console.error(err)});
            }).catch(err => {console.error(err)});
        })
        .catch(err => {
            res.send(err);
        })
elmurci commented 3 years ago

I'd use preparePayment for this. If you look at the Payment type you can see the you can use a destination.tag.

intelliot commented 3 years ago

@block-hac You can add the DestinationTag field to the transaction JSON:

{
                "TransactionType": 'Payment',
                "Account": req.body.fromaddress,
                "Amount": api.xrpToDrops(req.body.amount),
                "Destination": req.body.toaddress,
                "DestinationTag": destination_tag
}

Be sure to replace destination_tag with the destination tag that you wish to use!

prodevcn commented 3 years ago

thanks @intelliot I already tried it but it returned error message.

[nodemon] 2.0.5
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
Server running on port 3841
Prepared transaction instructions: {"TransactionType":"Payment","Account":"rGdK9QpdNKFdqpSrYt6EVijBP2kSo2LsFJ","Amount":"20000000","Destination":"rEb8TK3gBgk5auZkwc6sHnwrGVJH8DuaLh","DestinationTag":"106018755","Flags":2147483648,"LastLedgerSequence":60150148,"Fee":"12","Sequence":59683374}
Transaction cost: 0.000012 XRP
Transaction expires after ledger: 60150148
ValidationError: Serialized transaction does not match original txJSON. See `error.data`
    at checkTxSerialization (/root/brokers/xrp/node_modules/ripple-lib/dist/npm/transaction/sign.js:117:23)
    at signWithKeypair (/root/brokers/xrp/node_modules/ripple-lib/dist/npm/transaction/sign.js:50:5)
    at RippleAPI.sign (/root/brokers/xrp/node_modules/ripple-lib/dist/npm/transaction/sign.js:137:16)
    at /root/brokers/xrp/src/routes/api.js:155:38
    at processTicksAndRejections (internal/process/task_queues.js:93:5) {
  data: {
    decoded: {
      TransactionType: 'Payment',
      Flags: 2147483648,
      Sequence: 59683374,
      DestinationTag: 106018755,
      LastLedgerSequence: 60150148,
      Amount: '20000000',
      Fee: '12',
      Account: 'rGdK9QpdNKFdqpSrYt6EVijBP2kSo2LsFJ',
      Destination: 'rEb8TK3gBgk5auZkwc6sHnwrGVJH8DuaLh'
    },
    tx: {
      TransactionType: 'Payment',
      Account: 'rGdK9QpdNKFdqpSrYt6EVijBP2kSo2LsFJ',
      Amount: '20000000',
      Destination: 'rEb8TK3gBgk5auZkwc6sHnwrGVJH8DuaLh',
      DestinationTag: '106018755',
      Flags: 2147483648,
      LastLedgerSequence: 60150148,
      Fee: '12',
      Sequence: 59683374
    },
    diff: { DestinationTag: 106018755 }
  }
}
intelliot commented 3 years ago

@block-hac DestinationTag must be a number. Try removing the "quotes" around the number. Just use "DestinationTag": 106018755 instead.

prodevcn commented 3 years ago

Thanks @intelliot , I just found that.

elmurci commented 3 years ago

Are you happy to close this issue @block-hac?