Class: BTC::AssetMarker

Inherits:
Object
  • Object
show all
Includes:
Opcodes
Defined in:
lib/btcruby/open_assets/asset_marker.rb

Constant Summary collapse

MAGIC =
"\x4f\x41".freeze
VERSION1 =
"\x01\x00".freeze
PREFIX_V1 =
MAGIC + VERSION1

Constants included from Opcodes

Opcodes::OPCODE_NAME_TO_VALUE, Opcodes::OPCODE_VALUE_TO_NAME, Opcodes::OP_0, Opcodes::OP_0NOTEQUAL, Opcodes::OP_1, Opcodes::OP_10, Opcodes::OP_11, Opcodes::OP_12, Opcodes::OP_13, Opcodes::OP_14, Opcodes::OP_15, Opcodes::OP_16, Opcodes::OP_1ADD, Opcodes::OP_1NEGATE, Opcodes::OP_1SUB, Opcodes::OP_2, Opcodes::OP_2DIV, Opcodes::OP_2DROP, Opcodes::OP_2DUP, Opcodes::OP_2MUL, Opcodes::OP_2OVER, Opcodes::OP_2ROT, Opcodes::OP_2SWAP, Opcodes::OP_3, Opcodes::OP_3DUP, Opcodes::OP_4, Opcodes::OP_5, Opcodes::OP_6, Opcodes::OP_7, Opcodes::OP_8, Opcodes::OP_9, Opcodes::OP_ABS, Opcodes::OP_ADD, Opcodes::OP_AND, Opcodes::OP_BOOLAND, Opcodes::OP_BOOLOR, Opcodes::OP_CAT, Opcodes::OP_CHECKLOCKTIMEVERIFY, Opcodes::OP_CHECKMULTISIG, Opcodes::OP_CHECKMULTISIGVERIFY, Opcodes::OP_CHECKSIG, Opcodes::OP_CHECKSIGVERIFY, Opcodes::OP_CODESEPARATOR, Opcodes::OP_DEPTH, Opcodes::OP_DIV, Opcodes::OP_DROP, Opcodes::OP_DUP, Opcodes::OP_ELSE, Opcodes::OP_ENDIF, Opcodes::OP_EQUAL, Opcodes::OP_EQUALVERIFY, Opcodes::OP_FALSE, Opcodes::OP_FROMALTSTACK, Opcodes::OP_GREATERTHAN, Opcodes::OP_GREATERTHANOREQUAL, Opcodes::OP_HASH160, Opcodes::OP_HASH256, Opcodes::OP_IF, Opcodes::OP_IFDUP, Opcodes::OP_INVALIDOPCODE, Opcodes::OP_INVERT, Opcodes::OP_LEFT, Opcodes::OP_LESSTHAN, Opcodes::OP_LESSTHANOREQUAL, Opcodes::OP_LSHIFT, Opcodes::OP_MAX, Opcodes::OP_MIN, Opcodes::OP_MOD, Opcodes::OP_MUL, Opcodes::OP_NEGATE, Opcodes::OP_NIP, Opcodes::OP_NOP, Opcodes::OP_NOP1, Opcodes::OP_NOP10, Opcodes::OP_NOP2, Opcodes::OP_NOP3, Opcodes::OP_NOP4, Opcodes::OP_NOP5, Opcodes::OP_NOP6, Opcodes::OP_NOP7, Opcodes::OP_NOP8, Opcodes::OP_NOP9, Opcodes::OP_NOT, Opcodes::OP_NOTIF, Opcodes::OP_NUMEQUAL, Opcodes::OP_NUMEQUALVERIFY, Opcodes::OP_NUMNOTEQUAL, Opcodes::OP_OR, Opcodes::OP_OVER, Opcodes::OP_PICK, Opcodes::OP_PUSHDATA1, Opcodes::OP_PUSHDATA2, Opcodes::OP_PUSHDATA4, Opcodes::OP_RESERVED, Opcodes::OP_RESERVED1, Opcodes::OP_RESERVED2, Opcodes::OP_RETURN, Opcodes::OP_RIGHT, Opcodes::OP_RIPEMD160, Opcodes::OP_ROLL, Opcodes::OP_ROT, Opcodes::OP_RSHIFT, Opcodes::OP_SHA1, Opcodes::OP_SHA256, Opcodes::OP_SIZE, Opcodes::OP_SUB, Opcodes::OP_SUBSTR, Opcodes::OP_SWAP, Opcodes::OP_TOALTSTACK, Opcodes::OP_TRUE, Opcodes::OP_TUCK, Opcodes::OP_VER, Opcodes::OP_VERIF, Opcodes::OP_VERIFY, Opcodes::OP_VERNOTIF, Opcodes::OP_WITHIN, Opcodes::OP_XOR

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output: nil, script: nil, data: nil, quantities: nil, metadata: nil) ⇒ AssetMarker

Returns a new instance of AssetMarker.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/btcruby/open_assets/asset_marker.rb', line 16

def initialize(output: nil, script: nil, data: nil, quantities: nil, metadata: nil)
  if output || script
    script ||= output.script
    raise ArgumentError, "Script is not an OP_RETURN script" if !script.op_return_script?
    data = script.op_return_data
    raise ArgumentError, "No pushdata found in script" if !data || data.bytesize == 0
    @output = output
    @script = script
  end
  if data
    data = BTC::Data.ensure_binary_encoding(data)
    
    raise ArgumentError, "Data must be at least 6 bytes long (4 bytes prefix and 2 bytes for varints)" if data.bytesize < 6
    raise ArgumentError, "Prefix is invalid. Expected #{BTC.to_hex(PREFIX_V1)}" if data[0, PREFIX_V1.bytesize] != PREFIX_V1

    offset = PREFIX_V1.bytesize
    count, bytesread = WireFormat.read_varint(data: data, offset: offset)
    raise ArgumentError, "Cannot read Asset Quantity Count varint" if !count
    offset = bytesread
    @quantities = []
    count.times do
      qty, bytesread = WireFormat.read_uleb128(data: data, offset: offset)
      raise ArgumentError, "Cannot read Asset Quantity LEB128 unsigned integer" if !qty
      raise ArgumentError, "Open Assets limit LEB128 encoding for quantities to 9 bytes" if (bytesread - offset) > 9
      @quantities << qty
      offset = bytesread
    end
    , bytesread = WireFormat.read_string(data: data, offset: offset)
    raise ArgumentError, "Cannot read Asset Metadata" if !
    @metadata = 
    @data = data[0, bytesread]
  else
    # Initialize with optional attributes
    @quantities = quantities
    @metadata = 
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



12
13
14
# File 'lib/btcruby/open_assets/asset_marker.rb', line 12

def data
  @data
end

#metadataObject

Returns the value of attribute metadata.



10
11
12
# File 'lib/btcruby/open_assets/asset_marker.rb', line 10

def 
  @metadata
end

#outputObject (readonly)

Returns the value of attribute output.



13
14
15
# File 'lib/btcruby/open_assets/asset_marker.rb', line 13

def output
  @output
end

#quantitiesObject

Returns the value of attribute quantities.



9
10
11
# File 'lib/btcruby/open_assets/asset_marker.rb', line 9

def quantities
  @quantities
end

#scriptObject (readonly)

Returns the value of attribute script.



14
15
16
# File 'lib/btcruby/open_assets/asset_marker.rb', line 14

def script
  @script
end