Class: Runar::SDK::LocalSigner

Inherits:
Signer
  • Object
show all
Defined in:
lib/runar/sdk/local_signer.rb

Overview

Holds a secp256k1 private key in memory and delegates real ECDSA signing to the bsv-sdk gem.

Suitable for CLI tooling and automated tests where a hot key is acceptable. For production wallets consider ExternalSigner with a hardware-wallet callback instead.

Examples:

signer = Runar::SDK::LocalSigner.new('...64-char hex key...')
signer.get_public_key  #=> "03..." (66 hex chars)
signer.get_address     #=> "1..."
signer.sign(tx_hex, 0, subscript_hex, satoshis)

Instance Method Summary collapse

Constructor Details

#initialize(key_hex) ⇒ LocalSigner

Create a LocalSigner from a hex-encoded private key.

Parameters:

  • key_hex (String)

    64-character hex private key

Raises:

  • (RuntimeError)

    when the bsv-sdk gem is not installed



42
43
44
45
46
47
48
49
50
51
# File 'lib/runar/sdk/local_signer.rb', line 42

def initialize(key_hex)
  super()
  unless BSV_SDK_AVAILABLE
    raise 'LocalSigner requires the bsv-sdk gem. ' \
          "Add it to your Gemfile: gem 'bsv-sdk'"
  end

  @private_key = BSV::Primitives::PrivateKey.from_hex(key_hex)
  @public_key  = @private_key.public_key
end

Instance Method Details

#get_addressString

Return the BSV mainnet P2PKH address for this key.

Returns:

  • (String)

    Base58Check address



64
65
66
# File 'lib/runar/sdk/local_signer.rb', line 64

def get_address
  @public_key.address
end

#get_public_keyString

Return the hex-encoded compressed public key (66 chars).

rubocop:disable Naming/AccessorMethodName

Returns:

  • (String)

    compressed public key hex



57
58
59
# File 'lib/runar/sdk/local_signer.rb', line 57

def get_public_key
  @public_key.compressed.unpack1('H*')
end

#sign(tx_hex, input_index, subscript, satoshis, sighash_type = nil) ⇒ String

Sign a transaction input using BIP-143 sighash and real ECDSA.

Returns the DER-encoded signature with the sighash byte appended, hex-encoded.

Parameters:

  • tx_hex (String)

    raw unsigned transaction hex

  • input_index (Integer)

    index of the input to sign

  • subscript (String)

    hex-encoded locking script (scriptCode)

  • satoshis (Integer)

    value of the UTXO being spent

  • sighash_type (Integer, nil) (defaults to: nil)

    sighash flags (default: SIGHASH_ALL|FORKID = 0x41)

Returns:

  • (String)

    DER + sighash byte, hex-encoded



80
81
82
83
# File 'lib/runar/sdk/local_signer.rb', line 80

def sign(tx_hex, input_index, subscript, satoshis, sighash_type = nil)
  flag = sighash_type || BSV::Transaction::Sighash::ALL_FORK_ID
  build_signature(tx_hex, input_index, subscript, satoshis, flag)
end