James Brotchie

Finance;
Software;
Open Source.

Spending Non-Wallet Dogecoin Transactions

James Brotchie

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:

Create Raw Transaction

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

01000000018b209d47c2a677260816ddd9fccf3a39f0de7fed001139fa10c00b8f05c2a2ff0000000000ffffffff01003fc146d82b00001976a914ef81804d284e1c623d56c23f28c7bdad8d8d00a988ac00000000

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.

Sign Raw Transaction

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 01000000018b209d47c2a677260816ddd9fccf3a39f0de7fed001139fa10c00b8f05c2a2ff0000000000ffffffff01003fc146d82b00001976a914ef81804d284e1c623d56c23f28c7bdad8d8d00a988ac00000000 [] ["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" : "01000000018b209d47c2a677260816ddd9fccf3a39f0de7fed001139fa10c00b8f05c2a2ff0000000048473044022052c32ad49591eb538c52f30930074b7d24523094a66635146687af52c7f715d502207825dd3f5e03b4cec38119fdec358256832be83762cd252e77598f81a982b4e701ffffffff01003fc146d82b00001976a914ef81804d284e1c623d56c23f28c7bdad8d8d00a988ac00000000",
    "complete" : true
}

Spend Raw Transaction

You can now broadcast this transaction over the p2p network.

Template: sendrawtransaction $signed_hex

sendrawtransaction 01000000018b209d47c2a677260816ddd9fccf3a39f0de7fed001139fa10c00b8f05c2a2ff0000000048473044022052c32ad49591eb538c52f30930074b7d24523094a66635146687af52c7f715d502207825dd3f5e03b4cec38119fdec358256832be83762cd252e77598f81a982b4e701ffffffff01003fc146d82b00001976a914ef81804d284e1c623d56c23f28c7bdad8d8d00a988ac00000000

This will return the txid of the sent transaction.

acd897d728d69b21665a0fd09d6a4c17fad7c2fb0d29d3261ece50dc938a861e

Note 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.