0

I am using bitcore-lib to create transactions. I can add an output with CLTV to the transaction but am unable to add an output address to it. This is my code (typescript/node):

const lockScript = Script.empty()
                         .add(blockAsBuffer)
                         .add(177) // OP_CLTV
                         .add(117) // OP_DROP
                         .add(Script.buildPublicKeyHashOut(this.privateKeyAddress));

const transaction = new Transaction();
transaction.from(unspentOutputs);

// Following is where I'm adding the CLTV but can't add an output address
transaction.addOutput(new Transaction.Output({
  script: lockScript,
  satoshis: satoshisToLock
}));

transaction.change(this.privateKeyAddress);
transaction.fee(feeInSatoshis);
transaction.sign(this.privateKey);

With the above code, the transaction-output is something like the following. Note that there's no address attached to the CLTV output. Is there a way to add an address to that output using the bitcore-lib (or any other javascript lib)?

...
{
  "value": 0.00001,
  "n": 2,
  "scriptPubKey": {
    "asm": "1658883 OP_CHECKLOCKTIMEVERIFY OP_DROP OP_DUP OP_HASH160 89605b86b7ad185ebc10fe457b98776838972cf0 OP_EQUALVERIFY OP_CHECKSIG",
    "hex": "0403501900b17576a91489605b86b7ad185ebc10fe457b98776838972cf088ac",
    "type": "nonstandard"
   }
},
...

In case you are curious as to why do I want to do this, see the previous question that I had asked: Using CLTV can I freeze some funds from a wallet, and make them available, in a single transaction?

1
  • 1
    What do you mean by "address"? You have a public key hash in the script. The holder of the corresponding public key can redeem the funds. Maybe what you want to do is wrap the script in a P2SH address? We actually have a guide for exactly this in bcoin (another JS lib): bcoin.io/guides/cltv.html
    – pinhead
    Jan 13, 2020 at 18:57

1 Answer 1

1

You're putting a script directly in the scriptPubKey. That's both discouraged, non-standard, and hard to use, as you've noticed there is no corresponding address.

There are only three types of addresses defined (with common acceptance) in Bitcoin: base58-encoded P2PKH addresses (1...), base58-encoded P2SH addresses (3...), and bech32-encoded native segwit addresses (bc1...). Unless the output you want to construct has the structure corresponding to one of these, there simply does not exist any address for it.

This is with good reason: when you give an address to someone else to pay you, they have no business knowing how you want to protect your coins. As the policy you want to use gets more complex, sending to more and more complicated scripts would require them also to pay increasing fee. This is weird: it's you who cares about that policy, so you should pay for it instead.

With P2SH (and later P2WSH), the script is no longer put in the output itself. Instead, a hash of the script is put in the output, and the recipient, when spending the output reveals what the actual script was and satisfies it.

So you probably want to either do this manually by reading how to construct a P2SH/P2WSH output (see BIP16 and BIP141 respectively), or figure out how the library you're using lets you construct such outputs automatically.

1
  • Thanks for the details in the response Peter, it helped me understand the different types of addresses. I used the BIP65 demo to implement the freeze and spend transactions.
    – Ali
    Jan 22, 2020 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.