SBaronda.com a little place I like to call home.


Talking Directly to Dogecoind

Feb. 26, 2014

Here is some quick reference material on talking to Dogecoind directly through RPC-JSON and building a transaction manually.

Here is the information about basic wallet operations.

Start dogecoind and use "dogecoind getinfo" to test that dogecoind has connections and works as it should.

Python: from bitcoinrpc.authproxy import AuthServiceProxy dogecoin = AuthServiceProxy("http://rpcuser:rpcpassword@localhost:22555") print dogecoin.getinfo()

Change rpcuser and rpcpassword to values you have defined in dogecoin.conf file. If everything works, that code should print various state info in json-format.

Let's assume that you are building a service (gambling site, game with purchasable content) where every user account has a balance. For every user account you need an individual Dogecoin address so users can deposit Dogecoins to their balance:

address = dogecoin.getaccountaddress("test@example.org")
balance = dogecoin.getbalance("test@example.org")
transactions = dogecoin.listtransactions("test@example.org")  

These commands use the wallet's accounts. The first line returns an address that is connected with account test@example.org, the second line returns the balance of that account and the last line returns a list of transaction-objects for that account.

If you use the wallet's main account to hold Dogecoins that are used in your service, you can move an amount from user's account to the main account:

dogecoin.move("test@example.org", "", 25.0)

To send Dogecoins away from the wallet, you need to unlock it first:

dogecoin.walletpassphrase("walletpassphrase", 5)
dogecoin.sendfrom("test@example.org", "DDonateqA8bRW3xHMeUcWJ2yzY8Sm6fCh8", 99.0)
dogecoin.walletlock()

Here is the information about building the transaction manually.

There is a set of raw transaction commands exposed via the dogecoind JSON-RPC interface.

Here's an example of spending a transaction when you have the private key of one of its outputs but that private key isn't in a wallet.

txid: ffa2c2058f0bc010fa391100ed7fdef0393acffcd9dd16082677a6c2479d208b
vout: 0
amount: 482080
scriptPubKey: 210284ca3007f08bb8a597328c013f8436b0b45d135f8f0fd2d4d5183fa5dd0f41caac

The public key and private key associated with the scriptPubKey is

public_key: nh78WoMnS9F7GF266YHEq8Y9K66CV3z2C8
private_key: chG1LhVjM5vnfmFdo95Ge5G132zsFt2PSLGj1e5gtWdsG3bDfJQe

Because I know the private_key associated with this transaction output I can spend it how I wish. What I want to do is spend this transaction to some target public address

target_public_key: nr2Yu21WoUGMb4pLhagcqNYHeUsJwwQBhu

I do this using dogecoind as follow:

  1. Create a raw transaction with the txid, vout, and target public key from above. The send amount to the public key is one less (482079) than the spendable output (482080) because of a 1 doge transfer fee.

    Template: createrawtransaction 
        [{"txid": "$txid", "vout": $vout}]
        {"$target_public_key": $amount - 1}
    
    createrawtransaction 
        [{"txid": "ffa2c2058f0bc010fa391100ed7fdef0393acffcd9dd16082677a6c2479d208b", "vout": 0}]
        {"nr2Yu21WoUGMb4pLhagcqNYHeUsJwwQBhu": 482079}
    

This will return the hex representation as

    01000000018b209d47c2a6772608...

Not that this is a raw unsigned transaction. Before we transmit it we need to sign it with all the private keys of the input transactions.

  1. Sign the raw transaction with the private key, note unsigned_hex is the hex representation of the transaction from the previous step.

    Template: signrawtransaction $unsigned_hex [] ["$private_key"]

    signrawtransaction 01000000018b209d47c2a6772608… [] ["chG1LhVjM5vnfmFdo95Ge5G132zsFt2PSLGj1e5gtWdsG3bDfJQe"]

If the private key is correct then this will return a JSON object with the complete field set to true. The signed transaction is returned in the hex field.

    {
        "hex" : "01000000018b209d47c2a6772608...",
        "complete" : true
    }
  1. You can now broadcast this transaction over the p2p network.

    Template: sendrawtransaction $signed_hex
    
    sendrawtransaction 01000000018b209d47c2a6772608...
    

This will return the txid of the sent transaction.

    acd897d728d69b21665a0fd09d6a4c17fad7c2fb0d29d3261ece50dc938a861e

Not that nowhere in this process do I have to put anything in my wallet! I could run a new instance of dogecoind offline, type in the transaction details, public, and private keys, and generate a signed transaction. I could then paste this signed transaction's hex into an email to you and you send it via the network, without you having the private key!

I've just implemented this for a dogecoind based payment system and it works great! The raw transaction API is very very useful once you wrap your head around it.


comments powered by Disqus