Back

How to create Dash P2SH Addresses

I am trying to learn how to create P2SH transactions on the Dash blockchain and was very frustrated at the lack of examples or tutorials for general (not multisig) P2SH. Now that I am finally figuring it out, I am going to document it. Dash is closely related to Bitcoin, so the steps should be virtually the same if you are working in BTC.

P2SH is short for Pay To Script Hash, you can read more about it here. Basically it is a feature that allows a transaction to be locked so that its output can only be spent by someone who presents an unlocking script that satisfies the conditions of a locking script. In reality, this is how all transactions work, but usually the unlocking script simply presents a public key and signature. P2SH allows for a much larger set of conditions.

In this example, I am going to use a trivial locking script that is satisfied by a single numeric value:

OP_2 OP_EQUAL

If you aren’t familiar with Bitcoin’s scripting language, cleverly called Script, you’ll want to read up on it. This script pushes a 2 on the stack and then checks that the last two items on the stack are equal, so the only thing needed to satisfy the script is for there to already be a 2 on the top of the stack.

One of the features of P2SH is the ability to generate addresses that pay to a script hash instead of a public key like “normal” addresses. So the first thing we need to do is generate an address from our script. I am going to be using the dashcore library, which of a fork of bitcore for Dash.

The following code will generate the P2SH address we need.

const dashcore = require('dashcore-lib');

var script = dashcore.Script.fromString('OP_2 OP_EQUAL');
var address = dashcore.Address.payingTo(script, 'testnet');
console.log( address.toString() );

Running this code will output the address we need:

$ node simple_p2sh.js

8nrtzFk4y1t9sL15GzbwFK9DYuiEpTtAzA

We have now created a P2SH address with a trivial locking script. In the next post we will fund this address and create a transaction that unlocks it and spends those funds.

Leave a Reply

Your email address will not be published. Required fields are marked *