<p>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.</p>

<p>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.</p>

<p>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.</p>

<p>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.</p>

<p>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.</p>

CC BY-SA 4.0
2020-01-22T18:02:15.573Z
Thanks for the details in the response Peter, it helped me understand the different types of addresses. I used the BIP65 [demo](https://github.com/mruddy/bip65-demos/blob/master/freeze.js) to implement the freeze and spend transactions.
Ali
CC BY-SA 4.0
2020-01-13T19:15:53.110Z
2020-01-13T19:15:53.110Z
Pieter Wuille
<p>I am using <a href="https://github.com/bitpay/bitcore-lib" rel="nofollow noreferrer">bitcore-lib</a> 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):</p>



<pre><code>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);
</code></pre>

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

<pre><code>...
{
  "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"
   }
},
...
</code></pre>

<p>In case you are curious as to why do I want to do this, see the previous question that I had asked: <a href="https://bitcoin.stackexchange.com/questions/92739/using-cltv-can-i-freeze-some-funds-from-a-wallet-and-make-them-available-in-a">Using CLTV can I freeze some funds from a wallet, and make them available, in a single transaction?</a></p>

CC BY-SA 4.0
2020-01-13T18:57:37.500Z
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): https://bcoin.io/guides/cltv.html
pinhead
<bitcoin-core-development><checklocktimeverify>
How to add an address to a CheckLockTimeValue (CLTV) transaction output using bitcore-lib (or any other js lib)
Ali
# How to add an address to a CheckLockTimeValue (CLTV) transaction output using bitcore-lib (or any other js lib)

(Asked by: username_0 on 2020-01-10)

I am using [bitcore-lib](https://github.com/bitpay/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?](https://bitcoin.stackexchange.com/questions/92739/using-cltv-can-i-freeze-some-funds-from-a-wallet-and-make-them-available-in-a)

**Tags:** bitcoin-core-development, checklocktimeverify

> **Comments:**
> **username_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): https://bcoin.io/guides/cltv.html  

---

## 1 answers

### Answer by username_2 on 2020-01-13. Score: 1 | Accepted answer

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.

> **Comments:**
> **username_0**: Thanks for the details in the response Peter, it helped me understand the different types of addresses. I used the BIP65 [demo](https://github.com/mruddy/bip65-demos/blob/master/freeze.js) to implement the freeze and spend transactions.  

---

