Class: Sigma::Address

Inherits:
Object
  • Object
show all
Extended by:
FFI::Library
Defined in:
lib/sigma/address.rb

Overview

An address is a short string corresponding to some script used to protect a box. Unlike (string-encoded) binary representation of a script, an address has some useful characteristics:

  • Integrity of an address could be checked., as it is incorporating a checksum.

  • A prefix of address is showing network and an address type.

  • An address is using an encoding (namely, Base58) which is avoiding similarly l0Oking characters, friendly to

double-clicking and line-breaking in emails.

An address is encoding network type, address type, checksum, and enough information to watch for a particular scripts.

Possible network types are:

Mainnet - 0x00

Testnet - 0x10

For an address type, we form content bytes as follows:

P2PK - serialized (compressed) public key

P2SH - first 192 bits of the Blake2b256 hash of serialized script bytes

P2S - serialized script

Address examples for testnet:

3 - P2PK (3WvsT2Gm4EpsM9Pg18PdY6XyhNNMqXDsvJTbbf6ihLvAmSb7u5RN)

? - P2SH (rbcrmKEYduUvADj9Ts3dSVSG27h54pgrq5fPuwB)

? - P2S (Ms7smJwLGbUAjuWQ)

for mainnet:

9 - P2PK (9fRAWhdxEsTcdb8PhGNrZfwqa65zfkuYHAMmkQLcic1gdLSV5vA)

? - P2SH (8UApt8czfFVuTgQmMwtsRBZ4nfWquNiSwCWUjMg)

? - P2S (4MQyML64GnzMxZgm, BxKBaHkvrTvLZrDcZjcsxsF7aSsrN73ijeFZXtbj4CXZHHcvBtqSxQ)

Prefix byte = network type + address type

checksum = blake2b256(prefix byte ++ content bytes)

address = prefix byte ++ content bytes ++ checksum

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pointerObject

Returns the value of attribute pointer.



58
59
60
# File 'lib/sigma/address.rb', line 58

def pointer
  @pointer
end

Class Method Details

.with_base58_address(address_str) ⇒ Address

Decode (base58) address from string and create Address, no checking of network prefix

Parameters:

  • address_str (String)

Returns:



93
94
95
96
97
98
99
# File 'lib/sigma/address.rb', line 93

def self.with_base58_address(address_str)
  pointer = FFI::MemoryPointer.new(:pointer)
  error = ergo_lib_address_from_base58(address_str, pointer)
  Util.check_error!(error)

  init(pointer)
end

.with_mainnet_address(address_str) ⇒ Address

Decode (base58) mainnet address from string and create Address, checking that address is from the testnet

Parameters:

  • address_str (String)

Returns:



82
83
84
85
86
87
88
# File 'lib/sigma/address.rb', line 82

def self.with_mainnet_address(address_str)
  pointer = FFI::MemoryPointer.new(:pointer)
  error = ergo_lib_address_from_mainnet(address_str, pointer)
  Util.check_error!(error)

  init(pointer)
end

.with_raw_pointer(pointer) ⇒ Address

Note:

A user of sigma_rb generally does not need to call this function

Takes ownership of an existing Address Pointer.

Parameters:

  • pointer (FFI::MemoryPointer)

Returns:



64
65
66
# File 'lib/sigma/address.rb', line 64

def self.with_raw_pointer(pointer)
  init(pointer)
end

.with_testnet_address(address_str) ⇒ Address

Decode (base58) testnet address from string and create Address, checking that address is from the testnet

Parameters:

  • address_str (String)

Returns:



71
72
73
74
75
76
77
# File 'lib/sigma/address.rb', line 71

def self.with_testnet_address(address_str)
  pointer = FFI::MemoryPointer.new(:pointer)
  error = ergo_lib_address_from_testnet(address_str, pointer)
  Util.check_error!(error)

  init(pointer)
end

Instance Method Details

#to_base58(network_prefix) ⇒ String

Encode Address to a base58 string

Parameters:

  • network_prefix (Integer)

Returns:

  • (String)

See Also:



105
106
107
108
109
110
111
112
113
# File 'lib/sigma/address.rb', line 105

def to_base58(network_prefix)
  s_ptr = FFI::MemoryPointer.new(:pointer, 1)
  pointer = FFI::MemoryPointer.new(:pointer)
  ergo_lib_address_to_base58(self.pointer, network_prefix, s_ptr)
  s_ptr = s_ptr.read_pointer()
  str = s_ptr.read_string().force_encoding('UTF-8')
  Util.ergo_lib_delete_string(s_ptr)
  str
end

#type_prefixInteger

Get the Network Prefix type of Address

Returns:

  • (Integer)

See Also:



118
119
120
# File 'lib/sigma/address.rb', line 118

def type_prefix
  ergo_lib_address_type_prefix(self.pointer)
end