Class: BSV::Script::PushDropTemplate::Unlocker

Inherits:
Object
  • Object
show all
Defined in:
lib/bsv/script/push_drop_template.rb

Overview

Unlocking template returned by #unlock.

Satisfies the P2PKH condition embedded in a PushDrop locking script by computing the BIP-143 sighash and signing it with the wallet’s derived key.

Constant Summary collapse

ESTIMATED_LENGTH =

Estimated unlocking script length in bytes.

P2PKH unlock is 1 + ~72 (DER sig + hashtype) + 1 + 33 (pubkey) = 107 bytes. In PushDrop context the unlock wraps P2PKH, so the estimate is the same.

107

Instance Method Summary collapse

Constructor Details

#initialize(wallet, protocol_id, key_id, counterparty, originator) ⇒ Unlocker

Returns a new instance of Unlocker.

Parameters:

  • wallet (#create_signature, #get_public_key)

    BRC-100 wallet interface

  • protocol_id (Array)

    two-element [security_level, protocol_name]

  • key_id (String)

    key identifier

  • counterparty (String)

    ‘self’, ‘anyone’, or a hex public key

  • originator (String, nil)

    optional originator domain



82
83
84
85
86
87
88
# File 'lib/bsv/script/push_drop_template.rb', line 82

def initialize(wallet, protocol_id, key_id, counterparty, originator)
  @wallet = wallet
  @protocol_id = protocol_id
  @key_id = key_id
  @counterparty = counterparty
  @originator = originator
end

Instance Method Details

#estimated_length(_tx, _input_index) ⇒ Integer

Estimated byte length of the unlocking script.

Parameters:

Returns:

  • (Integer)


131
132
133
# File 'lib/bsv/script/push_drop_template.rb', line 131

def estimated_length(_tx, _input_index)
  ESTIMATED_LENGTH
end

#sign(tx, input_index) ⇒ BSV::Script::Script

Generate the unlocking script for the given input.

Computes the BIP-143 sighash (SIGHASH_ALL|FORK_ID), signs it with the wallet’s derived key, then returns a P2PKH unlock wrapped in a PushDrop unlock (which is a pass-through).

Parameters:

Returns:



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/bsv/script/push_drop_template.rb', line 99

def sign(tx, input_index)
  sighash_type = BSV::Transaction::Sighash::ALL_FORK_ID
  hash = tx.sighash(input_index, sighash_type)
  hash_bytes = hash.unpack('C*')

  sig_args = {
    hash_to_directly_sign: hash_bytes,
    protocol_id: @protocol_id,
    key_id: @key_id,
    counterparty: @counterparty
  }
  orig_kw = @originator ? { originator: @originator } : {}
  result = @wallet.create_signature(sig_args, **orig_kw)

  sig_bytes = result[:signature].pack('C*')
  sig_with_hashtype = sig_bytes + [sighash_type].pack('C')

  # Fetch the derived public key so the P2PKH unlock can include it
  pub_args = { protocol_id: @protocol_id, key_id: @key_id, counterparty: @counterparty }
  pub_result = @wallet.get_public_key(pub_args, **orig_kw)
  pubkey_bytes = [pub_result[:public_key]].pack('H*')

  BSV::Script::Script.pushdrop_unlock(
    BSV::Script::Script.p2pkh_unlock(sig_with_hashtype, pubkey_bytes)
  )
end