Module: Tapyrus::Multisig

Includes:
Opcodes
Defined in:
lib/tapyrus/script/multisig.rb

Overview

utility for multisig

Constant Summary

Constants included from Opcodes

Opcodes::DUPLICATE_KEY, Opcodes::NAME_MAP, Opcodes::OPCODES_MAP, Opcodes::OP_0, Opcodes::OP_0NOTEQUAL, Opcodes::OP_1, Opcodes::OP_10, Opcodes::OP_11, Opcodes::OP_12, Opcodes::OP_13, Opcodes::OP_14, Opcodes::OP_15, Opcodes::OP_16, Opcodes::OP_1ADD, Opcodes::OP_1NEGATE, Opcodes::OP_1SUB, Opcodes::OP_2, Opcodes::OP_2DIV, Opcodes::OP_2DROP, Opcodes::OP_2DUP, Opcodes::OP_2MUL, Opcodes::OP_2OVER, Opcodes::OP_2ROT, Opcodes::OP_2SWAP, Opcodes::OP_3, Opcodes::OP_3DUP, Opcodes::OP_4, Opcodes::OP_5, Opcodes::OP_6, Opcodes::OP_7, Opcodes::OP_8, Opcodes::OP_9, Opcodes::OP_ABS, Opcodes::OP_ADD, Opcodes::OP_AND, Opcodes::OP_BOOLAND, Opcodes::OP_BOOLOR, Opcodes::OP_CAT, Opcodes::OP_CHECKDATASIG, Opcodes::OP_CHECKDATASIGVERIFY, Opcodes::OP_CHECKMULTISIG, Opcodes::OP_CHECKMULTISIGVERIFY, Opcodes::OP_CHECKSIG, Opcodes::OP_CHECKSIGVERIFY, Opcodes::OP_CODESEPARATOR, Opcodes::OP_COLOR, Opcodes::OP_DEPTH, Opcodes::OP_DIV, Opcodes::OP_DROP, Opcodes::OP_DUP, Opcodes::OP_ELSE, Opcodes::OP_ENDIF, Opcodes::OP_EQUAL, Opcodes::OP_EQUALVERIFY, Opcodes::OP_FROMALTSTACK, Opcodes::OP_GREATERTHAN, Opcodes::OP_GREATERTHANOREQUAL, Opcodes::OP_HASH160, Opcodes::OP_HASH256, Opcodes::OP_IF, Opcodes::OP_IFDUP, Opcodes::OP_INVALIDOPCODE, Opcodes::OP_INVERT, Opcodes::OP_LEFT, Opcodes::OP_LESSTHAN, Opcodes::OP_LESSTHANOREQUAL, Opcodes::OP_LSHIFT, Opcodes::OP_MAX, Opcodes::OP_MIN, Opcodes::OP_MOD, Opcodes::OP_MUL, Opcodes::OP_NEGATE, Opcodes::OP_NIP, Opcodes::OP_NOP, Opcodes::OP_NOP1, Opcodes::OP_NOP10, Opcodes::OP_NOP2, Opcodes::OP_NOP3, Opcodes::OP_NOP4, Opcodes::OP_NOP5, Opcodes::OP_NOP6, Opcodes::OP_NOP7, Opcodes::OP_NOP8, Opcodes::OP_NOP9, Opcodes::OP_NOT, Opcodes::OP_NOTIF, Opcodes::OP_NUMEQUAL, Opcodes::OP_NUMEQUALVERIFY, Opcodes::OP_NUMNOTEQUAL, Opcodes::OP_OR, Opcodes::OP_OVER, Opcodes::OP_PICK, Opcodes::OP_PUBKEY, Opcodes::OP_PUBKEYHASH, Opcodes::OP_PUSHDATA1, Opcodes::OP_PUSHDATA2, Opcodes::OP_PUSHDATA4, Opcodes::OP_RESERVED, Opcodes::OP_RESERVED1, Opcodes::OP_RESERVED2, Opcodes::OP_RETURN, Opcodes::OP_RIGHT, Opcodes::OP_RIPEMD160, Opcodes::OP_ROLL, Opcodes::OP_ROT, Opcodes::OP_RSHIFT, Opcodes::OP_SHA1, Opcodes::OP_SHA256, Opcodes::OP_SIZE, Opcodes::OP_SUB, Opcodes::OP_SUBSTR, Opcodes::OP_SWAP, Opcodes::OP_TOALTSTACK, Opcodes::OP_TUCK, Opcodes::OP_VER, Opcodes::OP_VERIF, Opcodes::OP_VERIFY, Opcodes::OP_VERNOTIF, Opcodes::OP_WITHIN, Opcodes::OP_XOR

