Transaction check invalid address

try {
        let raw = cardanocliJs.transactionBuildRaw(txInfo);
    } catch {
        console.log("error")
    }

If I set wrong address I get this error:

option --tx-out:
unexpected "+"
invalid address: xxx

How can I check address is correct before transactionBuildRaw?

I do not see anything in cardanocli-js that can do that.

You could do some sanity checks manually. Does it start with addr1? Is the rest only comprised of the right characters and has the right length? You could even implement the BECH32 checksum by hand.

Or you can use cardano-serialization-lib instead: https://github.com/Emurgo/cardano-serialization-lib

$ cat test.js
"use strict"

const CardanoWasm = require("@emurgo/cardano-serialization-lib-nodejs/cardano_serialization_lib")

try {
    const paymentAddr1 = CardanoWasm.Address.from_bech32("addr1qyl58pw58ph8c0c4mg4ufes8t346xp5z795zp2j4jcdxp32ehzzp5nrmfwgee2ywlyfjhevwuktvc6e420m5wl29w7eq79d5f4")
} catch {
    console.log("Address 1 invalid")
}

try {
    const paymentAddr2 = CardanoWasm.Address.from_bech32("xxx")
} catch {
    console.log("Address 2 invalid")
}
$ node test.js
Address 2 invalid

But if you use CSL for this, you maybe want to get rid of cardanocli-js entirely.

1 Like

@HeptaSean Thank you
How can I check confirm or reject transaction?

For example, after transactionSubmit I get this hash id: 984dffd6fa432b8d7dc8bc392410a210d79343ece79e76a392557026838196a0

So, How can I check this hash id confirmed or rejected?

I use this code but does not work for me:

cardanocliJs.transactionView({ txBody: req.body.trxId });

If you really want to do it with cardanocli-js, I would do it with cardanocliJs.queryUtxo(address) on one of the addresses that you sent something to.

If the transaction went through, then the transaction ID has to be in the set of UTxOs.

Or you could use one of the available APIs – https://blockfrost.io/ or https://www.koios.rest/ – to query the blockchain:

$ curl --silent "https://testnet.koios.rest/api/v0/tx_info?_tx_hashes=\{984dffd6fa432b8d7dc8bc392410a210d79343ece79e76a392557026838196a0\}" | jq
[
  {
    "tx_hash": "984dffd6fa432b8d7dc8bc392410a210d79343ece79e76a392557026838196a0",
    "block_hash": "f64b992ec91148cb493091da1861e73871b1e316aeeb369123135fa1b7b0dfff",
    "block_height": 3591926,
    "epoch": 208,
    "epoch_slot": 54186,
    "absolute_slot": 59540586,
    "tx_timestamp": "2022-05-30T11:23:22",
    "tx_block_index": 4,
    "tx_size": 365,
    "total_output": "22445769",
    "fee": "180285",
    "deposit": "0",
    "invalid_before": 0,
    "invalid_after": 59550445,
    "collaterals": [],
    "inputs": [
      {
        "payment_addr": {
          "bech32": "addr_test1qze9g6yg9yyflay4g34jp58t4jcnh699fd8dltnfgpde746cw3p6ta58mjzld2rkh4a009rurhfl88zc96k5uumr9h8scjc3pm",
          "cred": "b254688829089ff495446b20d0ebacb13be8a54b4edfae69405b9f57"
        },
        "stake_addr": "stake_test1upv8gsa976raep0k4pmt67hhj37pm5lnn3vzat2wwd3jmnca2rltt",
        "tx_hash": "8d20a703d174db544054737494d7a630d02cf5127506ac6777a8d2f7accc0617",
        "tx_index": 0,
        "value": "22626054",
        "asset_list": []
      }
    ],
    "outputs": [
      {
        "payment_addr": {
          "bech32": "addr_test1qzasmg7w3eudfxyffzqyy6w2m68fmk645rwdmhvj3ga4gwr3ask84f4t9nwul9v6tgxpnmk8d7j6gwatv568xh7vd0ns8q4jvp",
          "cred": "bb0da3ce8e78d4988948804269cade8e9ddb55a0dcdddd928a3b5438"
        },
        "stake_addr": "stake_test1upc7ctr6564jehw0jkd95rqeamrklfdy8w4k2drntlxxhecwj4sms",
        "tx_hash": "984dffd6fa432b8d7dc8bc392410a210d79343ece79e76a392557026838196a0",
        "tx_index": 1,
        "value": "1000000",
        "asset_list": []
      },
      {
        "payment_addr": {
          "bech32": "addr_test1qze9g6yg9yyflay4g34jp58t4jcnh699fd8dltnfgpde746cw3p6ta58mjzld2rkh4a009rurhfl88zc96k5uumr9h8scjc3pm",
          "cred": "b254688829089ff495446b20d0ebacb13be8a54b4edfae69405b9f57"
        },
        "stake_addr": "stake_test1upv8gsa976raep0k4pmt67hhj37pm5lnn3vzat2wwd3jmnca2rltt",
        "tx_hash": "984dffd6fa432b8d7dc8bc392410a210d79343ece79e76a392557026838196a0",
        "tx_index": 0,
        "value": "21445769",
        "asset_list": []
      }
    ],
    "withdrawals": [],
    "assets_minted": [],
    "metadata": [
      {
        "key": "1",
        "json": {
          "cardanocliJs": "From main site"
        }
      }
    ],
    "certificates": [],
    "native_scripts": [],
    "plutus_contracts": []
  }
]

(What I have done with curl here, you can, of course, do from inside Javascript. I’m sure you know that better than me. I’m not even a Javascript developer.)

1 Like

@HeptaSean thank you very much
How can I understand the transaction ID is verified or rejected?

If one of the APIs – or Cardanoscan (https://testnet.cardanoscan.io/transaction/984dffd6fa432b8d7dc8bc392410a210d79343ece79e76a392557026838196a0) or something like that – show the transaction, it is done. It is on the chain and won’t be undone.

1 Like

@HeptaSean Than you again
Last question, How can I listen to wallet for receive new transaction? ( with cardano cli )

I think, a periodic queryUtxo on the receive address is the best you can do.

Would be nice to have some kind of callback architecture, but I’m not aware of any ready-made solution.

1 Like