Bitcoin Forum

Bitcoin => Electrum => Topic started by: hellossy on November 11, 2020, 07:28:23 AM



Title: How i can use electrum in node js ?
Post by: hellossy on November 11, 2020, 07:28:23 AM
Hi. I install electrum on my server. How i can connect to electrum from node js script ? I try build website with bitcoin payment;
I have not found examples of using electrum in the node js


Title: Re: How i can use electrum in node js ?
Post by: HCP on November 11, 2020, 09:08:42 AM
This apparently is a "multicurrency" wallet that uses a local version of electrum to work the "btc" functionality: https://github.com/jesobreira/multiwallet

You can see it just uses the Electrum JSONRPC API to communicate with Electrum from within node.js: https://github.com/jesobreira/multiwallet/blob/master/lib/currencies/btc.js#L122
You can read more about the Electrum JSONRPC here: https://electrum.readthedocs.io/en/latest/jsonrpc.html

Note, I believe that the full list of available commands is the same as you get when you use help() in the Electrum console:
Code:
>>> help()
[
    "add_lightning_request",
    "add_peer",
    "add_request",
    "addtransaction",
    "broadcast",
    "changegaplimit",
    "clear_invoices",
    "clear_ln_blacklist",
    "clear_requests",
    "close_channel",
    "close_wallet",
    "commands",
    "convert_xkey",
    "create",
    "createmultisig",
    "createnewaddress",
    "decode_invoice",
    "decrypt",
    "deserialize",
    "dumpgraph",
    "dumpprivkeys",
    "enable_htlc_settle",
    "encrypt",
    "export_channel_backup",
    "freeze",
    "get",
    "get_channel_ctx",
    "get_ssl_domain",
    "get_tx_status",
    "get_watchtower_ctn",
    "getaddressbalance",
    "getaddresshistory",
    "getaddressunspent",
    "getalias",
    "getbalance",
    "getconfig",
    "getfeerate",
    "getinfo",
    "getmasterprivate",
    "getmerkle",
    "getminacceptablegap",
    "getmpk",
    "getprivatekeyforpath",
    "getprivatekeys",
    "getpubkeys",
    "getrequest",
    "getseed",
    "getservers",
    "gettransaction",
    "getunusedaddress",
    "help",
    "import_channel_backup",
    "importprivkey",
    "init_lightning",
    "inject_fees",
    "is_synchronized",
    "ismine",
    "lightning_history",
    "list_channels",
    "list_invoices",
    "list_peers",
    "list_requests",
    "list_wallets",
    "listaddresses",
    "listcontacts",
    "listunspent",
    "lnpay",
    "load_wallet",
    "make_seed",
    "nodeid",
    "normal_swap",
    "notify",
    "onchain_history",
    "open_channel",
    "password",
    "payto",
    "paytomany",
    "remove_lightning",
    "removelocaltx",
    "restore",
    "reverse_swap",
    "rmrequest",
    "searchcontacts",
    "serialize",
    "setconfig",
    "setlabel",
    "signmessage",
    "signrequest",
    "signtransaction",
    "stop",
    "sweep",
    "unfreeze",
    "validateaddress",
    "verifymessage",
    "version"
]

The documentation for each one isn't great... but reading the python code here should help: https://github.com/spesmilo/electrum/blob/master/electrum/commands.py


Title: Re: How i can use electrum in node js ?
Post by: NotATether on November 11, 2020, 06:33:37 PM
If you just need middleware that talks to Electrum from node.js, then the JSONRPC API that HCP posted is sufficient.

If you also need a web frontend, you also need to follow the instructions at https://electrum.readthedocs.io/en/latest/merchant.html, which enables the Electrum daemon to generate a webpage people can pay at.

The most important command you need to run online your terminal is:

Code:
electrum -o setconfig payserver_address ecdsa.org:80

Replace ecdsa.org:80 with your domain name and port. This is the site that Electrum will create the payment frontend on. Then you’d use node.js to programmatically pay it using the appropriate JSONRPC command. But to finish the frontend initialization, you need to also run the other commands in the documentation I linked.