Class Method Summary collapse

Methods included from Opcodes

defined?, name_to_opcode, opcode_to_name, opcode_to_small_int, small_int_to_opcode

Class Method Details

.add_sig_to_multisig_script_sig(sig_to_add, script_sig, hash_type = ) ⇒ Object

take a multisig script sig (or p2sh multisig script sig) and add another signature to it after the OP_0. Used to sign a tx by multiple parties. Signatures must be in the same order as the pubkeys in the output script being redeemed.



24
25
26
27
28
# File 'lib/tapyrus/script/multisig.rb', line 24

def self.add_sig_to_multisig_script_sig(sig_to_add, script_sig, hash_type = SIGHASH_TYPE[:all])
  signature = sig_to_add + [hash_type].pack("C*")
  offset = script_sig.empty? ? 0 : 1
  script_sig.insert(offset, Tapyrus::Script.pack_pushdata(signature))
end

.prefixObject



6
7
8
# File 'lib/tapyrus/script/multisig.rb', line 6

def self.prefix
  [OP_0].pack("C*")
end

.sort_p2sh_multisig_signatures(script_sig, sig_hash) ⇒ String

Sort signatures in the given script_sig according to the order of pubkeys in the redeem script. Also needs the sig_hash to match signatures to pubkeys.

Parameters:

  • signature (String)

    for multisig.

  • sig_hash (String)

    to be signed.

Returns:

  • (String)

    sorted sig_hash.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/tapyrus/script/multisig.rb', line 45

def self.sort_p2sh_multisig_signatures(script_sig, sig_hash)
  script = Tapyrus::Script.parse_from_payload(script_sig)
  redeem_script = Tapyrus::Script.parse_from_payload(script.chunks[-1].pushed_data)
  pubkeys = redeem_script.get_multisig_pubkeys

  # find the pubkey for each signature by trying to verify it
  sigs =
    Hash[
      script.chunks[1...-1].map.with_index do |sig, idx|
        sig = sig.pushed_data
        pubkey =
          pubkeys.map { |key| Tapyrus::Key.new(pubkey: key.bth).verify(sig, sig_hash) ? key : nil }.compact.first
        raise "Key for signature ##{idx} not found in redeem script!" unless pubkey
        [pubkey, sig]
      end
    ]

  prefix + pubkeys.map { |k| sigs[k] ? Tapyrus::Script.pack_pushdata(sigs[k]) : nil }.join +
    Tapyrus::Script.pack_pushdata(script.chunks[-1].pushed_data)
end

.to_multisig_script_sig(*sigs) ⇒ String

generate input script sig spending a multisig output script. returns a raw binary script sig of the form:

OP_0 <sig> [<sig> ...]

Parameters:

  • array ([String])

    of signatures

Returns:

  • (String)

    script_sig for multisig



15
16
17
18
# File 'lib/tapyrus/script/multisig.rb', line 15

def self.to_multisig_script_sig(*sigs)
  hash_type = sigs.last.is_a?(Numeric) ? sigs.pop : SIGHASH_TYPE[:all]
  sigs.reverse.inject(prefix) { |joined, sig| add_sig_to_multisig_script_sig(sig, joined, hash_type) }
end

.to_p2sh_multisig_script_sig(redeem_script, *sigs) ⇒ String

generate input script sig spending a p2sh-multisig output script. returns a raw binary script sig of the form:

OP_0 <sig> [<sig> ...] <redeem_script>

Parameters:

Returns:

  • (String)

    script_sig for multisig



36
37
38
# File 'lib/tapyrus/script/multisig.rb', line 36

def self.to_p2sh_multisig_script_sig(redeem_script, *sigs)
  to_multisig_script_sig(*sigs.flatten) + Tapyrus::Script.pack_pushdata(redeem_script)
end