Class: Cryptos::Address

Inherits:
Object
  • Object
show all
Extended by:
Base58, Utils::Hashes
Includes:
Base58, Utils::Hashes
Defined in:
lib/cryptos/address.rb

Direct Known Subclasses

Bitcoin::Address, Litecoin::Address

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Hashes

hash160, hash256, ripemd160, sha256

Methods included from Base58

base58_decode, base58_encode

Methods included from Utils::Bytes

#bignum_to_bytes, #bytes_to_bignum

Constructor Details

#initialize(public_key, testnet: true) ⇒ Address

Returns a new instance of Address.



9
10
11
12
# File 'lib/cryptos/address.rb', line 9

def initialize(public_key, testnet: true)
  @public_key = public_key
  @testnet = testnet
end

Instance Attribute Details

#public_keyObject (readonly)

Returns the value of attribute public_key.



6
7
8
# File 'lib/cryptos/address.rb', line 6

def public_key
  @public_key
end

#testnetObject (readonly)

Returns the value of attribute testnet.



7
8
9
# File 'lib/cryptos/address.rb', line 7

def testnet
  @testnet
end

Class Method Details

.to_hash160(value) ⇒ Object



26
27
28
# File 'lib/cryptos/address.rb', line 26

def self.to_hash160(value)
  base58_decode(value)[2, 40]
end

Instance Method Details

#generate(network) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/cryptos/address.rb', line 17

def generate(network)
  prefix = network.to_s(16).rjust 2, '0'
  ripemd160 = hash160 public_key.to_sec
  with_version = "#{prefix}#{ripemd160}"
  checksum = hash256(with_version)[0, 8]
  wrap_encode = "#{with_version}#{checksum}"
  base58_encode(wrap_encode)
end

#get_balance(cli) ⇒ Object



46
47
48
# File 'lib/cryptos/address.rb', line 46

def get_balance(cli)
  cli.get_received_by_address self
end

#import(cli) ⇒ Object



42
43
44
# File 'lib/cryptos/address.rb', line 42

def import(cli)
  cli.import_address self
end

#p2pkhObject



34
35
36
# File 'lib/cryptos/address.rb', line 34

def p2pkh
  generate p2pkh_prefix
end

#p2shObject



38
39
40
# File 'lib/cryptos/address.rb', line 38

def p2sh
  generate p2sh_prefix
end

#to_hash160Object



30
31
32
# File 'lib/cryptos/address.rb', line 30

def to_hash160
  hash160(public_key.to_sec)
end

#to_sObject



50
51
52
# File 'lib/cryptos/address.rb', line 50

def to_s
  p2pkh
end