How to write OP_CHECKMULTISIG for the following 1-of-2 multisig P2SH script

0

OP_IF
  OP_SHA256
  <hash of secret>
  OP_EQUALVERIFY
  <pubKey of swap>
  OP_CHECKSIG
OP_ELSE
  <relative locktime>
  OP_CHECKSEQUENCEVERIFY
  OP_DROP
  <pubKey of refund>
  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

Aqeel

Posted 2019-06-11T06:24:04.163

Reputation: 3

Answers

0

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)
  <hash of secret>
  OP_EQUALVERIFY
  OP_1 
  OP_DATA (0x21) 
  <pubKey of swap1>
  OP_DATA (0x21)
  <pubkey of swap2>
  OP_2
  OP_CHECKMULTISIG
OP_ELSE
  OP_DATA (size of relative_locktime)
  <relative locktime>
  OP_CHECKSEQUENCEVERIFY
  OP_DROP
  OP_DATA (0x21)
  <pubKey of refund>
  OP_CHECKSIG
OP_ENDIF

You could spend this script with: <0> <sig1> <secret> 1

Ugam Kamat

Posted 2019-06-11T06:24:04.163

Reputation: 5 180

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 2019-06-11T11:11:17.123

@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 answerUgam Kamat 2019-06-11T11:47:13.203

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 errorAqeel 2019-06-11T12:41:30.410

@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 2019-06-11T13:17:24.033

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 2019-06-12T06:08:50.057

@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 itUgam Kamat 2019-06-12T06:17:01.757

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 2019-06-12T06:35:53.523

@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_CHECKMULTISIGUgam Kamat 2019-06-12T07:29:10.230