Class: BSV::Overlay::AdminTokenTemplate::Unlocker

Inherits:
Object
  • Object
show all
Defined in:
lib/bsv/overlay/admin_token_template.rb

Overview

Unlocker returned by #unlock.

Satisfies the P2PK condition in a PushDrop locking script by signing the BIP-143 sighash of the spending transaction using the wallet’s derived key for the appropriate protocol.

Constant Summary collapse

ESTIMATED_LENGTH =

Estimated length of a P2PK unlocking script: 1 push opcode + up to 72 DER-encoded signature bytes + 1 sighash byte = 73 bytes total.

73

Instance Method Summary collapse

Constructor Details

#initialize(wallet, protocol_id, originator) ⇒ Unlocker

Returns a new instance of Unlocker.

Parameters:

  • wallet (#create_signature)

    BRC-100 wallet interface

  • protocol_id (Array)

    two-element array [security_level, protocol_name]

  • originator (String, nil)

    optional originator domain



72
73
74
75
76
# File 'lib/bsv/overlay/admin_token_template.rb', line 72

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

Instance Method Details

#estimated_length(_tx, _input_index) ⇒ Integer

Estimated byte length of the unlocking script.

Parameters:

Returns:

  • (Integer)


107
108
109
# File 'lib/bsv/overlay/admin_token_template.rb', line 107

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) and signs it using the wallet’s derived key for the protocol.

Parameters:

Returns:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/bsv/overlay/admin_token_template.rb', line 86

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: '1', counterparty: 'self' }
  sig_args[:originator] = @originator if @originator
  result = @wallet.create_signature(sig_args)

  sig_bytes = result[:signature].pack('C*')
  sig_with_hashtype = sig_bytes + [sighash_type].pack('C')
  BSV::Script::Script.pushdrop_unlock(
    BSV::Script::Script.p2pk_unlock(sig_with_hashtype)
  )
end