Sha256: 8aef2f9f5d9f57cada95f72f16ebe5d96df4ac8c662accc211cc378982a97763

Contents?: true

Size: 1.44 KB

Versions: 4

Compression:

Stored size: 1.44 KB

Contents

module BTC
  # Represents an Asset Address, where the assets can be sent.
  class AssetAddress < BTC::Address
    NAMESPACE = 0x13
    
    def self.mainnet_version
      NAMESPACE
    end

    def self.testnet_version
      NAMESPACE
    end

    attr_reader :bitcoin_address
    
    def initialize(string: nil, bitcoin_address: nil, _raw_data: nil)
      if string
        _raw_data ||= Base58.data_from_base58check(string)
        raise FormatError, "Too short AssetAddress" if _raw_data.bytesize < 2
        raise FormatError, "Invalid namespace for AssetAddress" if _raw_data.bytes[0] != NAMESPACE
        @bitcoin_address = Address.parse_raw_data(_raw_data[1..-1])
        @base58check_string = string
      elsif bitcoin_address
        @base58check_string = nil
        @bitcoin_address = BTC::Address.parse(bitcoin_address)
      else
        raise ArgumentError, "Either data or string must be provided"
      end
      # If someone accidentally supplied AssetAddress as a bitcoin address, 
      # simply unwrap the bitcoin address from it.
      while @bitcoin_address.is_a?(self.class)
        @bitcoin_address = @bitcoin_address.bitcoin_address
        @base58check_string = nil
      end
    end

    def network
      @bitcoin_address.network
    end

    def script
      @bitcoin_address.script
    end

    def data_for_base58check_encoding
      BTC::Data.data_from_bytes([NAMESPACE]) + @bitcoin_address.data_for_base58check_encoding
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
btcruby-1.0.3 lib/btcruby/open_assets/asset_address.rb
btcruby-1.0.2 lib/btcruby/open_assets/asset_address.rb
btcruby-1.0.1 lib/btcruby/open_assets/asset_address.rb
btcruby-1.0.0 lib/btcruby/open_assets/asset_address.rb