<p>From what I understand from your question (correct me if I'm wrong), you want to have a script that in the <code>OP_IF</code> part pays to 1-of-2 public keys. You could try the below script:</p>

<pre><code>OP_IF
  OP_SHA256
  OP_DATA (0x20)
  &lt;hash of secret&gt;
  OP_EQUALVERIFY
  OP_1 
  OP_DATA (0x21) 
  &lt;pubKey of swap1&gt;
  OP_DATA (0x21)
  &lt;pubkey of swap2&gt;
  OP_2
  OP_CHECKMULTISIG
OP_ELSE
  OP_DATA (size of relative_locktime)
  &lt;relative locktime&gt;
  OP_CHECKSEQUENCEVERIFY
  OP_DROP
  OP_DATA (0x21)
  &lt;pubKey of refund&gt;
  OP_CHECKSIG
OP_ENDIF
</code></pre>

<p>You could spend this script with: <code>&lt;0&gt; &lt;sig1&gt; &lt;secret&gt; 1</code></p>

CC BY-SA 4.0
2019-06-11T11:11:17.123Z
Yes @Ugam Kamat you are actually right.
I have edit my question please see it again..Actually the complication which i am facing is, how to provide these two public keys . Is it the right way?    ```                  var add=[recipientPublicKey,exchangePublicKey]                  Whereas Inside compile Buffer.from(add, "hex"),
this.app.env.bitcoin.opcodes.OP_2,
this.app.env.bitcoin.opcodes.CHECKMULTISIG,                                                                          Is it correct?
Aqeel
CC BY-SA 4.0
2019-06-11T11:47:13.203Z
@Aqeel You need to use a `OP_DATA` opcode between them to indicate the bytes of data to push to stack. For example, to push hash_of_secret to stack you need to tell the program to push the next 32 bytes which is the reason we need `0x20` preceding that as shown in my edited answer
Ugam Kamat
CC BY-SA 4.0
2019-06-11T12:41:30.410Z
Thanks for reply. But Opcode you listed "OP_DATA" is not a valid opcode. I have printed the list of opcodes but that opcode is not in the list and therefor creating error
Aqeel
CC BY-SA 4.0
2019-06-11T13:17:24.033Z
@Aqeel It's not exactly an opcode but a parameter that helps you indicate to the software how much data you want to push on the stack. For example, if you see my script i used `0x20` before hash of secret. It indicates to the software to push the next 32 bytes (ox20 in hex) on the stack. Before the pubkey I used 0x21 to indicate pushing the next 33 bytes on the stack (assuming you are using compressed pub keys)
Ugam Kamat
CC BY-SA 4.0
2019-06-12T06:08:50.057Z
Can you provide me any helping material for 1-of-2 p2sh multisig script, which may helps me in writing script in desired format.
Aqeel
CC BY-SA 4.0
2019-06-12T06:17:01.757Z
@Aqeel I don't think there is one specific to 1-of-2 p2sh multisig. Even if it is out there I'm oblivious to it
Ugam Kamat
CC BY-SA 4.0
2019-06-12T06:35:53.523Z
actually the thing which i want to see is how can i pass list of pubkeys to apply OP_CHECKMULTISIG in desired code format...
The ref repo is: https://github.com/swaponline/swap.core/blob/97d1d5b6afffee97869302443924bef79ce42763/src/swap.swaps/BtcSwap.js#L181
I am changing this piece of script to 1-of-2 p2sh multisig. 
Any guidance for me after seeing this script form above reference link?
Aqeel
CC BY-SA 4.0
2019-06-12T07:29:10.230Z
@Aqeel I haven't gone through the code in detail, but maybe you can repeat `Buffer.from(recipientPublicKey, 'hex'),` twice with different public key and then instead of `this.app.env.bitcoin.opcodes.OP_CHECKSIG` use `this.app.env.bitcoin.opcodes.OP_CHECKMULTISIG`
Ugam Kamat
CC BY-SA 4.0
2019-06-11T09:06:22.300Z
2019-06-11T11:48:58.437Z
2019-06-11T11:48:58.437Z
Ugam Kamat
<pre><code>OP_IF
  OP_SHA256
  &lt;hash of secret&gt;
  OP_EQUALVERIFY
  &lt;pubKey of swap&gt;
  OP_CHECKSIG
OP_ELSE
  &lt;relative locktime&gt;
  OP_CHECKSEQUENCEVERIFY
  OP_DROP
  &lt;pubKey of refund&gt;
  OP_CHECKSIG
OP_ENDIF
</code></pre>

<p>Actual code which i am using for the above script is </p>

<pre><code>const script = this.app.env.bitcoin.script.compile([
      hashOpcode,
      Buffer.from(secretHash, "hex"),
      this.app.env.bitcoin.opcodes.OP_EQUALVERIFY,
     Buffer.from(recipientPublicKey, "hex"),
      this.app.env.bitcoin.opcodes.OP_EQUAL,
      this.app.env.bitcoin.opcodes.OP_IF,                   
      Buffer.from(recipientPublicKey, "hex"),
      this.app.env.bitcoin.opcodes.OP_CHECKSIG,
     this.app.env.bitcoin.opcodes.OP_ELSE,
    this.app.env.bitcoin.script.number.encode(lockTime),
      this.app.env.bitcoin.opcodes.OP_CHECKSEQUENCEVERIFY,
      this.app.env.bitcoin.opcodes.OP_DROP,
      Buffer.from(ownerPublicKey, "hex"),
      this.app.env.bitcoin.opcodes.OP_CHECKSIG,
      this.app.env.bitcoin.opcodes.OP_ENDIF
    ]);
</code></pre>

<p>As we see that this script has just one  public key(<strong>pubKey of swap</strong>) to sign with. But i want to sign it with two public keys.
I searched for it but found nothing, can anybody guide me?</p>

<p>I just want to follow this pattern</p>

<pre><code> OP_0 [Sig 1] OP_1 [PK 1][PK 2] OP_2 OP_CHECKMULTISIG

</code></pre>

<multi-signature>
How to write OP_CHECKMULTISIG for the following 1-of-2 multisig P2SH script
Aqeel
# How to write OP_CHECKMULTISIG for the following 1-of-2 multisig P2SH script

(Asked by: username_0 on 2019-06-11)

OP_IF
  OP_SHA256
  
  OP_EQUALVERIFY
  
  OP_CHECKSIG
OP_ELSE
  
  OP_CHECKSEQUENCEVERIFY
  OP_DROP
  
  OP_CHECKSIG
OP_ENDIF

Actual code which i am using for the above script is 
const script = this.app.env.bitcoin.script.compile([
      hashOpcode,
      Buffer.from(secretHash, "hex"),
      this.app.env.bitcoin.opcodes.OP_EQUALVERIFY,
     Buffer.from(recipientPublicKey, "hex"),
      this.app.env.bitcoin.opcodes.OP_EQUAL,
      this.app.env.bitcoin.opcodes.OP_IF,                   
      Buffer.from(recipientPublicKey, "hex"),
      this.app.env.bitcoin.opcodes.OP_CHECKSIG,
     this.app.env.bitcoin.opcodes.OP_ELSE,
    this.app.env.bitcoin.script.number.encode(lockTime),
      this.app.env.bitcoin.opcodes.OP_CHECKSEQUENCEVERIFY,
      this.app.env.bitcoin.opcodes.OP_DROP,
      Buffer.from(ownerPublicKey, "hex"),
      this.app.env.bitcoin.opcodes.OP_CHECKSIG,
      this.app.env.bitcoin.opcodes.OP_ENDIF
    ]);

As we see that this script has just one  public key(pubKey of swap) to sign with. But i want to sign it with two public keys.
I searched for it but found nothing, can anybody guide me?
I just want to follow this pattern
 OP_0 [Sig 1] OP_1 [PK 1][PK 2] OP_2 OP_CHECKMULTISIG

**Tags:** multi-signature


---

## 1 answers

### Answer by username_1 on 2019-06-11. Score: 0 | Accepted answer

From what I understand from your question (correct me if I'm wrong), you want to have a script that in the OP_IF part pays to 1-of-2 public keys. You could try the below script:
OP_IF
  OP_SHA256
  OP_DATA (0x20)
  
  OP_EQUALVERIFY
  OP_1 
  OP_DATA (0x21) 
  
  OP_DATA (0x21)
  
  OP_2
  OP_CHECKMULTISIG
OP_ELSE
  OP_DATA (size of relative_locktime)
  
  OP_CHECKSEQUENCEVERIFY
  OP_DROP
  OP_DATA (0x21)
  
  OP_CHECKSIG
OP_ENDIF

You could spend this script with: <0>   1

> **Comments:**
> **username_0**: Yes @username_1 you are actually right. I have edit my question please see it again..Actually the complication which i am facing is, how to provide these two public keys . Is it the right way?    ```                  var add=[recipientPublicKey,exchangePublicKey]                  Whereas Inside compile Buffer.from(add, "hex"), this.app.env.bitcoin.opcodes.OP_2, this.app.env.bitcoin.opcodes.CHECKMULTISIG,                                                                          Is it correct?  
> **username_1**: @username_0 You need to use a `OP_DATA` opcode between them to indicate the bytes of data to push to stack. For example, to push hash_of_secret to stack you need to tell the program to push the next 32 bytes which is the reason we need `0x20` preceding that as shown in my edited answer  
> **username_0**: Thanks for reply. But Opcode you listed "OP_DATA" is not a valid opcode. I have printed the list of opcodes but that opcode is not in the list and therefor creating error  
> **username_1**: @username_0 It's not exactly an opcode but a parameter that helps you indicate to the software how much data you want to push on the stack. For example, if you see my script i used `0x20` before hash of secret. It indicates to the software to push the next 32 bytes (ox20 in hex) on the stack. Before the pubkey I used 0x21 to indicate pushing the next 33 bytes on the stack (assuming you are using compressed pub keys)  
> **username_0**: Can you provide me any helping material for 1-of-2 p2sh multisig script, which may helps me in writing script in desired format.  
> **username_1**: @username_0 I don't think there is one specific to 1-of-2 p2sh multisig. Even if it is out there I'm oblivious to it  
> **username_0**: actually the thing which i want to see is how can i pass list of pubkeys to apply OP_CHECKMULTISIG in desired code format... The ref repo is: https://github.com/swaponline/swap.core/blob/97d1d5b6afffee97869302443924bef79ce42763/src/swap.swaps/BtcSwap.js#L181 I am changing this piece of script to 1-of-2 p2sh multisig.  Any guidance for me after seeing this script form above reference link?  
> **username_1**: @username_0 I haven't gone through the code in detail, but maybe you can repeat `Buffer.from(recipientPublicKey, 'hex'),` twice with different public key and then instead of `this.app.env.bitcoin.opcodes.OP_CHECKSIG` use `this.app.env.bitcoin.opcodes.OP_CHECKMULTISIG`  

---

