Class: CoinOp::Bit::MultiNode

Inherits:
Object
  • Object
show all
Includes:
Encodings
Defined in:
lib/coin-op/bit/multi_wallet.rb

Constant Summary collapse

CODE_TO_NETWORK =
{
  0 => :bitcoin,
  1 => :testnet3,
  2 => :litecoin,
  3 => :dogecoin
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Encodings

#base58, #decode_base58, #decode_hex, #hex, #int_to_byte_array

Constructor Details

#initialize(options) ⇒ MultiNode

Returns a new instance of MultiNode.



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/coin-op/bit/multi_wallet.rb', line 225

def initialize(options)
  @path = options[:path]

  @keys = {}
  @public_keys = {}
  @private = options[:private]
  @public = options[:public]

  @private.each do |name, node|
    key = Bitcoin::Key.new(node.private_key.to_hex, node.public_key.to_hex)
    @keys[name] = key
    @public_keys[name] = key
  end
  @public.each do |name, node|
    @public_keys[name] = Bitcoin::Key.new(nil, node.public_key.to_hex)
  end
end

Instance Attribute Details

#keysObject (readonly)

Returns the value of attribute keys.



224
225
226
# File 'lib/coin-op/bit/multi_wallet.rb', line 224

def keys
  @keys
end

#pathObject (readonly)

Returns the value of attribute path.



224
225
226
# File 'lib/coin-op/bit/multi_wallet.rb', line 224

def path
  @path
end

#privateObject (readonly)

Returns the value of attribute private.



224
225
226
# File 'lib/coin-op/bit/multi_wallet.rb', line 224

def private
  @private
end

#publicObject (readonly)

Returns the value of attribute public.



224
225
226
# File 'lib/coin-op/bit/multi_wallet.rb', line 224

def public
  @public
end

#public_keysObject (readonly)

Returns the value of attribute public_keys.



224
225
226
# File 'lib/coin-op/bit/multi_wallet.rb', line 224

def public_keys
  @public_keys
end

Instance Method Details

#addressObject



253
254
255
# File 'lib/coin-op/bit/multi_wallet.rb', line 253

def address
  script.address
end

#networkObject



243
244
245
# File 'lib/coin-op/bit/multi_wallet.rb', line 243

def network
  CODE_TO_NETWORK.fetch(@path.split('/')[2].to_i)
end

#p2sh_addressObject



257
258
259
# File 'lib/coin-op/bit/multi_wallet.rb', line 257

def p2sh_address
  script.p2sh_address
end

#p2sh_scriptObject



261
262
263
# File 'lib/coin-op/bit/multi_wallet.rb', line 261

def p2sh_script
  Script.new(:address => self.script.p2sh_address, network: network)
end

#script(m = 2) ⇒ Object



247
248
249
250
251
# File 'lib/coin-op/bit/multi_wallet.rb', line 247

def script(m=2)
  # m of n
  keys = @public_keys.sort_by {|name, key| name }.map {|name, key| key.pub }
  Script.new(public_keys: keys, needed: m, network: network)
end

#script_sig(signatures) ⇒ Object



274
275
276
# File 'lib/coin-op/bit/multi_wallet.rb', line 274

def script_sig(signatures)
  self.script.p2sh_sig(:signatures => signatures)
end

#signatures(value, names:) ⇒ Object



265
266
267
268
269
270
271
272
# File 'lib/coin-op/bit/multi_wallet.rb', line 265

def signatures(value, names:)
  out = {}
  @keys.each do |name, key|
    next unless names.include?(name)
    out[name] = base58(key.sign(value))
  end
  out
end