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

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

  • 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 Jun 11 at 11:11
  • @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 Jun 11 at 11:47
  • 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 Jun 11 at 12:41
  • @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 Jun 11 at 13:17
  • 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 Jun 12 at 6:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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